Ejemplo n.º 1
0
 public static LevelDB.ReadOptions CreateSnapshot(LevelDB.DB db)
 {
     return(new LevelDB.ReadOptions()
     {
         Snapshot = db.CreateSnapshot()
     });
 }
Ejemplo n.º 2
0
 public void GetIterable()
 {
     using var log = Log();
     using LevelDB.DB <string, string> db = GetTestDb();
     Assert.Equal(
         "a=1;b=2;c=3;l=12;m=13;n=14;x=24;y=25;z=26",
         string.Join(";", db.GetIterable().Select(kv => $"{kv.Key}={kv.Value}")));
 }
Ejemplo n.º 3
0
 public void GetIterableRangeReverse()
 {
     using var log = Log();
     using LevelDB.DB <string, string> db = GetTestDb();
     Assert.Equal(
         "y=25;x=24;n=14;m=13;l=12",
         string.Join(";", db.GetIterable().Range("c", "y").Reverse().Select(kv => $"{kv.Key}={kv.Value}")));
 }
Ejemplo n.º 4
0
 public BedrockWorld(string folder) : base(folder)
 {
     BedrockDB = new LevelDB.DB(new LevelDB.Options()
     {
         CreateIfMissing = false
     }, Path.Combine(folder, "db"));
     Name = File.ReadAllText(Path.Combine(Folder, "levelname.txt"));
 }
Ejemplo n.º 5
0
 public void Get()
 {
     using var log = Log();
     using LevelDB.DB <string, string> db = GetTestDb();
     Assert.Equal("1", db.Get("a"));
     Assert.Equal("26", db.Get("z"));
     Assert.Equal("1", db["a"]);
     Assert.Equal("26", db["z"]);
 }
Ejemplo n.º 6
0
        public MapIterator(LevelDB.DB db, LevelDB.ReadOptions snapshot, byte[] head)
        {
            this.db       = db;
            this.snapshot = snapshot;
            this.head     = head;

            this.it = new LevelDB.Iterator(db, snapshot);
            it.Seek(head);
        }
Ejemplo n.º 7
0
 public void Delete()
 {
     using var log = Log();
     using LevelDB.DB <string, string> db = GetTestDb();
     db.Delete("a").Delete("c").Delete("m").Delete("n");
     Assert.Equal(
         "b=2;l=12;x=24;y=25;z=26",
         string.Join(";", db.GetIterable().Select(kv => $"{kv.Key}={kv.Value}")));
 }
Ejemplo n.º 8
0
 public WriteBatch(LevelDB.DB _db, LevelDB.ReadOptions _snapshot = null)
 {
     this.db       = _db;
     this.snapshot = _snapshot;
     if (this.snapshot == null)
     {
         this.snapshot = Helper.CreateSnapshot(db);
     }
     this.wb = new LevelDB.WriteBatch();
     cache   = new System.Collections.Concurrent.ConcurrentDictionary <System.Numerics.BigInteger, byte[]>();
 }
Ejemplo n.º 9
0
        private LevelDB.DB <string, string> GetTestDb([CallerMemberName] string name = null)
        {
            LevelDB.Options options = new LevelDB.Options();
            options.CreateIfMissing = true;
            var db = new LevelDB.DB(options, $"/tmp/DBTests-{name}").Cast <string, string>();

            db.Put("c", "3").Put("b", "2").Put("a", "1");
            db.Put("l", "12").Put("m", "13").Put("n", "14");
            db.Put("z", "26").Put("y", "25").Put("x", "24");
            return(db);
        }
Ejemplo n.º 10
0
 public void Infinity()
 {
     using var log = Log();
     using LevelDB.DB <string, string> db = GetTestDb();
     Assert.Equal(
         "y=25;z=26",
         string.Join(";", db.GetIterable().Range("y", null).Select(kv => $"{kv.Key}={kv.Value}")));
     Assert.Equal(
         "z=26;y=25",
         string.Join(";", db.GetIterable().Reverse().Range(null, "x").Select(kv => $"{kv.Key}={kv.Value}")));
 }
Ejemplo n.º 11
0
 //table不是数据结构,只是给存储的数据加上前缀
 public static Table GetTable(LevelDB.DB db, byte[] tablename)
 {
     if (tablename == null || tablename.Length == 0)
     {
         return(new Table(db, new byte[0]));
     }
     if (tablename.Contains((byte)0x00))
     {
         throw new Exception("not a vaild tablename");
     }
     return(new Table(db, tablename));
 }
Ejemplo n.º 12
0
 public void Integers()
 {
     using var log = Log();
     LevelDB.Options options = new LevelDB.Options();
     options.CreateIfMissing = true;
     using LevelDB.DB <int, string> db = new LevelDB.DB(options, $"/tmp/DBTests-Integers").Cast <int, string>();
     db.Put(1, "aaa").Put(2, "bbb").Put(3, "ccc");
     db.Put(7, "ggg").Put(8, "hhh").Put(9, "iii");
     db.Put(6, "fff").Put(5, "eee").Put(4, "ddd");
     Assert.Equal(
         "4=ddd;5=eee;6=fff;7=ggg",
         string.Join(";", db.GetIterable().Range(4, 8).Select(kv => $"{kv.Key}={kv.Value}")));
 }
