public void CleanUp() { Stream tempStream = _tempStreamFactory(); tempStream.WriteStreamHeader(Header); var newIndex = new Dictionary <RecordKey, RecordHeader>(new RecordKeyComparer()); foreach (var kvp in _indexes) { byte[] data = ReadFromStream(_dataStream, kvp.Value); RecordHeader header = StoreToStream(tempStream, kvp.Value.Key, kvp.Value.Table, data, StorageActions.Store); WriteHeaderToIndexForStream(header, newIndex, tempStream); } _primaryStreamCleanUp(_dataStream); _dataStream = _primaryStreamFactory(); _dataStream.WriteStreamHeader(Header); foreach (var kvp in newIndex) { byte[] data = ReadFromStream(tempStream, kvp.Value); StoreToStream(_dataStream, kvp.Value.Key, kvp.Value.Table, data, StorageActions.Store); } _tempStreamCleanUp(tempStream); _indexes.Clear(); _dataStream.SeekStart(); ReadIndex(); }
void WriteHeaderToIndexForStream(RecordHeader header, Dictionary <RecordKey, RecordHeader> index, Stream stream) { var key = new RecordKey(header.Table, header.Key); if (header.Action == StorageActions.Store) { if (!index.ContainsKey(key)) { index.Add(key, header); } else { index[key] = header; } stream.MovePositionForward(header.RecordSize); } else if (header.Action == StorageActions.Delete) { if (index.ContainsKey(key)) { index.Remove(key); } } }
public static void SerializeRecordHeader(Stream dataStream, RecordHeader header) { var bw = new BinaryWriter(dataStream); bw.Write(header.HeaderVersion); bw.Write(header.RecordSize); bw.Write((int)header.Action); bw.Write(header.Table); bw.Write(header.Key); }
public byte[] Read(string table, string itemKey) { var key = new RecordKey(table, itemKey); if (_indexes.ContainsKey(key)) { RecordHeader header = _indexes[key]; return(ReadFromStream(_dataStream, header)); } return(null); }
public void ReadIndex() { LoadHeader(); while (_dataStream.Position < _dataStream.Length) { RecordHeader header = _dataStream.ReadRecordHeader(); if (header != null) { IndexRecord(header); } } }
RecordHeader StoreToStream(Stream dataStream, string key, string table, byte[] data, StorageActions storageAction) { int recordSize = data == null ? 0 : data.Length; var header = new RecordHeader { Key = key, Table = table, Action = storageAction, RecordSize = recordSize }; dataStream.SeekEnd(); dataStream.WriteRecordHeader(header); header.RecordLocation = dataStream.Position; if (data != null) { dataStream.Write(data); } return(header); }
public static RecordHeader DeserializeRecordHeader(Stream dataStream) { try { var br = new BinaryReader(dataStream); var result = new RecordHeader { HeaderVersion = br.ReadInt32(), RecordSize = br.ReadInt64(), Action = (StorageActions)br.ReadInt32(), Table = br.ReadString(), Key = br.ReadString() }; result.RecordLocation = dataStream.Position; return(result); } catch (Exception) { return(null); } }
public static void WriteRecordHeader(this Stream stream, RecordHeader header) { StreamStorageBinarySerializer.SerializeRecordHeader(stream, header); }
public void Store(string table, string key, byte[] data) { RecordHeader header = StoreToStream(_dataStream, key, table, data, StorageActions.Store); IndexRecord(header); }
byte[] ReadFromStream(Stream dataStream, RecordHeader header) { dataStream.SeekLocation(header.RecordLocation); return(dataStream.Read(header.RecordSize)); }
void IndexRecord(RecordHeader header) { WriteHeaderToIndexForStream(header, _indexes, _dataStream); }
public void Remove(string table, string key) { RecordHeader header = StoreToStream(_dataStream, key, table, null, StorageActions.Delete); IndexRecord(header); }