pub struct StatementCache { /* private fields */ }Expand description
The prepared-statement cache for a SQLite source (05 §4.3). Owns the
connection (Rc<Connection>, mirroring the spec’s Rc<Db> so the cache can
live alongside the per-snapshot write statements and be rebound on a
Snapshotter leapfrog, 05 §5.7) and lends out PooledStmt RAII guards.
The actual statement storage is rusqlite’s per-connection cache, reached via
[Connection::prepare_cached]; this type adds (1) the PooledStmt checkout
handle — the single place to hang the scanStatus capture-on-drop hook (05
§13 Q8, deferred) — and (2) open-cursor accounting (README Primitive #2), so a
test can prove the statement was released even on an early break.
Implementations§
Source§impl StatementCache
impl StatementCache
Sourcepub fn new(conn: Rc<Connection>) -> StatementCache
pub fn new(conn: Rc<Connection>) -> StatementCache
Wrap conn with the default LRU capacity (DEFAULT_CAPACITY).
Sourcepub fn with_capacity(conn: Rc<Connection>, capacity: usize) -> StatementCache
pub fn with_capacity(conn: Rc<Connection>, capacity: usize) -> StatementCache
Wrap conn and set the LRU capacity (number of distinct cached SQL
statements retained). 0 disables caching (every get prepares fresh).
Sourcepub fn set_capacity(&self, capacity: usize)
pub fn set_capacity(&self, capacity: usize)
Re-bound the LRU capacity. Resolves 05 §13 Q7 (the server-side
memory-footprint bound) without the JS external drop(n).
Sourcepub fn flush(&self)
pub fn flush(&self)
Finalize every cached statement (drops the LRU contents). The bounded
analogue of the JS drop(size); e.g. on schema change before a re-prepare.
Sourcepub fn conn(&self) -> &Rc<Connection>
pub fn conn(&self) -> &Rc<Connection>
The wrapped connection — needed by the source to prepare the write
statements (insert/delete/update/checkExists/getExisting, 05 §4.3) that
are held for a snapshot’s lifetime rather than cache-checked-out per fetch.
Sourcepub fn open_cursors(&self) -> i64
pub fn open_cursors(&self) -> i64
Cursors currently checked out (Primitive #2 accounting). 0 when every
PooledStmt has been dropped — i.e. no statement is mid-iteration and the
connection is free for a write.
Sourcepub fn get(&self, sql: &str) -> Result<PooledStmt<'_>>
pub fn get(&self, sql: &str) -> Result<PooledStmt<'_>>
Check out a prepared statement for sql, removed from the cache while in
use (a SQLite statement is not reentrant — 05 §3.10). Returns a
PooledStmt RAII guard that returns the statement to the cache on Drop.
A concurrent get for the same sql while this one is checked out finds
the slot empty and prepares a fresh statement (rusqlite cache.rs:147),
so the two iterate independently — the self-join / reentrant-fetch case.
Fails only if sql is not valid SQL on this connection — a builder bug in
practice, since fetch SQL is emitted by build_select_query (05 §4.4).
Surfaced as a Result rather than hidden (foundations §10).
Sourcepub fn with<T>(
&self,
sql: &str,
f: impl FnOnce(&mut PooledStmt<'_>) -> T,
) -> Result<T>
pub fn with<T>( &self, sql: &str, f: impl FnOnce(&mut PooledStmt<'_>) -> T, ) -> Result<T>
Check out a statement, run f against it, and return it to the cache —
the port of the JS StatementCache.use (statement-cache.ts:103-110) and
the StatementRunner one-liners (zero-cache/db/statements.ts). The
statement is released when f returns (or unwinds), automatically.