pub struct PersistentKeyValueStore<K, V> { /* private fields */ }
Implementations§
Source§impl<K, V> PersistentKeyValueStore<K, V>where
K: Serializable,
impl<K, V> PersistentKeyValueStore<K, V>where
K: Serializable,
Sourcepub fn new(path: impl AsRef<Path>, config: Config) -> Result<Self>
pub fn new(path: impl AsRef<Path>, config: Config) -> Result<Self>
Constructs a new store instance. The store will be backed by the given path and use the provided configuration. This function will block on restoring previously saved state from disk.
Only one store instance can be alive at a time for a given path.
§Iterators
The store acts like a collection and supports (read-only) Iterator
but
does not support any mutating collection traits (e.g. FromIterator
or
Extend
). This is needed since set() is fallible.
A consuming iterator consumes the in-memory store only, not the on-disk data.
§Example
use persistent_kv::{Config, PersistentKeyValueStore};
let path = "/tmp/mystore1";
let store: PersistentKeyValueStore<String, String> =
PersistentKeyValueStore::new(path, Config::default())?;
store.set("foo", "1")?;
// Drop the first store instance to ensure no two instances are alive.
drop(store);
// Second instance constructed from same path recovers the data.
let store: PersistentKeyValueStore<String, String> =
PersistentKeyValueStore::new(path, Config::default())?;
assert_eq!(store.get("foo"), Some("1".to_string()));
§Errors
Propagates IO errors when reading from disk, also fails when the snapshot files don’t follow the exact naming schema expected (and written) by this crate.
Sourcepub fn unset<Q>(&self, key: &Q) -> Result<()>
pub fn unset<Q>(&self, key: &Q) -> Result<()>
Removes a key from the store.
Supports using borrowed keys (e.g. str
for a String
key).
§Example
use persistent_kv::{Config, PersistentKeyValueStore};
let store: PersistentKeyValueStore<String, String> =
PersistentKeyValueStore::new("/tmp/mystore2", Config::default())?;
store.set("foo", "1")?;
assert_eq!(store.get("foo"), Some("1".to_string()));
store.unset("foo")?;
assert_eq!(store.get("foo"), None);
§Errors
Propagates any IO errors that occur directly as a result of the write operation.
Source§impl<K, V> PersistentKeyValueStore<K, V>
Store methods for simple values: Vecu8, String, integers. We bypass all serialization
frameworks for these types.
impl<K, V> PersistentKeyValueStore<K, V>
Store methods for simple values: Vecu8, String, integers. We bypass all serialization frameworks for these types.
Sourcepub fn set(&self, key: impl Into<K>, value: impl Into<V>) -> Result<()>
pub fn set(&self, key: impl Into<K>, value: impl Into<V>) -> Result<()>
Sets a key-value pair in the store. If the key already exists, the value will be overwritten.
§Example
use persistent_kv::{Config, PersistentKeyValueStore};
let store: PersistentKeyValueStore<String, String> =
PersistentKeyValueStore::new("/tmp/mystore3", Config::default())?;
store.set("foo", "1")?;
assert_eq!(store.get("foo"), Some("1".to_string()));
§Errors
Propagates any IO errors that occur directly as a result of the write operation.
Sourcepub fn get<Q>(&self, key: &Q) -> Option<V>
pub fn get<Q>(&self, key: &Q) -> Option<V>
Retrieves a value from the store.
Supports lookups using borrowed keys (e.g. str
for a String
key).
§Example
use persistent_kv::{Config, PersistentKeyValueStore};
let store: PersistentKeyValueStore<String, String> =
PersistentKeyValueStore::new("/tmp/mystore4", Config::default())?;
store.set("foo", "1")?;
assert_eq!(store.get("foo"), Some("1".to_string()));
Source§impl<K, V> PersistentKeyValueStore<K, V>
Store version for protobuf values.
impl<K, V> PersistentKeyValueStore<K, V>
Store version for protobuf values.
Sourcepub fn set_proto(&self, key: impl Into<K>, value: impl Message) -> Result<()>
pub fn set_proto(&self, key: impl Into<K>, value: impl Message) -> Result<()>
Sets a protobuf-coded value in the store. If the key already exists, the value will be overwritten.
§Example
use prost::Message;
use persistent_kv::{Config, PersistentKeyValueStore};
#[derive(Clone, PartialEq, Message)]
pub struct Foo {
#[prost(uint32, tag = "1")]
pub bar: u32,
}
let store: PersistentKeyValueStore<String, Foo> =
PersistentKeyValueStore::new("/tmp/mystore5", Config::default())?;
store.set_proto("foo", Foo {bar: 42})?;
assert_eq!(store.get_proto("foo")?, Some(Foo {bar: 42}));
§Errors
Propagates any IO errors that occur directly as a result of the write operation.
Sourcepub fn get_proto<Q>(&self, key: &Q) -> Result<Option<V>, DecodeError>
pub fn get_proto<Q>(&self, key: &Q) -> Result<Option<V>, DecodeError>
Retrieves a protobuf-coded value from the store.
Supports lookups using borrowed keys (e.g. str
for a String
key).
§Example
use prost::Message;
use persistent_kv::{Config, PersistentKeyValueStore};
#[derive(Clone, PartialEq, Message)]
pub struct Foo {
#[prost(uint32, tag = "1")]
pub bar: u32,
}
let store: PersistentKeyValueStore<String, Foo> =
PersistentKeyValueStore::new("/tmp/mystore6", Config::default())?;
store.set_proto("foo", Foo {bar: 42})?;
assert_eq!(store.get_proto("foo")?, Some(Foo {bar: 42}));
§Errors
Forwards proto decode errors.
Source§impl<K, V> PersistentKeyValueStore<K, V>
impl<K, V> PersistentKeyValueStore<K, V>
Trait Implementations§
Source§impl<K, V> Debug for PersistentKeyValueStore<K, V>
Debug trait for PersistentKeyValueStore
impl<K, V> Debug for PersistentKeyValueStore<K, V>
Debug trait for PersistentKeyValueStore
Source§impl<'a, K, V> IntoIterator for &'a PersistentKeyValueStore<K, V>
impl<'a, K, V> IntoIterator for &'a PersistentKeyValueStore<K, V>
Source§type IntoIter = Box<dyn Iterator<Item = <&'a PersistentKeyValueStore<K, V> as IntoIterator>::Item> + 'a>
type IntoIter = Box<dyn Iterator<Item = <&'a PersistentKeyValueStore<K, V> as IntoIterator>::Item> + 'a>
Source§impl<K, V> IntoIterator for PersistentKeyValueStore<K, V>
Implement IntoIterator for PersistentKeyValueStore and for &PersistentKeyValueStore
using the underlying store’s implementation, encoding/decoding keys and values as needed.
impl<K, V> IntoIterator for PersistentKeyValueStore<K, V>
Implement IntoIterator for PersistentKeyValueStore and for &PersistentKeyValueStore using the underlying store’s implementation, encoding/decoding keys and values as needed.