Skip to main content

DatabaseWorker

Struct DatabaseWorker 

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

An explicitly asynchronous database queue. Construct the container in open’s factory: its connection, caches, model handles and query observers stay on this worker until close. Only owned Send arguments and results cross threads. Clones share FIFO admission order. Do not return model Store/Elem handles or signals: their cross-thread facilities do not make them independent of this queue’s connection and lifetime. Return model values/IDs.

A container cannot accidentally escape through an owned result:

ⓘ
use day_persistence::{DatabaseWorker, ModelContainer};
fn escape(worker: &DatabaseWorker) {
    let request = worker.read(|db| Ok(db.clone()));
}

Implementations§

Source§

impl DatabaseWorker

Source

pub async fn open( factory: impl FnOnce() -> Result<ModelContainer, DbError> + Send + 'static, ) -> Result<Self, DbError>

Open/migrate on the owning queue, without blocking the caller’s executor. The factory may create non-Send drivers, schema hooks and model state because it runs on the worker.

§Errors

Returns the factory’s error, or an error when the worker thread cannot start.

Source

pub async fn open_with( options: WorkerOptions, factory: impl FnOnce() -> Result<ModelContainer, DbError> + Send + 'static, ) -> Result<Self, DbError>

DatabaseWorker::open, with a queue capacity and thread name.

§Errors

Returns a Busy error for a zero capacity, the factory’s error, or an error when the worker thread cannot start.

Source

pub fn read<R: Send + 'static>( &self, read: impl FnOnce(&ModelContainer) -> Result<R, DbError> + Send + 'static, ) -> WorkerRequest<R> ⓘ

Execute a read on the queue. Query-only mode and dirty-cache checks reject accidental writes; SQL reads and row materialization never run on the caller’s thread.

Source

pub fn write<R: Send + 'static>( &self, write: impl FnOnce(&ModelContainer) -> Result<R, DbError> + Send + 'static, ) -> WorkerRequest<R> ⓘ

Execute one atomic transaction, then acknowledge its durable commit. Intermediate saves and read-your-writes stay within the transaction. A failed operation restores the cache. Accepted writes survive dropping this future. Admission failure means nothing ran.

Source

pub fn write_with_origin<R: Send + 'static>( &self, origin: WriteOrigin, write: impl FnOnce(&ModelContainer) -> Result<R, DbError> + Send + 'static, ) -> WorkerRequest<R> ⓘ

A transactional import or maintenance operation. Remote imports persist normally; participants use the origin to suppress outbound echo, not the database writes.

Source

pub fn write_resetting_undo<R: Send + 'static>( &self, write: impl FnOnce(&ModelContainer) -> Result<R, DbError> + Send + 'static, ) -> WorkerRequest<R> ⓘ

Commit a wholesale replacement and discard obsolete undo/redo history only on success. Both actions run on the owning queue, so no edit can interleave between them. A failed replacement restores the previous history and rolls back the database transaction.

Source

pub fn enable_undo(&self, levels: usize) -> WorkerRequest<()> ⓘ

Install one queue-confined history. Call once before editing. Background imports should use day_model::with_author inside write so they do not enter user undo history.

Source

pub fn undo_status(&self) -> WorkerRequest<(bool, bool)> ⓘ

Owned availability for a UI’s native undo bridge; no reactive handles cross queues.

Source

pub fn undo(&self, redo: bool) -> WorkerRequest<bool> ⓘ

Replay and commit atomically on the owning queue. A failed commit restores the history.

Source

pub async fn observe<T: Clone + PartialEq + Send + 'static>( &self, project: impl Fn(&ModelContainer) -> Result<T, DbError> + Send + 'static, ) -> Result<WorkerSubscription<T>, DbError>

Observe an owned projection after committed writes. The projection must be read-only; it runs on the database queue. Registration participates in FIFO admission, so the first result includes all writes accepted before it. Subsequent results may coalesce revisions.

§Errors

Returns an error when the worker has closed or stopped before the registration ran.

Source

pub fn backup_to(&self, path: impl Into<PathBuf>) -> WorkerRequest<()> ⓘ

Create a consistent SQLite backup after all earlier accepted work. Backup runs outside an explicit transaction because SQLite’s VACUUM INTO requires autocommit mode. The driver supplies the snapshot guarantee. The destination must not already exist.

Source

pub fn checkpoint(&self) -> WorkerRequest<Checkpoint> ⓘ

Checkpoint the write-ahead log after all earlier accepted work, outside any transaction (a checkpoint cannot complete inside one). With checkpoint_fullfsync on, this is the one point where a replication engine forces committed journal entries through the drive cache before it publishes them.

Source

pub fn check_external(&self) -> WorkerRequest<bool> ⓘ

Explicitly merge writes from another connection on this queue, then refresh projections. Applications using one worker as the sole writer do not need polling.

Source

pub fn close_blocking(&self) -> Result<(), DbError>

Last-chance synchronous shutdown for an OS termination callback that cannot await. Normal application code should use close().await. This waits for every accepted operation and may take as long as the longest transaction. Never call it while holding a lock a submitted operation needs. Calling it from this worker is rejected.

§Errors

Returns the error the worker stopped with, or a driver error when called from the worker itself.

Source

pub async fn close(&self) -> Result<(), DbError>

Stop admission for every clone, drain accepted writes, and close on the owning queue. No join or disk flush blocks the caller. Repeated close calls share completion.

§Errors

Returns the error the worker stopped with, or a driver error when called from the worker itself.

Trait Implementations§

Source§

impl Clone for DatabaseWorker

Source§

fn clone(&self) -> Self

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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoReactive<T, StaticMark> for T
where T: Clone + 'static,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.