Skip to content

Commit

Permalink
stores: add initial support for rocksdb (no customization)
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdroychan committed Jul 31, 2024
1 parent 16c6ae7 commit 25cf9b2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ rustdoc-args = ["--cfg", "docsrs"]
ahash = "0.8.11"
bincode = "1.3.3"
chashmap = { version = "2.2.2", optional = true }
clap = { version = "=4.5.11", features = ["derive"] }
clap = { version = "=4.5.13", features = ["derive"] }
contrie = { version = "0.1.4", optional = true }
core_affinity = "0.8.1"
ctrlc = "3.4.4"
Expand All @@ -32,13 +32,14 @@ hdrhistogram = "7.5.4"
inventory = "0.3.15"
log = "0.4.22"
mio = { version = "1.0.1", features = ["net", "os-poll"] }
papaya = { version = "0.1.2", optional = true }
papaya = { version = "0.1.3", optional = true }
parking_lot = "0.12.3"
quanta = "0.12.3"
rand = "0.8.5"
rocksdb = { version = "0.22.0", optional = true }
scc = { version = "2.1.6", optional = true }
serde = { version = "1.0.204", features = ["derive"] }
toml = "0.8.16"
toml = "0.8.19"
zipf = "7.0.1"

[features]
Expand All @@ -47,6 +48,7 @@ contrie = ["dep:contrie"]
dashmap = ["dep:dashmap"]
flurry = ["dep:flurry"]
papaya = ["dep:papaya"]
rocksdb = ["dep:rocksdb"]
scc = ["dep:scc"]

[profile.release-lto]
Expand Down
2 changes: 2 additions & 0 deletions src/stores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub mod null;
#[cfg(feature = "papaya")]
pub mod papaya;
pub mod remote;
#[cfg(feature = "rocksdb")]
pub mod rocksdb;
#[cfg(feature = "scc")]
pub mod scc;

Expand Down
66 changes: 66 additions & 0 deletions src/stores/rocksdb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! Adapter implementation of [`rocksdb`].
//!
//! ## Configuration Format
//!
//! ``` toml
//! [map]
//! name = "rocksdb"
//! path = "..." # path to the rocksdb data directory
//! ```
//!
//! This store is [`KVMap`].

use crate::stores::{BenchKVMap, Registry};
use crate::*;
use rocksdb::DB;
use serde::Deserialize;

#[derive(Deserialize)]
pub struct RocksDBOpt {
path: String,
}

#[derive(Clone)]
pub struct RocksDB {
db: Arc<DB>,
}

impl RocksDB {
pub fn new(opt: &RocksDBOpt) -> Self {
let db = Arc::new(DB::open_default(&opt.path).unwrap());
Self { db }
}

pub fn new_benchkvmap(opt: &toml::Table) -> BenchKVMap {
let opt: RocksDBOpt = opt.clone().try_into().unwrap();
BenchKVMap::Regular(Box::new(Self::new(&opt)))
}
}

impl KVMap for RocksDB {
fn handle(&self) -> Box<dyn KVMapHandle> {
Box::new(self.clone())
}
}

impl KVMapHandle for RocksDB {
fn set(&mut self, key: &[u8], value: &[u8]) {
assert!(self.db.put(key, value).is_ok());
}

fn get(&mut self, key: &[u8]) -> Option<Box<[u8]>> {
if let Ok(v) = self.db.get(key) {
v.map(|vec| vec.into_boxed_slice())
} else {
None
}
}

fn delete(&mut self, key: &[u8]) {
assert!(self.db.delete(key).is_ok());
}
}

inventory::submit! {
Registry::new("rocksdb", RocksDB::new_benchkvmap)
}

0 comments on commit 25cf9b2

Please sign in to comment.