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§

source

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
source

fn len(&self) -> Result<u64, RuntimeError>

Returns the number of (key, value) pairs in the database.

§Errors

This will never return RuntimeError::KeyNotFound.

source

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
source

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
source

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.

Provided Methods§

source

fn contains(&self, key: &T::Key) -> Result<bool, RuntimeError>

Returns true if the database contains a value for the specified key.

§Errors

Note that this will never return Err(RuntimeError::KeyNotFound), as in that case, Ok(false) will be returned.

Other errors may still occur.

Implementors§