![]() |
VOOZH | about |
dotnet add package LightningDB --version 0.21.0
NuGet\Install-Package LightningDB -Version 0.21.0
<PackageReference Include="LightningDB" Version="0.21.0" />
<PackageVersion Include="LightningDB" Version="0.21.0" />Directory.Packages.props
<PackageReference Include="LightningDB" />Project file
paket add LightningDB --version 0.21.0
#r "nuget: LightningDB, 0.21.0"
#:package LightningDB@0.21.0
#addin nuget:?package=LightningDB&version=0.21.0Install as a Cake Addin
#tool nuget:?package=LightningDB&version=0.21.0Install as a Cake Tool
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.
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
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:
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:
DupSort flag to allow multiple values for a single key.LightningDB provides built-in, allocation-free comparers for custom key sorting and duplicate ordering. Use them with CompareWith() for keys or FindDuplicatesWith() for duplicate values:
var config = new DatabaseConfiguration
{
Flags = DatabaseOpenFlags.Create | DatabaseOpenFlags.DuplicatesSort
};
// Sort keys as signed integers (negative values sort before positive)
config.CompareWith(SignedIntegerComparer.Instance);
// Sort duplicate values in reverse order
config.FindDuplicatesWith(ReverseBitwiseComparer.Instance);
using var db = tx.OpenDatabase(configuration: config);
Available comparers in LightningDB.Comparers:
| Comparer | Description |
|---|---|
BitwiseComparer |
Lexicographic byte comparison (default LMDB behavior) |
ReverseBitwiseComparer |
Lexicographic descending |
SignedIntegerComparer |
4/8-byte signed integers with proper negative ordering |
UnsignedIntegerComparer |
4/8-byte unsigned integers |
Utf8StringComparer |
Ordinal UTF-8 string comparison |
LengthComparer |
Sort by length first, then content |
LengthOnlyComparer |
Sort by length only |
HashCodeComparer |
Hash-based comparison for large values |
Reverse variants are available for most comparers (e.g., ReverseSignedIntegerComparer).
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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
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. |
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.21.0 | 11,646 | 12/24/2025 |
| 0.20.0 | 5,296 | 11/12/2025 |
| 0.19.1 | 2,130 | 10/20/2025 |
| 0.19.0 | 6,065 | 9/22/2025 |
| 0.18.1 | 13,676 | 5/27/2025 |
| 0.18.0 | 6,106 | 3/3/2025 |
| 0.17.1 | 3,878 | 1/15/2025 |
| 0.17.0 | 4,132 | 11/27/2024 |
| 0.16.0 | 1,379,402 | 10/11/2023 |
| 0.16.0-preview.1 | 71,264 | 4/23/2023 |
| 0.15.0 | 315,131 | 2/4/2023 |
| 0.14.1 | 38,875 | 1/4/2022 |
| 0.14.0 | 604,535 | 9/6/2021 |
| 0.13.0 | 187,302 | 7/30/2020 |
| 0.12.0 | 21,585 | 7/6/2020 |
| 0.11.0 | 84,925 | 2/20/2020 |
| 0.10.0 | 102,325 | 11/26/2017 |
| 0.9.9 | 45,724 | 5/29/2017 |
| 0.9.8 | 36,353 | 5/2/2017 |
| 0.9.7 | 18,562 | 9/22/2016 |