예제 #1
0
        public void Test3()
        {
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mdbx2");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            using (MdbxEnvironment env = new MdbxEnvironment())
            {
                EnvironmentFlag flags = EnvironmentFlag.NoTLS |
                                        EnvironmentFlag.NoMetaSync |
                                        EnvironmentFlag.Coalesce |
                                        EnvironmentFlag.LifoReclaim;
                env.SetMaxDatabases(20)
                .SetMaxReaders(128)
                .SetMapSize(10485760 * 10)
                .Open(path, flags, Convert.ToInt32("666", 8));


                DatabaseOption option = DatabaseOption.Create | DatabaseOption.IntegerKey;
                // add some values
                using (MdbxTransaction tran = env.BeginTransaction())
                {
                    MdbxDatabase db = tran.OpenDatabase("cursor_test3", option);

                    for (long i = 0; i < 1000000; i++)
                    {
                        db.Put(i, Guid.NewGuid().ToByteArray());
                    }

                    tran.Commit();
                }

                using (MdbxTransaction tran = env.BeginTransaction(TransactionOption.ReadOnly))
                {
                    MdbxDatabase db = tran.OpenDatabase("cursor_test3", option);
                    using (MdbxCursor cursor = db.OpenCursor())
                    {
                        long   key   = 0;
                        byte[] value = null;
                        cursor.Get(ref key, ref value, CursorOp.First);

                        long index = 0;
                        Assert.Equal(index, key);

                        key   = 0;
                        value = null;
                        while (cursor.Get(ref key, ref value, CursorOp.Next))
                        {
                            index++;
                            Assert.Equal(index, key);
                        }
                    }
                }

                env.Close();
            }
        }
예제 #2
0
        public void Test4()
        {
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mdbx");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            using (MdbxEnvironment env = new MdbxEnvironment())
            {
                env.Open(path, EnvironmentFlag.NoTLS, Convert.ToInt32("666", 8));

                string key   = Guid.NewGuid().ToString("N");                      // some key
                byte[] value = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()); // some value in bytes


                // mdbx_get
                using (MdbxTransaction tran = env.BeginTransaction(TransactionOption.ReadOnly))
                {
                    MdbxDatabase db = tran.OpenDatabase();

                    byte[] getBytes = db.Get(key);
                    Assert.Null(getBytes);
                }


                // mdbx_put
                using (MdbxTransaction tran = env.BeginTransaction())
                {
                    MdbxDatabase db = tran.OpenDatabase();
                    db.Put(key, value);
                    tran.Commit();
                }


                // mdbx_get
                using (MdbxTransaction tran = env.BeginTransaction(TransactionOption.ReadOnly))
                {
                    MdbxDatabase db = tran.OpenDatabase();

                    byte[] getBytes = db.Get(key);
                    Assert.NotNull(getBytes);
                    Assert.Equal(value.Length, getBytes.Length);
                    Assert.Equal(value, getBytes);
                }


                env.Close();
            }
        }
예제 #3
0
        public void Test2()
        {
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mdbx");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            using (MdbxEnvironment env = new MdbxEnvironment())
            {
                env.Open(path, EnvironmentFlag.NoTLS, Convert.ToInt32("666", 8));

                var putBytes = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString());

                // mdbx_put
                using (MdbxTransaction tran = env.BeginTransaction())
                {
                    MdbxDatabase db = tran.OpenDatabase();
                    db.Put(putBytes, putBytes);
                    tran.Commit();
                }


                // mdbx_get
                using (MdbxTransaction tran = env.BeginTransaction(TransactionOption.ReadOnly))
                {
                    MdbxDatabase db = tran.OpenDatabase();

                    byte[] getBytes = db.Get(putBytes);
                    Assert.NotNull(getBytes);
                    Assert.Equal(putBytes.Length, getBytes.Length);
                    Assert.Equal(putBytes, getBytes);
                }

                // mdbx_del
                using (MdbxTransaction tran = env.BeginTransaction())
                {
                    MdbxDatabase db      = tran.OpenDatabase();
                    bool         deleted = db.Del(putBytes);
                    Assert.True(deleted);
                    deleted = db.Del(putBytes);
                    Assert.False(deleted);
                    tran.Commit();
                }


                env.Close();
            }
        }
예제 #4
0
        public void Test3()
        {
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mdbx");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            // register serializer for our custom type
            SerializerRegistry.Register(new BasicTest3PayloadSerializer());

            using (MdbxEnvironment env = new MdbxEnvironment())
            {
                env.Open(path, EnvironmentFlag.NoTLS, Convert.ToInt32("666", 8));


                // mdbx_put
                using (MdbxTransaction tran = env.BeginTransaction())
                {
                    MdbxDatabase db = tran.OpenDatabase();
                    db.Put("ana_key", new BasicTest3Payload()
                    {
                        Person = "Ana", Age = 50
                    });
                    tran.Commit();
                }


                // mdbx_get
                using (MdbxTransaction tran = env.BeginTransaction(TransactionOption.ReadOnly))
                {
                    MdbxDatabase db = tran.OpenDatabase();

                    BasicTest3Payload payload = db.Get <string, BasicTest3Payload>("ana_key");
                    Assert.NotNull(payload);
                    Assert.Equal("Ana", payload.Person);
                    Assert.Equal(50, payload.Age);
                }



                env.Close();
            }
        }
