sombra 0.3.6

High-performance graph database with ACID transactions, single-file storage, and bindings for Rust, TypeScript, and Python
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use std::collections::{HashSet, VecDeque};

use crate::db::GraphDB;
use crate::error::{GraphError, Result};
use crate::model::{EdgeDirection, NodeId};

impl GraphDB {
    /// Finds the nearest ancestor reachable via edges of the provided type that carries a label.
    ///
    /// This performs a breadth-first search that walks incoming edges of `edge_type`
    /// starting from `start` until it encounters a node whose labels contain `label`.
    /// The first match (closest ancestor) is returned.
    ///
    /// # Arguments
    /// * `start` - Node to begin searching from.
    /// * `label` - Target label to match on ancestor nodes (case-sensitive).
    /// * `edge_type` - Edge type to follow when traversing towards parents (e.g. `"CONTAINS"`).
    ///
    /// # Returns
    /// * `Ok(Some(node_id))` when a matching ancestor is found.
    /// * `Ok(None)` when no ancestor with the label exists via the given edge type.
    ///
    /// # Errors
    /// Propagates underlying storage errors such as [`GraphError::NotFound`] if the starting node
    /// or traversed edges are missing, and I/O errors emitted by the pager layer.
    ///
    /// # Time Complexity
    /// O(A) where A is the number of ancestors examined (bounded by reachable nodes via `edge_type`).
    ///
    /// # Space Complexity
    /// O(A) for the visited set and traversal queue.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use sombra::{GraphDB, Node, Edge, GraphError};
    /// # use tempfile::NamedTempFile;
    /// # fn main() -> Result<(), GraphError> {
    /// let path = NamedTempFile::new()?;
    /// let mut db = GraphDB::open(path.path())?;
    ///
    /// let mut file = Node::new(0);
    /// file.labels.push("File".into());
    /// let file_id = db.add_node(file)?;
    ///
    /// let mut func = Node::new(0);
    /// func.labels.push("Function".into());
    /// let func_id = db.add_node(func)?;
    ///
    /// db.add_edge(Edge::new(0, file_id, func_id, "CONTAINS"))?;
    ///
    /// let ancestor = db.find_ancestor_by_label(func_id, "File", "CONTAINS")?;
    /// assert_eq!(ancestor, Some(file_id));
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    /// * [`Self::get_ancestors`] - Enumerates all ancestors up to an optional depth.
    /// * [`Self::get_containing_file`] - Convenience wrapper for `"File"` nodes.
    pub fn find_ancestor_by_label(
        &mut self,
        start: NodeId,
        label: &str,
        edge_type: &str,
    ) -> Result<Option<NodeId>> {
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        let edge_types = [edge_type];

        visited.insert(start);
        queue.push_back(start);

        while let Some(current) = queue.pop_front() {
            for parent in
                self.get_neighbors_by_edge_type(current, &edge_types, EdgeDirection::Incoming)?
            {
                if !visited.insert(parent) {
                    continue;
                }

                let Some(node) = self.get_node(parent)? else {
                    continue;
                };
                if node.labels.iter().any(|candidate| candidate == label) {
                    return Ok(Some(parent));
                }

                queue.push_back(parent);
            }
        }

        Ok(None)
    }

