diff --git a/database/Cargo.toml b/database/Cargo.toml index a340d0d0..33c3937c 100644 --- a/database/Cargo.toml +++ b/database/Cargo.toml @@ -9,11 +9,13 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/database" keywords = ["cuprate", "database"] [features] -default = ["heed", "redb", "service"] +# default = ["heed", "redb", "service"] # default = ["redb", "service"] -heed = ["dep:heed"] -redb = ["dep:redb"] -service = ["dep:crossbeam", "dep:futures", "dep:tokio", "dep:tokio-util", "dep:tower", "dep:rayon"] +default = ["redb-memory", "service"] +heed = ["dep:heed"] +redb = ["dep:redb"] +redb-memory = ["redb"] +service = ["dep:crossbeam", "dep:futures", "dep:tokio", "dep:tokio-util", "dep:tower", "dep:rayon"] [dependencies] bytemuck = { version = "1.14.3", features = ["must_cast", "derive", "min_const_generics", "extern_crate_alloc"] } diff --git a/database/README.md b/database/README.md index e45f9732..74d07149 100644 --- a/database/README.md +++ b/database/README.md @@ -11,6 +11,7 @@ Cuprate's database implementation. 1. [Backends](#backends) - [`heed`](#heed) - [`redb`](#redb) + - [`redb-memory`](#redb-memory) - [`sanakirja`](#sanakirja) - [`MDBX`](#mdbx) 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?) +## `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`](https://docs.rs/sanakirja) was a candidate as a backend, however there were problems with maximum value sizes. diff --git a/database/src/backend/redb/env.rs b/database/src/backend/redb/env.rs index 0f4949ef..5708d6fa 100644 --- a/database/src/backend/redb/env.rs +++ b/database/src/backend/redb/env.rs @@ -72,16 +72,22 @@ impl Env for ConcreteEnv { // TODO: we can set cache sizes with: // env_builder.set_cache(bytes); - // Create the database directory if it doesn't exist. - std::fs::create_dir_all(config.db_directory())?; + // 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. + std::fs::create_dir_all(config.db_directory())?; - // Open the database file, create if needed. - let db_file = std::fs::OpenOptions::new() - .read(true) - .write(true) - .create(true) - .open(config.db_file())?; - let mut env = env_builder.create_file(db_file)?; + // Open the database file, create if needed. + let db_file = std::fs::OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open(config.db_file())?; + + env_builder.create_file(db_file)? + }; // Create all database tables. // `redb` creates tables if they don't exist.