pub struct KvStore<S: Storage> { /* private fields */ }Expand description
A versioned key-value store using a log-structured hash table.
Reads and writes serde keys and values. All reads/writes happen through
transactions. A store can be merged to discard old versions and thus store a
more compact representation containing only the latest version of each
key-value pair.
Versions are also used to implement a “move to trash” behavior. Whenever a
value is removed, it is not purged from storage but simply marked as
removed. It remains accessible until the “trash is emptied” during the next
merge. As a consequence there are 2 different methods that can read values
from the store, depending on whether the trash should be included or not,
Snapshot::get() (which will return None if the value was “moved to the
trash”) and Snapshot::get_unremoved() (which will return the last
unremoved version if the value was “moved to the trash”).
- Keys/values are
Serialize/Deserializeand are serialized/deserialized to/from MessagePack usingrmp-serde. - Keys are not read/written as-is, but always associated with a “slot”. These slots act as the different “indexes” of a store.
Implementations§
Source§impl<S: Storage> KvStore<S>
impl<S: Storage> KvStore<S>
Sourcepub async fn open(storage: S) -> Result<Self>
pub async fn open(storage: S) -> Result<Self>
Opens and reads a store from storage.
If no store exists at the storage location, a new store will be initialized. Otherwise, the store will be read and checked for corrupted data. In case of corruption, everything after the corrupted offset will be truncated and later writes will overwrite the corrupted entries. After the initial read, a hash table of all the keys in the store and their storage offsets is kept in memory.
Sourcepub fn into_storage(self) -> Result<S>
pub fn into_storage(self) -> Result<S>
Consumes the store to return its underlying storage.
Sourcepub async fn current(&self) -> Snapshot<'_, S>
pub async fn current(&self) -> Snapshot<'_, S>
Creates a transactional read-write snapshot of the store at the current
point in time, see Snapshot.
Sourcepub async fn merge(&mut self) -> Result<()>
pub async fn merge(&mut self) -> Result<()>
Merges and compacts the store by removing old versions.
Merging a store reclaims space by removing all versions that were superseded by newer writes to the same key. As a side effect, a merge “empties the trash” and ensures that removed values cannot be read and restored anymore.