Skip to main content

Database

Struct Database 

Source
pub struct Database { /* private fields */ }
Expand description

A clonable, thread-safe handle to one database file. See the module docs for the concurrency model.

Implementations§

Source§

impl Database

Source

pub fn open( path: impl Into<PathBuf>, cfg: Config, ) -> Result<(Self, OpenReport), HostError>

Opens path with every knob at its default and no embedder.

Source

pub fn open_readonly( path: impl Into<PathBuf>, cfg: Config, ) -> Result<ReadOnlyDatabase, HostError>

Opens path read-only over a memory-mapped snapshot: the engine borrows the mapped pages instead of copying the file into RAM, so a large read-mostly database residents only the pages recall/get touch. Requires a checkpointed database (empty journal) and takes a shared lock (N readers or one writer). See ReadOnlyDatabase.

§Errors

HostError::Locked, HostError::NeedsCheckpoint, HostError::Io, HostError::Engine — see ReadOnlyDatabase::open semantics.

Source

pub fn builder(cfg: Config) -> DatabaseBuilder

Starts a configured open (knobs).

Source

pub fn remember( &self, input: RememberInput<'_>, ) -> Result<RememberOutcome, HostError>

Remembers a fact. Without an explicit vector and with an embedder configured, the text is embedded first — outside the lock.

Source

pub fn remember_many( &self, inputs: Vec<RememberInput<'_>>, ) -> Result<Vec<RememberOutcome>, HostError>

Remembers a batch of facts in one shot — the bulk-write path (CLI import). Equivalent to remember on each input in order, but far cheaper for a batch: the texts that need embedding are embedded together in one embedder round-trip (outside the lock), and all facts are written under one write-guard with one post-mutation policy pass — instead of N HTTP calls and N critical sections.

Inputs that already carry a vector are not re-embedded. Chunking is the caller’s job: this writes the whole slice it is given, so a caller that needs bounded memory / a bounded HTTP body passes fixed-size batches (CLI import streams the file in --batch-sized slices).

Fail-fast: the first engine error returns Err; the facts written before it stay written (exactly as separate remembers — the journal replay is idempotent, so a retried bulk load is safe). Returns one RememberOutcome per input, in order.

Source

pub fn recall(&self, q: RecallQuery<'_>) -> Result<RecallResult, HostError>

Runs a recall. With a text, no vector and an embedder configured, the query text is embedded first — outside the lock.

Source

pub fn revise( &self, target: FactId, input: RememberInput<'_>, ) -> Result<RememberOutcome, HostError>

Revises target (same auto-embedding rule as remember).

Source

pub fn forget(&self, now: u64, id: FactId) -> Result<bool, HostError>

Tombstones a fact.

Upserts a typed edge.

Source

pub fn get(&self, id: FactId) -> Option<FactSnapshot>

An owned copy of one fact, or None for unknown/tombstoned ids.

Source

pub fn stats(&self) -> Stats

Engine size counters.

Source

pub fn export(&self) -> Vec<ExportedFact>

Dumps the currently-open facts for a human-readable backup See ExportedFact. Collects the whole set; for a large database prefer export_each, which streams.

Source

pub fn export_each(&self, f: impl FnMut(ExportedFact))

Streams the currently-open facts, calling f once per fact under the read guard — the whole dump is never materialized, so a huge database exports without a RAM spike (CLI export writes each line straight out). See ExportedFact.

Source

pub fn maintain(&self, now: u64) -> Result<MaintainReport, HostError>

Runs a maintenance pass now (purge, compaction, HNSW build past the threshold — for the cost model).

Disk-first (milestone H): the compacted image is written by streaming the two big pools (vectors, text) through temp files and then re-mapped, so peak RAM tracks the record count (metadata + graph), not the image size — a database larger than RAM can be maintained. It writes a fresh snapshot and clears the journal (like a checkpoint). The optional auto-maintain policy (maintain_every_forgets) still runs in RAM inline — it is for databases that fit.

The report’s byte counts are the on-disk image size before and after.

Source

pub fn checkpoint(&self, now: u64) -> Result<(), HostError>

Writes a full snapshot and clears the journal now (re-mapping the fresh file — see Database::resnapshot).

Source

pub fn verify(&self) -> Result<(), HostError>

Runs the on-demand integrity check — the equivalent of SQLite’s integrity_check. An open validates only the metadata, so the large byte pools stay non-resident on an mmap’d base; this sweeps them (text UTF-8, vector self-consistency and the fact↔slot bijection) and reports any latent corruption. Skipping it is safe — the accessors never panic on bad bytes; verify only turns corruption into an explicit error.

§Errors

HostError::Engine wrapping Error::Corrupt for the first inconsistency found.

Source

pub fn recover( src: impl AsRef<Path>, dst: impl AsRef<Path>, cfg: Config, now: u64, ) -> Result<RecoverReport, HostError>

Salvages a content-corrupt database (Tier 2): opens src, drops the facts that fail the per-fact content checks (verify’s predicate), compacts the survivors and their indexes, and writes a clean image to dst. src on disk is left untouched — the evidence is preserved.

It is disk-first (milestone H): src is opened as an mmap overlay (its pages are reclaimable) and the compacted image is written by streaming the two big pools (vectors, text) through temp files, so peak RAM tracks the record count (metadata + HNSW graph), not the image size. A database far larger than RAM can be recovered, as long as its graph fits.

This handles content corruption (bad text bytes, a broken fact↔slot vector bijection). Structural damage — a snapshot that will not parse — is not salvageable here: src fails to open and recover returns the engine’s typed error; restore from a backup instead (Tier 0).

§Errors

HostError::Locked if src or dst is owned elsewhere; HostError::Engine if src will not parse (structural corruption) or dst equals src; HostError::Io for filesystem failures.

Trait Implementations§

Source§

impl Clone for Database

Source§

fn clone(&self) -> Database

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Database

Source§

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

Summary only — the contents are the user’s memory.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.