oxide-graph 0.3.0

Semantic knowledge graph for Rust Oxide. In-memory typed nodes + labelled edges, fed by oxide-mirror records and oxide-browser-sh extractions; queryable through pattern + traversal primitives.
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
//! SQLite-backed [`PersistentGraph`].

use std::str::FromStr;

use async_trait::async_trait;
use serde_json::Value;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::{Row, SqlitePool};

use crate::error::{GraphError, Result};
use crate::graph::{Edge, EdgeId, GraphStore, Node, NodeId};

/// Persistent, SQLite-backed knowledge graph.
#[derive(Clone)]
pub struct PersistentGraph {
    pool: SqlitePool,
}

impl PersistentGraph {
    /// Open an in-memory database for testing.
    pub async fn in_memory() -> Result<Self> {
        let options = SqliteConnectOptions::from_str("sqlite::memory:")
            .unwrap()
            .create_if_missing(true);
        let pool = SqlitePoolOptions::new()
            .max_connections(1)
            .connect_with(options)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;
        let store = Self { pool };
        store.migrate().await?;
        Ok(store)
    }

    /// Open or create an on-disk graph at `path`.
    pub async fn open(path: &str) -> Result<Self> {
        let options = SqliteConnectOptions::new()
            .filename(path)
            .create_if_missing(true);
        let pool = SqlitePoolOptions::new()
            .max_connections(5)
            .connect_with(options)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;
        let store = Self { pool };
        store.migrate().await?;
        Ok(store)
    }

    async fn migrate(&self) -> Result<()> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS nodes (
                id TEXT PRIMARY KEY,
                labels TEXT NOT NULL,
                properties TEXT NOT NULL
            )"#,
        )
        .execute(&self.pool)
        .await
        .map_err(|e| GraphError::Other(e.into()))?;

        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS edges (
                id TEXT PRIMARY KEY,
                source TEXT NOT NULL,
                target TEXT NOT NULL,
                label TEXT NOT NULL,
                properties TEXT NOT NULL
            )"#,
        )
        .execute(&self.pool)
        .await
        .map_err(|e| GraphError::Other(e.into()))?;

        sqlx::query("CREATE INDEX IF NOT EXISTS idx_edges_source ON edges(source)")
            .execute(&self.pool)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;

        sqlx::query("CREATE INDEX IF NOT EXISTS idx_edges_target ON edges(target)")
            .execute(&self.pool)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;

        Ok(())
    }
}

#[async_trait]
impl GraphStore for PersistentGraph {
    async fn upsert_node(&self, node: Node) -> Result<()> {
        let labels_json =
            serde_json::to_string(&node.labels).map_err(|e| GraphError::Other(e.into()))?;
        let props_json =
            serde_json::to_string(&node.properties).map_err(|e| GraphError::Other(e.into()))?;

        sqlx::query(
            r#"INSERT INTO nodes (id, labels, properties)
               VALUES (?1, ?2, ?3)
               ON CONFLICT(id) DO UPDATE SET
                 labels = excluded.labels,
                 properties = excluded.properties"#,
        )
        .bind(&node.id)
        .bind(labels_json)
        .bind(props_json)
        .execute(&self.pool)
        .await
        .map_err(|e| GraphError::Other(e.into()))?;
        Ok(())
    }

    async fn add_edge(&self, edge: Edge) -> Result<EdgeId> {
        let props_json =
            serde_json::to_string(&edge.properties).map_err(|e| GraphError::Other(e.into()))?;

        // Ensure nodes exist
        let mut tx = self
            .pool
            .begin()
            .await
            .map_err(|e| GraphError::Other(e.into()))?;
        let source_exists: Option<i64> = sqlx::query_scalar("SELECT 1 FROM nodes WHERE id = ?1")
            .bind(&edge.from)
            .fetch_optional(&mut *tx)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;
        if source_exists.is_none() {
            return Err(GraphError::UnknownNode(edge.from));
        }

        let target_exists: Option<i64> = sqlx::query_scalar("SELECT 1 FROM nodes WHERE id = ?1")
            .bind(&edge.to)
            .fetch_optional(&mut *tx)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;
        if target_exists.is_none() {
            return Err(GraphError::UnknownNode(edge.to));
        }

        sqlx::query(
            r#"INSERT INTO edges (id, source, target, label, properties)
               VALUES (?1, ?2, ?3, ?4, ?5)"#,
        )
        .bind(&edge.id)
        .bind(&edge.from)
        .bind(&edge.to)
        .bind(&edge.label)
        .bind(props_json)
        .execute(&mut *tx)
        .await
        .map_err(|e| GraphError::Other(e.into()))?;

        tx.commit().await.map_err(|e| GraphError::Other(e.into()))?;
        Ok(edge.id)
    }

