Redb 0.0.4
dotnet add package Redb --version 0.0.4
NuGet\Install-Package Redb -Version 0.0.4
<PackageReference Include="Redb" Version="0.0.4" />
<PackageVersion Include="Redb" Version="0.0.4" />
<PackageReference Include="Redb" />
paket add Redb --version 0.0.4
#r "nuget: Redb, 0.0.4"
#:package Redb@0.0.4
#addin nuget:?package=Redb&version=0.0.4
#tool nuget:?package=Redb&version=0.0.4
Redb.NET
redb (An embedded key-value database) bindings for .NET and Unity.
English | 日本語
Overview
Redb.NET is a high-performance C# binding for redb, an embedded database implemented in Rust.
While SQLite is well-known as an embedded database, and LMDB or RocksDB as key-value databases, redb stands out as an excellent choice for its simplicity, stability, high performance, and support for concurrency.
Redb.NET provides a high-level binding for redb, offering an easy-to-use API for C#. The binding layer is carefully tuned for performance, ensuring no overhead.
Installation
Redb.NET includes native libraries, so the installation process differs significantly between .NET and Unity. Be careful not to confuse the two.
NuGet packages
Redb.NET requires .NET Standard 2.1 or higher. Packages are available on NuGet.
Unity
For Unity, all packages, including extension packages, can be installed via the Package Manager.
- Open the Package Manager from Window > Package Manager.
- Click the "+" button > Add package from git URL.
- Enter the URL for the corresponding package.
To use the extension packages, you need to separately add System.Text.Json or MessagePack using tools like NugetForUnity.
Due to differences in the binding implementation, the NuGet version of Redb.NET cannot be used in Unity. Always install it using the above method.
Platforms
Redb.NET supports the following platforms:
| Platform | Architecture | Supported | Notes |
|---|---|---|---|
| Windows | x64 | ✅ | |
| arm64 | ✅ | ||
| macOS | x64 | ✅ | |
| arm64 (Apple Silicon) | ✅ | ||
| Linux | x64 | ✅ | |
| arm64 | ✅ | ||
| iOS | arm64 | ✅ (untested) | (Unity only) |
| x64 | ✅ (untested) | (Unity only) | |
| Android | arm64 | ✅ (untested) | (Unity only) |
| armv7 | ✅ (untested) | (Unity only) | |
| x86_64 | ✅ (untested) | (Unity only) |
Quick Start
using Redb;
using var db = RedbDatabase.Create("test.redb", RedbDatabaseOptions.Default);
using (var tx = db.BeginWrite())
{
using (var table = tx.OpenTable<string, int>("my_table"))
{
table.Insert("foo", 12);
table.Insert("bar", 30);
}
tx.Commit();
}
using (var tx = db.BeginRead())
{
using (var table = tx.OpenTable<string, int>("my_table"))
{
var foo = table.Get("foo");
Console.WriteLine($"foo: {foo}");
if (table.TryGet("bar", out var bar))
{
Console.WriteLine($"bar: {bar}");
}
}
}
Table API
You can open Table/ReadOnlyTable using OpenTable(). Both generic and non-generic types are available.
// generic table
using var table1 = tx.OpenTable<string, int>("my_table");
// non-generic table
using var table2 = tx.OpenTable("my_table");
In Table<TKey, TValue>/ReadOnlyTable<TKey, TValue>, the keys and values are strongly typed when reading and writing values.
table1.Insert("foo", 1);
int value = readOnlyTable1.Get("foo");
This is automatically serialized by the IRedbEncoding set in RedbDatabase. By default, primitive types and some other types are supported, but you can support any type by connecting a serializer. See the C# Serialization section for details.
In Table/ReadOnlyTable, you can directly read and write untyped binary data.
table2.Insert("foo"u8, "bar"u8);
RedbBlob blob = readOnlyTable2.Get("foo"u8);
RedbBlob is a struct that wraps a pointer on the Rust side. You can read values without overhead through this, but you must dispose of it with Dispose().
var span = blob.AsSpan();
span.CopyTo(buffer);
blob.Dispose();
Compaction
You can perform compaction by calling Compact().
db.Compact();
The default size of a redb database file is over 1MB, but compaction can reduce it to a few dozen KB.
C# Serialization
By default, binary (ReadOnlySpan<byte>), primitive types, Guid, DateTime, and TimeSpan can be used as keys or values. By connecting a serializer to the database, you can use any object as a key or value.
Currently, System.Text.Json and MessagePack for C# are supported.
using Redb;
using Redb.SystemTextJson;
// using Redb.MessagePack;
using var db = RedbDatabase.Create("test.redb", RedbDatabaseOptions.Default)
.WithJsonSerializer(); // or .WithMessagePackSerializer();
using (var tx = db.BeginWrite())
{
using (var table = tx.OpenTable<string, Person>("persons"))
{
table.Insert("alice", new Person("Alice", 18));
table.Insert("bob", new Person("Bob", 30));
}
tx.Commit();
}
using (var tx = db.BeginRead())
{
using (var table = tx.OpenTable<string, Person>("persons"))
{
var alice = table.Get("alice");
var bob = table.Get("bob");
Console.WriteLine(alice);
Console.WriteLine(bob);
}
}
record Person(string Name, int Age);
Supported Features
Redb.NET is currently in preview, and some features like MultimapTable are not yet implemented in the C# API. These are planned to be supported in the stable release.
License
This library is provided under the MIT License.
| Product | Versions 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 is compatible. 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 was computed. 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. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Redb.Interop (>= 0.0.4)
- System.Runtime.CompilerServices.Unsafe (>= 6.0.0)
-
net10.0
- Redb.Interop (>= 0.0.4)
-
net8.0
- Redb.Interop (>= 0.0.4)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Redb:
| Package | Downloads |
|---|---|
|
Redb.SystemTextJson
Redb System.Text.Json Encoder |
|
|
Redb.MessagePack
Redb MessagePack Encoder |
GitHub repositories
This package is not used by any popular GitHub repositories.