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
impl Database
Sourcepub fn search_ranked(
&self,
query: &str,
opts: &QueryOptions,
) -> Result<Vec<RankedHit>>
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:
- Prepare the query (tokenize, drop stopwords, OR multi-token MATCH)
- Collect BM25-ordered candidates (oversample by
2 × limit) - Apply additive boosts for title/id/tag/alias token hits + multi-token coverage
- Apply multiplicative type priors from
QueryOptions::type_boosts - Augment with pure tag/alias exact matches FTS may have missed
- Sort by score, dedupe by id, truncate to
limit
§Errors
Returns crate::BrainError::FtsQuery for empty input after tokenization.
Tags attached to a node (empty if none).
Sourcepub fn list_pending_links(&self) -> Result<Vec<PendingLink>>
pub fn list_pending_links(&self) -> Result<Vec<PendingLink>>
List unresolved WikiLink / symbol targets.
Source§impl Database
impl Database
Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
Open or create a database at path, apply pragmas, and run migrations.
Sourcepub fn open_in_memory() -> Result<Self>
pub fn open_in_memory() -> Result<Self>
Open an in-memory database (tests).
Sourcepub fn with_transaction<T, F>(&self, f: F) -> Result<T>
pub fn with_transaction<T, F>(&self, f: F) -> Result<T>
Run f inside a single transaction (unchecked; suitable for &self).
Sourcepub fn insert_node(&self, node: &Node) -> Result<()>
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).
Sourcepub fn get_content_hash(&self, node_id: &str) -> Result<Option<String>>
pub fn get_content_hash(&self, node_id: &str) -> Result<Option<String>>
Fetch content_hash for a node if present.
Sourcepub fn insert_edge(&self, edge: &Edge) -> Result<()>
pub fn insert_edge(&self, edge: &Edge) -> Result<()>
Insert or update a weighted edge. Both endpoints must exist.
Sourcepub fn insert_pending_link(
&self,
source_id: &str,
raw_target: &str,
relation_type: &str,
created_at: i64,
) -> Result<()>
pub fn insert_pending_link( &self, source_id: &str, raw_target: &str, relation_type: &str, created_at: i64, ) -> Result<()>
Record an unresolved WikiLink for later resolution / reporting.
Sourcepub fn clear_pending_links_for(&self, source_id: &str) -> Result<()>
pub fn clear_pending_links_for(&self, source_id: &str) -> Result<()>
Clear all pending links originating from source_id (before re-resolve).
Sourcepub fn clear_all_auto_edges(&self) -> Result<()>
pub fn clear_all_auto_edges(&self) -> Result<()>
Delete all soft auto-edges (relation_type like auto_%).
Sourcepub fn clear_auto_edges_involving(&self, node_id: &str) -> Result<()>
pub fn clear_auto_edges_involving(&self, node_id: &str) -> Result<()>
Delete soft auto-edges where node_id is source or target.
Sourcepub fn insert_symbol_anchor(&self, anchor: &SymbolAnchor) -> Result<()>
pub fn insert_symbol_anchor(&self, anchor: &SymbolAnchor) -> Result<()>
Insert or update a code symbol anchor row (AST metadata).
Replace the full tag set for a node.
Sourcepub fn replace_node_aliases(
&self,
node_id: &str,
aliases: &[String],
) -> Result<()>
pub fn replace_node_aliases( &self, node_id: &str, aliases: &[String], ) -> Result<()>
Replace aliases for a node.
Sourcepub fn index_fts(
&self,
node_id: &str,
title: &str,
content: &str,
tags: &str,
) -> Result<()>
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.
Sourcepub fn get_fts_content(&self, node_id: &str) -> Result<Option<String>>
pub fn get_fts_content(&self, node_id: &str) -> Result<Option<String>>
Fetch indexed FTS body/content for a node (for context excerpts).
Sourcepub fn search_fts(&self, query: &str) -> Result<Vec<Node>>
pub fn search_fts(&self, query: &str) -> Result<Vec<Node>>
Search nodes using FTS5 BM25 ranking with a safely escaped query.
Sourcepub fn list_node_ids_by_type(&self, node_type: &str) -> Result<Vec<String>>
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").
Sourcepub fn get_node(&self, id: &str) -> Result<Option<Node>>
pub fn get_node(&self, id: &str) -> Result<Option<Node>>
Fetch a single node by id, if present.
Sourcepub fn count_nodes(&self) -> Result<usize>
pub fn count_nodes(&self) -> Result<usize>
Number of rows in nodes.
Sourcepub fn count_edges(&self) -> Result<usize>
pub fn count_edges(&self) -> Result<usize>
Number of rows in edges.
Sourcepub fn count_fts_rows(&self) -> Result<usize>
pub fn count_fts_rows(&self) -> Result<usize>
Number of rows in the FTS5 node_fts table (should equal indexed notes).
Sourcepub fn count_pending_links(&self) -> Result<usize>
pub fn count_pending_links(&self) -> Result<usize>
Number of unresolved WikiLink / symbol refs in pending_links.
Sourcepub fn count_symbol_anchors(&self) -> Result<usize>
pub fn count_symbol_anchors(&self) -> Result<usize>
Number of AST symbol anchor rows.
Sourcepub fn get_all_node_ids(&self) -> Result<Vec<String>>
pub fn get_all_node_ids(&self) -> Result<Vec<String>>
All node IDs in stable ascending order (CSR index order).
Sourcepub fn get_all_edges(&self) -> Result<Vec<Edge>>
pub fn get_all_edges(&self) -> Result<Vec<Edge>>
Full edge records (relation_type preserved).
Sourcepub fn get_csr_edges(&self) -> Result<Vec<(String, String, f32)>>
pub fn get_csr_edges(&self) -> Result<Vec<(String, String, f32)>>
Lightweight edge triples for CSR compile: (source, target, weight).
Sourcepub fn get_all_nodes(&self) -> Result<Vec<Node>>
pub fn get_all_nodes(&self) -> Result<Vec<Node>>
All nodes ordered by id ascending (export / CSR compile order).
Sourcepub fn link_resolution_maps(
&self,
) -> Result<(HashSet<String>, HashMap<String, String>, HashMap<String, String>)>
pub fn link_resolution_maps( &self, ) -> Result<(HashSet<String>, HashMap<String, String>, HashMap<String, String>)>
Build resolution maps: ids, alias→id, lowercase title→id.
Sourcepub fn resolve_pending_links(&self) -> Result<(usize, usize)>
pub fn resolve_pending_links(&self) -> Result<(usize, usize)>
Attempt to resolve all pending links; returns (resolved, still_pending).