Exemplo n.º 1
0
 public TableKeyFinder(SnapShot _snapshot, byte[] _tableid, byte[] _beginkey, byte[] _endkey)
 {
     this.snapshot      = _snapshot;
     this.tableid       = _tableid;
     this.beginkeyfinal = Helper.CalcKey(_tableid, _beginkey);
     this.endkeyfinal   = Helper.CalcKey(_tableid, _endkey);
 }
Exemplo n.º 2
0
 public TableIterator(SnapShot snapshot, byte[] _tableid, byte[] _beginkeyfinal, byte[] _endkeyfinal)
 {
     this.it            = snapshot.db.NewIterator(null, snapshot.readop);
     this.tableid       = _tableid;
     this.beginkeyfinal = _beginkeyfinal;
     this.endkeyfinal   = _endkeyfinal;
     //this.Reset();
 }
Exemplo n.º 3
0
        //创建快照
        private SnapShot CreateSnapInfo()
        {
            //看最新高度的快照是否已经产生
            var snapshot = new SnapShot(this.dbPtr);

            snapshot.Init();
            return(snapshot);
        }
Exemplo n.º 4
0
 public WriteBatch(IntPtr dbptr, SnapShot snapshot)
 {
     this.dbPtr    = dbptr;
     this.batchptr = RocksDbSharp.Native.Instance.rocksdb_writebatch_create();
     //this.batch = new RocksDbSharp.WriteBatch();
     this.snapshot = snapshot;
     this.cache    = new Dictionary <string, byte[]>();
 }
Exemplo n.º 5
0
 public TableIterator(SnapShot snapshot, byte[] _tableid, byte[] _beginkeyfinal, byte[] _endkeyfinal)
 {
     this.itPtr = RocksDbSharp.Native.Instance.rocksdb_create_iterator(snapshot.dbPtr, snapshot.readopHandle);
     //this.it = snapshot.db.NewIterator(null, snapshot.readop);
     this.tableid       = _tableid;
     this.beginkeyfinal = _beginkeyfinal;
     this.endkeyfinal   = _endkeyfinal;
     //this.Reset();
 }
Exemplo n.º 6
0
        //写入操作需要保持线性,线程安全
        private void WriteUnsafe(WriteTask task)
        {
            lock (systemtable_block)
            {
                using (var wb = new WriteBatch(this.db.Handle, snapshotLast))
                {
                    var heightbuf = BitConverter.GetBytes(snapshotLast.DataHeight);
                    foreach (var item in task.items)
                    {
                        if (item.value != null)
                        {
                            DBValue.QuickFixHeight(item.value, heightbuf);
                        }
                        switch (item.op)
                        {
                        case WriteTaskOP.CreateTable:
                            wb.CreateTable(item.tableID, item.value);
                            break;

                        case WriteTaskOP.DeleteTable:
                            wb.DeleteTable(item.tableID);
                            break;

                        case WriteTaskOP.PutValue:
                            wb.PutUnsafe(item.tableID, item.key, item.value);
                            break;

                        case WriteTaskOP.DeleteValue:
                            wb.Delete(item.tableID, item.key);
                            break;

                        case WriteTaskOP.Log:
                            break;
                        }
                    }
                    var taskblock = task.ToBytes();
                    //还要把这个block本身写入,高度写入
                    var finaldata = DBValue.FromValue(DBValue.Type.Bytes, taskblock).ToBytes();
                    DBValue.QuickFixHeight(finaldata, heightbuf);
                    var blockkey = BitConverter.GetBytes(snapshotLast.DataHeight);
                    wb.PutUnsafe(systemtable_block, blockkey, finaldata);
                    //wb.Put(systemtable_block, height, taskblock);

                    //height++
                    var finalheight = DBValue.FromValue(DBValue.Type.UINT64, (ulong)(snapshotLast.DataHeight + 1)).ToBytes();
                    DBValue.QuickFixHeight(finalheight, heightbuf);
                    wb.PutUnsafe(systemtable_info, "_height".ToBytes_UTF8Encode(), finalheight);

                    RocksDbSharp.Native.Instance.rocksdb_write(this.db.Handle, this.defaultWriteOpPtr, wb.batchptr);
                    //this.db.Write(wb.batch);
                    snapshotLast.Dispose();
                    snapshotLast = CreateSnapInfo();
                    snapshotLast.AddRef();
                }
            }
        }
Exemplo n.º 7
0
        //写入操作需要保持线性,线程安全
        //writetask 会被修改
        //所以要返回一个bytearray 留给外部
        private void WriteUnsafe(WriteTask task, Action <WriteTask, byte[], IWriteBatch> afterparser)
        {
            lock (systemtable_block)
            {
                using (var wb = new WriteBatch(this.dbPtr, snapshotLast))
                {
                    //先写入原始block
                    //先抽数据,之后就会改变了
                    var taskblock = task.ToBytes();
                    //还要把这个block本身写入,高度写入
                    var finaldata = DBValue.FromValue(DBValue.Type.Bytes, taskblock);
                    wb.Put(systemtable_block, snapshotLast.DataHeightBuf, finaldata);


                    //逐个执行每一个writetask
                    //var heightbuf = BitConverter.GetBytes(snapshotLast.DataHeight);
                    foreach (var item in task.items)
                    {
                        if (item.value != null)
                        {
                            item.value = DBValue.QuickFixHeight(item.value, snapshotLast.DataHeightBuf);
                        }
                        switch (item.op)
                        {
                        case WriteTaskOP.CreateTable:
                            wb.CreateTable(item.tableID, item.value);
                            break;

                        case WriteTaskOP.DeleteTable:
                            wb.DeleteTable(item.tableID);
                            break;

                        case WriteTaskOP.PutValue:
                            wb.PutUnsafe(item.tableID, item.key, item.value);
                            break;

                        case WriteTaskOP.DeleteValue:
                            wb.Delete(item.tableID, item.key);
                            break;

                        case WriteTaskOP.Log:
                            break;
                        }
                    }

                    //在高度增加之前给一个回调插入处理的时机
                    if (afterparser != null)
                    {
                        afterparser(task, taskblock, wb);
                    }

                    //增加高度
                    //height++
                    var finalheight = DBValue.FromValue(DBValue.Type.UINT64, (ulong)(snapshotLast.DataHeight + 1));
                    wb.Put(systemtable_info, "_height".ToBytes_UTF8Encode(), finalheight);

                    RocksDbSharp.Native.Instance.rocksdb_write(this.dbPtr, this.defaultWriteOpPtr, wb.batchptr);
                    //this.db.Write(wb.batch);
                    snapshotLast.Dispose();
                    snapshotLast = CreateSnapInfo();
                    snapshotLast.AddRef();

                    //return finaldata;
                }
            }
        }