Beispiel #1
0
        public static void test_put(string[] args)
        {
            //
            //DBTests tests = new DBTests();
            //tests.SetUp();
            //tests.Snapshot();
            var          tempPath     = System.IO.Directory.GetCurrentDirectory();
            var          randName     = "LevelDB";
            var          DatabasePath = System.IO.Path.Combine(tempPath, randName);
            LevelDBStore dbstore      = new LevelDBStore().Init(DatabasePath);

            //Êý¾ÝдÈë²âÊÔ
            System.Diagnostics.Stopwatch sp = new System.Diagnostics.Stopwatch();
            sp.Reset();
            sp.Start();
            int mCount = 0;

            while (true)
            {
                dbstore.Put(mCount.ToString(), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaeraaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
                if (System.Threading.Interlocked.Increment(ref mCount) % 10000 == 0)
                {
                    System.Console.WriteLine($"{mCount} has inserted. time use {sp.ElapsedMilliseconds}ms.");
                }
            }
        }
        static public void MakeSnapshot(Dictionary <string, string> param)
        {
            Console.WriteLine($"levelDB.Init {param["db"]}");
            LevelDBStore levelDB = new LevelDBStore();

            levelDB.Init(param["db"]);

            if (param.ContainsKey("height") && long.TryParse(param["height"], out long height))
            {
                levelDB.UndoTransfers(height);
            }
            long.TryParse(levelDB.Get("UndoHeight"), out long transferHeight);
            Console.WriteLine($"transferHeight: {transferHeight}");

            var DatabasePath = $"./Data/LevelDB_Snapshot_{transferHeight}";

            if (Directory.Exists(DatabasePath))
            {
                Console.WriteLine($"Directory LevelDB_Snapshot Exists");
                return;
            }
            LevelDBStore snapshotDB = new LevelDBStore();

            snapshotDB.Init(DatabasePath);

            int count = 0;

            using (var it = levelDB.db.CreateIterator())
            {
                for (it.SeekToFirst(); it.IsValid(); it.Next(), count++)
                {
                    //Log.Info($"Value as string: {it.KeyAsString()}");
                    if (it.KeyAsString().IndexOf("_undo_") == -1 &&
                        it.KeyAsString().IndexOf("Blocks") != 0 &&
                        it.KeyAsString().IndexOf("BlockChain") != 0 &&
                        it.KeyAsString().IndexOf("Queue") != 0 &&
                        it.KeyAsString().IndexOf("List") != 0 &&
                        it.KeyAsString().IndexOf("Heights") != 0 &&
                        it.KeyAsString().IndexOf("Undos___") != 0)
                    {
                        if (it.KeyAsString().IndexOf("Trans___") == 0)
                        {
                            var slice = JsonHelper.FromJson <DbCache <BlockSub> .Slice>(it.ValueAsString());
                            if (slice != null && slice.obj.height != 0)
                            {
                                snapshotDB.Put(it.KeyAsString(), $"{{\"obj\":{{\"height\":{slice.obj.height}}}}}");
                                Console.WriteLine($"Processed tran: {it.KeyAsString()}");
                            }
                        }
                        else
                        if (it.KeyAsString().IndexOf("Snap___") == 0)
                        {
                            if (it.KeyAsString().IndexOf("Snap___Rule_") == 0)
                            {
                                var key         = it.KeyAsString();
                                var pos1        = "Snap___Rule_".Length;
                                var pos2        = key.Length;
                                var hegihtTemp1 = key.Substring(pos1, pos2 - pos1);
                                var hegihtTemp2 = long.Parse(hegihtTemp1);
                                if (hegihtTemp2 > transferHeight - 5 && hegihtTemp2 < transferHeight + 5)
                                {
                                    snapshotDB.Put(it.KeyAsString(), it.ValueAsString());
                                    Console.WriteLine($"Processed  key: {it.KeyAsString()}");
                                }
                            }
                            else
                            if (it.KeyAsString().IndexOf("_Reward") != -1)
                            {
                                var key  = it.KeyAsString();
                                var pos1 = "Snap___".Length;
                                var pos2 = key.IndexOf("_Reward");

                                var hegihtTemp1 = key.Substring(pos1, pos2 - pos1);
                                var hegihtTemp2 = long.Parse(hegihtTemp1);
                                if (hegihtTemp2 > transferHeight - 5 && hegihtTemp2 < transferHeight + 5)
                                {
                                    snapshotDB.Put(it.KeyAsString(), it.ValueAsString());
                                    Console.WriteLine($"Processed  key: {it.KeyAsString()}");
                                }
                            }
                            else
                            {
                                snapshotDB.Put(it.KeyAsString(), it.ValueAsString());
                                Console.WriteLine($"Processed  key: {it.KeyAsString()}");
                            }
                        }
                        else
                        {
                            snapshotDB.Put(it.KeyAsString(), it.ValueAsString());
                            Console.WriteLine($"Processed  key: {it.KeyAsString()}");
                        }
                    }

                    if (count % 1000000 == 0)
                    {
                        Console.WriteLine($"Processed Count:{count}");
                    }
                }
            }

            using (DbSnapshot dbNew = snapshotDB.GetSnapshot(0, true))
                using (DbSnapshot dbOld = levelDB.GetSnapshot())
                {
                    for (long ii = transferHeight - 3; ii <= transferHeight + 2; ii++)
                    {
                        Console.WriteLine($"Processed height: {ii}");
                        var heights = dbOld.Heights.Get(ii.ToString());
                        for (int jj = 0; jj < heights.Count; jj++)
                        {
                            dbNew.Blocks.Add(heights[jj], dbOld.Blocks.Get(heights[jj]));
                        }

                        dbNew.Heights.Add(ii.ToString(), heights);
                        dbNew.BlockChains.Add(ii.ToString(), dbOld.BlockChains.Get(ii.ToString()));
                    }

                    dbNew.Commit();
                }

            Console.WriteLine($"MakeSnapshot Complete");

            while (true)
            {
                System.Threading.Thread.Sleep(1000);
            }
        }