Skip to main content

rinne_graph/
lib.rs

1//! The Rinne code graph: a local, incremental, tree-sitter structural index of
2//! the repo (files, symbols, imports, call edges) persisted in the blackboard's
3//! SQLite store. Retrieval returns a symbol's neighborhood — definition,
4//! callers, callees, imports — so workers fetch the relevant slice of a repo
5//! instead of re-reading whole files (issue #11).
6
7pub mod extract;
8pub mod indexer;
9pub mod lang;
10pub mod model;
11pub mod resolve;
12pub mod schema;
13pub mod store;
14
15use std::path::Path;
16use std::sync::Mutex;
17
18use rinne_types::graph::{CodeGraph, Neighborhood, SymbolRef};
19
20use crate::store::Store;
21
22/// Thread-safe handle to the code graph store.
23///
24/// Wraps a `Store` behind a `Mutex` so `Graph` is `Send + Sync` and can be
25/// shared across async task boundaries through the `CodeGraph` trait.
26pub struct Graph {
27    store: Mutex<Store>,
28}
29
30impl Graph {
31    /// Open (or create) the graph store at `db_path`, resolving repo-relative
32    /// file keys against `root` when computing the `stale` flag.
33    pub fn open(db_path: &Path, root: &Path) -> rusqlite::Result<Graph> {
34        let store = Store::open(db_path, root)?;
35        Ok(Graph {
36            store: Mutex::new(store),
37        })
38    }
39
40    /// Re-index `path` if the stored hash does not match the current `source`.
41    pub fn ensure_current(&self, path: &str, source: &str, mtime: i64) -> rusqlite::Result<()> {
42        let store = self.store.lock().expect("graph store lock poisoned");
43        if !store.is_current(path, source) {
44            store.index_file(path, source, mtime)?;
45        }
46        Ok(())
47    }
48
49    /// Returns (files, symbols, edges) counts for display in `rinne graph stats`.
50    pub fn stats(&self) -> (usize, usize, usize) {
51        self.store
52            .lock()
53            .expect("graph store lock poisoned")
54            .stats()
55    }
56
57    /// Returns all symbols defined in a given file (for `rinne graph symbols <file>`).
58    pub fn symbols_in(&self, path: &str) -> Vec<crate::model::Symbol> {
59        self.store
60            .lock()
61            .expect("graph store lock poisoned")
62            .symbols_in(path)
63    }
64}
65
66impl CodeGraph for Graph {
67    fn neighborhood(&self, symbol: &str) -> Option<Neighborhood> {
68        self.store
69            .lock()
70            .expect("graph store lock poisoned")
71            .neighborhood(symbol)
72    }
73
74    fn resolve_in_file(&self, file: &str, name: &str) -> Option<SymbolRef> {
75        self.store
76            .lock()
77            .expect("graph store lock poisoned")
78            .resolve_in_file(file, name)
79    }
80
81    fn symbol_names(&self) -> Vec<String> {
82        self.store
83            .lock()
84            .expect("graph store lock poisoned")
85            .symbol_names()
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92    use rinne_types::graph::CodeGraph;
93
94    #[test]
95    fn graph_exposes_codegraph_trait() {
96        let dir = std::env::temp_dir().join(format!("rinne-graph-test-{}", std::process::id()));
97        std::fs::create_dir_all(&dir).unwrap();
98        let db = dir.join("state.db");
99        let graph = Graph::open(&db, &dir).unwrap();
100        graph.ensure_current("m.rs", "fn helper() {}\nfn main() { helper(); }\n", 0).unwrap();
101        let nb = CodeGraph::neighborhood(&graph, "helper").unwrap();
102        assert_eq!(nb.definition.name, "helper");
103        let _ = std::fs::remove_dir_all(&dir);
104    }
105}