Skip to content

Commit

Permalink
stores: add adapter for btreemap
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdroychan committed Jul 25, 2024
1 parent fbfbb73 commit dd05d84
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 6 deletions.
2 changes: 2 additions & 0 deletions presets/stores/mutex_btreemap.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[map]
name = "mutex_btreemap"
2 changes: 2 additions & 0 deletions presets/stores/rwlock_btreemap.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[map]
name = "rwlock_btreemap"
22 changes: 20 additions & 2 deletions src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ mod tests {
}

#[test]
fn example_mutex() {
fn example_mutex_hashmap() {
const OPT: &str = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/presets/stores/mutex_hashmap.toml"
Expand All @@ -911,7 +911,7 @@ mod tests {
}

#[test]
fn example_rwlock() {
fn example_rwlock_hashmap() {
const OPT: &str = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/presets/stores/rwlock_hashmap.toml"
Expand Down Expand Up @@ -972,6 +972,24 @@ mod tests {
));
example(OPT);
}

#[test]
fn example_mutex_btreemap() {
const OPT: &str = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/presets/stores/mutex_btreemap.toml"
));
example(OPT);
}

#[test]
fn example_rwlock_btreemap() {
const OPT: &str = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/presets/stores/rwlock_btreemap.toml"
));
example(OPT);
}
}

// }}} tests
16 changes: 14 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,14 +905,14 @@ mod tests {
}

#[test]
fn batch_mutex() {
fn batch_mutex_hashmap() {
let opt = hashmap::MutexHashMapOpt { shards: 512 };
let map = BenchKVMap::Regular(Box::new(hashmap::MutexHashMap::new(&opt)));
batch(map);
}

#[test]
fn batch_rwlock() {
fn batch_rwlock_hashmap() {
let opt = hashmap::RwLockHashMapOpt { shards: 512 };
let map = BenchKVMap::Regular(Box::new(hashmap::RwLockHashMap::new(&opt)));
batch(map);
Expand Down Expand Up @@ -953,4 +953,16 @@ mod tests {
let map = BenchKVMap::Regular(Box::new(papaya::Papaya::new()));
batch(map);
}

#[test]
fn batch_mutex_btreemap() {
let map = BenchKVMap::Regular(Box::new(btreemap::MutexBTreeMap::new()));
batch(map);
}

#[test]
fn batch_rwlock_btreemap() {
let map = BenchKVMap::Regular(Box::new(btreemap::RwLockBTreeMap::new()));
batch(map);
}
}
109 changes: 109 additions & 0 deletions src/stores/btreemap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//! Adapter implementation of [`std::collections::BTreeMap`].
//!
//! ## Configuration Format
//!
//! ### [`Mutex`]-based:
//!
//! ``` toml
//! [map]
//! name = "mutex_btreemap"
//! ```
//!
//! This store is [`KVMap`].
//!
//! ### [`RwLock`]-based:
//! ``` toml
//! [map]
//! name = "rwlock_btreemap"
//! ```
//!
//! This store is [`KVMap`].
use crate::stores::*;
use parking_lot::{Mutex, RwLock};
use std::collections::BTreeMap;
use std::sync::Arc;

#[derive(Clone)]
pub struct MutexBTreeMap(Arc<Mutex<BTreeMap<Box<[u8]>, Box<[u8]>>>>);

impl MutexBTreeMap {
pub fn new() -> Self {
Self(Arc::new(
Mutex::new(BTreeMap::<Box<[u8]>, Box<[u8]>>::new()),
))
}

pub fn new_benchkvmap(_opt: &toml::Table) -> BenchKVMap {
BenchKVMap::Regular(Box::new(Self::new()))
}
}

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

impl KVMapHandle for MutexBTreeMap {
fn set(&mut self, key: &[u8], value: &[u8]) {
self.0.lock().insert(key.into(), value.into());
}

fn get(&mut self, key: &[u8]) -> Option<Box<[u8]>> {
match self.0.lock().get(key) {
Some(v) => Some(v.clone()),
None => None,
}
}

fn delete(&mut self, key: &[u8]) {
self.0.lock().remove(key);
}
}

inventory::submit! {
Registry::new("mutex_btreemap", MutexBTreeMap::new_benchkvmap)
}

#[derive(Clone)]
pub struct RwLockBTreeMap(Arc<RwLock<BTreeMap<Box<[u8]>, Box<[u8]>>>>);

impl RwLockBTreeMap {
pub fn new() -> Self {
Self(Arc::new(RwLock::new(
BTreeMap::<Box<[u8]>, Box<[u8]>>::new(),
)))
}

pub fn new_benchkvmap(_opt: &toml::Table) -> BenchKVMap {
BenchKVMap::Regular(Box::new(Self::new()))
}
}

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

impl KVMapHandle for RwLockBTreeMap {
fn set(&mut self, key: &[u8], value: &[u8]) {
self.0.write().insert(key.into(), value.into());
}

fn get(&mut self, key: &[u8]) -> Option<Box<[u8]>> {
match self.0.read().get(key) {
Some(v) => Some(v.clone()),
None => None,
}
}

fn delete(&mut self, key: &[u8]) {
self.0.write().remove(key);
}
}

inventory::submit! {
Registry::new("rwlock_btreemap", RwLockBTreeMap::new_benchkvmap)
}
3 changes: 1 addition & 2 deletions src/stores/hashmap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Adapter implementation of [`hashbrown::HashMap`]. Sharded, and in flavors of both [`Mutex`] and
//! [`RwLock`].
//! Adapter implementation of [`hashbrown::HashMap`]. Internally sharded.
//!
//! ## Configuration Format
//!
Expand Down
13 changes: 13 additions & 0 deletions src/stores/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl BenchKVMap {
}
}

pub mod btreemap;
pub mod chashmap;
pub mod contrie;
pub mod dashmap;
Expand Down Expand Up @@ -199,4 +200,16 @@ mod tests {
let mut map = papaya::Papaya::new();
map_test(&mut map);
}

#[test]
fn mutex_btreemap() {
let mut map = btreemap::MutexBTreeMap::new();
map_test(&mut map);
}

#[test]
fn rwlock_btreemap() {
let mut map = btreemap::RwLockBTreeMap::new();
map_test(&mut map);
}
}

0 comments on commit dd05d84

Please sign in to comment.