database: add redb-memory backend (#97)

* add `redb-memory` feature

* redb: use `redb::backend::InMemoryBackend` if enabled

* readme: add `redb-memory` section

* ci: add `redb-memory` testing

* ci: remove `redb-memory` testing

probably not worth adding time to CI for this
This commit is contained in:
hinto-janai 2024-03-26 20:50:10 -04:00 committed by GitHub
parent d503548716
commit ae0d9ff36a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 13 deletions

View file

@ -9,10 +9,12 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/database"
keywords = ["cuprate", "database"] keywords = ["cuprate", "database"]
[features] [features]
default = ["heed", "redb", "service"] # default = ["heed", "redb", "service"]
# default = ["redb", "service"] # default = ["redb", "service"]
default = ["redb-memory", "service"]
heed = ["dep:heed"] heed = ["dep:heed"]
redb = ["dep:redb"] redb = ["dep:redb"]
redb-memory = ["redb"]
service = ["dep:crossbeam", "dep:futures", "dep:tokio", "dep:tokio-util", "dep:tower", "dep:rayon"] service = ["dep:crossbeam", "dep:futures", "dep:tokio", "dep:tokio-util", "dep:tower", "dep:rayon"]
[dependencies] [dependencies]

View file

@ -11,6 +11,7 @@ Cuprate's database implementation.
1. [Backends](#backends) 1. [Backends](#backends)
- [`heed`](#heed) - [`heed`](#heed)
- [`redb`](#redb) - [`redb`](#redb)
- [`redb-memory`](#redb-memory)
- [`sanakirja`](#sanakirja) - [`sanakirja`](#sanakirja)
- [`MDBX`](#mdbx) - [`MDBX`](#mdbx)
1. [Layers](#layers) 1. [Layers](#layers)
@ -165,6 +166,11 @@ The upstream versions from [`crates.io`](https://crates.io/crates/redb) are used
TODO: document DB on remote filesystem (does redb allow this?) TODO: document DB on remote filesystem (does redb allow this?)
## `redb-memory`
This backend is 100% the same as `redb`, although, it uses `redb::backend::InMemoryBackend` which is a key-value store that completely resides in memory instead of a file.
All other details about this should be the same as the normal `redb` backend.
## `sanakirja` ## `sanakirja`
[`sanakirja`](https://docs.rs/sanakirja) was a candidate as a backend, however there were problems with maximum value sizes. [`sanakirja`](https://docs.rs/sanakirja) was a candidate as a backend, however there were problems with maximum value sizes.

View file

@ -72,6 +72,10 @@ impl Env for ConcreteEnv {
// TODO: we can set cache sizes with: // TODO: we can set cache sizes with:
// env_builder.set_cache(bytes); // env_builder.set_cache(bytes);
// Use the in-memory backend if the feature is enabled.
let mut env = if cfg!(feature = "redb-memory") {
env_builder.create_with_backend(redb::backends::InMemoryBackend::new())?
} else {
// Create the database directory if it doesn't exist. // Create the database directory if it doesn't exist.
std::fs::create_dir_all(config.db_directory())?; std::fs::create_dir_all(config.db_directory())?;
@ -81,7 +85,9 @@ impl Env for ConcreteEnv {
.write(true) .write(true)
.create(true) .create(true)
.open(config.db_file())?; .open(config.db_file())?;
let mut env = env_builder.create_file(db_file)?;
env_builder.create_file(db_file)?
};
// Create all database tables. // Create all database tables.
// `redb` creates tables if they don't exist. // `redb` creates tables if they don't exist.