• This repository has been archived on 21/Apr/2018
  • Stars
    star
    118
  • Rank 289,230 (Top 6 %)
  • Language
    C++
  • Created over 11 years ago
  • Updated over 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

LevelDB for Windows and .NET

leveldb for Windows and .NET


leveldb is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.

This project aims to provide .NET bindings to LevelDB in addition to making leveldb work well on Windows.

Building leveldb

See file "WINDOWS" for instructions on how to build this in Windows.

  • You'll need to install some Boost libraries to build against
  • You'll need to create a Microsoft Visual C++ project to build this
  • The WINDOWS file explains both of these processes.

We're looking for volunteers to build a true Win32 port of LevelDB for Windows.

Getting Started

Here's how you can get started with leveldb and .NET.

Opening A Database

A Leveldb database has a name which corresponds to a directory on the system. This then stores all files in this particular folder. In this example, you can create a new database (if missing) in the C:\temp\tempdb directory.

// Open a connection to a new DB and create if not found
var options = new Options { CreateIfMissing = true };
var db = new DB(options, @"C:\temp\tempdb");

Closing a Database

When you are finished, you can close the database by calling the Close method.

// Close the connection
db.Close();

The DB class also implements the IDisposable interface which allows you to use the using block:

var options = new Options { CreateIfMissing = true };
using (var db = new DB(options, @"C:\temp\tempdb")) 
{
    // Use leveldb
}

Reads and Writes

leveldb provides the Get, Put and Delete methods to query, update and delete database objects.

const string key = "New York";

// Put in the key value
keyValue.Put(key, "blue");

// Print out the value
var keyValue = db.Get(key);
Console.WriteLine(keyValue); 

// Delete the key
db.Delete(key);

Atomic Updates

leveldb also supports atomic updates through the WriteBatch class and the Write method on the DB. This ensures atomic updates should a process exit abnormally.

var options = new Options { CreateIfMissing = true };
using (var db = new DB(options, path))
{
    db.Put("NA", "Na");

    using(var batch = new WriteBatch())
    {
        batch.Delete("NA")
             .Put("Tampa", "Green")
             .Put("London", "red")
             .Put("New York", "blue");
        db.Write(batch);
    }
}

Synchronous Writes

For performance reasons, by default, every write to leveldb is asynchronous. This behavior can be changed by providing a WriteOptions class with the Sync flag set to true to a Put method call on the DB instance.

// Synchronously write
var writeOptions = new WriteOptions { Sync = true };
db.Put("New York", "blue");

The downside of this is that due to a process crash, these updates may be lost.

As an alternative, atomic updates can be used as a safer alternative with a synchronous write which the cost will be amortized across all of the writes in the batch.

var options = new Options { CreateIfMissing = true };
using (var db = new DB(options, path))
{
	db.Put("New York", "blue");

	// Create a batch to set key2 and delete key1
	using (var batch = new WriteBatch())
	{
		var keyValue = db.Get("New York");
		batch.Put("Tampa", keyValue);
		batch.Delete("New York");
		
		// Write the batch
		var writeOptions = new WriteOptions { Sync = true; }
		db.Write(batch, writeOptions);
	}
}

Iteration

The leveldb bindings also supports iteration using the standard GetEnumerator pattern. In this example, we can select all keys in a LINQ expression and then iterate the results, printing out each key.

var keys = 
    from kv in db as IEnumerable<KeyValuePair<string, string>>
    select kv.Key;

foreach (var key in keys) 
{
	Console.WriteLine("Key: {0}", key);
}

The following example shows how you can iterate all the keys as strings.

// Create new iterator
using (var iterator = db.CreateIterator())
{
	// Iterate to print the keys as strings
	for (it.SeekToFirst(); it.IsValid(); it.Next()) 
	{
	    Console.WriteLine("Key as string: {0}", it.KeyAsString());
	}
}

The next example shows how you can iterate all the values in the leveldb instance in reverse.

// Create new iterator
using (var iterator = db.CreateIterator())
{
	// Iterate in reverse to print the values as strings
	for (it.SeekToLast(); it.IsValid(); it.Prev()) 
	{
	    Console.WriteLine("Value as string: {0}", it.ValueAsString());
	}
}

Snapshots

Snapshots in leveldb provide a consistent read-only view of the entire state of the current key-value store. Note that the Snapshot implements IDisposable and should be disposed to allow leveldb to get rid of state that was being maintained just to support reading as of that snapshot.

var options = new Options { CreateIfMissing = true }
using (var db = new Db(options, path))
{
    db.Put("Tampa", "green");
    db.Put("London", "red");
    db.Delete("New York");

	using (var snapshot = db.CreateSnapshot()) 
	{
		var readOptions = new ReadOptions {Snapshot = snapShot};

		db.Put("New York", "blue");

		// Will return null as the snapshot created before
		// the updates happened
		Console.WriteLine(db.Get("New York", readOptions)); 
	}
}

Comparators

