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(); } }
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(); } }
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(); } }
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(); } }