pub struct KeyValue<K, V> { /* private fields */ }
Expand description
Key Value Database
Definition:
A key-value database, or key-value store, is a data storage paradigm designed for storing, retrieving, and managing associative arrays, and a data structure more commonly known today as a dictionary or hash table. Dictionaries contain a collection of objects, or records, which in turn have many different fields within them, each containing data. These records are stored and retrieved using a key that uniquely identifies the record, and is used to find the data within the database.
Keys and Values can be arbitrary data types, as long as they can be
serialized to bincode via serde. using #[derive(Serialize, Deserialize)]
on your structs should suffice. Keys must implement the Eq
and Hash
trait.
§Examples
// any datatype that can be serialized by serde works
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Value {
num: i32,
}
// create a new db
let mut kv = wired::KeyValue::<String, Value>::new(file)?;
// insert an item
let key = String::from("some identifier");
let value = Value { num: 42 };
kv.set(key, value);
// retrieve an item
let key = String::from("some identifier");
let value = kv.get(&key)?; // Some(Value { num: 42 })
// delete an item
let key = String::from("some identifier");
kv.remove(&key)?;
Implementations§
Source§impl<K, V> KeyValue<K, V>where
for<'de> K: Serialize + Hash + Eq + Deserialize<'de>,
for<'de> V: Serialize + Deserialize<'de>,
impl<K, V> KeyValue<K, V>where
for<'de> K: Serialize + Hash + Eq + Deserialize<'de>,
for<'de> V: Serialize + Deserialize<'de>,
pub fn new(file: File) -> Result<Self, Box<dyn Error>>
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn keys(&self) -> Vec<&K>
pub fn get(&self, key: &K) -> Result<Option<V>, Box<dyn Error>>
pub fn set(&mut self, key: K, value: V) -> Result<(), Box<dyn Error>>
pub fn remove(&mut self, key: &K) -> Result<(), Box<dyn Error>>
Auto Trait Implementations§
impl<K, V> Freeze for KeyValue<K, V>
impl<K, V> RefUnwindSafe for KeyValue<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for KeyValue<K, V>
impl<K, V> Sync for KeyValue<K, V>
impl<K, V> Unpin for KeyValue<K, V>
impl<K, V> UnwindSafe for KeyValue<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more