This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing
namespace
to the kv-store API. (#108)
- Loading branch information
1 parent
7d731a4
commit c07557b
Showing
30 changed files
with
413 additions
and
259 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,53 @@ | ||
/// Concatenates a namespace and a key into a fully-qualified key. | ||
#[allow(unused)] | ||
pub fn concat_ns_to_key<NS, K>(ns: NS, key: K) -> Vec<u8> | ||
where | ||
NS: AsRef<[u8]>, | ||
K: AsRef<[u8]>, | ||
{ | ||
let ns = ns.as_ref(); | ||
let key = key.as_ref(); | ||
|
||
let cap = if ns.len() > 0 { | ||
ns.len() + 1 + key.len() | ||
} else { | ||
key.len() | ||
}; | ||
|
||
let mut buf = Vec::with_capacity(cap); | ||
|
||
if ns.len() > 0 { | ||
buf.extend_from_slice(ns); | ||
buf.extend_from_slice(&[b':']); | ||
} | ||
|
||
buf.extend_from_slice(key); | ||
buf | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn concat_ns_to_key_empty_ns() { | ||
let ns = vec![]; | ||
let key = vec![b'a', b'b', b'c']; | ||
|
||
let actual = concat_ns_to_key(&ns, &key); | ||
let expected = "abc".as_bytes(); | ||
|
||
assert_eq!(&expected[..], &actual[..]); | ||
} | ||
|
||
#[test] | ||
fn concat_ns_to_key_non_empty_ns() { | ||
let ns = vec![b'n', b's']; | ||
let key = vec![b'a', b'b', b'c']; | ||
|
||
let actual = concat_ns_to_key(&ns, &key); | ||
let expected = "ns:abc".as_bytes(); | ||
|
||
assert_eq!(&expected[..], &actual[..]); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
/// `KVStore` is a trait for defining an interface against key-value stores. for example `in-memory / rocksdb` | ||
/// `KVStore` is a trait for defining an interface against key-value stores, for example `rocksdb/leveldb`. | ||
pub trait KVStore { | ||
/// Retrieves the value pointed by `key` (Optional). | ||
#[must_use] | ||
fn get(&self, key: &[u8]) -> Option<Vec<u8>>; | ||
fn get(&self, ns: &[u8], key: &[u8]) -> Option<Vec<u8>>; | ||
|
||
/// Stores a batch of changes. Each change is `key` -> `value` association. | ||
fn store(&mut self, changes: &[(&[u8], &[u8])]); | ||
/// Stores a batch of changes. Each change is `(ns, key) -> value` association. | ||
fn store(&mut self, changes: &[(&[u8], &[u8], &[u8])]); | ||
} |
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
Oops, something went wrong.