    /// Collects all ancestors reachable via edges of the provided type.
    ///
    /// The ancestors are returned in breadth-first order, starting with the closest parent.
    /// An optional depth limit (expressed in number of hops) may be supplied to bound the traversal.
    ///
    /// # Arguments
    /// * `start` - Node whose ancestors should be collected.
    /// * `edge_type` - Edge type used to walk towards parents (e.g. `"CONTAINS"`).
    /// * `max_depth` - Optional hop limit (1 for direct parents, 2 for grandparents, etc).
    ///
    /// # Returns
    /// * `Ok(Vec<NodeId>)` containing ancestors ordered by increasing distance from `start`.
    ///
    /// # Errors
    /// Propagates storage-level errors such as [`GraphError::NotFound`] when traversed nodes
    /// or edges are missing, and I/O errors from the pager.
    ///
    /// # Time Complexity
    /// O(A) where A is the number of ancestors discovered (respecting `max_depth` if provided).
    ///
    /// # Space Complexity
    /// O(A) for the visited set, output vector, and traversal queue.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use sombra::{GraphDB, Node, Edge, GraphError};
    /// # use tempfile::NamedTempFile;
    /// # fn main() -> Result<(), GraphError> {
    /// let path = NamedTempFile::new()?;
    /// let mut db = GraphDB::open(path.path())?;
    ///
    /// let mut file = Node::new(0);
    /// file.labels.push("File".into());
    /// let file_id = db.add_node(file)?;
    ///
    /// let mut class = Node::new(0);
    /// class.labels.push("Class".into());
    /// let class_id = db.add_node(class)?;
    ///
    /// let mut func = Node::new(0);
    /// func.labels.push("Function".into());
    /// let func_id = db.add_node(func)?;
    ///
    /// db.add_edge(Edge::new(0, file_id, class_id, "CONTAINS"))?;
    /// db.add_edge(Edge::new(0, class_id, func_id, "CONTAINS"))?;
    ///
    /// let ancestors = db.get_ancestors(func_id, "CONTAINS", Some(2))?;
    /// assert_eq!(ancestors, vec![class_id, file_id]);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    /// * [`Self::find_ancestor_by_label`] - Finds a single ancestor that matches a label.
    /// * [`Self::get_descendants`] - Performs the inverse traversal towards children.
    pub fn get_ancestors(
        &mut self,
        start: NodeId,
        edge_type: &str,
        max_depth: Option<usize>,
    ) -> Result<Vec<NodeId>> {
        let mut ancestors = Vec::new();
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        let edge_types = [edge_type];

        visited.insert(start);
        queue.push_back((start, 0usize));

        while let Some((current, depth)) = queue.pop_front() {
            if let Some(limit) = max_depth {
                if depth >= limit {
                    continue;
                }
            }

            for parent in
                self.get_neighbors_by_edge_type(current, &edge_types, EdgeDirection::Incoming)?
            {
                if visited.insert(parent) {
                    ancestors.push(parent);
                    queue.push_back((parent, depth + 1));
                }
            }
        }

        Ok(ancestors)
    }

    /// Enumerates descendants reachable via edges of the provided type using breadth-first order.
    ///
    /// Descendants are returned closest-first. Supply `max_depth` to cap the expansion radius.
    ///
    /// # Arguments
    /// * `start` - Node whose descendants should be traversed.
    /// * `edge_type` - Edge type to follow towards children (e.g. `"CONTAINS"`).
    /// * `max_depth` - Optional hop limit (1 for direct children, 2 for grandchildren, etc).
    ///
    /// # Returns
    /// * `Ok(Vec<NodeId>)` containing descendant node IDs ordered by increasing distance.
    ///
    /// # Errors
    /// Propagates underlying [`GraphError`] variants when nodes or edges are missing, as well as
    /// pager-related I/O failures.
    ///
    /// # Time Complexity
    /// O(D) where D is the number of descendants visited (bounded by `max_depth` when provided).
    ///
    /// # Space Complexity
    /// O(D) for the visited set, traversal queue, and result vector.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use sombra::{GraphDB, Node, Edge, GraphError};
    /// # use tempfile::NamedTempFile;
    /// # fn main() -> Result<(), GraphError> {
    /// let path = NamedTempFile::new()?;
    /// let mut db = GraphDB::open(path.path())?;
    ///
    /// let mut file = Node::new(0);
    /// file.labels.push("File".into());
    /// let file_id = db.add_node(file)?;
    ///
    /// let mut class = Node::new(0);
    /// class.labels.push("Class".into());
    /// let class_id = db.add_node(class)?;
    ///
    /// let mut method = Node::new(0);
    /// method.labels.push("Function".into());
    /// let method_id = db.add_node(method)?;
    ///
    /// db.add_edge(Edge::new(0, file_id, class_id, "CONTAINS"))?;
    /// db.add_edge(Edge::new(0, class_id, method_id, "CONTAINS"))?;
    ///
    /// let descendants = db.get_descendants(file_id, "CONTAINS", None)?;
    /// assert_eq!(descendants, vec![class_id, method_id]);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    /// * [`Self::get_ancestors`] - Traverses in the opposite direction.
    /// * [`Self::get_containing_file`] - Utility for locating enclosing `"File"` nodes.
    pub fn get_descendants(
        &mut self,
        start: NodeId,
        edge_type: &str,
        max_depth: Option<usize>,
    ) -> Result<Vec<NodeId>> {
        let mut descendants = Vec::new();
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        let edge_types = [edge_type];

        visited.insert(start);
        queue.push_back((start, 0usize));

        while let Some((current, depth)) = queue.pop_front() {
            if let Some(limit) = max_depth {
                if depth >= limit {
                    continue;
                }
            }

            for child in
                self.get_neighbors_by_edge_type(current, &edge_types, EdgeDirection::Outgoing)?
            {
                if visited.insert(child) {
                    descendants.push(child);
                    queue.push_back((child, depth + 1));
                }
            }
        }

        Ok(descendants)
    }