The leveldb keystore uses a default ordering function which orders bytes lexicographically, however, you can specify your own custom comparator when opening a database.

To specify a comparator, set the Comparator property on the Options instance by calling Comparator.Create. In this instance, we will compare both x and y modulo 2.

var options = new Options { CreateIfMissing = true };
options.Comparator = Comparator.Create(
    "integers mod 2",
    (xs, ys) => LexicographicalCompare(((NativeArray<int>) xs).Select(x => x % 2),
                                       ((NativeArray<int>) ys).Select(y => y % 2)));

using (var db = new Db(options, path))
{
    db.Put(1, new[] { 1, 2, 3 }, new WriteOptions());
    Console.WriteLine("put 1, [1,2,3]");

    var key = NativeArray.FromArray(new int[] { 3 });
    using (var xs = db.GetRaw<int>(key))
    {
		// Prints 1 2 3
        Console.WriteLine("get {0} => [{1}]", key[0], string.Join(",", xs));
    }
}

And the implementation of the Comparator is below:

private int LexicographicalCompare<T>(IEnumerable<T> xs, IEnumerable<T> ys)
{
    var comparator = System.Collections.Generic.Comparer<T>.Default;

    using(var xe = xs.GetEnumerator())
    using(var ye = ys.GetEnumerator())
    {
        for(;;)
        {
            var xh = xe.MoveNext();
            var yh = ye.MoveNext();
            if (xh != yh)
                return yh ? -1 : 1;
            if (!xh)
                return 0;

            // more elements
            int diff = comparator.Compare(xe.Current, ye.Current);
            if (diff != 0)
                return diff;
        }
    }
}

LICENSE


Copyright 2012 Microsoft Corporation

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

More Repositories

1

RxJS

The Reactive Extensions for JavaScript
JavaScript
19,515
star
2

rx.angular.js

AngularJS Bindings for RxJS
JavaScript
825
star
3

RxJS-DOM

HTML DOM Bindings for the Reactive Extensions for JavaScript
JavaScript
437
star
4

RxJSKoans

RxJS Koans
JavaScript
373
star
5

rx-node

RxJS Bindings for Node.js and io.js
JavaScript
219
star
6

rxjs-jquery

Reactive Extensions bindings for jQuery
JavaScript
213
star
7

IxJS

Interactive Extensions for JavaScript
JavaScript
158
star
8

IL2JS

IL to JavaScript Compiler
C#
143
star
9

RxPy

The Reactive Extensions for Python
Python
102
star
10

rxjs-node

Reactive Extensions bindings for node.js
JavaScript
70
star
11

FutureJS

Presentation from FutureJS
JavaScript
44
star
12

Reactor

Foundations for a distributed implementation of Rx.
C#
39
star
13

Rx.ObjC

Reactive Extensions for ObjC
Objective-C
29
star
14

rx.disposables

Standalone library for disposables from RxJS
JavaScript
27
star
15

RxJS-CLI

Reactive Extensions for JavaScript (RxJS) Command Line Interface
JavaScript
23
star
16

RxJS-WinJS

The Reactive Extensions for JavaScript bindings for Windows 8 and WinJS
JavaScript
22
star
17

BuildStuffWorkshop

Workshop for BuildStuff.LT 2014
15
star
18

NDC-Oslo-2014

NDC Oslo 2014 Presentation on Democratizing event processing at all scales and platforms with Reactive Extensions (Rx)
JavaScript
14
star
19

rxjs-winjs-sample

Using RxJS for Windows 8
JavaScript
14
star
20

RxToBand

Reactive Extensions (Rx) support for the Microsoft Band
C#
13
star
21

StrangeLoop2013

StrangeLoop 2013 Rx Presentation
JavaScript
12
star
22

RxJS-Combinators

A functional combinator library
JavaScript
11
star
23

rx.schedulers

Standalone implementation of the RxJS schedulers
JavaScript
11
star
24

RxJS-Contrib

The Reactive Extensions for JavaScript Contrib Project
JavaScript
11
star
25

rx.priorityqueue

Standalone Priority Queue from the RxJS library
JavaScript
10
star
26

RxJS-Dojo

Reactive Extensions bindings for the Dojo Toolkit
JavaScript
10
star
27

RxJS-ExtJS

Ext JS Bindings for the Reactive Extensions for JavaScript (RxJS)
JavaScript
9
star
28

Rx-Edge

Reactive Extensions for Edge.js
JavaScript
5
star
29

RxJS-MooTools

MooTools Bindings for the Reactive Extensions for JavaScript (RxJS)
JavaScript
4
star
30

IxJS-ES6

An ES6 version of the Interactive Extensions for JavaScript (IxJS)
JavaScript
3
star
31

RxJS6

2
star
32

Rx-Summit-2013

Reactive Extensions Summit 2013
2
star
33

blog-posts

Blog posts for posting about Rx
1
star
34

RxJSx

An experimental version of the Reactive Extensions
JavaScript
1
star