LightningDB 0.18.0

dotnet add package LightningDB --version 0.18.0                
NuGet\Install-Package LightningDB -Version 0.18.0                
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="LightningDB" Version="0.18.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LightningDB --version 0.18.0                
#r "nuget: LightningDB, 0.18.0"                
#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 LightningDB as a Cake Addin
#addin nuget:?package=LightningDB&version=0.18.0

// Install LightningDB as a Cake Tool
#tool nuget:?package=LightningDB&version=0.18.0                

Lightning.NET

.NET Tests NuGet version

Lightning.NET is a .NET library that provides a fast and easy-to-use interface to the Lightning Memory-Mapped Database (LMDB), a high-performance key-value store. This library enables .NET developers to leverage LMDB's efficiency and reliability in their applications.

Features

  • High Performance: Direct interaction with LMDB ensures minimal overhead (no copies / 0-alloc when using Span) and maximum speed.
  • Simplicity: The API is designed to be straightforward, making it easy to integrate into existing projects.
  • Flexibility: Supports various database configurations, including handling multiple values for the same key.
  • Reliable: It is fully transactional with complete ACID semantics.

Installation

Lightning.NET is available as a NuGet package. To install it, run the following command in the Package Manager Console:

Install-Package LightningDB

Alternatively, you can install it via the .NET CLI:

dotnet add package LightningDB

Basic Usage

Here's a simple example demonstrating how to create an environment, open a database, and perform basic put and get operations:

using System;
using System.Text;
using LightningDB;

class Program
{
    static void Main()
    {
        // Specify the path to the database environment
        using var env = new LightningEnvironment("path_to_your_database");
        env.Open();

        // Begin a transaction and open (or create) a database
        using (var tx = env.BeginTransaction())
        using (var db = tx.OpenDatabase(configuration: new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }))
        {
            // Put a key-value pair into the database
            tx.Put(db, UTF8.GetBytes("hello"), UTF8.GetBytes("world"));
            tx.Commit();
        }

        // Begin a read-only transaction to retrieve the value
        using (var tx = env.BeginTransaction(TransactionBeginFlags.ReadOnly))
        using (var db = tx.OpenDatabase())
        {
            var (resultCode, key, value) = tx.Get(db, Encoding.UTF8.GetBytes("hello"));
            if (resultCode == MDBResultCode.Success)
            {
                Console.WriteLine($"{UTF8.GetString(key)}: {UTF8.GetString(value)}");
            }
            else
            {
                Console.WriteLine("Key not found.");
            }
        }
    }
}

In this example:

  • We create a new LMDB environment at the specified path.
  • We open a database within a transaction, inserting the key-value pair ("hello", "world").
  • We commit the transaction to save the changes.
  • We then start a read-only transaction to retrieve and display the value associated with the key "hello".

Handling Multiple Values for the Same Key

LMDB supports storing multiple values for a single key when the database is configured with the Dupsort flag. Here's how you can work with duplicate keys and use the cursor's NextDuplicate function:

using System;
using System.Text;
using LightningDB;

class Program
{
    static void Main()
    {
        using var env = new LightningEnvironment("path_to_your_database");
        env.Open();

        // Configure the database to support duplicate keys
        var dbConfig = new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create | DatabaseOpenFlags.DuplicatesSort };

        // Begin a transaction and open the database
        using (var tx = env.BeginTransaction())
        using (var db = tx.OpenDatabase(configuration: dbConfig))
        {
            var key = Encoding.UTF8.GetBytes("fruit");
            var value1 = Encoding.UTF8.GetBytes("apple");
            var value2 = Encoding.UTF8.GetBytes("cherry");
            var value3 = Encoding.UTF8.GetBytes("banana");

            // Insert multiple values for the same key
            tx.Put(db, key, value1);
            tx.Put(db, key, value2);
            tx.Put(db, key, value3);
            tx.Commit();
        }

