internal static SectorPtr Unpack(byte[] data, int offset) { SectorPtr result; result.Ptr = PackUnpack.UnpackInt64LE(data, offset); result.Checksum = PackUnpack.UnpackUInt32LE(data, offset + 8); return(result); }
public void StartReceive(IChannel client) { _client = client; _stopwatch.Start(); _unlinker = _client.OnReceive.Subscribe(message => { if (message.Length != _messageLen) { throw new InvalidOperationException( $"Recived message of len {message.Length} instead {_messageLen}"); } var ticks = Stopwatch.GetTimestamp(); ticks -= PackUnpack.UnpackInt64LE(message.Buffer, message.Offset); _stats.Record(ticks); _receiveCounter++; if (_receiveCounter == _messageCount) { _elapsedTime = _stopwatch.Elapsed; _finished.TrySetResult(Unit.Default); } }); }
/// <summary> /// Reads and inserts all key value pairs into current prefix from stream /// </summary> /// <param name="transaction">transaction where to import all data</param> /// <param name="stream">where to read it from</param> public static void Import(IKeyValueDBTransaction transaction, Stream stream) { if (transaction == null) { throw new ArgumentNullException(nameof(transaction)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead) { throw new ArgumentException("stream must be readable", nameof(stream)); } var tempbuf = new byte[4096]; var tempbuf2 = new byte[4096]; if (stream.Read(tempbuf, 0, 16) != 16) { throw new EndOfStreamException(); } if (tempbuf[0] != 'B' || tempbuf[1] != 'T' || tempbuf[2] != 'D' || tempbuf[3] != 'B' || tempbuf[4] != 'E' || tempbuf[5] != 'X' || tempbuf[6] != 'P' || tempbuf[7] != '2') { throw new BTDBException("Invalid header (it should Start with BTDBEXP2)"); } var keyValuePairs = PackUnpack.UnpackInt64LE(tempbuf, 8); if (keyValuePairs < 0) { throw new BTDBException("Negative number of key value pairs"); } for (var kv = 0; kv < keyValuePairs; kv++) { if (stream.Read(tempbuf, 0, 4) != 4) { throw new EndOfStreamException(); } var keySize = PackUnpack.UnpackInt32LE(tempbuf, 0); if (keySize < 0) { throw new BTDBException("Negative key size"); } if (keySize > tempbuf.Length) { tempbuf = new byte[keySize]; } if (stream.Read(tempbuf, 0, keySize) != keySize) { throw new EndOfStreamException(); } if (stream.Read(tempbuf2, 0, 4) != 4) { throw new EndOfStreamException(); } var valueSize = PackUnpack.UnpackInt32LE(tempbuf2, 0); if (valueSize < 0) { throw new BTDBException("Negative value size"); } if (valueSize > tempbuf2.Length) { tempbuf2 = new byte[valueSize]; } if (stream.Read(tempbuf2, 0, valueSize) != valueSize) { throw new EndOfStreamException(); } transaction.CreateOrUpdateKeyValue(tempbuf.AsSpan(0, keySize), tempbuf2.AsSpan(0, valueSize)); } if (stream.Read(tempbuf, 0, 8) == 8) { transaction.SetCommitUlong(PackUnpack.UnpackUInt64LE(tempbuf, 0)); if (stream.Read(tempbuf, 0, 4) == 4) { var ulongCount = PackUnpack.UnpackUInt32LE(tempbuf, 0); for (var i = 0u; i < ulongCount; i++) { if (stream.Read(tempbuf, 0, 8) != 8) { throw new EndOfStreamException(); } transaction.SetUlong(i, PackUnpack.UnpackUInt64LE(tempbuf, 0)); } } } }