Skip to main content

StorageBackend

Trait StorageBackend 

pub trait StorageBackend:
    'static
    + Debug
    + Send
    + Sync {
    // Required methods
    fn len(&self) -> Result<u64, Error>;
    fn read(&self, offset: u64, out: &mut [u8]) -> Result<(), Error>;
    fn set_len(&self, len: u64) -> Result<(), Error>;
    fn sync_data(&self) -> Result<(), Error>;
    fn write(&self, offset: u64, data: &[u8]) -> Result<(), Error>;

    // Provided methods
    fn close(&self) -> Result<(), Error> { ... }
    fn try_lock_range(
        &self,
        _start: Bound<u64>,
        _end: Bound<u64>,
    ) -> Result<bool, BackendError> { ... }
    fn try_lock_shared_range(
        &self,
        _start: Bound<u64>,
        _end: Bound<u64>,
    ) -> Result<bool, BackendError> { ... }
    fn lock_range(
        &self,
        _start: Bound<u64>,
        _end: Bound<u64>,
    ) -> Result<(), BackendError> { ... }
    fn lock_shared_range(
        &self,
        _start: Bound<u64>,
        _end: Bound<u64>,
    ) -> Result<(), BackendError> { ... }
    fn unlock_range(
        &self,
        _start: Bound<u64>,
        _end: Bound<u64>,
    ) -> Result<(), BackendError> { ... }
    fn query_lock_range(
        &self,
        _start: Bound<u64>,
        _end: Bound<u64>,
    ) -> Result<bool, BackendError> { ... }
}
Expand description

Db::open_with_backend に渡すもの。redb の版を合わせなくて済むように出しておく Implements persistent storage for a database.

I/O failures are reported as io::Error, which is std::io::Error whenever std is available. Locking operations use [BackendError] to distinguish unsupported operations.

Locking is optional. Backends that implement it must override the lock methods. Locks belong to a backend instance and must conflict with locks held by independently opened instances, including those in the same process. All locks must be released by Self::close. redb never acquires overlapping locks on the same backend. Failed or refused acquisitions must not acquire any locks or change existing locks.

Lock bounds refer to byte offsets and may extend beyond the current length of the storage. An unbounded start means offset zero; an unbounded end includes all future growth. Bounds must describe a nonempty range. Implementations may reject offsets or lengths that their underlying locking API cannot represent with [BackendError::Io].

redb never queries backend-reserved ranges and only locks them during whole-storage fallback. See the design document.

Backends that support locking must support one of the following levels:

  1. All representable ranges requested by redb are supported. All concurrency modes will work.
  2. Only whole-storage locks (Unbounded, Unbounded or Included(0), Unbounded) are supported. Other ranges return [BackendError::Unsupported]. Only the ExclusiveWriter concurrency mode will work.
  3. All inputs return [BackendError::Unsupported]. Only the ExclusiveWriter concurrency mode will work, and redb will log a warning on open if logging is enabled.

Required Methods§

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

Gets the current length of the storage.

fn read(&self, offset: u64, out: &mut [u8]) -> Result<(), Error>

Reads the specified array of bytes from the storage.

If out.len() + offset exceeds the length of the storage an appropriate Error must be returned.

fn set_len(&self, len: u64) -> Result<(), Error>

Sets the length of the storage.

New positions in the storage must be initialized to zero.

fn sync_data(&self) -> Result<(), Error>

Syncs all buffered data with the persistent storage.

fn write(&self, offset: u64, data: &[u8]) -> Result<(), Error>

Writes the specified array to the storage.

Provided Methods§

fn close(&self) -> Result<(), Error>

Release any resources held by the backend

Must release all locks acquired by one of the lock methods.

Note: redb will not access the backend after calling this method and will call it exactly once: when the [Database] is dropped, or, if a [WriteTransaction] was live at that point, when that transaction completes, or if opening the database fails

fn try_lock_range( &self, _start: Bound<u64>, _end: Bound<u64>, ) -> Result<bool, BackendError>

Attempts to acquire an exclusive byte-range lock.

Returns Ok(true) on acquisition, Ok(false) on a conflicting lock, or an error. Defaults to [BackendError::Unsupported].

fn try_lock_shared_range( &self, _start: Bound<u64>, _end: Bound<u64>, ) -> Result<bool, BackendError>

Attempts to acquire a shared byte-range lock.

Returns Ok(true) on acquisition, Ok(false) on a conflicting lock, or an error. Defaults to [BackendError::Unsupported].

fn lock_range( &self, _start: Bound<u64>, _end: Bound<u64>, ) -> Result<(), BackendError>

Acquires an exclusive byte-range lock, waiting for conflicting locks to be released.

Defaults to [BackendError::Unsupported].

fn lock_shared_range( &self, _start: Bound<u64>, _end: Bound<u64>, ) -> Result<(), BackendError>

Acquires a shared byte-range lock, waiting for conflicting locks to be released.

Defaults to [BackendError::Unsupported].

fn unlock_range( &self, _start: Bound<u64>, _end: Bound<u64>, ) -> Result<(), BackendError>

Releases the lock acquired over exactly this range.

The range passed will exactly match a range successfully locked by one of the lock methods.

Defaults to [BackendError::Unsupported].

fn query_lock_range( &self, _start: Bound<u64>, _end: Bound<u64>, ) -> Result<bool, BackendError>

Reports whether an exclusive lock over the range would conflict with a lock held elsewhere.

The queried range will not overlap any lock currently held by this backend. Defaults to [BackendError::Unsupported].

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

§

impl StorageBackend for FileBackend

§

impl StorageBackend for InMemoryBackend