Example #1
0
        internal static void SkipHeader(AbstractBufferedReader reader)
        {
            FileCollectionWithFileInfos.SkipHeader(reader);
            var type            = (KVFileType)reader.ReadUInt8();
            var withCommitUlong = type == KVFileType.KeyIndexWithCommitUlong || type == KVFileType.ModernKeyIndex || type == KVFileType.ModernKeyIndexWithUlongs;

            reader.SkipVInt64();  // generation
            reader.SkipVUInt32(); // trLogFileId
            reader.SkipVUInt32(); // trLogOffset
            reader.SkipVUInt64(); // keyValueCount
            if (withCommitUlong)
            {
                reader.SkipVUInt64();                  // commitUlong
            }
            if (type == KVFileType.ModernKeyIndex || type == KVFileType.ModernKeyIndexWithUlongs)
            {
                reader.SkipUInt8();
            }
            if (type == KVFileType.ModernKeyIndexWithUlongs)
            {
                var ulongCount = reader.ReadVUInt32();
                while (ulongCount-- > 0)
                {
                    reader.SkipVUInt64();
                }
            }
        }
Example #2
0
 public FileKeyIndex(AbstractBufferedReader reader, Guid?guid, bool withCommitUlong, bool modern)
 {
     _guid            = guid;
     _generation      = reader.ReadVInt64();
     _trLogFileId     = reader.ReadVUInt32();
     _trLogOffset     = reader.ReadVUInt32();
     _keyValueCount   = (long)reader.ReadVUInt64();
     _commitUlong     = withCommitUlong ? reader.ReadVUInt64() : 0;
     _compressionType = modern ? (KeyIndexCompression)reader.ReadUInt8() : KeyIndexCompression.Old;
 }
Example #3
0
 public FileKeyIndex(AbstractBufferedReader reader, Guid?guid, bool withCommitUlong, bool modern, bool withUlongs)
 {
     _guid            = guid;
     _generation      = reader.ReadVInt64();
     _trLogFileId     = reader.ReadVUInt32();
     _trLogOffset     = reader.ReadVUInt32();
     _keyValueCount   = (long)reader.ReadVUInt64();
     _commitUlong     = withCommitUlong ? reader.ReadVUInt64() : 0;
     _compressionType = modern ? (KeyIndexCompression)reader.ReadUInt8() : KeyIndexCompression.Old;
     _ulongs          = null;
     if (withUlongs)
     {
         _ulongs = new ulong[reader.ReadVUInt32()];
         for (var i = 0; i < _ulongs.Length; i++)
         {
             _ulongs[i] = reader.ReadVUInt64();
         }
     }
 }
Example #4
0
        public void LoadTypeDescriptors(AbstractBufferedReader reader)
        {
            var typeId      = reader.ReadVUInt32();
            var firstTypeId = typeId;

            while (typeId != 0)
            {
                if (typeId < firstTypeId)
                {
                    firstTypeId = typeId;
                }
                var             typeCategory = (TypeCategory)reader.ReadUInt8();
                ITypeDescriptor descriptor;
                switch (typeCategory)
                {
                case TypeCategory.BuildIn:
                    throw new ArgumentOutOfRangeException();

                case TypeCategory.Class:
                    descriptor = new ObjectTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.List:
                    descriptor = new ListTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Dictionary:
                    descriptor = new DictionaryTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Enum:
                    descriptor = new EnumTypeDescriptor(_typeSerializers, reader);
                    break;

                case TypeCategory.Nullable:
                    descriptor = new NullableTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                while (typeId >= _id2DescriptorMap.Count)
                {
                    _id2DescriptorMap.Add(null);
                }
                if (_id2DescriptorMap[(int)typeId] == null)
                {
                    _id2DescriptorMap[(int)typeId] = new InfoForType {
                        Id = (int)typeId, Descriptor = descriptor
                    }
                }
                ;
                typeId = reader.ReadVUInt32();
            }
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                _id2DescriptorMap[(int)i].Descriptor.MapNestedTypes(d =>
                {
                    var placeHolderDescriptor = d as PlaceHolderDescriptor;
                    return(placeHolderDescriptor != null ? _id2DescriptorMap[(int)placeHolderDescriptor.TypeId].Descriptor : d);
                });
            }
            // This additional cycle is needed to fill names of recursive structures
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                _id2DescriptorMap[(int)i].Descriptor.MapNestedTypes(d => d);
            }
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                var infoForType = _id2DescriptorMap[(int)i];
                var descriptor  = _typeSerializers.MergeDescriptor(infoForType.Descriptor);
                infoForType.Descriptor             = descriptor;
                _typeOrDescriptor2Info[descriptor] = infoForType;
            }
        }

        ITypeDescriptor NestedDescriptorReader(AbstractBufferedReader reader)
        {
            var typeId = reader.ReadVUInt32();

            if (typeId < _id2DescriptorMap.Count)
            {
                var infoForType = _id2DescriptorMap[(int)typeId];
                if (infoForType != null)
                {
                    return(infoForType.Descriptor);
                }
            }
            return(new PlaceHolderDescriptor(typeId));
        }
