GithubHelp home page GithubHelp logo

rocksdb.extensions's Introduction

RocksDb.Extensions

Build

RocksDB.Extensions is a .NET library that provides an opinionated integration of RocksDB with the .NET dependency injection system. This library enables you to easily create key/value data stores backed by RocksDB, and provides several built-in serializer factories to serialize your data.

NuGet Status
RocksDb.Extensions NuGet
RocksDb.Extensions.Protobuf NuGet
RocksDb.Extensions.System.Text.Json NuGet
RocksDb.Extensions.ProtoBufNet NuGet

Quickstart

Install the package

To use RocksDb.Extensions in your project, add the NuGet package using the dotnet CLI:

dotnet add package RocksDb.Extensions

Create a key/value store

The library abstracts away the internals of RocksDb behind the concept of key/value data stores. To create your first store, you need a class that inherits from the base class RocksDbStore<TKey, TValue>. For example:

public class UsersStore : RocksDbStore<string, User>
{
    public UsersStore(IRocksDbAccessor<string, User> rocksDbAccessor) : base(rocksDbAccessor)
    {
    }
}

In this example, we're using string as the key and a custom type User as the value. The library is flexible regarding how you can serialize your keys and values. There are several serialization options available, and you can add your own by inheriting from the ISerializerFactory interface.

Choose a serializer

The library provides 4 built-in serializer factories:

  • PrimitiveTypesSerializerFactory
  • SystemTextJsonSerializerFactory
  • ProtoBufNetSerializerFactory
  • ProtobufSerializerFactory

You can use one or more of these factories to serialize your data. For example, if you want to use PrimitiveTypesSerializerFactory to serialize your keys and SystemTextJsonSerializerFactory to serialize your values, you can configure your RocksDb instance as follows:

var rocksDbBuilder = builder.Services.AddRocksDb(options =>
{

    // Specify the path for the RocksDb database
    options.Path = "./my-rocks-db";

    // Clear pre-configured SerializerFactories so we can have full control over the order in which serializers will be resolved.
    options.SerializerFactories.Clear();

    // Add the serializer factories for the key and value types
    options.SerializerFactories.Add(new PrimitiveTypesSerializerFactory());
    options.SerializerFactories.Add(new SystemTextJsonSerializerFactory());
});

Register your store

Before your store can be used, you need to register it with RocksDb. You can do this as follows:

rocksDbBuilder.AddStore<string, User, UsersStore>("users-store");

This registers an instance of UsersStore with RocksDb under the name "users-store".

Use your store

Once you have registered your store, you can use it to add, get, and remove data from RocksDb. For example:

[ApiController]
[Route("[controller]")]
public class UsersController : ControllerBase
{
    private readonly UsersStore _usersStore;

    public UsersController(UsersStore usersStore)
    {
        _usersStore = usersStore;
    }

    [HttpPost]
    public IActionResult Post([FromBody] User user)
    {
        var id = Guid.NewGuid().ToString();
        user.Id = id;
        _usersStore.Put(id, user);
        return Ok();
    }

    [HttpGet]
    public IEnumerable<User> Get()
    {
        return _usersStore.GetAll();
    }

    [HttpDelete]
    public IActionResult Delete(string userId)
    {
        _usersStore.Remove(userId);
        return Ok();
    }
}

Clean start

For certain scenarios, such as automated testing, it may be useful to remove the existing RocksDB database on startup to ensure a clean start. This can be achieved using the DeleteExistingDatabaseOnStartup configuration option.

To use this option, set it to true when configuring the RocksDbOptions object:

var rocksDbBuilder = builder.Services.AddRocksDb(options =>
{
    // Your RocksDb configuration... 
    options.DeleteExistingDatabaseOnStartup = true;
});

When this option is set to true, the existing database will be deleted on startup and a new one will be created. Note that all data in the existing database will be lost when this option is used.

By default, the DeleteExistingDatabaseOnStartup option is set to false to preserve the current behavior of not automatically deleting the database. If you need to ensure a clean start for your application, set this option to true in your configuration.

rocksdb.extensions's People

Contributors

havret avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rocksdb.extensions's Issues

Add API for putting range of values with key calculation function

This enhancement to the RocksDbStore API would allow users to put a range of values by calculating the corresponding keys with a provided function. This feature would be particularly useful when keys can be computed from the values. The benefit of this enhancement is that there would be no need to allocate an array to store the list of keys.

The proposed API is as follows:

public void PutRange(ReadOnlySpan<TValue> values, Func<TValue, TKey> keySelector)

Here's an example usage of the proposed API:

// Put a range of values using a lambda function to calculate the keys
var values = new int[] { 10, 20, 30 };
store.PutRange(values, value => $"key_{value}");

Add option to delete RocksDB database on start if it already exists

Currently, the RocksDB.Extensions library does not have an option to automatically delete an existing database when it is started. This can be problematic when testing and debugging, as it requires manually deleting the database before running the code again.

The proposed option could be added as a boolean parameter to the RocksDbOptions class, with a default value of false to preserve current behavior. If the option is set to true, the library would automatically delete the existing database on startup and create a new one.

Zero Allocation PutRange

The current implementation of PutRange allocates memory for each element, which can lead to performance issues in high-throughput scenarios or on systems with limited memory resources. A zero-allocation version would improve performance and reduce memory pressure.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.