예제 #1
0
        public void Snapshot()
        {
            // modify db
            var writeOptions = Native.leveldb_writeoptions_create();

            Native.leveldb_put(Database, writeOptions, "key1", "value1");
            Native.leveldb_writeoptions_destroy(writeOptions);

            // create snapshot
            var snapshot = Native.leveldb_create_snapshot(Database);

            // modify db again
            writeOptions = Native.leveldb_writeoptions_create();
            Native.leveldb_put(Database, writeOptions, "key2", "value2");
            Native.leveldb_writeoptions_destroy(writeOptions);

            // read from snapshot
            var readOptions = Native.leveldb_readoptions_create();

            Native.leveldb_readoptions_set_snapshot(readOptions, snapshot);
            var val1 = Native.leveldb_get(Database, readOptions, "key1");

            Assert.AreEqual("value1", val1);
            var val2 = Native.leveldb_get(Database, readOptions, "key2");

            Assert.IsNull(val2);
            Native.leveldb_readoptions_destroy(readOptions);

            // release snapshot
            Native.leveldb_release_snapshot(Database, snapshot);
            snapshot = IntPtr.Zero;
        }
예제 #2
0
        public void Enumerator()
        {
            var writeOptions = Native.leveldb_writeoptions_create();

            Native.leveldb_put(Database, writeOptions, "key1", "value1");
            Native.leveldb_put(Database, writeOptions, "key2", "value2");
            Native.leveldb_put(Database, writeOptions, "key3", "value3");

            var    entries     = new List <KeyValuePair <string, string> >();
            var    readOptions = Native.leveldb_readoptions_create();
            IntPtr iter        = Native.leveldb_create_iterator(Database, readOptions);

            for (Native.leveldb_iter_seek_to_first(iter);
                 (int)Native.leveldb_iter_valid(iter) != 0;
                 Native.leveldb_iter_next(iter))
            {
                string key   = Native.leveldb_iter_key(iter);
                string value = Native.leveldb_iter_value(iter);
                var    entry = new KeyValuePair <string, string>(key, value);
                entries.Add(entry);
            }
            Native.leveldb_iter_destroy(iter);
            Native.leveldb_readoptions_destroy(readOptions);

            Assert.AreEqual(3, entries.Count);
            Assert.AreEqual("key1", entries[0].Key);
            Assert.AreEqual("value1", entries[0].Value);
            Assert.AreEqual("key2", entries[1].Key);
            Assert.AreEqual("value2", entries[1].Value);
            Assert.AreEqual("key3", entries[2].Key);
            Assert.AreEqual("value3", entries[2].Value);

            Native.leveldb_writeoptions_destroy(writeOptions);
        }
예제 #3
0
        public void Get()
        {
            var options = Native.leveldb_readoptions_create();

            Native.leveldb_put(Database, options, "key1", "value1");
            var value1 = Native.leveldb_get(Database, options, "key1");

            Assert.AreEqual("value1", value1);

            Native.leveldb_put(Database, options, "key2", "value2");
            var value2 = Native.leveldb_get(Database, options, "key2");

            Assert.AreEqual("value2", value2);

            Native.leveldb_put(Database, options, "key3", "value3");
            var value3 = Native.leveldb_get(Database, options, "key3");

            Assert.AreEqual("value3", value3);

            // verify checksums
            Native.leveldb_readoptions_set_verify_checksums(options, true);
            value1 = Native.leveldb_get(Database, options, "key1");
            Assert.AreEqual("value1", value1);

            // no fill cache
            Native.leveldb_readoptions_set_fill_cache(options, false);
            value2 = Native.leveldb_get(Database, options, "key2");
            Assert.AreEqual("value2", value2);

            Native.leveldb_readoptions_destroy(options);
        }
예제 #4
0
        public void Reopen()
        {
            Native.leveldb_close(Database);
            Database = IntPtr.Zero;

            var options = Native.leveldb_options_create();

            Database = Native.leveldb_open(options, DatabasePath);
            var readOptions = Native.leveldb_readoptions_create();

            Native.leveldb_get(Database, readOptions, "key1");
            Native.leveldb_readoptions_destroy(readOptions);
        }
예제 #5
0
        public void Delete()
        {
            var writeOptions = Native.leveldb_writeoptions_create();

            Native.leveldb_put(Database, writeOptions, "key1", "value1");

            var readOptions = Native.leveldb_readoptions_create();
            var value1      = Native.leveldb_get(Database, readOptions, "key1");

            Assert.AreEqual("value1", value1);
            Native.leveldb_delete(Database, writeOptions, "key1");
            value1 = Native.leveldb_get(Database, readOptions, "key1");
            Assert.IsNull(value1);

            Native.leveldb_writeoptions_destroy(writeOptions);
            Native.leveldb_readoptions_destroy(readOptions);
        }