Skip to main content

SqlitePool

Struct SqlitePool 

Source
pub struct SqlitePool { /* private fields */ }
Expand description

Thread-safe SQLite connection wrapper.

Backed by an r2d2 pool of rusqlite::Connections with WAL mode enabled, so concurrent reads can proceed in parallel (writes are still serialized at the SQLite level). Replaces the previous Arc<Mutex<Connection>> shape that funnelled every operation through a single mutex (Wave F, Issue 68).

When an Encryptor is set, content columns are transparently encrypted on write and decrypted on read by the store layers.

Implementations§

Source§

impl SqlitePool

Source

pub fn latest_schema_version() -> i64

The highest schema version this build knows how to apply — the version a freshly migrated database lands on. Used by the open-time reconciliation gate to detect a future (downgrade) schema and by doctor to report binary-vs-disk skew.

Source

pub fn schema_version(&self) -> Result<i64, SqliteError>

Get the current schema version.

Source§

impl SqlitePool

Source

pub fn open(path: &Path) -> Result<Self, SqliteError>

Open a new SQLite database at the given path.

  • Creates the file if it doesn’t exist
  • Enables WAL mode for concurrent reads
  • Enables foreign keys
  • Reconciles the on-disk schema version against this build (refuses a future schema; snapshots before a forward migration)
  • Runs all schema migrations
Source

pub fn open_with( path: &Path, allow_downgrade: bool, ) -> Result<Self, SqliteError>

Like SqlitePool::open, but allow_downgrade suppresses the SqliteError::SchemaTooNew guard so an older binary can be forced onto a newer on-disk schema. The forward-only migration runner still won’t alter the schema downward — this only un-gates the open so a recovery/export path can read what it can.

Source

pub fn open_memory() -> Result<Self, SqliteError>

Open an in-memory database (for testing).

Pool size is forced to 1 — multiple Connection::open_in_memory handles each get a fresh DB, which would break any test that writes from one checkout and reads from another. Single connection preserves the legacy single-connection semantics.

Source

pub fn with_conn<F, T>(&self, f: F) -> Result<T, SqliteError>
where F: FnOnce(&Connection) -> Result<T, SqliteError>,

Execute a closure with a connection borrowed from the pool.

The connection returns to the pool when the closure exits. Multiple concurrent readers can hold distinct connections at the same time; writes still serialize at the SQLite level (single-writer is fundamental to SQLite, WAL or not).

Source

pub fn open_connections(&self) -> u32

Number of connections the r2d2 pool currently holds (idle + checked out), capped by the configured max_size. Backs the open-connections resource gauge; a value pinned at the max for a sustained period is a sign of connection-pressure or a leak.

Source

pub fn with_encryptor(self, enc: Encryptor) -> Self

Attach an encryptor to this pool (builder pattern).

Once set, encrypt_content / decrypt_content are active on all store layers that use this pool.

Source

pub fn is_encrypted(&self) -> bool

Returns true if an encryptor is active.

Source

pub fn encrypt_content(&self, plaintext: &str) -> String

Encrypt a string if encryption is enabled, otherwise return as-is.

Source

pub fn decrypt_content(&self, maybe_ciphertext: &str) -> String

Decrypt a string if encryption is enabled.

Falls back to returning the input unchanged if decryption fails (e.g. legacy plaintext rows written before encryption was enabled).

Source

pub fn try_decrypt_content(&self, maybe_ciphertext: &str) -> Option<String>

Try to decrypt a string, returning None if decryption fails.

Unlike decrypt_content, this does NOT fall back to returning raw ciphertext. Use this at read boundaries to filter out rows that were encrypted with a different key or are corrupted.

Source

pub fn wal_checkpoint(&self) -> Result<(), SqliteError>

Flush the WAL file into the main database file.

Should be called on graceful shutdown to ensure all committed writes are fully persisted and the WAL file is clean. Uses TRUNCATE mode which also resets the WAL to zero size.

Source

pub fn insert_scheduled_intent( &self, description: &str, cron: Option<&str>, namespace: &str, metadata: Option<&str>, ) -> Result<String, SqliteError>

Persist a scheduled intent and return its generated ID.

Source

pub fn list_scheduled_intents( &self, namespace: Option<&str>, ) -> Result<Vec<ScheduledIntent>, SqliteError>

List scheduled intents, optionally filtered by namespace.

Source

pub fn update_scheduled_intent_status( &self, id: &str, status: &str, ) -> Result<bool, SqliteError>

Update a scheduled intent status. Returns true when a row was updated.

Source

pub fn cancel_scheduled_intent(&self, id: &str) -> Result<bool, SqliteError>

Cancel a scheduled intent (set status to “cancelled”).

Source

pub fn due_scheduled_intents(&self) -> Result<Vec<ScheduledIntent>, SqliteError>

Return all scheduled intents with status "scheduled" (i.e. pending execution).

Source

pub fn insert_notification( &self, content: &str, priority: i32, triggered_by: &str, channel: Option<&str>, ) -> Result<String, SqliteError>

Insert a notification into the outbox for later delivery.

Source

pub fn pending_notifications( &self, limit: usize, ) -> Result<Vec<Notification>, SqliteError>

Fetch all pending (undelivered) notifications, ordered by priority then age.

Source

pub fn mark_notification_delivered(&self, id: &str) -> Result<bool, SqliteError>

Mark a notification as delivered (sets delivered_at to now).

Source

pub fn mark_notifications_delivered( &self, ids: &[String], ) -> Result<usize, SqliteError>

Mark multiple notifications as delivered in a single UPDATE. Returns the count of notifications actually marked delivered.

Source

pub fn prune_notifications( &self, max_age_days: u32, ) -> Result<usize, SqliteError>

Prune old delivered notifications and stale undelivered ones.

Source

pub fn export_all_facts(&self) -> Result<Vec<ExportedFact>, SqliteError>

Export all semantic facts ordered by ID.

Source

pub fn export_all_episodes(&self) -> Result<Vec<ExportedEpisode>, SqliteError>

Export all episodes with session info, ordered by timestamp.

Source

pub fn import_facts( &self, facts: &[ExportedFact], ) -> Result<(usize, Vec<usize>), SqliteError>

Import facts (ON CONFLICT DO NOTHING). Returns (imported_count, new_indices).

Source

pub fn import_episodes( &self, episodes: &[ExportedEpisode], ) -> Result<usize, SqliteError>

Import episodes (ON CONFLICT DO NOTHING). Returns count of newly imported episodes.

Source

pub fn table_stats(&self) -> Result<Vec<(String, i64)>, SqliteError>

Get table row counts for status display.

Trait Implementations§

Source§

impl Clone for SqlitePool

Source§

fn clone(&self) -> SqlitePool

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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