Example #1
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);
        }
Example #2
0
        public static void Dump(IntPtr db)
        {
            var    options = Native.leveldb_readoptions_create();
            IntPtr iter    = Native.leveldb_create_iterator(db, options);

            for (Native.leveldb_iter_seek_to_first(iter);
                 Native.leveldb_iter_valid(iter);
                 Native.leveldb_iter_next(iter))
            {
                string key   = Native.leveldb_iter_key(iter);
                string value = Native.leveldb_iter_value(iter);
                Console.WriteLine("'{0}' => '{1}'", key, value);
            }
            Native.leveldb_iter_destroy(iter);
        }
Example #3
0
        public IEnumerator <KeyValuePair <string, string> > GetEnumerator()
        {
            CheckDisposed();
            var    options = new ReadOptions();
            IntPtr iter    = IntPtr.Zero;

            try {
                iter = Native.leveldb_create_iterator(Handle, options.Handle);
                for (Native.leveldb_iter_seek_to_first(iter);
                     Native.leveldb_iter_valid(iter);
                     Native.leveldb_iter_next(iter))
                {
                    string key   = Native.leveldb_iter_key(iter);
                    string value = Native.leveldb_iter_value(iter);
                    var    kvp   = new KeyValuePair <string, string>(key, value);
                    yield return(kvp);
                }
            } finally {
                if (iter != IntPtr.Zero)
                {
                    Native.leveldb_iter_destroy(iter);
                }
            }
        }