Skip to content

Commit

Permalink
*: minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdroychan committed Aug 2, 2024
1 parent 388d4be commit 03530a6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
15 changes: 0 additions & 15 deletions src/stores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,10 @@

use crate::bench::Benchmark;
use crate::*;
use ahash::AHasher;
use hashbrown::HashMap;
use log::debug;
use std::hash::Hasher;
use toml::Table;

pub fn hash(key: &[u8]) -> u64 {
let mut hasher = AHasher::default();
hasher.write(key);
u64::from(hasher.finish())
}

pub fn find_shard(key: &[u8], nr_shards: usize) -> usize {
let mut hasher = AHasher::default();
hasher.write(key);
let hash = u64::from(hasher.finish());
usize::try_from(hash).unwrap() % nr_shards
}

/// A unified enum for a created key-value store that is ready to run.
pub enum BenchKVMap {
Regular(Box<dyn KVMap>),
Expand Down
29 changes: 22 additions & 7 deletions src/stores/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,27 @@
//!
//! This store is [`KVMap`].

use crate::stores::*;
use crate::stores::{BenchKVMap, Registry};
use crate::*;
use ::hashbrown::HashMap;
use ahash::AHasher;
use parking_lot::{Mutex, RwLock};
use serde::Deserialize;
use std::hash::Hasher;
use std::sync::Arc;

/// Calculate the [`u64`] hash value of a given key using [`AHasher`].
pub fn hash(key: &[u8]) -> u64 {
let mut hasher = AHasher::default();
hasher.write(key);
u64::from(hasher.finish())
}

pub fn shard(key: &[u8], nr_shards: usize) -> usize {
let hash = hash(key);
usize::try_from(hash).unwrap() % nr_shards
}

/// A wrapper around raw [`HashMap`] with variable-sized keys and values.
///
/// It is used as the building block of other types. Note that this is not [`KVMap`].
Expand Down Expand Up @@ -68,20 +83,20 @@ impl KVMap for MutexHashMap {

impl KVMapHandle for MutexHashMap {
fn set(&mut self, key: &[u8], value: &[u8]) {
let sid = find_shard(key, self.nr_shards);
let sid = shard(key, self.nr_shards);
self.shards[sid].lock().insert(key.into(), value.into());
}

fn get(&mut self, key: &[u8]) -> Option<Box<[u8]>> {
let sid = find_shard(key, self.nr_shards);
let sid = shard(key, self.nr_shards);
match self.shards[sid].lock().get(key) {
Some(v) => Some(v.clone()),
None => None,
}
}

fn delete(&mut self, key: &[u8]) {
let sid = find_shard(key, self.nr_shards);
let sid = shard(key, self.nr_shards);
self.shards[sid].lock().remove(key);
}
}
Expand Down Expand Up @@ -130,20 +145,20 @@ impl KVMap for RwLockHashMap {

impl KVMapHandle for RwLockHashMap {
fn set(&mut self, key: &[u8], value: &[u8]) {
let sid = find_shard(key, self.nr_shards);
let sid = shard(key, self.nr_shards);
self.shards[sid].write().insert(key.into(), value.into());
}

fn get(&mut self, key: &[u8]) -> Option<Box<[u8]>> {
let sid = find_shard(key, self.nr_shards);
let sid = shard(key, self.nr_shards);
match self.shards[sid].read().get(key) {
Some(v) => Some(v.clone()),
None => None,
}
}

fn delete(&mut self, key: &[u8]) {
let sid = find_shard(key, self.nr_shards);
let sid = shard(key, self.nr_shards);
self.shards[sid].write().remove(key);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//!
//! A key-value store is generally passive. However, some store may act like a server with
//! active threads. In that case, one may employ its own implementation of spawn-join. If that is
//! the case, their join handle (like `[std::thread::JoinHandle`]) should implement the
//! [`JoinHandle`] trait and the spawn struct needs to implement `[Spawn]`.
//! the case, their join handle (like [`std::thread::JoinHandle`]) should implement the
//! [`JoinHandle`] trait and the spawn struct needs to implement [`Spawn]`.
//!
//! Note that for simplicity, the function spawn is generic over should not have a return value. So
//! it is with the [`JoinHandle`]. Because the purpose is not general spawn-join but solely for
Expand Down

0 comments on commit 03530a6

Please sign in to comment.