pub struct AsyncDb { /* private fields */ }async only.Expand description
Async-facing wrapper around the blocking Db.
AsyncDb is cheap to clone (one Arc bump) so it can be shared
across spawned tasks without locking. The blocking engine sits
behind a single Arc<Db>; every async method hands its body off
to the blocking thread pool.
Public construction goes through AsyncDb::open /
AsyncDb::open_with / AsyncDb::memory /
AsyncDb::memory_with / AsyncDb::open_readonly /
AsyncDb::from_blocking.
Implementations§
Source§impl AsyncDb
impl AsyncDb
Sourcepub fn from_blocking(db: Db) -> Self
pub fn from_blocking(db: Db) -> Self
Construct an AsyncDb from an already-opened blocking Db.
Synchronous on purpose — wrapping an in-hand Db does no
I/O. Useful when the caller already opened the database from
a blocking context (e.g. CLI bootstrap) and wants to drive it
async-style from the rest of the program.
Sourcepub async fn memory_with(config: Config) -> Result<Self>
pub async fn memory_with(config: Config) -> Result<Self>
Sourcepub async fn open_readonly<P: AsRef<Path>>(path: P) -> Result<Self>
pub async fn open_readonly<P: AsRef<Path>>(path: P) -> Result<Self>
Sourcepub fn as_blocking(&self) -> &Db
pub fn as_blocking(&self) -> &Db
Borrow the underlying blocking Db.
Useful when async code needs to hand a &Db to a third-party
API that only takes the blocking type — note the borrow is
scoped to the local task, not the blocking pool.
Sourcepub async fn update<T, F>(&self, id: Id, f: F) -> Result<()>
pub async fn update<T, F>(&self, id: Id, f: F) -> Result<()>
Update the document at id. See Db::update.
The closure runs synchronously inside the blocking task,
so it must be Send + 'static. This mirrors the wider
“closure runs on the blocking pool” contract documented on
AsyncDb::transaction.
§Errors
As Db::update.
Sourcepub async fn find_unique<T, K>(
&self,
index_name: &str,
key: K,
) -> Result<Option<T>>
pub async fn find_unique<T, K>( &self, index_name: &str, key: K, ) -> Result<Option<T>>
Sourcepub async fn all<T>(&self) -> Result<Vec<T>>
pub async fn all<T>(&self) -> Result<Vec<T>>
Materialise the collection’s primary B-tree into Vec<T>. See
Db::all.
Streaming async iteration is intentionally not wrapped in
this phase — the entire collection is collected inside the
blocking task. For very large collections, drive the blocking
Db::iter_all from a dedicated tokio::task::spawn_blocking
(or equivalent) until the async streaming surface lands.
§Errors
As Db::all.
Sourcepub async fn transaction<R, F>(&self, body: F) -> Result<R>
pub async fn transaction<R, F>(&self, body: F) -> Result<R>
Run a closure inside a write transaction. See
Db::transaction.
§Closure contract
The closure runs synchronously inside the blocking task,
so it must be Send + 'static; the return value R likewise.
No async fn inside the closure — that is a deliberate
“async-over-blocking” restriction, matching the contract used
by sqlx and other async-over-sync database wrappers.
§Errors
As Db::transaction.
Sourcepub async fn read_transaction<R, F>(&self, body: F) -> Result<R>
pub async fn read_transaction<R, F>(&self, body: F) -> Result<R>
Run a closure inside a read transaction. See
Db::read_transaction.
Same Send + 'static closure restriction as
AsyncDb::transaction.
§Errors
Sourcepub async fn attach<P>(
&mut self,
path: P,
namespace: impl Into<String>,
) -> Result<()>
pub async fn attach<P>( &mut self, path: P, namespace: impl Into<String>, ) -> Result<()>
Attach a read-only .obj file under namespace. See
Db::attach.
Takes &mut self because the blocking Db::attach takes
&mut Db. We temporarily unwrap the Arc<Db> (it must be
uniquely owned at this point) so the blocking task can take
the &mut. If the Arc is shared, attach falls back to
Error::Busy with the
WriterInProcess kind — clone the AsyncDb only after all
attach / detach calls.
§Errors
As Db::attach, plus
Error::Busy when the Arc<Db> is
not uniquely owned at call time.
Sourcepub async fn detach(&mut self, namespace: &str) -> Result<()>
pub async fn detach(&mut self, namespace: &str) -> Result<()>
Detach the attachment registered under namespace. See
Db::detach.
Same &mut self contract as AsyncDb::attach.
§Errors
As Db::detach.
Sourcepub async fn integrity_check(&self) -> Result<IntegrityReport>
pub async fn integrity_check(&self) -> Result<IntegrityReport>
Sourcepub fn collection<T>(&self, name: impl Into<String>) -> AsyncCollection<T>
pub fn collection<T>(&self, name: impl Into<String>) -> AsyncCollection<T>
Open a read-only typed handle to a runtime-named collection.
See Db::collection.
Construction is infallible; the handle dispatches into the blocking pool on every read-only method call.
Sourcepub fn query<T>(&self) -> AsyncQuery<T>
pub fn query<T>(&self) -> AsyncQuery<T>
Construct a fresh AsyncQuery builder rooted at this
database. See Db::query.