Skip to main content

Database

Struct Database 

Source
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: String

Name 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 BEGIN is rejected
  • ROLLBACK restores tables from 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: JournalMode

Phase 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: MvStore

Phase 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

Source

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());
Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn in_transaction(&self) -> bool

Returns true while a BEGIN … COMMIT/ROLLBACK block is open.

Source

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.

Source

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.

Source

pub fn rollback_transaction(&mut self) -> Result<()>

Restores tables from the transaction snapshot and clears it. Errors if no transaction is open.

Trait Implementations§

Source§

impl Debug for Database

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.