pub struct Database { /* private fields */ }Expand description
A kit database handle.
Wraps a MongrelDB core database and a kit schema, providing table metadata and transaction creation.
Implementations§
Source§impl Database
impl Database
Sourcepub fn open_encrypted(path: &Path, passphrase: &str) -> Result<Self>
pub fn open_encrypted(path: &Path, passphrase: &str) -> Result<Self>
Open an existing page-encrypted kit database with its passphrase.
Sourcepub fn create_encrypted(
path: &Path,
schema: KitSchema,
passphrase: &str,
) -> Result<Self>
pub fn create_encrypted( path: &Path, schema: KitSchema, passphrase: &str, ) -> Result<Self>
Create a fresh page-encrypted kit database (AES-256-GCM; the passphrase
derives the key hierarchy). Columns flagged encrypted /
encrypted_indexable are encrypted at rest.
Sourcepub fn create(path: &Path, schema: KitSchema) -> Result<Self>
pub fn create(path: &Path, schema: KitSchema) -> Result<Self>
Create a fresh kit database with the given schema.
Sourcepub fn register_default(
&mut self,
name: impl Into<String>,
provider: impl Fn() -> Value + Send + Sync + 'static,
)
pub fn register_default( &mut self, name: impl Into<String>, provider: impl Fn() -> Value + Send + Sync + 'static, )
Register a named default provider used by DefaultKind::CustomName
columns. Returns the database for chaining.
Sourcepub fn raw(&self) -> &CoreDatabase
pub fn raw(&self) -> &CoreDatabase
The raw, unguarded MongrelDB core database. This is the Rust analogue of
the TypeScript kit’s nativeDb escape hatch: writes made directly
against it bypass all kit constraints.
Sourcepub fn table_names(&self) -> Vec<String>
pub fn table_names(&self) -> Vec<String>
Application table names, excluding the reserved __kit_* tables.
pub fn create_procedure(&self, spec: &ProcedureSpec) -> Result<StoredProcedure>
pub fn replace_procedure(&self, spec: &ProcedureSpec) -> Result<StoredProcedure>
pub fn drop_procedure(&self, name: &str) -> Result<()>
pub fn call_procedure( &self, name: &str, args: Map<String, Value>, ) -> Result<ProcedureCallResult>
pub fn create_trigger(&self, spec: &TriggerSpec) -> Result<StoredTrigger>
pub fn replace_trigger(&self, spec: &TriggerSpec) -> Result<StoredTrigger>
pub fn drop_trigger(&self, name: &str) -> Result<()>
pub fn triggers(&self) -> Vec<StoredTrigger>
pub fn trigger(&self, name: &str) -> Option<StoredTrigger>
Sourcepub fn allocate_sequence(&self, name: &str, count: i64) -> Result<i64>
pub fn allocate_sequence(&self, name: &str, count: i64) -> Result<i64>
Allocate count values from the named sequence, returning the first
allocated value. A fresh sequence starts at 1 (SQL AUTO_INCREMENT
semantics). The allocation
runs in its own committed transaction and retries on write-write
conflicts.
Sourcepub fn transaction<T, F>(&self, max_retries: usize, f: F) -> Result<T>
pub fn transaction<T, F>(&self, max_retries: usize, f: F) -> Result<T>
Run f inside a kit transaction, committing on success and retrying on
retryable write-write conflicts up to max_retries times.
Sourcepub fn begin(&self) -> Result<Transaction<'_>>
pub fn begin(&self) -> Result<Transaction<'_>>
Begin a new kit transaction.
Sourcepub fn set_schema(&mut self, schema: KitSchema)
pub fn set_schema(&mut self, schema: KitSchema)
Replace the in-memory schema, usually after a migration.
Sourcepub fn check_internal_tables(&self) -> Result<()>
pub fn check_internal_tables(&self) -> Result<()>
Verify that the sidecar schema file and all reserved __kit_* tables
are present.
Sourcepub fn gc(&self) -> Result<usize>
pub fn gc(&self) -> Result<usize>
Reclaim orphaned runs and stale WAL/shadow files; returns the count removed. Safe to run on a live database.
Sourcepub fn check(&self) -> Vec<Value>
pub fn check(&self) -> Vec<Value>
Verify run footer checksums; returns any integrity issues as JSON objects
(table_id, table_name, severity, description). Empty ⇒ healthy.
Sourcepub fn doctor(&self) -> Result<Vec<u64>>
pub fn doctor(&self) -> Result<Vec<u64>>
Drop corrupt runs; returns the ids of the runs that were dropped.
Sourcepub fn snapshot_epoch(&self) -> u64
pub fn snapshot_epoch(&self) -> u64
The current visible commit epoch — a monotonically increasing version stamp. A committed write bumps it; a snapshot at this epoch sees all currently-committed data.
Sourcepub fn export_tsv(&self, table: &str) -> Result<String>
pub fn export_tsv(&self, table: &str) -> Result<String>
Export every visible row of table as a TSV document (header row of
column names, tab-separated cells, NULL = empty field). See
crate::tsv for the escaping rules.
Sourcepub fn import_tsv(&self, table: &str, text: &str) -> Result<usize>
pub fn import_tsv(&self, table: &str, text: &str) -> Result<usize>
Import a TSV document into table (one committed transaction). Each row
passes through defaults, validation, and constraint checks like a normal
insert. Returns the number of rows inserted.
Sourcepub fn explain(&self, table: &str, predicate: &Expr) -> Result<ExplainPlan>
pub fn explain(&self, table: &str, predicate: &Expr) -> Result<ExplainPlan>
Describe how predicate would be executed against table: which native
index conditions push down, whether the push-down is exact (no residual
re-filtering), and whether any index acceleration applies at all. A pure
diagnostic — it plans but does not run the query.
Sourcepub fn rows_at_epoch(&self, table: &str, epoch: u64) -> Result<Vec<Row>>
pub fn rows_at_epoch(&self, table: &str, epoch: u64) -> Result<Vec<Row>>
Read every row of table visible at commit epoch — a point-in-time
(MVCC time-travel) read. epoch must not exceed the current snapshot
epoch. Rows reclaimed by GC/compaction for retired snapshots may no
longer be reconstructable; this reads whatever the engine still retains
at that epoch.
Sourcepub fn approx_aggregate(
&self,
table: &str,
column: Option<&str>,
agg: ApproxAggKind,
z: f64,
) -> Result<Option<ApproxAggregate>>
pub fn approx_aggregate( &self, table: &str, column: Option<&str>, agg: ApproxAggKind, z: f64, ) -> Result<Option<ApproxAggregate>>
Estimate an aggregate over table from the engine’s reservoir sample,
returning a point estimate and a z-score confidence interval (e.g.
z = 1.96 for ~95%). column is required for Sum/Avg and ignored
for Count. Returns None when the reservoir is empty (no sampled rows
yet). Fast and O(sample) — trades exactness for speed on large tables.
Sourcepub fn scan_batched<F>(
&self,
table: &str,
batch_size: usize,
f: F,
) -> Result<()>
pub fn scan_batched<F>( &self, table: &str, batch_size: usize, f: F, ) -> Result<()>
Stream table in row batches without materializing the whole table at
once. f receives successive chunks of at most batch_size value-maps,
all from one snapshot. Backed by the engine’s native scan cursor when the
table has a sorted run; for an overlay-only table (no run yet) it falls
back to a single in-memory pass, still chunked to batch_size.
Sourcepub fn set_similarity(
&self,
table: &str,
column: &str,
query: &[String],
k: usize,
) -> Result<Vec<SimilarRow>>
pub fn set_similarity( &self, table: &str, column: &str, query: &[String], k: usize, ) -> Result<Vec<SimilarRow>>
Rank rows of table by Jaccard set-similarity between query and the
string set stored (as a JSON array) in column, returning the top k
with similarity > 0, highest first — the dedup/join primitive.
When column has a MinHash index, candidate rows come from the engine’s
LSH index (sub-linear) and are then re-verified with exact Jaccard, so the
top-k is exact for the recalled candidates (LSH recall is high but < 100%).
Without an index it is an exact linear scan.
Sourcepub fn flush(&self) -> Result<()>
pub fn flush(&self) -> Result<()>
Flush every table’s in-memory writes to durable sorted runs. Besides
durability, this empties the memtable, which is what enables the engine’s
incremental-aggregate fast path (see Self::incremental_aggregate).
Sourcepub fn incremental_aggregate(
&self,
table: &str,
column: Option<&str>,
agg: IncrementalAggKind,
filter: Option<&Expr>,
) -> Result<IncrementalAggregate>
pub fn incremental_aggregate( &self, table: &str, column: Option<&str>, agg: IncrementalAggKind, filter: Option<&Expr>, ) -> Result<IncrementalAggregate>
Maintain and read an aggregate over table that updates by merging only
newly-committed rows instead of rescanning. column is required for
Sum/Min/Max/Avg and ignored for Count. An optional filter
restricts the aggregate; it must translate exactly to index
conditions (no residual), otherwise this errors — an inexact filter would
silently aggregate the wrong rows.
The engine keeps a per-(table, column, agg, filter) cached state and,
on a warm cache with an advanced epoch and no deletes/pending writes,
folds in just the delta. The returned value is always exact; the
incremental flag reports whether the fast path was taken.
Sourcepub fn applied_migrations(&self) -> Result<Vec<Migration>>
pub fn applied_migrations(&self) -> Result<Vec<Migration>>
Return the migrations already recorded in __kit_schema_migrations.
Sourcepub fn close(&self) -> Result<()>
pub fn close(&self) -> Result<()>
Best-effort flush-on-close (§4.4): force-flush pending writes on every
table to .sr sorted runs so WAL segments stay bounded across repeated
short-lived process invocations (e.g. the CLI). Call as the last action
before exit. The daemon does not need this (auto-compactor handles it).
Sourcepub fn compact_all(&self) -> Result<(usize, usize)>
pub fn compact_all(&self) -> Result<(usize, usize)>
Compact all tables: merge sorted runs into one clean run each so query
latency stays flat. Returns (compacted, skipped). Safe to run at any
time — honors snapshot retention. The daemon’s background auto-compactor
already does this periodically; this is for manual/cron use.
Sourcepub fn compact_table(&self, name: &str) -> Result<bool>
pub fn compact_table(&self, name: &str) -> Result<bool>
Compact a single table by name. Returns true if compacted, false if
skipped (< 2 runs).
Sourcepub fn rename_table(&mut self, from: &str, to: &str) -> Result<()>
pub fn rename_table(&mut self, from: &str, to: &str) -> Result<()>
Rename a live table. Fails if from does not exist or to is already
in use; a no-op when from == to. Names beginning with __kit_ are
reserved for internal tables and rejected here (parity with the
TypeScript kit).
Updates both the engine table and the kit schema catalog (in memory and
persisted to kit_schema.json), so subsequent table_names(),
table(name), and transactional reads by the new name all work. Foreign
keys in other tables that reference from are retargeted to to.
Sourcepub fn analyze(&self) -> Result<()>
pub fn analyze(&self) -> Result<()>
Rebuild statistics/metadata for every table’s indexes (the engine’s
ANALYZE equivalent: ensure_indexes_complete on each table). Safe to
run at any time; useful after bulk loads so the query planner and
learned indexes have fresh data.
Sourcepub fn vacuum(&self) -> Result<usize>
pub fn vacuum(&self) -> Result<usize>
Reclaim space across all tables: compacts every table’s sorted runs,
then runs gc. Returns the count of reclaimed orphaned runs/files.
Equivalent to the engine’s VACUUM. Safe to run at any time.
Sourcepub fn create_view(&self, spec: &ViewSpec) -> Result<()>
pub fn create_view(&self, spec: &ViewSpec) -> Result<()>
Create a SQL view (CREATE VIEW <name> AS <select>). The engine
overwrites any existing view with the same name, so this also serves as
replace. The view lives in the kit’s long-lived SQL session — it is not
persisted to the catalog, so reopening the database loses it (re-apply
a CreateView migration to restore).
Sourcepub fn drop_view(&self, name: &str) -> Result<()>
pub fn drop_view(&self, name: &str) -> Result<()>
Drop a SQL view by name (idempotent — DROP VIEW IF EXISTS).
Sourcepub fn reserve_auto_inc(&self, table: &str) -> Result<Option<i64>>
pub fn reserve_auto_inc(&self, table: &str) -> Result<Option<i64>>
Reserve (without inserting) the next engine-native AUTO_INCREMENT value
for table, advancing the per-table counter. Returns None when the
table has no auto-increment column. This is the escape hatch for callers
that stage a row with an explicit id inside a transaction; the
reservation becomes durable when a row carrying the id commits, and an
unused reservation just leaves a gap. Parity with the TypeScript kit’s
reserveAutoIncSync.
Sourcepub fn sql(&self, statement: &str) -> Result<Vec<RecordBatch>>
pub fn sql(&self, statement: &str) -> Result<Vec<RecordBatch>>
Run a SQL statement through the embedded MongrelSession (DataFusion
frontend) and return the result as Arrow [RecordBatch]es. This is the
Rust analogue of the TypeScript kit’s db.sql(sql) (which returns Arrow
IPC bytes) and the NAPI Database.sql.
Read statements return their rows; DDL/DML (e.g. CREATE VIEW,
ANALYZE, VACUUM, CREATE VIRTUAL TABLE) return an empty vec. Writes
made directly through SQL bypass Kit-level constraints (defaults,
enums, min/max, length, regex, triggers) — use the transactional
Transaction API for constrained writes. The
engine’s own declarative constraints (unique, FK, check) still apply.
The session is held for the database’s lifetime, so session-scoped
objects (views, prepared statements, the result cache) persist across
calls — mirroring a long-lived database connection. After a migration
that creates/drops tables, call Database::refresh_sql_session so the
session sees the new table set.
Sourcepub fn refresh_sql_session(&self) -> Result<()>
pub fn refresh_sql_session(&self) -> Result<()>
(Re)build the cached SQL session so it sees the current table set. The
engine’s MongrelSession snapshots the table list at construction; this
rebuilds it after a migration creates or drops tables. Views and other
session-scoped state are reset.
Sourcepub fn sql_arrow(&self, statement: &str) -> Result<Vec<u8>>
pub fn sql_arrow(&self, statement: &str) -> Result<Vec<u8>>
Like Database::sql, but returns the result serialized as Arrow IPC
file bytes — the same wire format the NAPI addon and the daemon emit.
Decode with pyarrow.ipc.open_file, the JS apache-arrow
tableFromIPC, or crate::arrow_util::read_arrow_ipc. Empty for
DDL/DML.
Auto Trait Implementations§
impl !Freeze for Database
impl !RefUnwindSafe for Database
impl !UnwindSafe for Database
impl Send for Database
impl Sync for Database
impl Unpin for Database
impl UnsafeUnpin for Database
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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