Exemple #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);
        }
Exemple #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))
            {
                byte[] key   = Native.leveldb_iter_key_raw(iter);
                byte[] value = Native.leveldb_iter_value_raw(iter);
                Console.WriteLine("'{0}' => '{1}'", key, value);
            }
            Native.leveldb_iter_destroy(iter);
        }
Exemple #3
0
        public void IsValid()
        {
            var writeOptions = Native.leveldb_writeoptions_create();

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

            var    readOptions = Native.leveldb_readoptions_create();
            IntPtr iter        = Native.leveldb_create_iterator(Database, readOptions);

            Native.leveldb_iter_seek_to_last(iter);
            Assert.IsTrue(Native.leveldb_iter_valid(iter));

            Native.leveldb_iter_next(iter);
            Assert.IsFalse(Native.leveldb_iter_valid(iter));
        }
Exemple #4
0
 public Iterator(DB db, ReadOptions readOptions)
 {
     if (db == null)
     {
         throw new ArgumentNullException("db");
     }
     DB = db;
     // keep reference so it doesn't get GCed
     ReadOptions = readOptions;
     if (ReadOptions == null)
     {
         ReadOptions = new ReadOptions();
     }
     Handle      = Native.leveldb_create_iterator(db.Handle, ReadOptions.Handle);
     IsFirstMove = true;
 }
Exemple #5
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);
                }
            }
        }