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 branches;
8pub mod extract;
9pub mod indexer;
10pub mod lang;
11pub mod model;
12pub mod resolve;
13pub mod schema;
14pub mod store;
15
16use std::path::Path;
17use std::sync::Mutex;
18
19use rinne_types::graph::{CodeGraph, Neighborhood, SymbolRef};
20
21use crate::store::Store;
22
23/// Thread-safe handle to the code graph store.
24///
25/// Wraps a `Store` behind a `Mutex` so `Graph` is `Send + Sync` and can be
26/// shared across async task boundaries through the `CodeGraph` trait.
27pub struct Graph {
28    store: Mutex<Store>,
29}
30
31impl Graph {
32    /// Open (or create) the graph store at `db_path`, resolving repo-relative
33    /// file keys against `root` when computing the `stale` flag.
34    pub fn open(db_path: &Path, root: &Path) -> rusqlite::Result<Graph> {
35        let store = Store::open(db_path, root)?;
36        Ok(Graph {
37            store: Mutex::new(store),
38        })
39    }
40
41    /// Re-index `path` if the stored hash does not match the current `source`.
42    pub fn ensure_current(&self, path: &str, source: &str, mtime: i64) -> rusqlite::Result<()> {
43        let store = self.store.lock().expect("graph store lock poisoned");
44        if !store.is_current(path, source) {
45            store.index_file(path, source, mtime)?;
46        }
47        Ok(())
48    }
49
50    /// Returns (files, symbols, edges) counts for display in `rinne graph stats`.
51    pub fn stats(&self) -> (usize, usize, usize) {
52        self.store
53            .lock()
54            .expect("graph store lock poisoned")
55            .stats()
56    }
57
58    /// Returns all symbols defined in a given file (for `rinne graph symbols <file>`).
59    pub fn symbols_in(&self, path: &str) -> Vec<crate::model::Symbol> {
60        self.store
61            .lock()
62            .expect("graph store lock poisoned")
63            .symbols_in(path)
64    }
65}
66
67impl CodeGraph for Graph {
68    fn neighborhood(&self, symbol: &str) -> Option<Neighborhood> {
69        self.store
70            .lock()
71            .expect("graph store lock poisoned")
72            .neighborhood(symbol)
73    }
74
75    fn neighborhood_all(&self, symbol: &str) -> Vec<Neighborhood> {
76        self.store
77            .lock()
78            .expect("graph store lock poisoned")
79            .neighborhood_all(symbol)
80    }
81
82    fn resolve_in_file(&self, file: &str, name: &str) -> Option<SymbolRef> {
83        self.store
84            .lock()
85            .expect("graph store lock poisoned")
86            .resolve_in_file(file, name)
87    }
88
89    fn symbol_names(&self) -> Vec<String> {
90        self.store
91            .lock()
92            .expect("graph store lock poisoned")
93            .symbol_names()
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100    use rinne_types::graph::CodeGraph;
101
102    #[test]
103    fn graph_exposes_codegraph_trait() {
104        let dir = std::env::temp_dir().join(format!("rinne-graph-test-{}", std::process::id()));
105        std::fs::create_dir_all(&dir).unwrap();
106        let db = dir.join("state.db");
107        let graph = Graph::open(&db, &dir).unwrap();
108        graph
109            .ensure_current("m.rs", "fn helper() {}\nfn main() { helper(); }\n", 0)
110            .unwrap();
111        let nb = CodeGraph::neighborhood(&graph, "helper").unwrap();
112        assert_eq!(nb.definition.name, "helper");
113        let _ = std::fs::remove_dir_all(&dir);
114    }
115
116    #[test]
117    fn neighborhood_reports_symbol_end_line() {
118        let dir = std::env::temp_dir().join(format!("rinne-graph-endline-{}", std::process::id()));
119        std::fs::create_dir_all(&dir).unwrap();
120        let db = dir.join("state.db");
121        let graph = Graph::open(&db, &dir).unwrap();
122        // `big` spans lines 1..=3; `helper` is line 4.
123        graph
124            .ensure_current("m.rs", "fn big() {\n    let x = 1;\n}\nfn helper() {}\n", 0)
125            .unwrap();
126        let nb = CodeGraph::neighborhood(&graph, "big").unwrap();
127        assert_eq!(nb.definition.line, 1, "start line");
128        assert_eq!(nb.definition.end_line, 3, "end line covers the whole body");
129        let _ = std::fs::remove_dir_all(&dir);
130    }
131}