Ejemplo n.º 13
0
        public void Join()
        {
            using var log = Log();
            LevelDB.Options options = new LevelDB.Options();
            options.CreateIfMissing = true;
            using LevelDB.DB db     = new LevelDB.DB(options, $"/tmp/DBTests-Join");
            var parents  = db.Cast <Parent, string>();
            var children = db.Cast <Child, string>();

            parents.Put(new Parent(1), "aaa");
            parents.Put(new Parent(2), "bbb");
            children.Put(new Child(1, 11), "afirst");
            children.Put(new Child(1, 12), "asecond");
            children.Put(new Child(2, 21), "afirst");
            children.Put(new Child(2, 22), "asecond");

            {
                var join =
                    from parent
                    in parents.GetIterable().Range(new Parent(1), new Parent(3))
                    join child
                    in children.GetIterable().Range(new Child(1, 1), new Child(3, 1))
                    on parent.Key.parentId equals child.Key.parentId
                    select $"[{parent.Value} => {child.Key.parentId}:{child.Key.childId}:{child.Value}]";
                Assert.Equal(
                    "[aaa => 1:11:afirst]; [aaa => 1:12:asecond]; [bbb => 2:21:afirst]; [bbb => 2:22:asecond]",
                    string.Join("; ", join));
            }
            {
                IEnumerable <KeyValuePair <Tuple <Parent, Child>, Tuple <string, string> > > streamJoin =
                    parents.GetIterable().Range(new Parent(1), new Parent(3))
                    .Join(
                        children.GetIterable().Range(new Child(1, 1), new Child(3, 1)),
                        (k1, k2) => k1.parentId.CompareTo(k2.parentId));
                Assert.Equal(
                    "1/11 => aaa/afirst; 1/12 => aaa/asecond; 2/21 => bbb/afirst; 2/22 => bbb/asecond",
                    string.Join("; ", streamJoin.Select(row => $"{row.Key.Item1.parentId}/{row.Key.Item2.childId} => {row.Value.Item1}/{row.Value.Item2}")));
            }
            {
                IMergeJoinIterable <Parent, Child, string, string> streamJoin =
                    Tuple.Create(
                        parents.GetIterable().Range(new Parent(1), new Parent(3)),
                        children.GetIterable().Range(new Child(1, 1), new Child(3, 1)))
                    .Join2(sizeof(int), sizeof(int));
                Assert.Equal(
                    "1/11 => aaa/afirst; 1/12 => aaa/asecond; 2/21 => bbb/afirst; 2/22 => bbb/asecond",
                    string.Join("; ", streamJoin.Select(row => $"{row.Key.Left.parentId}/{row.Key.Right.childId} => {row.Value.Left}/{row.Value.Right}")));
            }
        }
Ejemplo n.º 14
0
        public void Snapshot()
        {
            using var log = Log();
            using LevelDB.DB <string, string> db = GetTestDb();
            IIterable <string, string> it        = db.GetIterable().Snapshot();

            db.Delete("a").Delete("c").Delete("m").Delete("n");
            Assert.Equal(
                "a=1;b=2;c=3;l=12;m=13;n=14;x=24;y=25;z=26",
                string.Join(";", it.Select(kv => $"{kv.Key}={kv.Value}")));
            it.Snapshot();
            Assert.Equal(
                "b=2;l=12;x=24;y=25;z=26",
                string.Join(";", it.Select(kv => $"{kv.Key}={kv.Value}")));
        }
Ejemplo n.º 15
0
        public void Start(string dbPath, byte[] magic)
        {
            var curlibVersion = this.GetType().Assembly.GetName().Version;

            Console.WriteLine("zoro.one V" + curlibVersion);

            db = LevelDB.Ex.Helper.OpenDB(dbPath);

            this.blockChainMagic = magic;

            dbTable = new LevelDB.Ex.Table(db, magic);

            InitBlock();
            System.Threading.Thread t = new System.Threading.Thread(TimerThread);
            t.IsBackground = true;//设置为后台线程,主程序退出这个线程就会玩完儿了,不用特别管他
            t.Start();
        }
Ejemplo n.º 16
0
        public void Init(LevelDB.DB db, byte[] data)
        {
            this.db = db;
            if (data == null || data.Length == 0)
            {
                throw new Exception("error map in Init");
            }

            var type = (Value_DataType)data[0];

            if (type != Value_DataType.Map)
            {
                throw new Exception("error map in init.");
            }

            this.Value = data.Skip(1).ToArray();
        }
