pub struct Storage { /* private fields */ }Expand description
Low-level SQLite storage layer.
Manages the SQLite connection, metadata tables, and per-DynamoDB-table data tables. All SQL lives here — higher layers work with Rust types.
Native-only: this type is the rusqlite-backed backend and is compiled out
of backend-neutral builds (for example wasm-sqlite).
Implementations§
Source§impl Storage
impl Storage
Sourcepub fn with_clock(self, clock: Arc<dyn Clock>) -> Self
pub fn with_clock(self, clock: Arc<dyn Clock>) -> Self
Replace the Clock used by the stream and TTL paths. Returns self
so callers can chain construction (Storage::memory()?.with_clock(c)).
Default for every constructor is SystemClock. Tests use this to
inject a ManualClock.
Sourcepub fn conn(&self) -> &Connection
pub fn conn(&self) -> &Connection
Get a reference to the underlying connection (for transactions, etc.).
Sourcepub fn conn_mut(&mut self) -> &mut Connection
pub fn conn_mut(&mut self) -> &mut Connection
Get a mutable reference to the underlying connection.
Sourcepub fn insert_table_metadata(&self, m: &CreateTableMetadata<'_>) -> Result<()>
pub fn insert_table_metadata(&self, m: &CreateTableMetadata<'_>) -> Result<()>
Insert a row into the _tables metadata table.
Sourcepub fn get_table_metadata(
&self,
table_name: &str,
) -> Result<Option<TableMetadata>>
pub fn get_table_metadata( &self, table_name: &str, ) -> Result<Option<TableMetadata>>
Get metadata for a table. Returns None if the table doesn’t exist.
Results are cached in memory. The cache is invalidated when metadata
is modified via insert_table_metadata, delete_table_metadata,
enable_stream, or update_ttl_config.
Sourcepub fn delete_table_metadata(&self, table_name: &str) -> Result<bool>
pub fn delete_table_metadata(&self, table_name: &str) -> Result<bool>
Delete metadata for a table.
Sourcepub fn update_table_metadata(
&self,
table_name: &str,
attribute_definitions: &str,
gsi_definitions: Option<&str>,
) -> Result<()>
pub fn update_table_metadata( &self, table_name: &str, attribute_definitions: &str, gsi_definitions: Option<&str>, ) -> Result<()>
Update attribute definitions and GSI definitions for a table.
Sourcepub fn update_provisioned_throughput(
&self,
table_name: &str,
provisioned_throughput: &str,
) -> Result<()>
pub fn update_provisioned_throughput( &self, table_name: &str, provisioned_throughput: &str, ) -> Result<()>
Update provisioned throughput for a table.
Sourcepub fn clear_provisioned_throughput(&self, table_name: &str) -> Result<()>
pub fn clear_provisioned_throughput(&self, table_name: &str) -> Result<()>
Clear provisioned throughput for a table (sets to SQL NULL).
Sourcepub fn update_billing_mode(
&self,
table_name: &str,
billing_mode: &str,
) -> Result<()>
pub fn update_billing_mode( &self, table_name: &str, billing_mode: &str, ) -> Result<()>
Update billing mode for a table.
Sourcepub fn update_table_class(
&self,
table_name: &str,
table_class: &str,
) -> Result<()>
pub fn update_table_class( &self, table_name: &str, table_class: &str, ) -> Result<()>
Update the table class for a table.
Sourcepub fn update_on_demand_throughput(
&self,
table_name: &str,
on_demand_throughput: &str,
) -> Result<()>
pub fn update_on_demand_throughput( &self, table_name: &str, on_demand_throughput: &str, ) -> Result<()>
Update the on-demand throughput (stored as JSON) for a table.
Get tags for a table.
Set (merge) tags on a table. New keys overwrite existing keys.
Sourcepub fn update_deletion_protection(
&self,
table_name: &str,
enabled: bool,
) -> Result<()>
pub fn update_deletion_protection( &self, table_name: &str, enabled: bool, ) -> Result<()>
Update the deletion protection setting for a table.
Remove tags by key from a table.
Sourcepub fn list_table_names(&self) -> Result<Vec<String>>
pub fn list_table_names(&self) -> Result<Vec<String>>
List all table names.
Sourcepub fn table_exists(&self, table_name: &str) -> Result<bool>
pub fn table_exists(&self, table_name: &str) -> Result<bool>
Check if a table exists in metadata.
Sourcepub fn create_data_table(&self, table_name: &str) -> Result<()>
pub fn create_data_table(&self, table_name: &str) -> Result<()>
Create a data table for a DynamoDB table.
Sourcepub fn drop_data_table(&self, table_name: &str) -> Result<()>
pub fn drop_data_table(&self, table_name: &str) -> Result<()>
Drop a data table.
Sourcepub fn create_gsi_table(&self, table_name: &str, index_name: &str) -> Result<()>
pub fn create_gsi_table(&self, table_name: &str, index_name: &str) -> Result<()>
Create a GSI table.
Sourcepub fn drop_gsi_table(&self, table_name: &str, index_name: &str) -> Result<()>
pub fn drop_gsi_table(&self, table_name: &str, index_name: &str) -> Result<()>
Drop a GSI table.
Sourcepub fn insert_gsi_item(
&self,
table_name: &str,
index_name: &str,
gsi_pk: &str,
gsi_sk: &str,
table_pk: &str,
table_sk: &str,
item_json: &str,
) -> Result<()>
pub fn insert_gsi_item( &self, table_name: &str, index_name: &str, gsi_pk: &str, gsi_sk: &str, table_pk: &str, table_sk: &str, item_json: &str, ) -> Result<()>
Insert an item into a GSI table.
Sourcepub fn insert_gsi_items(
&self,
table_name: &str,
index_name: &str,
rows: &[GsiItemRow],
) -> Result<()>
pub fn insert_gsi_items( &self, table_name: &str, index_name: &str, rows: &[GsiItemRow], ) -> Result<()>
Bulk-insert many rows into one GSI table using a single cached
prepared statement. Equivalent to calling Self::insert_gsi_item
once per row, but reuses the statement across the batch.
Sourcepub fn delete_gsi_item(
&self,
table_name: &str,
index_name: &str,
table_pk: &str,
table_sk: &str,
) -> Result<()>
pub fn delete_gsi_item( &self, table_name: &str, index_name: &str, table_pk: &str, table_sk: &str, ) -> Result<()>
Delete an item from a GSI table by base table primary key.
Sourcepub fn query_gsi_items(
&self,
table_name: &str,
index_name: &str,
gsi_pk: &str,
params: &QueryParams<'_>,
) -> Result<Vec<(String, String, String)>>
pub fn query_gsi_items( &self, table_name: &str, index_name: &str, gsi_pk: &str, params: &QueryParams<'_>, ) -> Result<Vec<(String, String, String)>>
Query items from a GSI table.
Sourcepub fn scan_gsi_items(
&self,
table_name: &str,
index_name: &str,
params: &ScanParams<'_>,
) -> Result<Vec<(String, String, String)>>
pub fn scan_gsi_items( &self, table_name: &str, index_name: &str, params: &ScanParams<'_>, ) -> Result<Vec<(String, String, String)>>
Scan all items from a GSI table.
Sourcepub fn create_lsi_table(&self, table_name: &str, index_name: &str) -> Result<()>
pub fn create_lsi_table(&self, table_name: &str, index_name: &str) -> Result<()>
Create an LSI table for a given base table and index name.
Sourcepub fn drop_lsi_table(&self, table_name: &str, index_name: &str) -> Result<()>
pub fn drop_lsi_table(&self, table_name: &str, index_name: &str) -> Result<()>
Drop an LSI table.
Sourcepub fn insert_lsi_item(
&self,
table_name: &str,
index_name: &str,
pk: &str,
sk: &str,
base_pk: &str,
base_sk: &str,
item_json: &str,
) -> Result<()>
pub fn insert_lsi_item( &self, table_name: &str, index_name: &str, pk: &str, sk: &str, base_pk: &str, base_sk: &str, item_json: &str, ) -> Result<()>
Insert an item into an LSI table.
Sourcepub fn delete_lsi_item(
&self,
table_name: &str,
index_name: &str,
base_pk: &str,
base_sk: &str,
) -> Result<()>
pub fn delete_lsi_item( &self, table_name: &str, index_name: &str, base_pk: &str, base_sk: &str, ) -> Result<()>
Delete an item from an LSI table by base table primary key.
Sourcepub fn query_lsi_items(
&self,
table_name: &str,
index_name: &str,
pk: &str,
params: &QueryParams<'_>,
) -> Result<Vec<(String, String, String)>>
pub fn query_lsi_items( &self, table_name: &str, index_name: &str, pk: &str, params: &QueryParams<'_>, ) -> Result<Vec<(String, String, String)>>
Query items from an LSI table.
Sourcepub fn scan_lsi_items(
&self,
table_name: &str,
index_name: &str,
params: &ScanParams<'_>,
) -> Result<Vec<(String, String, String)>>
pub fn scan_lsi_items( &self, table_name: &str, index_name: &str, params: &ScanParams<'_>, ) -> Result<Vec<(String, String, String)>>
Scan all items from an LSI table.
Sourcepub fn begin_transaction(&self) -> Result<()>
pub fn begin_transaction(&self) -> Result<()>
Begin an immediate SQLite transaction.
Sourcepub fn enable_bulk_loading(&self) -> Result<()>
pub fn enable_bulk_loading(&self) -> Result<()>
Set aggressive PRAGMAs for bulk loading.
Disables fsync, increases cache, and enables memory-mapped I/O. Only safe when data loss on crash is acceptable (e.g., fresh import that can be re-run).
Sourcepub fn disable_bulk_loading(&self) -> Result<()>
pub fn disable_bulk_loading(&self) -> Result<()>
Restore normal PRAGMAs after bulk loading.
Sourcepub fn put_item(
&self,
table_name: &str,
pk: &str,
sk: &str,
item_json: &str,
item_size: usize,
) -> Result<Option<String>>
pub fn put_item( &self, table_name: &str, pk: &str, sk: &str, item_json: &str, item_size: usize, ) -> Result<Option<String>>
Insert or replace an item.
Sourcepub fn put_item_with_hash(
&self,
table_name: &str,
pk: &str,
sk: &str,
item_json: &str,
item_size: usize,
hash_prefix: &str,
) -> Result<Option<String>>
pub fn put_item_with_hash( &self, table_name: &str, pk: &str, sk: &str, item_json: &str, item_size: usize, hash_prefix: &str, ) -> Result<Option<String>>
Put an item with an explicit hash prefix for parallel scan ordering.
Sourcepub fn put_base_items(
&self,
table_name: &str,
rows: &[BaseItemRow],
) -> Result<()>
pub fn put_base_items( &self, table_name: &str, rows: &[BaseItemRow], ) -> Result<()>
Bulk-insert many base-table rows using a single cached prepared
statement (INSERT OR REPLACE). Unlike Self::put_item_with_hash,
which preserves any existing cached_at, this writes cached_at
verbatim from each row, matching the import flow’s semantics.
Sourcepub fn get_item(
&self,
table_name: &str,
pk: &str,
sk: &str,
) -> Result<Option<String>>
pub fn get_item( &self, table_name: &str, pk: &str, sk: &str, ) -> Result<Option<String>>
Get a single item by primary key.
Sourcepub fn get_partition_size(&self, table_name: &str, pk: &str) -> Result<i64>
pub fn get_partition_size(&self, table_name: &str, pk: &str) -> Result<i64>
Return the total item_size for all items sharing the given partition key.
Sourcepub fn get_lsi_partition_size(
&self,
table_name: &str,
index_name: &str,
pk: &str,
) -> Result<i64>
pub fn get_lsi_partition_size( &self, table_name: &str, index_name: &str, pk: &str, ) -> Result<i64>
Return the total size of LSI items for a given partition key. LSI items are stored as JSON text, so we use length(item_json).
Sourcepub fn delete_item(
&self,
table_name: &str,
pk: &str,
sk: &str,
) -> Result<Option<String>>
pub fn delete_item( &self, table_name: &str, pk: &str, sk: &str, ) -> Result<Option<String>>
Delete an item by primary key. Returns the old item_json if it existed.
Sourcepub fn query_items(
&self,
table_name: &str,
pk: &str,
params: &QueryParams<'_>,
) -> Result<Vec<(String, String, String)>>
pub fn query_items( &self, table_name: &str, pk: &str, params: &QueryParams<'_>, ) -> Result<Vec<(String, String, String)>>
Query items by partition key with optional sort key condition.
sk_condition is a SQL fragment like AND sk > ? with sk_params providing values.
Returns (items, has_more) where items is a vec of (pk, sk, item_json).
Sourcepub fn scan_items(
&self,
table_name: &str,
params: &ScanParams<'_>,
) -> Result<Vec<(String, String, String)>>
pub fn scan_items( &self, table_name: &str, params: &ScanParams<'_>, ) -> Result<Vec<(String, String, String)>>
Scan items from a table with pagination.
Returns (pk, sk, item_json) tuples ordered by hash_prefix for
dynalite-compatible parallel scan behaviour.
Sourcepub fn count_items(&self, table_name: &str) -> Result<i64>
pub fn count_items(&self, table_name: &str) -> Result<i64>
Count items in a table.
Sourcepub fn db_path(&self) -> Option<String>
pub fn db_path(&self) -> Option<String>
Get the database file path, or None for in-memory databases.
Sourcepub fn db_size_bytes(&self) -> Result<u64>
pub fn db_size_bytes(&self) -> Result<u64>
Get the total database size in bytes.
Sourcepub fn table_count(&self) -> Result<usize>
pub fn table_count(&self) -> Result<usize>
Count the number of DynamoDB tables.
Sourcepub fn table_stats(&self) -> Result<Vec<TableStats>>
pub fn table_stats(&self) -> Result<Vec<TableStats>>
Get per-table statistics: name, item count, and approximate size in bytes.
Uses a single query per table (COUNT + SUM combined) instead of separate queries.
Sourcepub fn database_info(&self) -> Result<DatabaseInfo>
pub fn database_info(&self) -> Result<DatabaseInfo>
Get combined database info in a single call for atomic consistency.
Returns all introspection data that get_database_info tools need
without releasing the lock between queries.
Sourcepub fn vacuum_into(&self, path: &str) -> Result<()>
pub fn vacuum_into(&self, path: &str) -> Result<()>
Create a snapshot of the database by copying it to the given path.
Uses VACUUM INTO which works for both in-memory and file-backed databases.
Sourcepub fn restore_from(&mut self, path: &str) -> Result<()>
pub fn restore_from(&mut self, path: &str) -> Result<()>
Restore the database from a snapshot file using SQLite’s backup API. This replaces the current database contents with the snapshot contents. Works for both in-memory and file-backed databases.
Sourcepub fn backup_to_memory(&self) -> Result<Connection>
pub fn backup_to_memory(&self) -> Result<Connection>
Backup the current database to a new in-memory SQLite connection.
Used for in-memory snapshot storage — the returned connection holds a complete copy of the database without touching the filesystem.
Sourcepub fn restore_from_connection(&mut self, source: &Connection) -> Result<()>
pub fn restore_from_connection(&mut self, source: &Connection) -> Result<()>
Restore the database from another SQLite connection using the backup API.
Replaces the current database contents with the source connection’s contents. Invalidates the metadata cache.
Sourcepub fn connection_size_bytes(conn: &Connection) -> Result<u64>
pub fn connection_size_bytes(conn: &Connection) -> Result<u64>
Get the database size in bytes for an arbitrary connection.
Sourcepub fn enable_stream(
&self,
table_name: &str,
view_type: &str,
label: &str,
) -> Result<()>
pub fn enable_stream( &self, table_name: &str, view_type: &str, label: &str, ) -> Result<()>
Enable streams on a table.
Sourcepub fn disable_stream(&self, table_name: &str) -> Result<()>
pub fn disable_stream(&self, table_name: &str) -> Result<()>
Disable streams on a table.
Sourcepub fn insert_stream_record(
&self,
table_name: &str,
event_name: &str,
keys_json: &str,
new_image: Option<&str>,
old_image: Option<&str>,
sequence_number: &str,
shard_id: &str,
created_at: i64,
) -> Result<()>
pub fn insert_stream_record( &self, table_name: &str, event_name: &str, keys_json: &str, new_image: Option<&str>, old_image: Option<&str>, sequence_number: &str, shard_id: &str, created_at: i64, ) -> Result<()>
Insert a stream record.
Sourcepub fn insert_stream_record_with_identity(
&self,
table_name: &str,
event_name: &str,
keys_json: &str,
new_image: Option<&str>,
old_image: Option<&str>,
sequence_number: &str,
shard_id: &str,
created_at: i64,
user_identity: Option<&str>,
) -> Result<()>
pub fn insert_stream_record_with_identity( &self, table_name: &str, event_name: &str, keys_json: &str, new_image: Option<&str>, old_image: Option<&str>, sequence_number: &str, shard_id: &str, created_at: i64, user_identity: Option<&str>, ) -> Result<()>
Insert a stream record with optional user identity (for TTL deletions).
Sourcepub fn next_stream_sequence_number(&self, table_name: &str) -> Result<i64>
pub fn next_stream_sequence_number(&self, table_name: &str) -> Result<i64>
Get the next sequence number for a table’s stream.
Sourcepub fn get_stream_records(
&self,
table_name: &str,
shard_id: &str,
after_sequence: i64,
limit: usize,
) -> Result<Vec<StreamRecord>>
pub fn get_stream_records( &self, table_name: &str, shard_id: &str, after_sequence: i64, limit: usize, ) -> Result<Vec<StreamRecord>>
Get stream records for a shard starting after a given sequence number.
Sourcepub fn list_stream_enabled_tables(&self) -> Result<Vec<TableMetadata>>
pub fn list_stream_enabled_tables(&self) -> Result<Vec<TableMetadata>>
List tables that have streams enabled.
Sourcepub fn update_ttl_config(
&self,
table_name: &str,
attribute_name: Option<&str>,
enabled: bool,
) -> Result<()>
pub fn update_ttl_config( &self, table_name: &str, attribute_name: Option<&str>, enabled: bool, ) -> Result<()>
Update TTL configuration for a table.
Sourcepub fn list_ttl_enabled_tables(&self) -> Result<Vec<TableMetadata>>
pub fn list_ttl_enabled_tables(&self) -> Result<Vec<TableMetadata>>
List tables that have TTL enabled.
Sourcepub fn get_shard_sequence_range(
&self,
table_name: &str,
shard_id: &str,
) -> Result<(Option<String>, Option<String>)>
pub fn get_shard_sequence_range( &self, table_name: &str, shard_id: &str, ) -> Result<(Option<String>, Option<String>)>
Get the min and max sequence numbers for a shard.
Trait Implementations§
Source§impl StorageBackend for Storage
impl StorageBackend for Storage
async fn insert_table_metadata( &self, m: &CreateTableMetadata<'_>, ) -> Result<(), BackendError>
async fn get_table_metadata( &self, table_name: &str, ) -> Result<Option<TableMetadata>, BackendError>
async fn delete_table_metadata( &self, table_name: &str, ) -> Result<bool, BackendError>
async fn update_table_metadata( &self, table_name: &str, attribute_definitions: &str, gsi_definitions: Option<&str>, ) -> Result<(), BackendError>
async fn update_provisioned_throughput( &self, table_name: &str, provisioned_throughput: &str, ) -> Result<(), BackendError>
async fn clear_provisioned_throughput( &self, table_name: &str, ) -> Result<(), BackendError>
async fn update_billing_mode( &self, table_name: &str, billing_mode: &str, ) -> Result<(), BackendError>
async fn update_table_class( &self, table_name: &str, table_class: &str, ) -> Result<(), BackendError>
async fn update_on_demand_throughput( &self, table_name: &str, on_demand_throughput: &str, ) -> Result<(), BackendError>
async fn update_deletion_protection( &self, table_name: &str, enabled: bool, ) -> Result<(), BackendError>
async fn list_table_names(&self) -> Result<Vec<String>, BackendError>
async fn table_exists(&self, table_name: &str) -> Result<bool, BackendError>
async fn create_data_table(&self, table_name: &str) -> Result<(), BackendError>
async fn drop_data_table(&self, table_name: &str) -> Result<(), BackendError>
async fn create_gsi_table( &self, table_name: &str, index_name: &str, ) -> Result<(), BackendError>
async fn drop_gsi_table( &self, table_name: &str, index_name: &str, ) -> Result<(), BackendError>
async fn create_lsi_table( &self, table_name: &str, index_name: &str, ) -> Result<(), BackendError>
async fn drop_lsi_table( &self, table_name: &str, index_name: &str, ) -> Result<(), BackendError>
async fn insert_gsi_item( &self, table_name: &str, index_name: &str, gsi_pk: &str, gsi_sk: &str, table_pk: &str, table_sk: &str, item_json: &str, ) -> Result<(), BackendError>
Source§async fn insert_gsi_items(
&self,
table_name: &str,
index_name: &str,
rows: &[GsiItemRow],
) -> Result<(), BackendError>
async fn insert_gsi_items( &self, table_name: &str, index_name: &str, rows: &[GsiItemRow], ) -> Result<(), BackendError>
async fn delete_gsi_item( &self, table_name: &str, index_name: &str, table_pk: &str, table_sk: &str, ) -> Result<(), BackendError>
async fn query_gsi_items( &self, table_name: &str, index_name: &str, gsi_pk: &str, params: &QueryParams<'_>, ) -> Result<Vec<(String, String, String)>, BackendError>
async fn scan_gsi_items( &self, table_name: &str, index_name: &str, params: &ScanParams<'_>, ) -> Result<Vec<(String, String, String)>, BackendError>
async fn insert_lsi_item( &self, table_name: &str, index_name: &str, pk: &str, sk: &str, base_pk: &str, base_sk: &str, item_json: &str, ) -> Result<(), BackendError>
async fn delete_lsi_item( &self, table_name: &str, index_name: &str, base_pk: &str, base_sk: &str, ) -> Result<(), BackendError>
async fn query_lsi_items( &self, table_name: &str, index_name: &str, pk: &str, params: &QueryParams<'_>, ) -> Result<Vec<(String, String, String)>, BackendError>
async fn scan_lsi_items( &self, table_name: &str, index_name: &str, params: &ScanParams<'_>, ) -> Result<Vec<(String, String, String)>, BackendError>
async fn begin_transaction(&self) -> Result<(), BackendError>
async fn commit(&self) -> Result<(), BackendError>
async fn rollback(&self) -> Result<(), BackendError>
async fn enable_bulk_loading(&self) -> Result<(), BackendError>
async fn disable_bulk_loading(&self) -> Result<(), BackendError>
async fn put_item( &self, table_name: &str, pk: &str, sk: &str, item_json: &str, item_size: usize, ) -> Result<Option<String>, BackendError>
async fn put_item_with_hash( &self, table_name: &str, pk: &str, sk: &str, item_json: &str, item_size: usize, hash_prefix: &str, ) -> Result<Option<String>, BackendError>
Source§async fn put_base_items(
&self,
table_name: &str,
rows: &[BaseItemRow],
) -> Result<(), BackendError>
async fn put_base_items( &self, table_name: &str, rows: &[BaseItemRow], ) -> Result<(), BackendError>
INSERT OR REPLACE). Read more