Ejemplo n.º 1
0
        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);
                }
            }
        }
Ejemplo n.º 2
0
 public override void DeleteInternal(TKey key)
 {
     batch?.Delete(new byte[] { tableId }, key.Serialize());
 }
Ejemplo n.º 3
0
        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);
                }
            }
        }