Struct KeyValue

Source
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.

Wikipedia

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>,

Source

pub fn new(file: File) -> Result<Self, Box<dyn Error>>

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn keys(&self) -> Vec<&K>

Source

pub fn get(&self, key: &K) -> Result<Option<V>, Box<dyn Error>>

Source

pub fn set(&mut self, key: K, value: V) -> Result<(), Box<dyn Error>>

Source

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>

§

impl<K, V> Send for KeyValue<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for KeyValue<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for KeyValue<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for KeyValue<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.