ferrograph 1.3.0

Graph-powered Rust code intelligence
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! CozoDB-backed graph storage.

use std::collections::BTreeMap;
use std::str::FromStr;

use anyhow::Result;
use cozo::{DataValue, DbInstance, NamedRows, ScriptMutability};

use crate::graph::query::Query;
use crate::graph::schema::{EdgeType, NodeId, NodeType};
use crate::graph::unquote_datavalue;

fn cozo_err(e: &cozo::Error) -> anyhow::Error {
    anyhow::anyhow!("{e:#}")
}

/// In-memory graph store using `CozoDB`.
pub struct Store {
    db: DbInstance,
}

impl Store {
    /// Create a new in-memory store.
    ///
    /// # Errors
    /// Fails if the Cozo in-memory engine cannot be initialized.
    pub fn new_memory() -> Result<Self> {
        let db = DbInstance::new("mem", "", Default::default()).map_err(|e| cozo_err(&e))?;
        let store = Store { db };
        store.init_schema()?;
        Ok(store)
    }

    /// Create or open a persistent store at the given path.
    ///
    /// # Errors
    /// Fails if the `SQLite` backend cannot be opened or the schema cannot be created.
    pub fn new_persistent(path: &std::path::Path) -> Result<Self> {
        let path_str = path.to_string_lossy();
        let db = DbInstance::new("sqlite", path_str.as_ref(), Default::default())
            .map_err(|e| cozo_err(&e))?;
        let store = Store { db };
        store.init_schema()?;
        Ok(store)
    }

    fn init_schema(&self) -> Result<()> {
        // Nodes: idempotent create (ignore error if relation already exists on reopen)
        self.db
            .run_script(
                "%ignore_error { :create nodes { id: String => type: String, payload: String? } }",
                BTreeMap::new(),
                ScriptMutability::Mutable,
            )
            .map_err(|e| cozo_err(&e))?;
        // Edges: composite key (from_id, to_id, edge_type)
        self.db
            .run_script(
                "%ignore_error { :create edges { from_id: String, to_id: String, edge_type: String } }",
                BTreeMap::new(),
                ScriptMutability::Mutable,
            )
            .map_err(|e| cozo_err(&e))?;
        // Dead functions (populated by pipeline phase 9)
        self.db
            .run_script(
                "%ignore_error { :create dead_functions { id: String } }",
                BTreeMap::new(),
                ScriptMutability::Mutable,
            )
            .map_err(|e| cozo_err(&e))?;
        Ok(())
    }

    /// Insert a node.
    ///
    /// # Errors
    /// Fails if the Cozo script or serialization fails.
    pub fn put_node(&self, id: &NodeId, node_type: &NodeType, payload: Option<&str>) -> Result<()> {
        let type_str = node_type.to_string();
        let payload_val = payload.map_or(DataValue::Null, DataValue::from);
        let mut params = BTreeMap::new();
        params.insert("id".to_string(), DataValue::from(id.as_str()));
        params.insert("type".to_string(), DataValue::from(type_str.as_str()));
        params.insert("payload".to_string(), payload_val);
        self.db
            .run_script(
                "?[id, type, payload] <- [[$id, $type, $payload]] :put nodes { id => type, payload }",
                params,
                ScriptMutability::Mutable,
            )
            .map_err(|e| cozo_err(&e))?;
        Ok(())
    }

    /// Insert an edge.
    ///
    /// # Errors
    /// Fails if the Cozo script or serialization fails.
    pub fn put_edge(&self, from: &NodeId, to: &NodeId, edge_type: &EdgeType) -> Result<()> {
        let type_str = edge_type.to_string();
        let mut params = BTreeMap::new();
        params.insert("from_id".to_string(), DataValue::from(from.as_str()));
        params.insert("to_id".to_string(), DataValue::from(to.as_str()));
        params.insert("edge_type".to_string(), DataValue::from(type_str.as_str()));
        self.db
            .run_script(
                "?[from_id, to_id, edge_type] <- [[$from_id, $to_id, $edge_type]] :put edges { from_id, to_id, edge_type }",
                params,
                ScriptMutability::Mutable,
            )
            .map_err(|e| cozo_err(&e))?;
        Ok(())
    }