Ejemplo n.º 17
0
        public static string ChromiumLevelDBReadString(LevelDB.DB database, string url, string keyName)
        {
            foreach (var a in database)
            {
                var str = System.Text.Encoding.ASCII.GetString(a.Key);
                Console.WriteLine(str);
            }

            var rawKeyName = $"_{url}\0\u0001{keyName}";
            var rawValue   = database.Get(rawKeyName);

            if (rawValue == null)
            {
                throw new KeyNotFoundException();
            }
            return(rawValue.Replace("\u0001", "").TrimStart(new char[] { '"' }).TrimEnd(new char[] { '"' }));
        }
Ejemplo n.º 18
0
        public void PutToDB(LevelDB.DB db, byte[] key)
        {
            var snapshot = Helper.CreateSnapshot(db);

            if (this.Value == null)
            {//申请新的实例ID,然后初始化存储Map
                var key_instMax = Helper.tagKey_InstanceMax;
                var instid      = db.Get(snapshot, key_instMax);
                if (instid == null || instid.Length == 0)
                {
                    instid = BitConverter.GetBytes((UInt64)1);
                }

                this.Value = instid;
                this.db    = db;
                //刷新max
                {
                    UInt64 v = BitConverter.ToUInt64(instid, 0);
                    v++;
                    instid = BitConverter.GetBytes((UInt64)v);
                    db.Put(key_instMax, instid);
                }

                //初始化字典数量
                byte[] key_count = Helper.tagKey_MapCount.Concat(this.Value).ToArray();
                db.Put(key_count, BitConverter.GetBytes((UInt64)0));
            }
            else
            {//检查count是否存在,
                byte[] key_count = Helper.tagKey_MapCount.Concat(this.Value).ToArray();
                var    count     = db.Get(snapshot, key_count);
                if (count == null || count.Length == 0)
                {
                    throw new Exception("error map instance.");
                }
            }

            db.Put(key, Helper.tagValue_Map.Concat(this.Value).ToArray());
        }
Ejemplo n.º 19
0
        public static IValue CreateValue(LevelDB.DB db, byte[] data)
        {
            if (data == null || data.Length == 0)
            {
                return(null);
            }
            IValue value = null;

            if (data[0] == (byte)Value_DataType.Bytes)
            {
                value = new Bytes();
            }
            if (data[0] == (byte)Value_DataType.Map)
            {
                value = new Map();
            }
            if (value == null)
            {
                throw new Exception("unknown datatype.");
            }
            (value as IValueCreator).Init(db, data);
            return(value);
        }
Ejemplo n.º 20
0
        private static string TryGetAuthIDFromDiscordStorage()
        {
            string tmpDir      = null;
            string returnValue = null;

            try
            {
                tmpDir = Utils.GetTemporaryDirectory();

                var discordLocalStoragePath = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"discord\Local Storage\leveldb");

                Utils.CopyFilesRecursively(discordLocalStoragePath, tmpDir);
                using (var db = new LevelDB.DB(new LevelDB.Options
                {
                    CreateIfMissing = false
                }, tmpDir))
                {
                    try
                    {
                        returnValue = Utils.ChromiumLevelDBReadString(db, "https://discordapp.com", "token");
                    }
                    catch (KeyNotFoundException)
                    {
                        returnValue = Utils.ChromiumLevelDBReadString(db, "https://discord.com", "token");
                    }
                }
            }
            catch { } //Well, too bad but at least we've tried

            if (tmpDir != null)
            {
                try { Directory.Delete(tmpDir, true); } catch { }
            }

            return(returnValue);
        }
Ejemplo n.º 21
0
 public void Prefix()
 {
     using var log = Log();
     LevelDB.Options options = new LevelDB.Options();
     options.CreateIfMissing = true;
     using var db            = new LevelDB.DB(options, $"/tmp/DBTests-Prefix").Cast <string, int>();
     db
     .Put("a", 1)
     .Put("aa", 2)
     .Put("ab", 3)
     .Put("b", 4)
     .Put("ba", 5)
     .Put("bb", 6)
     .Put("c", 7);
     Assert.Equal(
         "a=1;aa=2;ab=3",
         string.Join(";", db.GetIterable().Prefix("a").Select(kv => $"{kv.Key}={kv.Value}")));
     Assert.Equal(
         "b=4;ab=3;aa=2",
         string.Join(";", db.GetIterable().Reverse().Prefix("a").Select(kv => $"{kv.Key}={kv.Value}")));
     Assert.Equal(
         "b=4;ab=3;aa=2",
         string.Join(";", db.GetIterable().Prefix("a").Reverse().Select(kv => $"{kv.Key}={kv.Value}")));
 }
Ejemplo n.º 22
0
 public override void Initialize()
 {
     db = OpenLevelDB(Path);
 }
Ejemplo n.º 23
0
 public void Batch_PutToDB(LevelDB.WriteBatch batch, LevelDB.DB db, byte[] key)
 {
     throw new NotSupportedException();
     batch.Put(key, this.Value);
 }
Ejemplo n.º 24
0
 public LevelDBKeyValueStorage(string path)
 {
     this.path = path;
     db        = OpenLevelDB(path);
 }
Ejemplo n.º 25
0
 public Table(LevelDB.DB db, byte[] tablename)
 {
     this.db     = db;
     this.prefix = tablename;
 }