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
impl DatabaseWorker
Sourcepub async fn open(
factory: impl FnOnce() -> Result<ModelContainer, DbError> + Send + 'static,
) -> Result<Self, DbError>
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.
Sourcepub async fn open_with(
options: WorkerOptions,
factory: impl FnOnce() -> Result<ModelContainer, DbError> + Send + 'static,
) -> Result<Self, DbError>
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.
Sourcepub fn read<R: Send + 'static>(
&self,
read: impl FnOnce(&ModelContainer) -> Result<R, DbError> + Send + 'static,
) -> WorkerRequest<R> ⓘ
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.
Sourcepub fn write<R: Send + 'static>(
&self,
write: impl FnOnce(&ModelContainer) -> Result<R, DbError> + Send + 'static,
) -> WorkerRequest<R> ⓘ
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.
Sourcepub fn write_with_origin<R: Send + 'static>(
&self,
origin: WriteOrigin,
write: impl FnOnce(&ModelContainer) -> Result<R, DbError> + Send + 'static,
) -> WorkerRequest<R> ⓘ
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.
Sourcepub fn write_resetting_undo<R: Send + 'static>(
&self,
write: impl FnOnce(&ModelContainer) -> Result<R, DbError> + Send + 'static,
) -> WorkerRequest<R> ⓘ
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.
Sourcepub fn enable_undo(&self, levels: usize) -> WorkerRequest<()> ⓘ
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.
Sourcepub fn undo_status(&self) -> WorkerRequest<(bool, bool)> ⓘ
pub fn undo_status(&self) -> WorkerRequest<(bool, bool)> ⓘ
Owned availability for a UI’s native undo bridge; no reactive handles cross queues.
Sourcepub fn undo(&self, redo: bool) -> WorkerRequest<bool> ⓘ
pub fn undo(&self, redo: bool) -> WorkerRequest<bool> ⓘ
Replay and commit atomically on the owning queue. A failed commit restores the history.
Sourcepub async fn observe<T: Clone + PartialEq + Send + 'static>(
&self,
project: impl Fn(&ModelContainer) -> Result<T, DbError> + Send + 'static,
) -> Result<WorkerSubscription<T>, DbError>
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.
Sourcepub fn backup_to(&self, path: impl Into<PathBuf>) -> WorkerRequest<()> ⓘ
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.
Sourcepub fn checkpoint(&self) -> WorkerRequest<Checkpoint> ⓘ
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.
Sourcepub fn check_external(&self) -> WorkerRequest<bool> ⓘ
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.
Sourcepub fn close_blocking(&self) -> Result<(), DbError>
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.
Sourcepub async fn close(&self) -> Result<(), DbError>
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§
Auto Trait Implementations§
impl Forget for DatabaseWorker
impl Freeze for DatabaseWorker
impl Move for DatabaseWorker
impl RefUnwindSafe for DatabaseWorker
impl Send for DatabaseWorker
impl Sync for DatabaseWorker
impl Unpin for DatabaseWorker
impl UnsafeUnpin for DatabaseWorker
impl UnwindSafe for DatabaseWorker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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