pub struct Database {
pub db_name: String,
pub tables: HashMap<String, Table>,
pub source_path: Option<PathBuf>,
pub pager: Option<Pager>,
pub txn: Option<TxnSnapshot>,
pub auto_vacuum_threshold: Option<f32>,
pub journal_mode: JournalMode,
pub mvcc_clock: Arc<MvccClock>,
pub mv_store: MvStore,
}Expand description
The database is represented by this structure.assert_eq!
Fields§
§db_name: StringName of this database. (schema name, not filename)
tables: HashMap<String, Table>HashMap of tables in this database
source_path: Option<PathBuf>If Some, every committing SQL statement auto-flushes the DB to
this path. None → transient in-memory mode (the default; the
REPL only enters persistent mode after .open FILE).
pager: Option<Pager>Long-lived pager attached when the database is file-backed. Keeps
an in-memory snapshot of every page so auto-saves can diff
against the last-committed state and skip rewriting unchanged
pages. None means “in-memory only” or “not yet opened”.
txn: Option<TxnSnapshot>Active transaction state (Phase 4f). Some between BEGIN and
the matching COMMIT / ROLLBACK. While set:
- auto-save is suppressed (mutations stay in-memory)
- nested
BEGINis rejected ROLLBACKrestorestablesfrom the snapshot
auto_vacuum_threshold: Option<f32>Auto-VACUUM trigger (SQLR-10). After a page-releasing DDL
(DROP TABLE / DROP INDEX / ALTER TABLE DROP COLUMN) commits and
flushes, if the freelist exceeds this fraction of page_count
the engine quietly compacts the file. None disables the
trigger; defaults to Some(DEFAULT_AUTO_VACUUM_THRESHOLD)
(SQLite parity at 25%). Per-connection runtime state — not
persisted across reopens.
journal_mode: JournalModePhase 11.3 — current journal mode for the database. Default
is JournalMode::Wal (every pre-Phase-11 caller). Toggled
by PRAGMA journal_mode = mvcc | wal. The setting is
per-database (every Connection to this Database observes
the same value) — see the open question in
docs/concurrent-writes-plan.md
§8 for the per-connection vs. per-database trade-off; v0
picked per-database for simplicity.
mvcc_clock: Arc<MvccClock>Phase 11.3 — process-wide MVCC clock. Shared between every
Connection to this Database (and 11.4’s MvStore).
Seeded from the WAL header’s clock_high_water at open
time so timestamps don’t repeat across reopens. Allocated
here even in JournalMode::Wal so PRAGMA journal_mode = mvcc doesn’t require lazy-creating the clock.
mv_store: MvStorePhase 11.3 — in-memory version index. Allocated on every
Database::new so the toggle to MVCC mode doesn’t require
a re-init step. Empty until 11.4 wires the commit path to
publish row versions; reads still go through the legacy
path until then.
Implementations§
Source§impl Database
impl Database
Sourcepub fn new(db_name: String) -> Self
pub fn new(db_name: String) -> Self
Creates an empty in-memory Database.
§Examples
use sqlrite::Database;
let mut db = Database::new("my_db".to_string());Sourcepub fn journal_mode(&self) -> JournalMode
pub fn journal_mode(&self) -> JournalMode
Phase 11.3 — current journal mode. Toggled by PRAGMA journal_mode = mvcc | wal. Wal (the default) keeps every
pre-Phase-11 caller’s behaviour; Mvcc opts the database
into MVCC + BEGIN CONCURRENT (Phase 11.4 wires this end-to-
end; today the toggle is observable but the read/write
paths don’t change).
Sourcepub fn set_journal_mode(&mut self, mode: JournalMode) -> Result<()>
pub fn set_journal_mode(&mut self, mode: JournalMode) -> Result<()>
Phase 11.3 — switch the database’s journal mode. Wal → Mvcc
is unconditional in v0 (no in-flight transactions to drain
because nothing publishes versions yet). Mvcc → Wal is
rejected if mv_store carries any committed versions —
switching back would silently strand them. v0 keeps this
strict; the loosening (and the discard-versions path) lands
when 11.4 starts populating the store.
Sourcepub fn mvcc_clock(&self) -> &Arc<MvccClock>
pub fn mvcc_clock(&self) -> &Arc<MvccClock>
Phase 11.3 — the shared MVCC logical clock. Returned by
reference (not cloned) because callers typically just read
now() / tick() against the same Arc Database already
holds.
Sourcepub fn mv_store(&self) -> &MvStore
pub fn mv_store(&self) -> &MvStore
Phase 11.3 — the in-memory version index. Read-only access
is enough for 11.3’s tests; 11.4 grows the commit-path
helpers into typed methods on Database rather than mutating
this directly.
Sourcepub fn auto_vacuum_threshold(&self) -> Option<f32>
pub fn auto_vacuum_threshold(&self) -> Option<f32>
Returns the current auto-VACUUM threshold, or None if disabled.
See Database::set_auto_vacuum_threshold for semantics.
Sourcepub fn set_auto_vacuum_threshold(
&mut self,
threshold: Option<f32>,
) -> Result<()>
pub fn set_auto_vacuum_threshold( &mut self, threshold: Option<f32>, ) -> Result<()>
Sets the auto-VACUUM threshold (SQLR-10). Some(t) with t in
0.0..=1.0 arms the trigger: after a page-releasing DDL
commits, if the freelist exceeds t * page_count the engine
runs a full-file compact. None disables the trigger. Values
outside 0.0..=1.0 (or NaN / infinite) return a typed error
rather than silently saturating.
Sourcepub fn contains_table(&self, table_name: String) -> bool
pub fn contains_table(&self, table_name: String) -> bool
Returns true if the database contains a table with the specified key as a table name.
Sourcepub fn get_table(&self, table_name: String) -> Result<&Table>
pub fn get_table(&self, table_name: String) -> Result<&Table>
Returns an immutable reference of sql::db::table::Table if the database contains a
table with the specified key as a table name.
Sourcepub fn get_table_mut(&mut self, table_name: String) -> Result<&mut Table>
pub fn get_table_mut(&mut self, table_name: String) -> Result<&mut Table>
Returns an mutable reference of sql::db::table::Table if the database contains a
table with the specified key as a table name.
Sourcepub fn is_read_only(&self) -> bool
pub fn is_read_only(&self) -> bool
Returns true if this database is attached to a file and that
file was opened in AccessMode::ReadOnly. In-memory databases
(no pager) and read-write file-backed databases both return
false. Callers use this to reject mutating SQL at the
dispatcher level so the in-memory tables don’t drift away from
disk on a would-be INSERT / UPDATE / DELETE.
Sourcepub fn in_transaction(&self) -> bool
pub fn in_transaction(&self) -> bool
Returns true while a BEGIN … COMMIT/ROLLBACK block is open.
Sourcepub fn begin_transaction(&mut self) -> Result<()>
pub fn begin_transaction(&mut self) -> Result<()>
Starts a transaction: snapshots every table deep-cloned so that
a later rollback_transaction can restore the pre-BEGIN state.
Nested transactions are rejected — explicit savepoints are not
on this phase’s roadmap. Errors on a read-only database.
Sourcepub fn commit_transaction(&mut self) -> Result<()>
pub fn commit_transaction(&mut self) -> Result<()>
Drops the transaction snapshot and returns it for the caller to
discard. The in-memory tables state is the new committed state;
the caller is responsible for flushing to disk via the pager.
Errors if no transaction is open.
Sourcepub fn rollback_transaction(&mut self) -> Result<()>
pub fn rollback_transaction(&mut self) -> Result<()>
Restores tables from the transaction snapshot and clears it.
Errors if no transaction is open.