KvStore

Struct KvStore 

Source
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/Deserialize and are serialized/deserialized to/from MessagePack using rmp-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>

Source

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.

Source

pub fn name(&self) -> &str

Returns the (file-)name of the storage.

Source

pub fn into_storage(self) -> Result<S>

Consumes the store to return its underlying storage.

Source

pub async fn len(&self) -> u64

Returns the total length of the storage in bytes.

Source

pub async fn is_empty(&self) -> bool

Returns true if the storage is empty.

Source

pub async fn current(&self) -> Snapshot<'_, S>

Creates a transactional read-write snapshot of the store at the current point in time, see Snapshot.

Source

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.

Auto Trait Implementations§

§

impl<S> !Freeze for KvStore<S>

§

impl<S> !RefUnwindSafe for KvStore<S>

§

impl<S> Send for KvStore<S>
where S: Send,

§

impl<S> Sync for KvStore<S>
where S: Send,

§

impl<S> Unpin for KvStore<S>
where S: Unpin,

§

impl<S> UnwindSafe for KvStore<S>
where S: 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.