Example #5
0
        public void Replay()
        {
            while (!_reader.Eof)
            {
                var operation = (KVReplayOperation)_reader.ReadUInt8();
                Console.WriteLine(operation);
                uint tri;
                IKeyValueDBTransaction tr;
                int    valOfs;
                int    valLen;
                byte[] valBuf;
                int    keyLen;
                int    keyOfs;
                byte[] keyBuf;
                switch (operation)
                {
                case KVReplayOperation.Open:
                    var initialStreamSize = _reader.ReadVUInt64();
                    _dbstream = new MemoryPositionLessStream();
                    var   buf = new byte[4096];
                    ulong pos = 0;
                    while (pos < initialStreamSize)
                    {
                        var r = (int)Math.Min((ulong)buf.Length, initialStreamSize - pos);
                        _reader.ReadBlock(buf, 0, r);
                        _dbstream.Write(buf, 0, r, pos);
                        pos += (ulong)r;
                    }
                    _db.Open(_dbstream, true);
                    break;

                case KVReplayOperation.KeyValueDBDispose:
                    _db.Dispose();
                    break;

                case KVReplayOperation.StartTransaction:
                    tr        = _db.StartTransaction();
                    tri       = _reader.ReadVUInt32();
                    _trs[tri] = tr;
                    break;

                case KVReplayOperation.StartWritingTransaction:
                    tr        = _db.StartWritingTransaction().Result;
                    tri       = _reader.ReadVUInt32();
                    _trs[tri] = tr;
                    break;

                case KVReplayOperation.CalculateStats:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    tr.CalculateStats();
                    break;

                case KVReplayOperation.SetKeyPrefix:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    int prefixLen = _reader.ReadVInt32();
                    int prefixOfs = _reader.ReadVInt32();
                    var prefixBuf = new byte[prefixOfs + prefixLen];
                    _reader.ReadBlock(prefixBuf, prefixOfs, prefixLen);
                    tr.SetKeyPrefix(prefixBuf, prefixOfs, prefixLen);
                    break;

                case KVReplayOperation.FindKey:
                    tri    = _reader.ReadVUInt32();
                    tr     = _trs[tri];
                    keyLen = _reader.ReadVInt32();
                    keyOfs = _reader.ReadVInt32();
                    keyBuf = new byte[keyOfs + keyLen];
                    _reader.ReadBlock(keyBuf, keyOfs, keyLen);
                    var strategy = (FindKeyStrategy)_reader.ReadVUInt32();
                    tr.FindKey(keyBuf, keyOfs, keyLen, strategy);
                    break;

                case KVReplayOperation.GetKeyIndex:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    tr.GetKeyIndex();
                    break;

                case KVReplayOperation.SetValue:
                    tri    = _reader.ReadVUInt32();
                    tr     = _trs[tri];
                    valOfs = _reader.ReadVInt32();
                    valLen = _reader.ReadVInt32();
                    valBuf = new byte[valOfs + valLen];
                    _reader.ReadBlock(valBuf, valOfs, valLen);
                    tr.SetValue(valBuf, valOfs, valLen);
                    break;

                case KVReplayOperation.SetValueSize:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    var valueSize = _reader.ReadVInt64();
                    tr.SetValueSize(valueSize);
                    break;

                case KVReplayOperation.WriteValue:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    var ofs = _reader.ReadVInt64();
                    valOfs = _reader.ReadVInt32();
                    valLen = _reader.ReadVInt32();
                    valBuf = new byte[valOfs + valLen];
                    _reader.ReadBlock(valBuf, valOfs, valLen);
                    tr.WriteValue(ofs, valLen, valBuf, valOfs);
                    break;

                case KVReplayOperation.CreateOrUpdateKeyValue:
                    tri    = _reader.ReadVUInt32();
                    tr     = _trs[tri];
                    keyLen = _reader.ReadVInt32();
                    keyOfs = _reader.ReadVInt32();
                    keyBuf = new byte[keyOfs + keyLen];
                    _reader.ReadBlock(keyBuf, keyOfs, keyLen);
                    valLen = _reader.ReadVInt32();
                    valOfs = _reader.ReadVInt32();
                    valBuf = new byte[valOfs + valLen];
                    _reader.ReadBlock(valBuf, valOfs, valLen);
                    tr.CreateOrUpdateKeyValue(keyBuf, keyOfs, keyLen, valBuf, valOfs, valLen);
                    break;

                case KVReplayOperation.Commit:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    tr.Commit();
                    break;

                case KVReplayOperation.TransactionDispose:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    tr.Dispose();
                    _trs.Remove(tri);
                    break;

                case KVReplayOperation.EraseCurrent:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    tr.EraseCurrent();
                    break;

                case KVReplayOperation.EraseAll:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    tr.EraseAll();
                    break;

                case KVReplayOperation.FindFirstKey:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    tr.FindFirstKey();
                    break;

                case KVReplayOperation.FindPreviousKey:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    tr.FindPreviousKey();
                    break;

                case KVReplayOperation.FindNextKey:
                    tri = _reader.ReadVUInt32();
                    tr  = _trs[tri];
                    tr.FindNextKey();
                    break;

                default:
                    Console.WriteLine(string.Format("Unimplemented operation {0}({1})", operation, (byte)operation));
                    throw new NotSupportedException(string.Format("Unimplemented operation {0}({1})", operation, (byte)operation));
                }
            }
            Console.WriteLine("Finish");
        }