Example #1
0
 internal static void SkipHeader(AbstractBufferedReader reader)
 {
     reader.SkipBlock(DiskChunkCache.MagicStartOfFile.Length + 1); // magic + type of file
     reader.SkipVInt64();                                          // generation
     reader.SkipVUInt32();                                         // keySize
     reader.SkipVUInt64();                                         // keyValueCount
 }
Example #2
0
        public void CreateSource()
        {
            using (var os = new System.IO.StreamWriter("source.cs", false, Encoding.UTF8))
            {
                while (!_reader.Eof)
                {
                    var operation = (KVReplayOperation)_reader.ReadUInt8();
                    Console.WriteLine(operation);
                    uint tri;
                    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;
                        }
                        os.WriteLine("var db=new KeyValueDB();");
                        os.WriteLine("var dbstream = new MemoryPositionLessStream();");
                        os.WriteLine("db.Open(dbstream,true);");
                        break;

                    case KVReplayOperation.KeyValueDBDispose:
                        os.WriteLine("db.Dispose();");
                        break;

                    case KVReplayOperation.StartTransaction:
                        tri = _reader.ReadVUInt32();
                        os.WriteLine(string.Format("var tr{0}=db.StartTransaction();", tri));
                        break;

                    case KVReplayOperation.CalculateStats:
                        tri = _reader.ReadVUInt32();
                        break;

                    case KVReplayOperation.FindKey:
                        tri = _reader.ReadVUInt32();
                        int keyLen = _reader.ReadVInt32();
                        int keyOfs = _reader.ReadVInt32();
                        var keyBuf = new byte[keyLen];
                        _reader.ReadBlock(keyBuf, 0, keyLen);
                        var strategy = (FindKeyStrategy)_reader.ReadVUInt32();
                        var content  = new StringBuilder();
                        content.Append("{");
                        if (keyBuf.Length != 0)
                        {
                            foreach (var b in keyBuf)
                            {
                                content.Append(b);
                                content.Append(",");
                            }
                            content.Length--;
                        }
                        content.Append("}");
                        if (strategy == FindKeyStrategy.Create)
                        {
                            os.WriteLine(string.Format("tr{0}.CreateKey(new byte[] {1});", tri, content));
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                        break;

                    case KVReplayOperation.GetKeyIndex:
                        tri = _reader.ReadVUInt32();
                        break;

                    case KVReplayOperation.SetValue:
                        tri = _reader.ReadVUInt32();
                        int valOfs = _reader.ReadVInt32();
                        int valLen = _reader.ReadVInt32();
                        _reader.SkipBlock(valLen);
                        os.WriteLine(string.Format("tr{0}.SetValue(new byte[{1}]);", tri, valLen));
                        break;

                    case KVReplayOperation.Commit:
                        tri = _reader.ReadVUInt32();
                        os.WriteLine(string.Format("tr{0}.Commit();", tri));
                        break;

                    case KVReplayOperation.TransactionDispose:
                        tri = _reader.ReadVUInt32();
                        os.WriteLine(string.Format("tr{0}.Dispose();", tri));
                        break;

                    case KVReplayOperation.EraseCurrent:
                        tri = _reader.ReadVUInt32();
                        os.WriteLine(string.Format("tr{0}.EraseCurrent();", tri));
                        break;

                    case KVReplayOperation.FindPreviousKey:
                        tri = _reader.ReadVUInt32();
                        os.WriteLine(string.Format("tr{0}.FindPreviousKey();", tri));
                        break;

                    case KVReplayOperation.FindNextKey:
                        tri = _reader.ReadVUInt32();
                        os.WriteLine(string.Format("tr{0}.FindNextKey();", tri));
                        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));
                    }
                }
            }
        }