    /// Insert multiple nodes in one script (more efficient than repeated `put_node`).
    ///
    /// # Errors
    /// Fails if the Cozo script or serialization fails.
    pub fn put_nodes_batch(&self, nodes: &[(NodeId, NodeType, Option<&str>)]) -> Result<()> {
        const CHUNK: usize = 100;
        for chunk in nodes.chunks(CHUNK) {
            let mut rows = Vec::with_capacity(chunk.len());
            let mut params = BTreeMap::new();
            for (i, (id, node_type, payload)) in chunk.iter().enumerate() {
                let type_str = node_type.to_string();
                let payload_val = payload.map_or(DataValue::Null, DataValue::from);
                params.insert(format!("id{i}"), DataValue::from(id.as_str()));
                params.insert(format!("type{i}"), DataValue::from(type_str.as_str()));
                params.insert(format!("payload{i}"), payload_val);
                rows.push(format!("[$id{i}, $type{i}, $payload{i}]"));
            }
            let script = format!(
                "?[id, type, payload] <- [{}] :put nodes {{ id => type, payload }}",
                rows.join(", ")
            );
            self.db
                .run_script(&script, params, ScriptMutability::Mutable)
                .map_err(|e| cozo_err(&e))?;
        }
        Ok(())
    }

    /// Insert multiple edges in one script (more efficient than repeated `put_edge`).
    ///
    /// # Errors
    /// Fails if the Cozo script or serialization fails.
    pub fn put_edges_batch(&self, edges: &[(NodeId, NodeId, EdgeType)]) -> Result<()> {
        const CHUNK: usize = 100;
        for chunk in edges.chunks(CHUNK) {
            let mut rows = Vec::with_capacity(chunk.len());
            let mut params = BTreeMap::new();
            for (i, (from, to, edge_type)) in chunk.iter().enumerate() {
                let type_str = edge_type.to_string();
                params.insert(format!("from_id{i}"), DataValue::from(from.as_str()));
                params.insert(format!("to_id{i}"), DataValue::from(to.as_str()));
                params.insert(format!("edge_type{i}"), DataValue::from(type_str.as_str()));
                rows.push(format!("[$from_id{i}, $to_id{i}, $edge_type{i}]"));
            }
            let script = format!(
                "?[from_id, to_id, edge_type] <- [{}] :put edges {{ from_id, to_id, edge_type }}",
                rows.join(", ")
            );
            self.db
                .run_script(&script, params, ScriptMutability::Mutable)
                .map_err(|e| cozo_err(&e))?;
        }
        Ok(())
    }

    /// Return the number of nodes (without loading all rows).
    ///
    /// # Errors
    /// Fails if the store query fails.
    pub fn node_count(&self) -> Result<usize> {
        let result = self
            .db
            .run_script(
                "?[count(id)] := *nodes[id, type, payload]",
                BTreeMap::new(),
                ScriptMutability::Immutable,
            )
            .map_err(|e| cozo_err(&e))?;
        let n: i64 = result
            .rows
            .first()
            .and_then(|r| r.first())
            .and_then(DataValue::get_int)
            .unwrap_or(0);
        Ok(usize::try_from(n).unwrap_or(0))
    }

    /// Return the number of edges (without loading all rows).
    ///
    /// # Errors
    /// Fails if the store query fails.
    pub fn edge_count(&self) -> Result<usize> {
        let result = self
            .db
            .run_script(
                "?[count(from_id)] := *edges[from_id, to_id, edge_type]",
                BTreeMap::new(),
                ScriptMutability::Immutable,
            )
            .map_err(|e| cozo_err(&e))?;
        let n: i64 = result
            .rows
            .first()
            .and_then(|r| r.first())
            .and_then(DataValue::get_int)
            .unwrap_or(0);
        Ok(usize::try_from(n).unwrap_or(0))
    }

