-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stores: add initial support for rocksdb (no customization)
- Loading branch information
1 parent
16c6ae7
commit 03b652b
Showing
3 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 serde::Deserialize; | ||
use rocksdb::DB; | ||
|
||
#[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) | ||
} |