Skip to main content

Database

Struct Database 

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

Master transactional store for a single brain (.brain/db.sqlite).

The connection is crate-visible for ranked-query helpers; external consumers use typed methods only.

Implementations§

Source§

impl Database

Source

pub fn search_ranked( &self, query: &str, opts: &QueryOptions, ) -> Result<Vec<RankedHit>>

Ranked search combining FTS5 BM25 with tag, alias, title, and id boosts.

Steps:

  1. Prepare the query (tokenize, drop stopwords, OR multi-token MATCH)
  2. Collect BM25-ordered candidates (oversample by 2 × limit)
  3. Apply additive boosts for title/id/tag/alias token hits + multi-token coverage
  4. Apply multiplicative type priors from QueryOptions::type_boosts
  5. Augment with pure tag/alias exact matches FTS may have missed
  6. Sort by score, dedupe by id, truncate to limit
§Errors

Returns crate::BrainError::FtsQuery for empty input after tokenization.

Source

pub fn get_tags_for(&self, node_id: &str) -> Result<Vec<String>>

Tags attached to a node (empty if none).

List unresolved WikiLink / symbol targets.

Source

pub fn count_nodes_by_type(&self) -> Result<Vec<(String, usize)>>

Count nodes grouped by node_type.

Source§

impl Database

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>

Open or create a database at path, apply pragmas, and run migrations.

Source

pub fn open_in_memory() -> Result<Self>

Open an in-memory database (tests).

Source

pub fn with_transaction<T, F>(&self, f: F) -> Result<T>
where F: FnOnce(&Connection) -> Result<T>,

Run f inside a single transaction (unchecked; suitable for &self).

Source

pub fn insert_node(&self, node: &Node) -> Result<()>

Insert or update a node. Preserves created_at on conflict when the existing row is older (caller should pass original created_at when known).

Source

pub fn get_content_hash(&self, node_id: &str) -> Result<Option<String>>

Fetch content_hash for a node if present.

Source

pub fn insert_edge(&self, edge: &Edge) -> Result<()>

Insert or update a weighted edge. Both endpoints must exist.

Record an unresolved WikiLink for later resolution / reporting.

Clear all pending links originating from source_id (before re-resolve).

Source

pub fn clear_all_auto_edges(&self) -> Result<()>

Delete all soft auto-edges (relation_type like auto_%).

Source

pub fn clear_auto_edges_involving(&self, node_id: &str) -> Result<()>

Delete soft auto-edges where node_id is source or target.

Source

pub fn insert_symbol_anchor(&self, anchor: &SymbolAnchor) -> Result<()>

Insert or update a code symbol anchor row (AST metadata).

Source

pub fn replace_node_tags(&self, node_id: &str, tags: &[String]) -> Result<()>

Replace the full tag set for a node.

Source

pub fn replace_node_aliases( &self, node_id: &str, aliases: &[String], ) -> Result<()>

Replace aliases for a node.

Source

pub fn index_fts( &self, node_id: &str, title: &str, content: &str, tags: &str, ) -> Result<()>

Idempotent FTS upsert: delete existing rows for node_id, then insert once.

Source

pub fn get_fts_content(&self, node_id: &str) -> Result<Option<String>>

Fetch indexed FTS body/content for a node (for context excerpts).

Source

pub fn search_fts(&self, query: &str) -> Result<Vec<Node>>

Search nodes using FTS5 BM25 ranking with a safely escaped query.

Source

pub fn list_node_ids_by_type(&self, node_type: &str) -> Result<Vec<String>>

List node ids with the given node_type string (e.g. "adr").

Source

pub fn get_node(&self, id: &str) -> Result<Option<Node>>

Fetch a single node by id, if present.

Source

pub fn count_nodes(&self) -> Result<usize>

Number of rows in nodes.

Source

pub fn count_edges(&self) -> Result<usize>

Number of rows in edges.

Source

pub fn count_fts_rows(&self) -> Result<usize>

Number of rows in the FTS5 node_fts table (should equal indexed notes).

Number of unresolved WikiLink / symbol refs in pending_links.

Source

pub fn count_symbol_anchors(&self) -> Result<usize>

Number of AST symbol anchor rows.

Source

pub fn get_all_node_ids(&self) -> Result<Vec<String>>

All node IDs in stable ascending order (CSR index order).

Source

pub fn get_all_edges(&self) -> Result<Vec<Edge>>

Full edge records (relation_type preserved).

Source

pub fn get_csr_edges(&self) -> Result<Vec<(String, String, f32)>>

Lightweight edge triples for CSR compile: (source, target, weight).

Source

pub fn get_all_nodes(&self) -> Result<Vec<Node>>

All nodes ordered by id ascending (export / CSR compile order).

Build resolution maps: ids, alias→id, lowercase title→id.

Attempt to resolve all pending links; returns (resolved, still_pending).

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.