    async fn get_node(&self, id: &NodeId) -> Result<Option<Node>> {
        let row = sqlx::query("SELECT labels, properties FROM nodes WHERE id = ?1")
            .bind(id)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;

        if let Some(r) = row {
            let labels_str: String = r
                .try_get("labels")
                .map_err(|e| GraphError::Other(e.into()))?;
            let props_str: String = r
                .try_get("properties")
                .map_err(|e| GraphError::Other(e.into()))?;

            let labels: Vec<String> =
                serde_json::from_str(&labels_str).map_err(|e| GraphError::Other(e.into()))?;
            let properties: serde_json::Map<String, Value> =
                serde_json::from_str(&props_str).map_err(|e| GraphError::Other(e.into()))?;

            Ok(Some(Node {
                id: id.clone(),
                labels,
                properties,
            }))
        } else {
            Ok(None)
        }
    }

    async fn get_edge(&self, id: &EdgeId) -> Result<Option<Edge>> {
        let row = sqlx::query("SELECT source, target, label, properties FROM edges WHERE id = ?1")
            .bind(id)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;

        if let Some(r) = row {
            let from: String = r
                .try_get("source")
                .map_err(|e| GraphError::Other(e.into()))?;
            let to: String = r
                .try_get("target")
                .map_err(|e| GraphError::Other(e.into()))?;
            let label: String = r
                .try_get("label")
                .map_err(|e| GraphError::Other(e.into()))?;
            let props_str: String = r
                .try_get("properties")
                .map_err(|e| GraphError::Other(e.into()))?;

            let properties: serde_json::Map<String, Value> =
                serde_json::from_str(&props_str).map_err(|e| GraphError::Other(e.into()))?;

            Ok(Some(Edge {
                id: id.clone(),
                from,
                to,
                label,
                properties,
            }))
        } else {
            Ok(None)
        }
    }

    async fn nodes_by_label(&self, label: &str) -> Result<Vec<Node>> {
        let pattern = format!("%\"{}\"%", label);
        let rows = sqlx::query("SELECT id, labels, properties FROM nodes WHERE labels LIKE ?1")
            .bind(pattern)
            .fetch_all(&self.pool)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;

        let mut nodes = Vec::new();
        for r in rows {
            let id: String = r.try_get("id").map_err(|e| GraphError::Other(e.into()))?;
            let labels_str: String = r
                .try_get("labels")
                .map_err(|e| GraphError::Other(e.into()))?;
            let props_str: String = r
                .try_get("properties")
                .map_err(|e| GraphError::Other(e.into()))?;

            let labels: Vec<String> =
                serde_json::from_str(&labels_str).map_err(|e| GraphError::Other(e.into()))?;
            if labels.contains(&label.to_string()) {
                let properties: serde_json::Map<String, Value> =
                    serde_json::from_str(&props_str).map_err(|e| GraphError::Other(e.into()))?;
                nodes.push(Node {
                    id,
                    labels,
                    properties,
                });
            }
        }
        Ok(nodes)
    }

    async fn edges_from(&self, from: &NodeId, label: Option<&str>) -> Result<Vec<Edge>> {
        let query = if label.is_some() {
            "SELECT id, target, label, properties FROM edges WHERE source = ?1 AND label = ?2"
        } else {
            "SELECT id, target, label, properties FROM edges WHERE source = ?1"
        };

        let mut q = sqlx::query(query).bind(from);
        if let Some(l) = label {
            q = q.bind(l);
        }

        let rows = q
            .fetch_all(&self.pool)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;

        let mut edges = Vec::new();
        for r in rows {
            let id: String = r.try_get("id").map_err(|e| GraphError::Other(e.into()))?;
            let to: String = r
                .try_get("target")
                .map_err(|e| GraphError::Other(e.into()))?;
            let l: String = r
                .try_get("label")
                .map_err(|e| GraphError::Other(e.into()))?;
            let props_str: String = r
                .try_get("properties")
                .map_err(|e| GraphError::Other(e.into()))?;

            let properties: serde_json::Map<String, Value> =
                serde_json::from_str(&props_str).map_err(|e| GraphError::Other(e.into()))?;
            edges.push(Edge {
                id,
                from: from.clone(),
                to,
                label: l,
                properties,
            });
        }
        Ok(edges)
    }