    /// Remove a single edge by key.
    ///
    /// # Errors
    /// Fails if the Cozo script or serialization fails.
    pub fn remove_edge(&self, from: &NodeId, to: &NodeId, edge_type: &EdgeType) -> Result<()> {
        let type_str = edge_type.to_string();
        let mut params = BTreeMap::new();
        params.insert("from_id".to_string(), DataValue::from(from.as_str()));
        params.insert("to_id".to_string(), DataValue::from(to.as_str()));
        params.insert("edge_type".to_string(), DataValue::from(type_str.as_str()));
        self.db
            .run_script(
                "?[from_id, to_id, edge_type] <- [[$from_id, $to_id, $edge_type]] :rm edges { from_id, to_id, edge_type }",
                params,
                ScriptMutability::Mutable,
            )
            .map_err(|e| cozo_err(&e))?;
        Ok(())
    }

    /// Run a Datalog script (read-only).
    ///
    /// # Errors
    /// Fails if the script is invalid or execution fails.
    pub fn run_query(
        &self,
        script: &str,
        params: BTreeMap<String, DataValue>,
    ) -> Result<NamedRows> {
        let result = self
            .db
            .run_script(script, params, ScriptMutability::Immutable)
            .map_err(|e| cozo_err(&e))?;
        Ok(result)
    }

    /// Remove all nodes, edges, and `dead_functions` in one script (atomic for Cozo).
    ///
    /// # Errors
    /// Fails if the Cozo script fails.
    pub fn clear(&self) -> Result<()> {
        self.db
            .run_script(
                r"
                { ?[id] := *nodes[id, type, payload]; :rm nodes { id } }
                { ?[from_id, to_id, edge_type] := *edges[from_id, to_id, edge_type]; :rm edges { from_id, to_id, edge_type } }
                { ?[id] := *dead_functions[id]; :rm dead_functions { id } }
                ",
                BTreeMap::new(),
                ScriptMutability::Mutable,
            )
            .map_err(|e| cozo_err(&e))?;
        Ok(())
    }

    /// Clear the `dead_functions` relation (called by `clear()` and before repopulating).
    ///
    /// # Errors
    /// Fails if the Cozo script fails.
    pub fn clear_dead_functions(&self) -> Result<()> {
        self.db
            .run_script(
                "{ ?[id] := *dead_functions[id]; :rm dead_functions { id } }",
                BTreeMap::new(),
                ScriptMutability::Mutable,
            )
            .map_err(|e| cozo_err(&e))?;
        Ok(())
    }

    /// Insert a single id into the `dead_functions` relation.
    ///
    /// # Errors
    /// Fails if the Cozo script fails.
    pub fn put_dead_function(&self, id: &str) -> Result<()> {
        let mut params = BTreeMap::new();
        params.insert("id".to_string(), DataValue::from(id));
        self.db
            .run_script(
                "?[id] <- [[$id]] :put dead_functions { id }",
                params,
                ScriptMutability::Mutable,
            )
            .map_err(|e| cozo_err(&e))?;
        Ok(())
    }

