ZoneTree 1.0.4

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package ZoneTree --version 1.0.4
NuGet\Install-Package ZoneTree -Version 1.0.4
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="ZoneTree" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ZoneTree --version 1.0.4
#r "nuget: ZoneTree, 1.0.4"
#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 ZoneTree as a Cake Addin
#addin nuget:?package=ZoneTree&version=1.0.4

// Install ZoneTree as a Cake Tool
#tool nuget:?package=ZoneTree&version=1.0.4

ZoneTree

ZoneTree is a persistent, high-performance key-value database for .NET. It can operate in memory or on disk. (Optimized for SSDs)

Download

ZoneTree is a fast and high-performance LSM Tree for .NET.

LSM Tree (Log-structured merge-tree) is the most popular data structure and it is being used by many popular databases internally.

Why ZoneTree?

  1. It is pure C#. Easy to maintain, easy to develop new features.
  2. It is faster than using C/C++ based key-value stores like RocksDB. Because ZoneTree does not need to transfer bytes to the native external libraries (Zero Marshaling).
  3. .NET EcoSystem does not have any feature-complete and thread-safe LSM Tree that operates both in memory and on disk.

How fast is it?

2 Million int key and int value inserts in 7 seconds. (Config: 1M mutable segment size, 2M readonly segments merge-threshold)

20 Million int key and int value inserts in 73 seconds. (Config: 1M mutable segment size, 2M readonly segments merge-threshold)

20 Million int key and int value reads in 16 seconds. (Config: 1M mutable segment size, 2M readonly segments merge-threshold)

Doing database benchmark is tough. A proper and fair performance analysis requires a lot of work.

For now, we are confident that ZoneTree is fast enough to be used in production.

How to use ZoneTree?

The following sample demonstrates creating a database.

  var dataPath = "data/mydatabase";
  var walPath = "data/mydatabase/wal";
  using var zoneTree = new ZoneTreeFactory<int, string>()
    .SetComparer(new IntegerComparerAscending())
    .SetDataDirectory(dataPath)
    .SetWriteAheadLogDirectory(walPath)
    .SetKeySerializer(new Int32Serializer())
    .SetValueSerializer(new Utf8StringSerializer())
    .OpenOrCreate();
    
    // atomic (thread-safe) on single mutable-segment.
    zoneTree.Upsert(39, "Hello Zone Tree!");
    
    // atomic across all segments
    zoneTree.TryAtomicAddOrUpdate(39, "a", (x) => x + "b");

How to maintain LSM Tree?

Big LSM Trees require maintenance tasks. ZoneTree provides the IZoneTreeMaintenance interface to give you full power on maintenance tasks. It also comes with a default maintainer to let you focus on your business logic without wasting time with LSM details. You can start using the default maintainer like in the following sample code. Note: For small data you don't need a maintainer.

  var dataPath = "data/mydatabase";
  var walPath = "data/mydatabase/wal";

  // 1. Create your ZoneTree
  using var zoneTree = new ZoneTreeFactory<int, string>()
    .SetComparer(new IntegerComparerAscending())
    .SetDataDirectory(dataPath)
    .SetWriteAheadLogDirectory(walPath)
    .SetKeySerializer(new Int32Serializer())
    .SetValueSerializer(new Utf8StringSerializer())
    .OpenOrCreate();
 
  using var maintainer = new BasicZoneTreeMaintainer<int, string>(zoneTree);

  // 2. Read/Write data
  zoneTree.Upsert(39, "Hello ZoneTree!");

  // 3. Complete maintainer running tasks.
  maintainer.CompleteRunningTasks().AsTask().Wait();

How to delete keys?

In LSM trees, the deletions are handled by upserting key/value with deleted flag. Later on, during the compaction stage, the actual deletion happens. ZoneTree does not implement this flag format by default. It lets the user to define the suitable deletion flag themselves. For example, the deletion flag might be defined by user as -1 for int values. If user wants to use any int value as a valid record, then the value-type should be changed. For example, one can define the following struct and use this type as a value-type.

[StructLayout(LayoutKind.Sequential)]
struct MyDeletableValueType {
   int Number; 
   bool IsDeleted; 
}

ZoneTree gives flexibility to micro-manage the the tree size. The following sample shows how to configure the deletion markers for your database.

using var zoneTree = new ZoneTreeFactory<int, int>()
  // Additional stuff goes here
  .SetIsValueDeletedDelegate((in int x) => x == -1)
  .SetMarkValueDeletedDelegate((ref int x) => x = -1)
  .OpenOrCreate();  

or

using var zoneTree = new ZoneTreeFactory<int, MyDeletableValueType>()
  // Additional stuff goes here
  .SetIsValueDeletedDelegate((in MyDeletableValueType x) => x.IsDeleted)
  .SetMarkValueDeletedDelegate((ref MyDeletableValueType x) => x.IsDeleted = true)
  .OpenOrCreate();  