예제 #5
0
        public void Test2()
        {
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mdbx");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            using (MdbxEnvironment env = new MdbxEnvironment())
            {
                env.SetMaxDatabases(20)
                .SetMaxReaders(128)
                .Open(path, EnvironmentFlag.NoTLS, Convert.ToInt32("666", 8));


                using (MdbxTransaction tran = env.BeginTransaction())
                {
                    MdbxDatabase db = tran.OpenDatabase("cursor_test2", DatabaseOption.Create
                                                        | DatabaseOption.IntegerKey /*opitimized for fixed size int or long key*/
                                                        );
                    db.Empty();                                                     // clean this data table for test

                    // add some keys
                    for (int i = 0; i < 5; i++)
                    {
                        db.Put(i + 1, (i + 1).ToString());
                    }

                    tran.Commit();
                }

                using (MdbxTransaction tran = env.BeginTransaction())
                {
                    MdbxDatabase db = tran.OpenDatabase("cursor_test2");
                    using (MdbxCursor cursor = db.OpenCursor())
                    {
                        cursor.Put(2, "2a"); // update by key

                        int    key   = 0;
                        string value = null;
                        cursor.Get(ref key, ref value, CursorOp.Next); // move to next

                        Assert.Equal(3, key);

                        cursor.Del();  // delete current one

                        key   = 0;
                        value = null;
                        cursor.Get(ref key, ref value, CursorOp.GetCurrent);
                        Assert.Equal(4, key);

                        key   = 0;
                        value = null;
                        cursor.Get(ref key, ref value, CursorOp.Prev);
                        Assert.Equal(2, key);
                        Assert.Equal("2a", value);
                    }

                    tran.Commit();
                }

                env.Close();
            }
        }
예제 #6
0
        public void Test1()
        {
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mdbx");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            using (MdbxEnvironment env = new MdbxEnvironment())
            {
                env.SetMaxDatabases(20)
                .SetMaxReaders(128)
                .Open(path, EnvironmentFlag.NoTLS, Convert.ToInt32("666", 8));


                using (MdbxTransaction tran = env.BeginTransaction())
                {
                    MdbxDatabase db = tran.OpenDatabase("cursor_test1", DatabaseOption.Create);
                    db.Empty(); // clean this data table for test

                    string[] keys = new string[]
                    {
                        "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
                        "A", "S", "D", "F", "G", "H", "J", "K", "L",
                        "Z", "X", "C", "V", "B", "N", "M"
                    };

                    // add some keys
                    foreach (string key in keys)
                    {
                        db.Put(key, key);
                    }

                    tran.Commit();
                }

                using (MdbxTransaction tran = env.BeginTransaction(TransactionOption.ReadOnly))
                {
                    MdbxDatabase db = tran.OpenDatabase("cursor_test1");
                    using (MdbxCursor cursor = db.OpenCursor())
                    {
                        string key = null, value = null;
                        cursor.Get(ref key, ref value, CursorOp.First);

                        char c = 'A';
                        Assert.Equal(c.ToString(), key);
                        Assert.Equal(c.ToString(), value);

                        while (cursor.Get(ref key, ref value, CursorOp.Next))
                        {
                            c = (char)((int)c + 1);
                            Assert.Equal(c.ToString(), key);
                            Assert.Equal(c.ToString(), value);
                        }
                    }
                }

                env.Close();
            }
        }
예제 #7
0
        public void Test1()
        {
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mdbx");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            using (MdbxEnvironment env = new MdbxEnvironment())
            {
                env.SetMaxDatabases(10) /* allow us to use a different db for testing */
                .Open(path, EnvironmentFlag.NoTLS, Convert.ToInt32("666", 8));

                DatabaseOption option = DatabaseOption.Create /* needed to create a new db if not exists */
                                        | DatabaseOption.IntegerKey /* opitimized for fixed key */;

                // mdbx_put
                using (MdbxTransaction tran = env.BeginTransaction())
                {
                    MdbxDatabase db = tran.OpenDatabase("basic_op_test", option);
                    db.Put(10L, "ten");
                    db.Put(1000L, "thousand");
                    db.Put(1000000000L, "billion");
                    db.Put(1000000L, "million");
                    db.Put(100L, "hundred");
                    db.Put(1L, "one");
                    tran.Commit();
                }


                // mdbx_get
                using (MdbxTransaction tran = env.BeginTransaction(TransactionOption.ReadOnly))
                {
                    MdbxDatabase db = tran.OpenDatabase("basic_op_test", option);

                    string text = db.Get <long, string>(1000000L);
                    Assert.NotNull(text);
                    Assert.Equal("million", text);
                }

                // mdbx_del
                using (MdbxTransaction tran = env.BeginTransaction())
                {
                    MdbxDatabase db      = tran.OpenDatabase("basic_op_test", option);
                    bool         deleted = db.Del(100L);
                    Assert.True(deleted);
                    deleted = db.Del(100L);
                    Assert.False(deleted);
                    tran.Commit();
                }


                // mdbx_get
                using (MdbxTransaction tran = env.BeginTransaction(TransactionOption.ReadOnly))
                {
                    MdbxDatabase db = tran.OpenDatabase("basic_op_test", option);

                    string text = db.Get <long, string>(100L);
                    Assert.Null(text);
                }

                env.Close();
            }
        }