SqlStore

Struct SqlStore 

Source
pub struct SqlStore { /* private fields */ }

Implementations§

Source§

impl SqlStore

Source

pub async fn new(connection_string: &str) -> Result<Self, StorageError>

Create a new SQL store with startup-mode retry (fails fast if config is wrong).

Source

pub async fn with_registry( connection_string: &str, registry: Arc<SchemaRegistry>, ) -> Result<Self, StorageError>

Create a new SQL store with a shared schema registry.

Use this when you want the SyncEngine to share the registry for dynamic schema registration.

Source

pub fn pool(&self) -> AnyPool

Get a clone of the connection pool for sharing with other stores.

Source

pub fn registry(&self) -> &Arc<SchemaRegistry>

Get the schema registry for this store.

Source

pub async fn ensure_table(&self, table_name: &str) -> Result<(), StorageError>

Create a schema-specific table with the same structure as sync_items.

Used for horizontal partitioning: each schema (e.g., “users”, “orders”) gets its own table with identical DDL. This enables:

  • Better query performance (smaller tables, focused indices)
  • Easier maintenance (vacuum, backup per-schema)
  • Future sharding potential

For SQLite, this is a no-op (returns Ok) since partitioning doesn’t help.

§Arguments
  • table_name - Name of the table to create (e.g., “users_items”)
§Example
store.ensure_table("users_items").await.unwrap();
store.ensure_table("orders_items").await.unwrap();
Source

pub async fn table_exists(&self, table_name: &str) -> Result<bool, StorageError>

Check if a table exists.

Source

pub fn is_sqlite(&self) -> bool

Check if this is a SQLite database.

Source§

impl SqlStore

Source

pub async fn search_in_table( &self, table: &str, where_clause: &str, params: &[SqlParam], limit: usize, ) -> Result<Vec<SyncItem>, StorageError>

Search in a specific table using a WHERE clause.

Source

pub async fn count_where_in_table( &self, table: &str, where_clause: &str, params: &[SqlParam], ) -> Result<u64, StorageError>

Count items in a specific table matching a WHERE clause.

Source

pub async fn scan_batch( &self, limit: usize, ) -> Result<Vec<SyncItem>, StorageError>

Scan a batch of items (for WAL drain).

Source

pub async fn delete_batch(&self, ids: &[String]) -> Result<usize, StorageError>

Delete multiple items by ID in a single query.

Source

pub async fn get_dirty_merkle_ids( &self, limit: usize, ) -> Result<Vec<String>, StorageError>

Get IDs of items with merkle_dirty = 1 (need merkle recalculation).

Used by background merkle processor to batch recalculate affected trees.

Source

pub async fn count_dirty_merkle(&self) -> Result<u64, StorageError>

Count items with merkle_dirty = 1.

Source

pub async fn mark_merkle_clean( &self, ids: &[String], ) -> Result<usize, StorageError>

Mark items as merkle-clean after recalculation.

Updates all schema-partitioned tables based on the item ID’s routing.

Source

pub async fn has_dirty_merkle(&self) -> Result<bool, StorageError>

Check if there are any dirty merkle items.

Source

pub async fn branch_dirty_count( &self, prefix: &str, ) -> Result<u64, StorageError>

Count dirty merkle items within a specific branch prefix.

Used for branch-level hygiene checks. Returns 0 if the branch is “clean” (all merkle hashes up-to-date), allowing safe comparison with peers.

§Arguments
  • prefix - Branch prefix (e.g., “uk.nhs” matches “uk.nhs.patient.123”)
Source

pub async fn get_dirty_prefixes(&self) -> Result<Vec<String>, StorageError>

Get distinct top-level prefixes that have dirty items.

Returns prefixes like [“uk”, “us”, “de”] that have pending merkle recalcs. Branches NOT in this list are “clean” and safe to compare with peers.

Source

pub async fn get_dirty_merkle_items( &self, limit: usize, ) -> Result<Vec<SyncItem>, StorageError>

Get full SyncItems with merkle_dirty = 1 (need merkle recalculation).

Queries all schema-partitioned tables plus the default table. Returns the items themselves so merkle can be calculated. Use mark_merkle_clean() after processing to clear the flag.

Source

pub async fn get_by_state( &self, state: &str, limit: usize, ) -> Result<Vec<SyncItem>, StorageError>

Get items by state (e.g., “delta”, “base”, “pending”).

Uses indexed query for fast retrieval.

Source

pub async fn count_by_state(&self, state: &str) -> Result<u64, StorageError>

Count items in a given state.

Source

pub async fn list_state_ids( &self, state: &str, limit: usize, ) -> Result<Vec<String>, StorageError>

Get just the IDs of items in a given state (lightweight query).

Source

pub async fn set_state( &self, id: &str, new_state: &str, ) -> Result<bool, StorageError>

Update the state of an item by ID.

Source

pub async fn delete_by_state(&self, state: &str) -> Result<u64, StorageError>

Delete all items in a given state.

Returns the number of deleted items.

Source

pub async fn scan_prefix( &self, prefix: &str, limit: usize, ) -> Result<Vec<SyncItem>, StorageError>

Scan items by ID prefix.

Efficiently retrieves all items whose ID starts with the given prefix. Uses SQL LIKE 'prefix%' which leverages the primary key index.

§Example
// Get all deltas for object user.123
let deltas = store.scan_prefix("delta:user.123:", 1000).await?;
Source

pub async fn count_prefix(&self, prefix: &str) -> Result<u64, StorageError>

Count items matching an ID prefix.

Source

pub async fn delete_prefix(&self, prefix: &str) -> Result<u64, StorageError>

Delete all items matching an ID prefix.

Returns the number of deleted items.

Trait Implementations§

Source§

impl ArchiveStore for SqlStore

Source§

fn put_batch<'life0, 'life1, 'async_trait>( &'life0 self, items: &'life1 mut [SyncItem], ) -> Pin<Box<dyn Future<Output = Result<BatchWriteResult, StorageError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Write a batch of items in a single multi-row INSERT with verification.

Items are grouped by target table (based on schema registry) and written in separate batches per table.

Source§

fn get<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<SyncItem>, StorageError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Source§

fn put<'life0, 'life1, 'async_trait>( &'life0 self, item: &'life1 SyncItem, ) -> Pin<Box<dyn Future<Output = Result<(), StorageError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Source§

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), StorageError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Source§

fn exists<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<bool, StorageError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Check if an item exists (SQL EXISTS query - fast, no data transfer).
Source§

fn scan_keys<'life0, 'async_trait>( &'life0 self, offset: u64, limit: usize, ) -> Pin<Box<dyn Future<Output = Result<Vec<String>, StorageError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Scan keys for cuckoo filter warmup (paginated). Returns empty vec when offset exceeds total count.
Source§

fn count_all<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<u64, StorageError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Count total items in store.
Source§

fn search<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, where_clause: &'life1 str, params: &'life2 [SqlParam], limit: usize, ) -> Pin<Box<dyn Future<Output = Result<Vec<SyncItem>, StorageError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Search using SQL WHERE clause with JSON_EXTRACT. Returns matching items.
Source§

fn count_where<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, where_clause: &'life1 str, params: &'life2 [SqlParam], ) -> Pin<Box<dyn Future<Output = Result<u64, StorageError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Count items matching a WHERE clause (fast COUNT(*) query). Use for exhaustiveness checks without fetching data.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more