private std::error_code write(IWriteBatch batch, bool sync) { rocksdb.WriteOptions writeOptions = new rocksdb.WriteOptions(); writeOptions.sync = sync; rocksdb.WriteBatch rocksdbBatch = new rocksdb.WriteBatch(); List <Tuple <string, string> > rawData = new List <Tuple <string, string> >(batch.extractRawDataToInsert()); foreach (Tuple <string, string> kvPair in rawData) { rocksdbBatch.Put(new rocksdb.Slice(kvPair.Item1), new rocksdb.Slice(kvPair.Item2)); } List <string> rawKeys = new List <string>(batch.extractRawKeysToRemove()); foreach (string key in rawKeys) { rocksdbBatch.Delete(new rocksdb.Slice(key)); } rocksdb.Status status = db.Write(writeOptions, rocksdbBatch); if (!status.ok()) { logger(ERROR) << "Can't write to DB. " << status.ToString(); return(GlobalMembers.make_error_code(CryptoNote.error.DataBaseErrorCodes.INTERNAL_ERROR)); } else { return(std::error_code()); } }
public void WriteBatch(IWriteBatch wb) { RocksDbSharp.Native.Instance.rocksdb_write(this.dbPtr, this.defaultWriteOpPtr, (wb as WriteBatch).batchptr); snapshotLast.Dispose(); snapshotLast = CreateSnapInfo(); snapshotLast.AddRef(); }
public void Restore(UInt64 curHeight, IList <TrackForMongodb> list) { IWriteBatch wb = StorageService.maindb.CreateWriteBatch(); foreach (var l in list) { var lHeight = l.height; if (lHeight > curHeight) { StorageService.maindb.WriteBatch(wb); wb = StorageService.maindb.CreateWriteBatch(); curHeight++; } if (l.state == (byte)TrackState.Added) { //Console.WriteLine(Neo.Helper.ToHexString(l.key?.Bytes)); wb.Put(l.tableid.ToBytes(), l.key?.Bytes, l.value?.Bytes); } else if (l.state == (byte)TrackState.Deleted) { wb.Delete(l.tableid.ToBytes(), l.key?.Bytes); } else if (l.state == (byte)TrackState.Changed) { wb.Put(l.tableid.ToBytes(), l.key?.Bytes, l.value?.Bytes); } } }
public override std::error_code writeSync(IWriteBatch batch) { if (state.load() != State.INITIALIZED) { throw std::system_error(GlobalMembers.make_error_code(CryptoNote.error.DataBaseErrorCodes.NOT_INITIALIZED)); } return(write(batch, true)); }
public SimpleDbSnapShot(DB db) { this.db = db; this.snapshot = db.UseSnapShot(); this.batch = db.CreateWriteBatch(); Logs = new SimpleDbCache <LogKey, Log>(this.db, this.batch, TableId.DATA_Log); CareAddrs = new SimpleDbCache <UInt160, UInt160>(this.db, this.batch, TableId.DATA_CareAddr); CareAssets = new SimpleDbCache <UInt160, UInt160>(this.db, this.batch, TableId.DATA_CareAsset); CareEvents = new SimpleDbCache <UInt256, CareEvent>(this.db, this.batch, TableId.DATA_CareEvent); Transfers = new SimpleDbCache <TransferKey, TransferGroup>(this.db, this.batch, TableId.DATA_Transfer); TransferBlockNumberList = new SimpleDbCache <UInt160, TransferBlockNumberList>(this.db, this.batch, TableId.DATA_TransferBlockNumberList); BlockNumber = new SimpleDbCache <UInt32Wrapper, UInt32Wrapper>(this.db, this.batch, TableId.DATA_BlockNumber); }
public void WriteBatch(IWriteBatch wb) { //每次写入的时候增加一个高度 var finalheight = snapshotLast.DataHeight + 1; wb.Put(new byte[1] { 0x01 }, "_height".ToBytes_UTF8Encode(), BitConverter.GetBytes(finalheight)); RocksDbSharp.Native.Instance.rocksdb_write(this.dbPtr, this.defaultWriteOpPtr, (wb as WriteBatch).batchptr); (wb as WriteBatch).Dispose(); snapshotLast.Dispose(); snapshotLast = CreateSnapInfo(); snapshotLast.AddRef(); }
public SimpleDbCache(DB _db, IWriteBatch _batch, byte _tableId) { this.db = _db; this.batch = _batch; this.tableId = _tableId; }
public NetMessage Process(UInt64 peerid, byte[] data) { using (MemoryStream ms = new MemoryStream(data)) { NetMessage netMsg = NetMessage.Unpack(ms); string cmd = netMsg.Cmd; string id = netMsg.ID; NetMessage netMsgBack; try { switch (cmd) { case "_db.usesnapshot": { var snapshot = StorageService.maindb.UseSnapShot(); peerSnapshots[snapshot.DataHeight] = StorageService.maindb.UseSnapShot(); var p = new Param() { result = true, snapid = snapshot.DataHeight }; netMsgBack = NetMessage.Create(cmd, p, id); return(netMsgBack); } case "_db.disposeSnapshot": { ISnapShot snapshot; peerSnapshots.TryRemove(netMsg.Param.snapid, out snapshot); snapshot.Dispose(); var p = new Param() { result = true }; netMsgBack = NetMessage.Create(cmd, p, id); return(netMsgBack); } case "_db.CreateWriteBatch": { var wb = StorageService.maindb.CreateWriteBatch(); peerWriteBatch[wb.Wbid] = wb; var p = new Param() { result = true, wbid = wb.Wbid }; netMsgBack = NetMessage.Create(cmd, p, id); return(netMsgBack); } case "_db.put": { IWriteBatch writeBatch = peerWriteBatch[netMsg.Param.wbid]; writeBatch.Put(netMsg.Param.tableid, netMsg.Param.key, netMsg.Param.value); var p = new Param() { result = true }; netMsgBack = NetMessage.Create(cmd, p, id); return(netMsgBack); } case "_db.delete": { IWriteBatch writeBatch = peerWriteBatch[netMsg.Param.wbid]; writeBatch.Delete(netMsg.Param.tableid, netMsg.Param.key); var p = new Param() { result = true }; netMsgBack = NetMessage.Create(cmd, p, id); return(netMsgBack); } case "_db.write": { IWriteBatch writeBatch = peerWriteBatch[netMsg.Param.wbid]; var p = new Param() { result = false }; if (writeBatch.wbcount > 0) { StorageService.maindb.WriteBatch(writeBatch); peerWriteBatch.Remove(netMsg.Param.wbid, out writeBatch); p = new Param() { result = true }; } netMsgBack = NetMessage.Create(cmd, p, id); return(netMsgBack); } case "_db.getvalue": //使用最新的snapshot 基本就是给apiserver用的 { ISnapShot snapshot = StorageService.maindb.UseSnapShot(); var tableid = netMsg.Param.tableid; var key = netMsg.Param.key; var value = snapshot.GetValueData(tableid, key); Param param = new Param(); param.snapid = snapshot.DataHeight; param.result = true; param.value = value; param.key = key; param.tableid = tableid; netMsgBack = NetMessage.Create(cmd, param, id); return(netMsgBack); } case "_db.snapshot.getvalue": { ISnapShot snapshot = peerSnapshots[netMsg.Param.snapid]; var tableid = netMsg.Param.tableid; var key = netMsg.Param.key; var value = snapshot.GetValueData(tableid, key); Param param = new Param(); param.snapid = snapshot.DataHeight; param.result = true; param.value = value; param.key = key; param.tableid = tableid; netMsgBack = NetMessage.Create(cmd, param, id); return(netMsgBack); } case "_db.snapshot.newiterator": { ISnapShot snapshot = peerSnapshots[netMsg.Param.snapid]; var beginKey = netMsg.Param.key; var endKey = netMsg.Param.value; var tableid = netMsg.Param.tableid; var iter = snapshot.CreateKeyIterator(tableid, beginKey, endKey); var itid = iter.HandleID; peerKeyIterator[itid] = iter; var p = new Param() { result = true, itid = itid, snapid = netMsg.Param.snapid }; netMsgBack = NetMessage.Create(cmd, p, id); return(netMsgBack); } case "_db.iterator.current": { IKeyIterator keyIterator = peerKeyIterator[netMsg.Param.itid]; var cur = keyIterator.Current; Param param = new Param(); param.itid = netMsg.Param.itid; param.result = true; param.value = cur; netMsgBack = NetMessage.Create(cmd, param, id); return(netMsgBack); } case "_db.iterator.next": { IKeyIterator keyIterator = peerKeyIterator[netMsg.Param.itid]; var result = keyIterator.MoveNext(); Param param = new Param(); param.itid = netMsg.Param.itid; param.result = result; netMsgBack = NetMessage.Create(cmd, param, id); return(netMsgBack); } case "_db.iterator.seektofirst": { IKeyIterator keyIterator = peerKeyIterator[netMsg.Param.itid]; keyIterator.SeekToFirst(); var p = new Param() { result = true, itid = netMsg.Param.itid }; netMsgBack = NetMessage.Create(cmd, p, id); return(netMsgBack); } case "_db.iterator.reset": { IKeyIterator keyIterator = peerKeyIterator[netMsg.Param.itid]; keyIterator.Reset(); Param param = new Param(); param.result = true; param.itid = netMsg.Param.itid; netMsgBack = NetMessage.Create(cmd, param, id); return(netMsgBack); } default: throw new Exception("unknown msg cmd:" + netMsg.Cmd); } } catch (Exception e) { netMsgBack = NetMessage.Create(cmd, new Param() { error = Encoding.UTF8.GetBytes(e.Message) }, id); return(netMsgBack); } } }