Skip to content

Commit

Permalink
*: skeleton of range query
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdroychan committed Aug 5, 2024
1 parent 9fe8fd4 commit f277b98
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,9 @@ fn bench_worker_regular(
Operation::Delete { key } => {
handle.delete(&key[..]);
}
Operation::Scan { key, n } => {
let _ = handle.scan(&key[..], n);
}
}
let op_end = latency_tick();
if let Some(ref mut l) = latency {
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ pub trait KVMapHandle {
/// Removing a key if it exists.
fn delete(&mut self, key: &[u8]);

// fn read_modify_write(&mut self, key: &[u8]);
/// Querying a range starting from the first key greater than or equal to the given key.
fn scan(&mut self, key: &[u8], n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)>;
}

/// A single operation that is applied to the key-value store.
Expand All @@ -104,6 +105,9 @@ pub enum Operation {

/// Removing a key if it exists.
Delete { key: Box<[u8]> },

/// Querying a range starting from the first key greater than or equal to the given key.
Scan { key: Box<[u8]>, n: usize },
}

/// A request sent by a client to a server.
Expand Down
3 changes: 3 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ fn serve_requests_regular(
handle.delete(key);
assert!(write_response(&mut *writer, id, None).is_ok());
}
Operation::Scan { .. } => {
todo!()
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/stores/btreemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ impl KVMapHandle for MutexBTreeMap {
fn delete(&mut self, key: &[u8]) {
self.0.lock().remove(key);
}

fn scan(&mut self, _key: &[u8], _n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
// technically iteration is supported but querying a specific range is not a stable feature
unimplemented!("Range query is not supported");
}
}

inventory::submit! {
Expand Down Expand Up @@ -102,6 +107,11 @@ impl KVMapHandle for RwLockBTreeMap {
fn delete(&mut self, key: &[u8]) {
self.0.write().remove(key);
}

fn scan(&mut self, _key: &[u8], _n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
// technically iteration is supported but querying a specific range is not a stable feature
unimplemented!("Range query is not supported");
}
}

inventory::submit! {
Expand Down
4 changes: 4 additions & 0 deletions src/stores/chashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ impl KVMapHandle for CHashMap {
fn delete(&mut self, key: &[u8]) {
self.0.remove(key);
}

fn scan(&mut self, _key: &[u8], _n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
unimplemented!("Range query is not supported");
}
}

inventory::submit! {
Expand Down
4 changes: 4 additions & 0 deletions src/stores/contrie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl KVMapHandle for Contrie {
fn delete(&mut self, key: &[u8]) {
self.0.remove(key);
}

fn scan(&mut self, _key: &[u8], _n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
unimplemented!("Range query is not supported");
}
}

inventory::submit! {
Expand Down
4 changes: 4 additions & 0 deletions src/stores/dashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl KVMapHandle for DashMap {
fn delete(&mut self, key: &[u8]) {
self.0.remove(key);
}

fn scan(&mut self, _key: &[u8], _n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
unimplemented!("Range query is not supported");
}
}

inventory::submit! {
Expand Down
4 changes: 4 additions & 0 deletions src/stores/flurry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl KVMapHandle for Flurry {
fn delete(&mut self, key: &[u8]) {
self.0.pin().remove(key);
}

fn scan(&mut self, _key: &[u8], _n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
unimplemented!("Range query is not supported");
}
}

inventory::submit! {
Expand Down
8 changes: 8 additions & 0 deletions src/stores/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ impl KVMapHandle for MutexHashMap {
let sid = shard(key, self.nr_shards);
self.shards[sid].lock().remove(key);
}

fn scan(&mut self, _key: &[u8], _n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
unimplemented!("Range query is not supported");
}
}

inventory::submit! {
Expand Down Expand Up @@ -161,6 +165,10 @@ impl KVMapHandle for RwLockHashMap {
let sid = shard(key, self.nr_shards);
self.shards[sid].write().remove(key);
}

fn scan(&mut self, _key: &[u8], _n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
unimplemented!("Range query is not supported");
}
}

inventory::submit! {
Expand Down
4 changes: 4 additions & 0 deletions src/stores/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ impl KVMapHandle for NullMap {
}

fn delete(&mut self, _key: &[u8]) {}

fn scan(&mut self, _key: &[u8], _n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
Vec::new()
}
}

inventory::submit! {
Expand Down
4 changes: 4 additions & 0 deletions src/stores/papaya.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl KVMapHandle for Papaya {
fn delete(&mut self, key: &[u8]) {
self.0.pin().remove(key);
}

fn scan(&mut self, _key: &[u8], _n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
unimplemented!("Range query is not supported");
}
}

inventory::submit! {
Expand Down
18 changes: 17 additions & 1 deletion src/stores/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use crate::stores::{BenchKVMap, Registry};
use crate::*;
use rocksdb::DB;
use rocksdb::{Direction, IteratorMode, DB};
use serde::Deserialize;

#[derive(Deserialize)]
Expand Down Expand Up @@ -59,6 +59,22 @@ impl KVMapHandle for RocksDB {
fn delete(&mut self, key: &[u8]) {
assert!(self.db.delete(key).is_ok());
}

fn scan(&mut self, key: &[u8], n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
let mut kv = Vec::with_capacity(n);
let iter = self
.db
.iterator(IteratorMode::From(key, Direction::Forward));
let mut i = 0;
for item in iter {
kv.push(item.unwrap());
i += 1;
if i == n {
break;
}
}
kv
}
}

inventory::submit! {
Expand Down
4 changes: 4 additions & 0 deletions src/stores/scc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ impl KVMapHandle for SccHashMap {
fn delete(&mut self, key: &[u8]) {
self.0.remove(key);
}

fn scan(&mut self, _key: &[u8], _n: usize) -> Vec<(Box<[u8]>, Box<[u8]>)> {
unimplemented!("Range query is not supported");
}
}

inventory::submit! {
Expand Down
2 changes: 1 addition & 1 deletion src/workload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ mod tests {
dist.entry(key).and_modify(|c| *c += 1).or_insert(0);
get += 1;
}
Operation::Delete { .. } => {
Operation::Delete { .. } | Operation::Scan { .. } => {
unreachable!();
}
}
Expand Down

0 comments on commit f277b98

Please sign in to comment.