Trait cuprate_database::DatabaseRw

source ·
pub trait DatabaseRw<T: Table>: DatabaseRo<T> {
    // Required methods
    fn put(
        &mut self,
        key: &T::Key,
        value: &T::Value,
    ) -> Result<(), RuntimeError>;
    fn delete(&mut self, key: &T::Key) -> Result<(), RuntimeError>;
    fn take(&mut self, key: &T::Key) -> Result<T::Value, RuntimeError>;
    fn pop_first(&mut self) -> Result<(T::Key, T::Value), RuntimeError>;
    fn pop_last(&mut self) -> Result<(T::Key, T::Value), RuntimeError>;

    // Provided method
    fn update<F>(&mut self, key: &T::Key, f: F) -> Result<(), RuntimeError>
       where F: FnMut(T::Value) -> Option<T::Value> { ... }
}
Expand description

Database (key-value store) read/write abstraction.

All DatabaseRo functions are also callable by DatabaseRw.

Required Methods§

source

fn put(&mut self, key: &T::Key, value: &T::Value) -> Result<(), RuntimeError>

Insert a key-value pair into the database.

This will overwrite any existing key-value pairs.

§Errors

This will return RuntimeError::KeyNotFound if:

  • Input does not exist OR
  • Database is empty

This will never RuntimeError::KeyExists.

source

fn delete(&mut self, key: &T::Key) -> Result<(), RuntimeError>

Delete a key-value pair in the database.

This will return Ok(()) if the key does not exist.

§Errors

This will return RuntimeError::KeyNotFound if:

  • Input does not exist OR
  • Database is empty

This will never RuntimeError::KeyExists.

source

fn take(&mut self, key: &T::Key) -> Result<T::Value, RuntimeError>

Delete and return a key-value pair in the database.

This is the same as DatabaseRw::delete, however, it will serialize the T::Value and return it.

§Errors

This will return RuntimeError::KeyNotFound if:

  • Input does not exist OR
  • Database is empty
source

fn pop_first(&mut self) -> Result<(T::Key, T::Value), RuntimeError>

Removes and returns the first (key, value) pair in the database.

§Errors

This will return RuntimeError::KeyNotFound if:

  • Input does not exist OR
  • Database is empty
source

fn pop_last(&mut self) -> Result<(T::Key, T::Value), RuntimeError>

Removes and returns the last (key, value) pair in the database.

§Errors

This will return RuntimeError::KeyNotFound if:

  • Input does not exist OR
  • Database is empty

Provided Methods§

source

fn update<F>(&mut self, key: &T::Key, f: F) -> Result<(), RuntimeError>
where F: FnMut(T::Value) -> Option<T::Value>,

Fetch the value, and apply a function to it - or delete the entry.

This will call DatabaseRo::get and call your provided function f on it.

The Option f returns will dictate whether update():

  • Updates the current value OR

  • Deletes the (key, value) pair

  • If f returns Some(value), that will be DatabaseRw::put as the new value

  • If f returns None, the entry will be DatabaseRw::deleted

§Errors

This will return RuntimeError::KeyNotFound if:

  • Input does not exist OR
  • Database is empty

Object Safety§

This trait is not object safe.

Implementors§