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§
sourcefn put(&mut self, key: &T::Key, value: &T::Value) -> Result<(), RuntimeError>
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.
sourcefn delete(&mut self, key: &T::Key) -> Result<(), RuntimeError>
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.
sourcefn take(&mut self, key: &T::Key) -> Result<T::Value, RuntimeError>
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
sourcefn pop_first(&mut self) -> Result<(T::Key, T::Value), RuntimeError>
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
sourcefn pop_last(&mut self) -> Result<(T::Key, T::Value), RuntimeError>
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§
sourcefn update<F>(&mut self, key: &T::Key, f: F) -> Result<(), RuntimeError>
fn update<F>(&mut self, key: &T::Key, f: F) -> Result<(), RuntimeError>
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
freturnsSome(value), that will beDatabaseRw::putas the new value -
If
freturnsNone, the entry will beDatabaseRw::deleted
§Errors
This will return RuntimeError::KeyNotFound if:
- Input does not exist OR
- Database is empty