Skip to main content

StorageBackend

Trait StorageBackend 

Source
pub trait StorageBackend: Send + Sync {
    // Required methods
    fn get(&self, table: &str, key: &[u8]) -> Result<Option<Vec<u8>>>;
    fn put(&self, table: &str, key: &[u8], value: &[u8]) -> Result<()>;
    fn delete(&self, table: &str, key: &[u8]) -> Result<bool>;
    fn list(&self, table: &str) -> Result<Vec<(Vec<u8>, Vec<u8>)>>;
    fn create_table(&self, table: &str) -> Result<()>;

    // Provided methods
    fn list_keys_with_prefix(
        &self,
        table: &str,
        prefix: &[u8],
    ) -> Result<Vec<Vec<u8>>> { ... }
    fn delete_batch(&self, table: &str, keys: &[&[u8]]) -> Result<()> { ... }
    fn put_batch(&self, table: &str, entries: &[(&[u8], &[u8])]) -> Result<()> { ... }
    fn write_atomic(&self, ops: &[AtomicOp<'_>]) -> Result<()> { ... }
    fn schema_version(&self) -> Result<u32> { ... }
    fn needs_migration(&self) -> Result<bool> { ... }
    fn run_migrations(&self) -> Result<MigrationResult> { ... }
}
Expand description

Trait for pluggable storage backends.

Implementations must be thread-safe (Send + Sync).

Required Methods§

Source

fn get(&self, table: &str, key: &[u8]) -> Result<Option<Vec<u8>>>

Get a value by key from the specified table.

Source

fn put(&self, table: &str, key: &[u8], value: &[u8]) -> Result<()>

Store a key-value pair in the specified table.

Source

fn delete(&self, table: &str, key: &[u8]) -> Result<bool>

Delete a key from the specified table. Returns true if the key existed.

Source

fn list(&self, table: &str) -> Result<Vec<(Vec<u8>, Vec<u8>)>>

List all key-value pairs in the specified table.

Source

fn create_table(&self, table: &str) -> Result<()>

Create a table if it doesn’t exist.

Provided Methods§

Source

fn list_keys_with_prefix( &self, table: &str, prefix: &[u8], ) -> Result<Vec<Vec<u8>>>

List only the keys (no values) in the specified table whose bytes start with prefix. The default implementation falls back to a full scan and is suitable for backends without a native prefix iterator.

Source

fn delete_batch(&self, table: &str, keys: &[&[u8]]) -> Result<()>

Delete multiple keys from the specified table in a single atomic operation. The default implementation calls delete per key and is not atomic; backends with transaction support should override this.

Source

fn put_batch(&self, table: &str, entries: &[(&[u8], &[u8])]) -> Result<()>

Store multiple key-value pairs in a single atomic operation.

Source

fn write_atomic(&self, ops: &[AtomicOp<'_>]) -> Result<()>

Apply several put/delete operations across tables. The default implementation applies each op sequentially via put/delete and is NOT atomic; backends with transaction support should override this so a crash mid-batch cannot leave the ops half-applied. Each op must target a DISTINCT table.

Source

fn schema_version(&self) -> Result<u32>

Get the current schema version.

Default implementation returns CURRENT_SCHEMA_VERSION, suitable for in-memory backends that don’t persist schema versions.

Source

fn needs_migration(&self) -> Result<bool>

Check if migrations are needed.

Default implementation returns false, suitable for in-memory backends that always start fresh without persisted data to migrate.

Source

fn run_migrations(&self) -> Result<MigrationResult>

Run pending migrations.

Default implementation is a no-op that reports zero migrations run, suitable for in-memory backends that don’t persist data between sessions.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§