public DbSnapshot(DB db, long height, bool bUndo)
        {
            Monitor.Enter(db);

            this.db       = db;
            this.snapshot = db.CreateSnapshot();
            this.batch    = new WriteBatch();
            this.options  = new ReadOptions {
                FillCache = false, Snapshot = snapshot
            };

            if (bUndo)
            {
                Undos        = new DbUndo();
                Undos.height = height;
                long.TryParse(db.Get("UndoHeight"), out long UndoHeight);
                if (UndoHeight + 1 != Undos.height)
                {
                    throw new InvalidOperationException($"{UndoHeight} heightTotal+1 != Undos.height {Undos.height}");
                }
            }

            Snap            = new DbCache <string>(db, options, batch, Undos, "Snap");
            Blocks          = new DbCache <Block>(db, options, batch, null, "Blocks");
            Heights         = new DbCache <List <string> >(db, options, batch, null, "Heights");
            Transfers       = new DbCache <BlockSub>(db, options, batch, Undos, "Trans");
            Accounts        = new DbCache <Account>(db, options, batch, Undos, "Accounts");
            Contracts       = new DbCache <LuaVMScript>(db, options, batch, Undos, "Contracts");
            Storages        = new DbCache <LuaVMContext>(db, options, batch, Undos, "Storages");
            BlockChains     = new DbCache <BlockChain>(db, options, batch, Undos, "BlockChain");
            StoragesAccount = new DbCache <string>(db, options, batch, Undos, "StgAcc");
            ABC             = new DbCache <List <string> >(db, options, batch, Undos, "ABC");
            List            = new DbList <string>(db, options, batch, Undos, "List");
        }
 public DbCacheSerializable(DB db, ReadOptions options, WriteBatch batch, DbUndo undos, string prefix)
 {
     this.db      = db;
     this.options = options ?? DbSnapshot.ReadOptions_Default;
     this.batch   = batch;
     this.prefix  = prefix;
     this.undos   = undos;
 }
Exemple #3
0
 public DbList(DB db, ReadOptions options, WriteBatch batch, DbUndo undos, string prefix)
 {
     this.db       = db;
     this.options  = options ?? DbSnapshot.ReadOptions_Default;
     this.batch    = batch;
     this.prefix   = prefix;
     this.undos    = undos;
     this.isString = typeof(TValue).Name == "String";
 }
        // ------------------------------------------
        public void UndoTransfers(long height)
        {
            height = Math.Max(height, 1);
            long.TryParse(Get("UndoHeight"), out long height_total);
            if (height == height_total)
            {
                return;
            }

            Log.Debug($"UndoTransfers {height_total} to {height}");

            WriteBatch batch = new WriteBatch();
            long       ii    = height_total;

            for ( ; ii > height; ii--)
            {
                string Undos_Value = db.Get($"Undos___{ii}");
                if (Undos_Value != null)
                {
                    DbUndo undos = JsonHelper.FromJson <DbCache <DbUndo> .Slice>(Undos_Value).obj;
                    for (int jj = 0; jj < undos.keys.Count; jj++)
                    {
                        string key   = $"{undos.keys[jj]}_undo_{undos.height}";
                        string value = db.Get(key);
                        if (value != null && value != "")
                        {
                            batch.Put(undos.keys[jj], value);
                        }
                        else
                        {
                            batch.Delete(undos.keys[jj]);
                        }
                        batch.Delete(key);
                    }
                    batch.Delete($"Undos___{ii}");
                }
                else
                {
                    break;
                }
            }

            batch.Put("UndoHeight", System.Math.Min(ii, height_total).ToString());

            db.Write(batch, new WriteOptions {
                Sync = true
            });
            batch?.Dispose();

            Entity.Root.GetComponent <Consensus>()?.cacheRule.Clear();
        }