    /// Copy all nodes, edges, and `dead_functions` from another store into this one.
    /// Caller should typically call `clear()` first. Used by watch to replace
    /// contents after a successful pipeline run into a temp store.
    ///
    /// # Errors
    /// Fails if queries or writes fail.
    pub fn copy_from(&self, other: &Store) -> Result<()> {
        let nodes = Query::all_nodes(other)?;
        let batch: Vec<(NodeId, NodeType, Option<String>)> = nodes
            .rows
            .iter()
            .filter_map(|row| {
                let id = row.first().map(unquote_datavalue)?;
                let type_str = row.get(1).map(unquote_datavalue)?;
                let payload = row.get(2).and_then(|v| {
                    if matches!(v, DataValue::Null) {
                        None
                    } else {
                        Some(unquote_datavalue(v))
                    }
                });
                let node_type = NodeType::from_str(&type_str).ok()?;
                Some((NodeId(id), node_type, payload))
            })
            .collect();
        if !batch.is_empty() {
            let batch_refs: Vec<(NodeId, NodeType, Option<&str>)> = batch
                .iter()
                .map(|(id, ty, p)| (id.clone(), ty.clone(), p.as_deref()))
                .collect();
            self.put_nodes_batch(&batch_refs)?;
        }
        let edges = Query::all_edges(other)?;
        let edge_batch: Vec<_> = edges
            .rows
            .iter()
            .filter_map(|row| {
                let from_str = row.first().map(unquote_datavalue)?;
                let to_str = row.get(1).map(unquote_datavalue)?;
                let type_str = row.get(2).map(unquote_datavalue)?;
                let edge_type = EdgeType::from_str(&type_str).ok()?;
                Some((NodeId(from_str), NodeId(to_str), edge_type))
            })
            .collect();
        if !edge_batch.is_empty() {
            self.put_edges_batch(&edge_batch)?;
        }
        let dead = Query::stored_dead_functions(other)?;
        for id in &dead {
            self.put_dead_function(id)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use super::Store;
    use crate::graph::query::Query;
    use crate::graph::schema::{EdgeType, NodeId, NodeType};

    #[test]
    fn store_put_node_and_query() {
        let store = Store::new_memory().unwrap();
        let id = NodeId("n1".to_string());
        store
            .put_node(&id, &NodeType::Function, Some("foo"))
            .unwrap();
        let rows = Query::all_nodes(&store).unwrap();
        assert_eq!(rows.rows.len(), 1);
        assert_eq!(rows.rows[0][0].to_string().trim_matches('"'), "n1");
    }

    #[test]
    fn store_put_edge_and_query() {
        let store = Store::new_memory().unwrap();
        let from = NodeId("a".to_string());
        let to = NodeId("b".to_string());
        store.put_node(&from, &NodeType::Function, None).unwrap();
        store.put_node(&to, &NodeType::Function, None).unwrap();
        store.put_edge(&from, &to, &EdgeType::Calls).unwrap();
        let rows = Query::all_edges(&store).unwrap();
        assert_eq!(rows.rows.len(), 1);
        assert_eq!(rows.rows[0][0].to_string().trim_matches('"'), "a");
        assert_eq!(rows.rows[0][1].to_string().trim_matches('"'), "b");
    }

    #[test]
    fn store_datalog_query() {
        let store = Store::new_memory().unwrap();
        store
            .put_node(&NodeId("f".to_string()), &NodeType::Function, Some("main"))
            .unwrap();
        let script = "?[id, type, payload] := *nodes[id, type, payload]";
        let rows = store.run_query(script, BTreeMap::new()).unwrap();
        assert_eq!(rows.rows.len(), 1);
        assert!(rows.rows[0][2].to_string().contains("main"));
    }

    #[test]
    fn store_clear_removes_all() {
        let store = Store::new_memory().unwrap();
        store
            .put_node(&NodeId("a".to_string()), &NodeType::Function, None)
            .unwrap();
        store
            .put_node(&NodeId("b".to_string()), &NodeType::Function, None)
            .unwrap();
        store.clear().unwrap();
        let rows = Query::all_nodes(&store).unwrap();
        assert!(rows.rows.is_empty());
    }

    #[test]
    fn store_persistent_reopen_preserves_data() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("reopen_test");
        {
            let store = Store::new_persistent(&db_path).unwrap();
            store
                .put_node(
                    &NodeId("persisted".to_string()),
                    &NodeType::Function,
                    Some("survives"),
                )
                .unwrap();
        }
        let store2 = Store::new_persistent(&db_path).unwrap();
        let rows = Query::all_nodes(&store2).unwrap();
        assert_eq!(rows.rows.len(), 1);
        assert!(rows.rows[0][0].to_string().contains("persisted"));
        assert!(rows.rows[0][2].to_string().contains("survives"));
    }
}