LevelDB.Net_All 2.2.3

dotnet add package LevelDB.Net_All --version 2.2.3                
NuGet\Install-Package LevelDB.Net_All -Version 2.2.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LevelDB.Net_All" Version="2.2.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LevelDB.Net_All --version 2.2.3                
#r "nuget: LevelDB.Net_All, 2.2.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LevelDB.Net_All as a Cake Addin
#addin nuget:?package=LevelDB.Net_All&version=2.2.3

// Install LevelDB.Net_All as a Cake Tool
#tool nuget:?package=LevelDB.Net_All&version=2.2.3                

Updates

This repository is a fork of the original repository available at https://github.com/oodrive/leveldb.net. Unlike the original, which only supported up to .NET 2, this fork has been updated to support all modern .NET versions.

The current source repository is available at https://github.com/etboard/leveldb.net_All.

Additionally, an example demonstrating the use of LevelDB with Arduino IDE V2 has been added. You can find this example in the examples folder.

The usage instructions in the original README were incorrect and have been updated accordingly.

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.

Installation

LevelDB.Standard is available as a NuGet package:

PM> Install-Package LevelDB.Standard

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
db.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 (iterator.SeekToFirst(); iterator.IsValid(); iterator.Next()) 
	{
	    Console.WriteLine("Key as string: {0}", iterator.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 (iterator.SeekToLast(); iterator.IsValid(); iterator.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


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.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed.  xamarinmac20 is compatible. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.0

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • Xamarin.Mac 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.3 114 9/10/2024
2.2.2 77 9/8/2024
2.2.1 71 9/8/2024
2.2.0 99 9/8/2024