    /// Returns the file node that directly or transitively contains the given node.
    ///
    /// This is a convenience wrapper around [`Self::find_ancestor_by_label`] that searches
    /// for a `"File"` node walking `"CONTAINS"` edges.
    ///
    /// # Arguments
    /// * `node_id` - Node whose containing file is required.
    ///
    /// # Returns
    /// * `Ok(node_id)` - Identifier of the containing file node.
    ///
    /// # Errors
    /// * [`GraphError::NotFound`] if no containing file exists along `"CONTAINS"` edges.
    /// * Propagates storage errors (missing nodes/edges, I/O failures).
    ///
    /// # Time Complexity
    /// O(A) where A is the number of ancestors examined until a `"File"` label is found.
    ///
    /// # Space Complexity
    /// O(A) for traversal bookkeeping.
    ///
    /// # Example
    /// ```rust,no_run
    /// # use sombra::{GraphDB, Node, Edge, GraphError};
    /// # use tempfile::NamedTempFile;
    /// # fn main() -> Result<(), GraphError> {
    /// let path = NamedTempFile::new()?;
    /// let mut db = GraphDB::open(path.path())?;
    ///
    /// let mut file = Node::new(0);
    /// file.labels.push("File".into());
    /// let file_id = db.add_node(file)?;
    ///
    /// let func_id = db.add_node(Node::new(0))?;
    /// db.add_edge(Edge::new(0, file_id, func_id, "CONTAINS"))?;
    ///
    /// let containing_file = db.get_containing_file(func_id)?;
    /// assert_eq!(containing_file, file_id);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    /// * [`Self::find_ancestor_by_label`] - Underlying search routine.
    /// * [`Self::get_ancestors`] - Retrieves every ancestor instead of one.
    pub fn get_containing_file(&mut self, node_id: NodeId) -> Result<NodeId> {
        match self.find_ancestor_by_label(node_id, "File", "CONTAINS")? {
            Some(file_id) => Ok(file_id),
            None => Err(GraphError::NotFound("containing file")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{Edge, Node};

    fn temp_db(name: &str) -> (tempfile::TempPath, std::path::PathBuf) {
        let file = tempfile::Builder::new()
            .prefix(name)
            .suffix(".db")
            .tempfile()
            .expect("create temp file");
        let path = file.path().to_path_buf();
        (file.into_temp_path(), path)
    }

    #[test]
    fn find_ancestor_by_label_returns_closest_match() {
        let (_guard, path) = temp_db("hierarchy_find_ancestor");
        let mut db = GraphDB::open(&path).expect("open db");

        let mut file = Node::new(0);
        file.labels.push("File".to_string());
        let file_id = db.add_node(file).expect("add file");

        let mut module = Node::new(0);
        module.labels.push("Module".to_string());
        let module_id = db.add_node(module).expect("add module");

        let mut class = Node::new(0);
        class.labels.push("Class".to_string());
        let class_id = db.add_node(class).expect("add class");

        let mut func = Node::new(0);
        func.labels.push("Function".to_string());
        let func_id = db.add_node(func).expect("add function");

        db.add_edge(Edge::new(0, file_id, module_id, "CONTAINS"))
            .expect("add file->module");
        db.add_edge(Edge::new(0, module_id, class_id, "CONTAINS"))
            .expect("add module->class");
        db.add_edge(Edge::new(0, class_id, func_id, "CONTAINS"))
            .expect("add class->func");

        let file_ancestor = db
            .find_ancestor_by_label(func_id, "File", "CONTAINS")
            .expect("find file ancestor");
        assert_eq!(file_ancestor, Some(file_id));

        let module_ancestor = db
            .find_ancestor_by_label(func_id, "Module", "CONTAINS")
            .expect("find module ancestor");
        assert_eq!(module_ancestor, Some(module_id));

        let missing_ancestor = db
            .find_ancestor_by_label(func_id, "Interface", "CONTAINS")
            .expect("find missing ancestor");
        assert!(missing_ancestor.is_none());

        let containing_file = db
            .get_containing_file(func_id)
            .expect("get containing file");
        assert_eq!(containing_file, file_id);
    }

    #[test]
    fn get_ancestors_respects_depth_limit() {
        let (_guard, path) = temp_db("hierarchy_get_ancestors");
        let mut db = GraphDB::open(&path).expect("open db");

        let mut root = Node::new(0);
        root.labels.push("File".to_string());
        let root_id = db.add_node(root).expect("add root");

        let mut module = Node::new(0);
        module.labels.push("Module".to_string());
        let module_id = db.add_node(module).expect("add module");

        let mut class = Node::new(0);
        class.labels.push("Class".to_string());
        let class_id = db.add_node(class).expect("add class");

        let mut func = Node::new(0);
        func.labels.push("Function".to_string());
        let func_id = db.add_node(func).expect("add function");

        db.add_edge(Edge::new(0, root_id, module_id, "CONTAINS"))
            .expect("add root->module");
        db.add_edge(Edge::new(0, module_id, class_id, "CONTAINS"))
            .expect("add module->class");
        db.add_edge(Edge::new(0, class_id, func_id, "CONTAINS"))
            .expect("add class->func");

        let ancestors = db
            .get_ancestors(func_id, "CONTAINS", None)
            .expect("get all ancestors");
        assert_eq!(ancestors, vec![class_id, module_id, root_id]);

        let limited = db
            .get_ancestors(func_id, "CONTAINS", Some(2))
            .expect("get ancestors limited");
        assert_eq!(limited, vec![class_id, module_id]);
    }

    #[test]
    fn get_descendants_respects_depth_limit() {
        let (_guard, path) = temp_db("hierarchy_get_descendants");
        let mut db = GraphDB::open(&path).expect("open db");

        let mut file = Node::new(0);
        file.labels.push("File".to_string());
        let file_id = db.add_node(file).expect("add file");

        let mut class_a = Node::new(0);
        class_a.labels.push("Class".to_string());
        let class_a_id = db.add_node(class_a).expect("add class A");

        let mut class_b = Node::new(0);
        class_b.labels.push("Class".to_string());
        let class_b_id = db.add_node(class_b).expect("add class B");

        let method_a_id = db.add_node(Node::new(0)).expect("add method A");
        let method_b_id = db.add_node(Node::new(0)).expect("add method B");

        db.add_edge(Edge::new(0, file_id, class_a_id, "CONTAINS"))
            .expect("add file->class A");
        db.add_edge(Edge::new(0, file_id, class_b_id, "CONTAINS"))
            .expect("add file->class B");
        db.add_edge(Edge::new(0, class_a_id, method_a_id, "CONTAINS"))
            .expect("add class A->method A");
        db.add_edge(Edge::new(0, class_b_id, method_b_id, "CONTAINS"))
            .expect("add class B->method B");

        let mut descendants = db
            .get_descendants(file_id, "CONTAINS", None)
            .expect("get descendants");
        descendants.sort();
        let mut expected = vec![class_a_id, class_b_id, method_a_id, method_b_id];
        expected.sort();
        assert_eq!(descendants, expected);

        let mut limited = db
            .get_descendants(file_id, "CONTAINS", Some(1))
            .expect("get descendants limited");
        limited.sort();
        let mut expected_limited = vec![class_a_id, class_b_id];
        expected_limited.sort();
        assert_eq!(limited, expected_limited);
    }

    #[test]
    fn get_containing_file_reports_missing() {
        let (_guard, path) = temp_db("hierarchy_missing_file");
        let mut db = GraphDB::open(&path).expect("open db");

        let func_id = db.add_node(Node::new(0)).expect("add function");

        let err = db.get_containing_file(func_id).expect_err("missing file");
        assert!(matches!(err, GraphError::NotFound("containing file")));
    }
}