    async fn edges_to(&self, to: &NodeId, label: Option<&str>) -> Result<Vec<Edge>> {
        let query = if label.is_some() {
            "SELECT id, source, label, properties FROM edges WHERE target = ?1 AND label = ?2"
        } else {
            "SELECT id, source, label, properties FROM edges WHERE target = ?1"
        };

        let mut q = sqlx::query(query).bind(to);
        if let Some(l) = label {
            q = q.bind(l);
        }

        let rows = q
            .fetch_all(&self.pool)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;

        let mut edges = Vec::new();
        for r in rows {
            let id: String = r.try_get("id").map_err(|e| GraphError::Other(e.into()))?;
            let from: String = r
                .try_get("source")
                .map_err(|e| GraphError::Other(e.into()))?;
            let l: String = r
                .try_get("label")
                .map_err(|e| GraphError::Other(e.into()))?;
            let props_str: String = r
                .try_get("properties")
                .map_err(|e| GraphError::Other(e.into()))?;

            let properties: serde_json::Map<String, Value> =
                serde_json::from_str(&props_str).map_err(|e| GraphError::Other(e.into()))?;
            edges.push(Edge {
                id,
                from,
                to: to.clone(),
                label: l,
                properties,
            });
        }
        Ok(edges)
    }

    async fn stats(&self) -> Result<(usize, usize)> {
        let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM nodes")
            .fetch_one(&self.pool)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;
        let e: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM edges")
            .fetch_one(&self.pool)
            .await
            .map_err(|e| GraphError::Other(e.into()))?;
        Ok((n as usize, e as usize))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[tokio::test]
    async fn upsert_and_query_by_label() {
        let g = PersistentGraph::in_memory().await.unwrap();
        g.upsert_node(Node::new("pet:1", "pet").with_property("name", json!("Rex")))
            .await
            .unwrap();
        g.upsert_node(Node::new("pet:2", "pet").with_property("name", json!("Buddy")))
            .await
            .unwrap();
        g.upsert_node(Node::new("user:1", "user")).await.unwrap();

        let pets = g.nodes_by_label("pet").await.unwrap();
        assert_eq!(pets.len(), 2);
        let users = g.nodes_by_label("user").await.unwrap();
        assert_eq!(users.len(), 1);
    }

    #[tokio::test]
    async fn edges_link_existing_nodes_only() {
        let g = PersistentGraph::in_memory().await.unwrap();
        g.upsert_node(Node::new("a", "node")).await.unwrap();
        g.upsert_node(Node::new("b", "node")).await.unwrap();
        let id = g.add_edge(Edge::new("a", "b", "links")).await.unwrap();
        assert!(g.get_edge(&id).await.unwrap().is_some());

        let err = g
            .add_edge(Edge::new("a", "missing", "links"))
            .await
            .unwrap_err();
        assert!(matches!(err, GraphError::UnknownNode(_)));
    }

    #[tokio::test]
    async fn directional_edge_queries() {
        let g = PersistentGraph::in_memory().await.unwrap();
        for n in ["a", "b", "c"] {
            g.upsert_node(Node::new(n, "n")).await.unwrap();
        }
        g.add_edge(Edge::new("a", "b", "knows")).await.unwrap();
        g.add_edge(Edge::new("a", "c", "knows")).await.unwrap();
        g.add_edge(Edge::new("b", "c", "owns")).await.unwrap();

        let from_a = g.edges_from(&"a".into(), None).await.unwrap();
        assert_eq!(from_a.len(), 2);
        let from_a_owns = g.edges_from(&"a".into(), Some("owns")).await.unwrap();
        assert_eq!(from_a_owns.len(), 0);
        let to_c = g.edges_to(&"c".into(), None).await.unwrap();
        assert_eq!(to_c.len(), 2);
        let to_c_owns = g.edges_to(&"c".into(), Some("owns")).await.unwrap();
        assert_eq!(to_c_owns.len(), 1);
    }

    #[tokio::test]
    async fn stats_reflect_inserts() {
        let g = PersistentGraph::in_memory().await.unwrap();
        g.upsert_node(Node::new("a", "n")).await.unwrap();
        g.upsert_node(Node::new("b", "n")).await.unwrap();
        g.add_edge(Edge::new("a", "b", "x")).await.unwrap();
        assert_eq!(g.stats().await.unwrap(), (2, 1));
    }
}