pub struct Storage<K, V> { /* private fields */ }
Expand description
A thread-safe key-value storage using DashMap.
§Examples
use kvdb_lib::Storage;
let storage = Storage::new(); storage.set(1, “value1”);
assert_eq!(storage.get(&1), Some(“value1”));
Implementations§
Source§impl<K, V> Storage<K, V>
impl<K, V> Storage<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty Storage
.
§Examples
use kvdb_lib::Storage;
let storage: Storage<i32, &str> = Storage::new();
Sourcepub fn set(&self, key: K, value: V)
pub fn set(&self, key: K, value: V)
Inserts a key-value pair into the storage.
If the storage did not have this key present, None
is returned.
If the storage did have this key present, the value is updated, and the old value is returned.
§Examples
use kvdb_lib::Storage;
let storage = Storage::new(); storage.set(1, “value1”);
assert_eq!(storage.get(&1), Some(“value1”));
Sourcepub fn get(&self, key: &K) -> Option<V>
pub fn get(&self, key: &K) -> Option<V>
Retrieves a value from the storage, given a key.
§Examples
use kvdb_lib::Storage;
let storage = Storage::new(); storage.set(1, “value1”);
assert_eq!(storage.get(&1), Some(“value1”));
Sourcepub fn get_all(&self) -> Vec<(K, V)>
pub fn get_all(&self) -> Vec<(K, V)>
Retrieves all key-value pairs from the storage as a vector of tuples.
§Examples
use kvdb_lib::Storage;
let storage = Storage::new();
storage.set(1, “value1”); storage.set(2, “value2”); let all = storage.get_all();
assert_eq!(all.len(), 2); assert!(all.contains(&(1, “value1”))); assert!(all.contains(&(2, “value2”)));