private (TKey key, TValue value, long nextOffset, int nextSize) ReadDataRecord(long dataOffset, int dataSize) { if (valueBuffer.Length < dataSize) { valueBuffer = new byte[256 * (1 + (dataSize + 255) / 256)]; } dataStorage.ReadAll(dataOffset, valueBuffer, 0, dataSize); var keySize = BitConverter.ToInt32(valueBuffer, 0); var dataKey = keySerializer.GetValue(valueBuffer.Skip(sizeof(int)).Take(keySize).ToArray()); var valueSize = BitConverter.ToInt32(valueBuffer, sizeof(int) + keySize); var dataValue = valueSerializer.GetValue(valueBuffer.Skip(sizeof(int) + keySize + sizeof(int)).Take(valueSize).ToArray()); var nextOffset = BitConverter.ToInt64(valueBuffer, dataSize - sizeof(long) - sizeof(int)); var nextSize = BitConverter.ToInt32(valueBuffer, dataSize - sizeof(int)); return(dataKey, dataValue, nextOffset, nextSize); }
public TValue this[long index] { get { var offset = linearIndex[index]; if (offset == 0) { throw new ArgumentOutOfRangeException(nameof(index)); } dataStorage.ReadAll(offset, sizeBuffer, 0, sizeBuffer.Length); int dataSize = BitConverter.ToInt32(sizeBuffer, 0); //TODO: Improve serializer, no need to reallocate if (valueBuffer == null || valueBuffer.Length != dataSize) { valueBuffer = new byte[dataSize]; } dataStorage.ReadAll(offset + sizeBuffer.Length, valueBuffer, 0, valueBuffer.Length); return(valueSerializer.GetValue(valueBuffer)); } set { var offset = linearIndex[index]; if (offset == 0) { offset = dataStorage.Length; linearIndex[index] = dataStorage.Length; } var data = valueSerializer.GetBytes(value); var dataSize = BitConverter.GetBytes(data.Length); dataStorage.WriteAll(offset, dataSize, 0, dataSize.Length); dataStorage.WriteAll(offset + dataSize.Length, data, 0, data.Length); } }