コード例 #1
0
        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();
        }
コード例 #2
0
        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);
                }
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        public void ReadIndex()
        {
            LoadHeader();

            while (_dataStream.Position < _dataStream.Length)
            {
                RecordHeader header = _dataStream.ReadRecordHeader();
                if (header != null)
                {
                    IndexRecord(header);
                }
            }
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
0
 public static void WriteRecordHeader(this Stream stream, RecordHeader header)
 {
     StreamStorageBinarySerializer.SerializeRecordHeader(stream, header);
 }
コード例 #9
0
        public void Store(string table, string key, byte[] data)
        {
            RecordHeader header = StoreToStream(_dataStream, key, table, data, StorageActions.Store);

            IndexRecord(header);
        }
コード例 #10
0
 byte[] ReadFromStream(Stream dataStream, RecordHeader header)
 {
     dataStream.SeekLocation(header.RecordLocation);
     return(dataStream.Read(header.RecordSize));
 }
コード例 #11
0
 void IndexRecord(RecordHeader header)
 {
     WriteHeaderToIndexForStream(header, _indexes, _dataStream);
 }
コード例 #12
0
        public void Remove(string table, string key)
        {
            RecordHeader header = StoreToStream(_dataStream, key, table, null, StorageActions.Delete);

            IndexRecord(header);
        }
コード例 #13
0
 public static void WriteRecordHeader(this Stream stream, RecordHeader header)
 {
     StreamStorageBinarySerializer.SerializeRecordHeader(stream, header);
 }