Trait cuprate_database::DatabaseRo
source · pub unsafe trait DatabaseRo<T: Table> {
// Required methods
fn get(&self, key: &T::Key) -> Result<T::Value, RuntimeError>;
fn len(&self) -> Result<u64, RuntimeError>;
fn first(&self) -> Result<(T::Key, T::Value), RuntimeError>;
fn last(&self) -> Result<(T::Key, T::Value), RuntimeError>;
fn is_empty(&self) -> Result<bool, RuntimeError>;
// Provided method
fn contains(&self, key: &T::Key) -> Result<bool, RuntimeError> { ... }
}Expand description
Database (key-value store) read abstraction.
This is a read-only database table,
write operations are defined in DatabaseRw.
§Safety
The table type that implements this MUST be Send.
However if the table holds a reference to a transaction:
- only the transaction only has to be
Send - the table cannot implement
Send
For example:
heed’s transactions are Send but HeedTableRo contains a &
to the transaction, as such, if Send were implemented on HeedTableRo
then 1 transaction could be used to open multiple tables, then sent to
other threads - this would be a soundness hole against HeedTableRo.
&T is only Send if T: Sync.
heed::RoTxn: !Sync, therefore our table
holding &heed::RoTxn must NOT be Send.
Required Methods§
sourcefn get(&self, key: &T::Key) -> Result<T::Value, RuntimeError>
fn get(&self, key: &T::Key) -> Result<T::Value, RuntimeError>
Get the value corresponding to a key.
§Errors
This will return RuntimeError::KeyNotFound if:
- Input does not exist OR
- Database is empty
sourcefn len(&self) -> Result<u64, RuntimeError>
fn len(&self) -> Result<u64, RuntimeError>
Returns the number of (key, value) pairs in the database.
§Errors
This will never return RuntimeError::KeyNotFound.
sourcefn first(&self) -> Result<(T::Key, T::Value), RuntimeError>
fn first(&self) -> Result<(T::Key, T::Value), RuntimeError>
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 last(&self) -> Result<(T::Key, T::Value), RuntimeError>
fn last(&self) -> Result<(T::Key, T::Value), RuntimeError>
Returns the last (key, value) pair in the database.
§Errors
This will return RuntimeError::KeyNotFound if:
- Input does not exist OR
- Database is empty
sourcefn is_empty(&self) -> Result<bool, RuntimeError>
fn is_empty(&self) -> Result<bool, RuntimeError>
Returns true if the database contains no (key, value) pairs.
§Errors
This can only return RuntimeError::Io on errors.