        // Begin a read-only transaction to retrieve the values
        using (var tx = env.BeginTransaction(TransactionBeginFlags.ReadOnly))
        using (var db = tx.OpenDatabase())
        using (var cursor = tx.CreateCursor(db))
        {
            var key = Encoding.UTF8.GetBytes("fruit");

            // Position the cursor at the first occurrence of the key
            var result = cursor.Set(key);
            if(result == MDBResultCode.Success)
            {
                do
                {
                    var current = cursor.GetCurrent();
                    var currentKey = current.key.AsSpan();
                    var currentValue = current.value.AsSpan();
                    Console.WriteLine($"{UTF8.GetString(currentKey)}: {UTF8.GetString(currentValue)}");
                }
                // Move to the next duplicate value
                while (cursor.NextDuplicate().resultCode == MDBResultCode.Success);
            }
            else
            {
                Console.WriteLine("Key not found.");
            }
            
            //Or even simpler
            var values = cursor.AllValuesFor(key);
            foreach(var value in values)
            {
                Console.WriteLine($"fruit: {Encoding.UTF8.GetString(value.AsSpan())}");
            }
        }
    }
}

In this example:

  • We configure the database with the DupSort flag to allow multiple values for a single key.
  • We insert three different values ("apple", "cherry", "banana") under the same key "fruit".
  • Using a cursor, we iterate over all values associated with the key "fruit" by moving to the next duplicate entry and see the values retrieved are ordered.
  • Then we demonstrate doing the same thing with IEnumerable instead.

Additional Resources

For more detailed examples and advanced usage, refer to the unit tests in the Lightning.NET repository.

The <a href="http://lmdb.tech/doc" target="_blank">Official LMDB API documentation</a> is also a valuable resource for understanding the underlying database engine.

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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.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 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. 
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.

NuGet packages (9)

Showing the top 5 NuGet packages that depend on LightningDB:

Package Downloads
Akka.DistributedData.LightningDB

Replicated data using CRDT structures

LightningQueues

LightningQueues

LightningQueues.Storage.LMDB

LightningQueues.Storage.LMDB

YesSql.Storage.LightningDB

Package Description

LightningStore

LightningDb facades to easily build an embedded event stream and a document store. By default JIL serialization is build in.

GitHub repositories (4)

Showing the top 4 popular GitHub repositories that depend on LightningDB:

Repository Stars
akkadotnet/akka.net
Canonical actor model implementation for .NET with local + distributed actors in C# and F#.
bleroy/lunr-core
Lunr-core is a small, full text search library for use in small applications. It's a .NET port of LUNR.js.
LightningQueues/LightningQueues
Fast persistent queues for .NET
Bobris/BTDB
Key Value Database in .Net with Object DB Layer, RPC, dynamic IL and much more
Version Downloads Last updated
0.18.0 0 3/3/2025
0.17.1 695 1/15/2025
0.17.0 941 11/27/2024
0.16.0 371,363 10/11/2023
0.16.0-preview.1 27,624 4/23/2023
0.15.0 228,676 2/4/2023
0.14.1 32,656 1/4/2022
0.14.0 580,021 9/6/2021
0.13.0 180,811 7/30/2020
0.12.0 19,399 7/6/2020
0.11.0 76,508 2/20/2020
0.10.0 95,593 11/26/2017
0.9.9 43,757 5/29/2017
0.9.8 33,427 5/2/2017
0.9.7 16,326 9/22/2016
0.9.6 3,929 9/2/2016
0.9.5 2,016 8/29/2016
0.9.4 4,554 6/29/2016
0.9.3 2,100 6/28/2016
0.9.3-rc2-51 1,681 5/22/2016
0.9.3-rc1-45 3,581 2/12/2016
0.9.3-rc1-44 1,834 11/23/2015
0.9.3-rc1-43 1,758 11/20/2015
0.9.2.40 3,075 9/10/2015
0.9.1.35 2,978 8/3/2015
0.9.0.31 2,663 7/30/2015
0.8.14.51400 2,178 5/31/2015
0.8.12.51737 1,920 1/31/2015
0.8.8.52157 1,805 1/18/2015
0.8.7.52133 1,698 1/18/2015
0.8.6.51452 2,142 12/28/2014
0.8.5.51145 2,119 12/25/2014
0.8.3.52343 1,896 6/4/2014
0.8.2.51054 1,536 5/9/2014
0.8.1.52127 1,794 2/20/2014
0.8.1.51635 1,587 12/15/2013
0.8.0.52232 1,750 10/10/2013