Skip to main content

cypherlite_query/executor/operators/
subgraph_scan.rs

1// SubgraphScan operator: scans all subgraph entities from SubgraphStore.
2// Each subgraph produces a Record with the variable bound to Value::Subgraph(id).
3
4use crate::executor::{Record, Value};
5use cypherlite_core::SubgraphId;
6use cypherlite_storage::StorageEngine;
7
8/// Scan all subgraphs from the engine.
9/// Each subgraph produces a Record with the variable bound to Value::Subgraph(subgraph_id).
10pub fn execute_subgraph_scan(variable: &str, engine: &StorageEngine) -> Vec<Record> {
11    engine
12        .scan_subgraphs()
13        .into_iter()
14        .map(|sg| {
15            let mut record = Record::new();
16            record.insert(
17                variable.to_string(),
18                Value::Subgraph(SubgraphId(sg.subgraph_id.0)),
19            );
20            record
21        })
22        .collect()
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28    use cypherlite_core::{DatabaseConfig, SyncMode};
29    use tempfile::tempdir;
30
31    fn test_engine(dir: &std::path::Path) -> StorageEngine {
32        let config = DatabaseConfig {
33            path: dir.join("test.cyl"),
34            wal_sync_mode: SyncMode::Normal,
35            ..Default::default()
36        };
37        StorageEngine::open(config).expect("open")
38    }
39
40    #[test]
41    fn test_subgraph_scan_empty() {
42        let dir = tempdir().expect("tempdir");
43        let engine = test_engine(dir.path());
44        let records = execute_subgraph_scan("sg", &engine);
45        assert!(records.is_empty());
46    }
47
48    #[test]
49    fn test_subgraph_scan_multiple() {
50        let dir = tempdir().expect("tempdir");
51        let mut engine = test_engine(dir.path());
52
53        engine.create_subgraph(vec![], None);
54        engine.create_subgraph(vec![], Some(1000));
55        engine.create_subgraph(vec![], None);
56
57        let records = execute_subgraph_scan("sg", &engine);
58        assert_eq!(records.len(), 3);
59
60        for record in &records {
61            assert!(record.contains_key("sg"));
62            assert!(matches!(record.get("sg"), Some(Value::Subgraph(_))));
63        }
64    }
65}