If you forget to provide the deletion marker delegates, you can never delete the record from your database.

How to iterate over data?

Iteration is possible in both directions, forward and backward. Unlike other LSM tree implementations, iteration performance is equal in both directions. The following sample shows how to do the iteration.

 using var zoneTree = new ZoneTreeFactory<int, int>()
    // Additional stuff goes here
    .OpenOrCreate();
 using var iterator = zoneTree.CreateIterator();
 while(iterator.Next()) {
    var key = iterator.CurrentKey;
    var value = iterator.CurrentValue;
 } 
 
 using var reverseIterator = zoneTree.CreateReverseIterator();
 while(reverseIterator.Next()) {
    var key = reverseIterator.CurrentKey;
    var value = reverseIterator.CurrentValue;
 }

How to iterate starting with a key (Seekable Iterator)?

ZoneTreeIterator provides Seek() method to jump into any record with in O(log(n)) complexity. That is useful for doing prefix search with forward-iterator or with backward-iterator.

 using var zoneTree = new ZoneTreeFactory<string, int>()
    // Additional stuff goes here
    .OpenOrCreate();
 using var iterator = zoneTree.CreateIterator();
 // iterator jumps into the first record starting with "SomePrefix" in O(log(n)) complexity. 
 iterator.Seek("SomePrefix");
 
 //iterator.Next() complexity is O(1)
 while(iterator.Next()) {
    var key = iterator.CurrentKey;
    var value = iterator.CurrentValue;
 } 

I need more information. Where can I find it?

I am going to write more detailed documentation as soon as possible.

I want to contribute. What should I do?

I appreciate any contribution to the project. These are the things I do think we need at the moment:

  1. Write tests / benchmarks.
  2. Write documentation.
  3. Convert documentation to a website using static site generators.
  4. Feature requests & bug fixes.
  5. Performance improvements.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on ZoneTree:

Package Downloads
DifferentialComputeDotNet.Core

Package Description

ManagedCode.ZoneTree.BlobFileSystem The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Azure Blob FileSystem for ZoneTree

ManagedCode.Database.ZoneTree The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Repository for ZoneTree

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.7.0 466 12/12/2023
1.6.9 539 9/13/2023
1.6.8 110 9/13/2023
1.6.7 121 9/13/2023
1.6.6 156 8/18/2023
1.6.5 169 6/17/2023
1.6.4 118 6/16/2023
1.6.3 167 5/29/2023
1.6.2 16,150 5/26/2023
1.6.1 256 4/5/2023
1.6.0 1,695 1/14/2023
1.5.9 282 1/14/2023
1.5.8 957 11/20/2022
1.5.7 352 11/14/2022
1.5.6 331 11/13/2022
1.5.5 437 10/20/2022
1.5.2 467 9/14/2022
1.5.1 405 9/3/2022
1.5.0 389 9/2/2022
1.4.9 388 8/31/2022
1.4.8 369 8/30/2022
1.4.7 389 8/30/2022
1.4.6 400 8/29/2022
1.4.5 415 8/28/2022
1.4.4 582 8/27/2022
1.4.3 403 8/26/2022
1.4.2 393 8/26/2022
1.4.1 364 8/26/2022
1.4.0 392 8/25/2022
1.3.9 377 8/25/2022
1.3.8 397 8/24/2022
1.3.7 643 8/23/2022
1.3.6 379 8/22/2022
1.3.5 400 8/22/2022
1.3.4 393 8/21/2022
1.3.3 381 8/18/2022
1.3.2 409 8/17/2022
1.3.1 380 8/17/2022
1.3.0 388 8/17/2022
1.2.9 379 8/16/2022
1.2.8 372 8/15/2022
1.2.7 398 8/15/2022
1.2.6 388 8/13/2022
1.2.5 404 8/13/2022
1.2.4 383 8/13/2022
1.2.3 412 8/9/2022
1.2.2 382 8/9/2022
1.2.1 426 8/9/2022
1.2.0 429 8/8/2022
1.1.9 401 8/8/2022
1.1.8 391 8/8/2022
1.1.7 413 8/8/2022
1.1.6 397 8/7/2022
1.1.5 408 8/6/2022
1.1.4 406 8/6/2022
1.1.3 400 8/5/2022
1.1.2 410 8/5/2022
1.1.1 406 7/27/2022
1.1.0 409 7/27/2022
1.0.9 399 7/25/2022
1.0.8 399 7/24/2022
1.0.7 374 7/23/2022
1.0.6 398 7/20/2022
1.0.5 414 7/18/2022
1.0.4 415 7/11/2022
1.0.3 452 7/11/2022
1.0.2 440 7/11/2022
1.0.1 427 7/6/2022
1.0.0 417 7/6/2022