tsift-libsql 0.1.80

libSQL graph store backend for tsift
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
use anyhow::{Context, Result};
use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;
use tsift_core::{GraphEdge, GraphNode, GraphPath, GraphStore, SQLITE_GRAPH_SCHEMA_VERSION};

fn block_on<F: std::future::Future>(rt: &tokio::runtime::Runtime, f: F) -> F::Output {
    rt.block_on(f)
}

pub struct LibsqlGraphStore {
    conn: libsql::Connection,
    rt: tokio::runtime::Runtime,
}

impl LibsqlGraphStore {
    pub fn open(db_path: &Path) -> Result<Self> {
        if let Some(parent) = db_path.parent() {
            std::fs::create_dir_all(parent).with_context(|| {
                format!("creating libsql graph substrate dir: {}", parent.display())
            })?;
        }
        let rt = tokio::runtime::Runtime::new().context("creating tokio runtime for libsql")?;
        let db = block_on(&rt, libsql::Builder::new_local(db_path).build())
            .with_context(|| format!("opening libsql graph substrate db: {}", db_path.display()))?;
        let conn = db.connect().with_context(|| {
            format!(
                "connecting to libsql graph substrate db: {}",
                db_path.display()
            )
        })?;
        let store = Self { conn, rt };
        store.init_schema()?;
        Ok(store)
    }

    pub fn open_remote(url: &str, auth_token: &str) -> Result<Self> {
        let rt =
            tokio::runtime::Runtime::new().context("creating tokio runtime for libsql remote")?;
        let db = block_on(
            &rt,
            libsql::Builder::new_remote(url.to_string(), auth_token.to_string()).build(),
        )
        .context("building libsql remote database")?;
        let conn = db
            .connect()
            .context("connecting to libsql remote database")?;
        let store = Self { conn, rt };
        store.init_schema()?;
        Ok(store)
    }

    pub fn in_memory() -> Result<Self> {
        let rt = tokio::runtime::Runtime::new().context("creating tokio runtime for libsql")?;
        let db = block_on(&rt, libsql::Builder::new_local(":memory:").build())
            .context("opening in-memory libsql database")?;
        let conn = db
            .connect()
            .context("connecting to in-memory libsql database")?;
        let store = Self { conn, rt };
        store.init_schema()?;
        Ok(store)
    }

    fn init_schema(&self) -> Result<()> {
        block_on(&self.rt, async {
            self.conn
                .execute_batch(&format!(
                    r#"
                PRAGMA foreign_keys = ON;
                PRAGMA busy_timeout = 5000;

                CREATE TABLE IF NOT EXISTS graph_nodes (
                    id TEXT PRIMARY KEY,
                    kind TEXT NOT NULL,
                    label TEXT NOT NULL,
                    properties_json TEXT NOT NULL DEFAULT '{{}}',
                    provenance_json TEXT NOT NULL DEFAULT '[]',
                    freshness_json TEXT,
                    row_hash TEXT,
                    source_watermark TEXT
                );
                CREATE INDEX IF NOT EXISTS idx_graph_nodes_kind
                    ON graph_nodes(kind);
                CREATE INDEX IF NOT EXISTS idx_graph_nodes_kind_label
                    ON graph_nodes(kind, label, id);

                CREATE TABLE IF NOT EXISTS graph_edges (
                    edge_key TEXT NOT NULL UNIQUE,
                    from_id TEXT NOT NULL,
                    to_id TEXT NOT NULL,
                    kind TEXT NOT NULL,
                    properties_json TEXT NOT NULL DEFAULT '{{}}',
                    provenance_json TEXT NOT NULL DEFAULT '[]',
                    freshness_json TEXT,
                    row_hash TEXT,
                    source_watermark TEXT,
                    PRIMARY KEY (from_id, to_id, kind),
                    FOREIGN KEY (from_id) REFERENCES graph_nodes(id) ON DELETE CASCADE,
                    FOREIGN KEY (to_id) REFERENCES graph_nodes(id) ON DELETE CASCADE
                );
                CREATE INDEX IF NOT EXISTS idx_graph_edges_from_kind
                    ON graph_edges(from_id, kind);
                CREATE INDEX IF NOT EXISTS idx_graph_edges_to_kind
                    ON graph_edges(to_id, kind);

                CREATE TABLE IF NOT EXISTS graph_node_properties (
                    node_id TEXT NOT NULL,
                    key TEXT NOT NULL,
                    value TEXT NOT NULL,
                    PRIMARY KEY (node_id, key),
                    FOREIGN KEY (node_id) REFERENCES graph_nodes(id) ON DELETE CASCADE
                );
                CREATE INDEX IF NOT EXISTS idx_graph_node_properties_key_value_node
                    ON graph_node_properties(key, value, node_id);

                CREATE TABLE IF NOT EXISTS graph_edge_properties (
                    edge_key TEXT NOT NULL,
                    key TEXT NOT NULL,
                    value TEXT NOT NULL,
                    PRIMARY KEY (edge_key, key),
                    FOREIGN KEY (edge_key) REFERENCES graph_edges(edge_key) ON DELETE CASCADE
                );
                CREATE INDEX IF NOT EXISTS idx_graph_edge_properties_key_value_edge
                    ON graph_edge_properties(key, value, edge_key);

                CREATE TABLE IF NOT EXISTS graph_projection_versions (
                    scope TEXT PRIMARY KEY,
                    projection_version TEXT NOT NULL,
                    content_hash TEXT,
                    source_watermark TEXT,
                    observed_at_unix INTEGER NOT NULL
                );

                CREATE TABLE IF NOT EXISTS graph_tombstones (
                    row_key TEXT PRIMARY KEY,
                    row_kind TEXT NOT NULL,
                    deleted_at_unix INTEGER NOT NULL
                );

                PRAGMA user_version = {SQLITE_GRAPH_SCHEMA_VERSION};
                "#,
                ))
                .await
                .context("initializing libsql graph schema")?;
            Ok::<(), anyhow::Error>(())
        })?;
        Ok(())
    }
}

fn to_json<T: serde::Serialize>(value: &T) -> Result<String> {
    serde_json::to_string(value).map_err(Into::into)
}

fn optional_to_json<T: serde::Serialize>(value: &Option<T>) -> Result<Option<String>> {
    value.as_ref().map(to_json).transpose()
}

fn node_from_row(row: &libsql::Row) -> Result<GraphNode> {
    let id: String = row.get(0)?;
    let kind: String = row.get(1)?;
    let label: String = row.get(2)?;
    let properties_json: String = row.get(3)?;
    let provenance_json: String = row.get(4)?;
    let freshness_json: Option<String> = row.get(5)?;
    Ok(GraphNode {
        id,
        kind,
        label,
        properties: serde_json::from_str(&properties_json)?,
        provenance: serde_json::from_str(&provenance_json)?,
        freshness: freshness_json
            .map(|v| serde_json::from_str(&v))
            .transpose()?,
    })
}

fn edge_from_row(row: &libsql::Row) -> Result<GraphEdge> {
    let edge_key: String = row.get(0)?;
    let from_id: String = row.get(1)?;
    let to_id: String = row.get(2)?;
    let kind: String = row.get(3)?;
    let properties_json: String = row.get(4)?;
    let provenance_json: String = row.get(5)?;
    let freshness_json: Option<String> = row.get(6)?;
    Ok(GraphEdge {
        id: edge_key,
        from_id,
        to_id,
        kind,
        properties: serde_json::from_str(&properties_json)?,
        provenance: serde_json::from_str(&provenance_json)?,
        freshness: freshness_json
            .map(|v| serde_json::from_str(&v))
            .transpose()?,
    })
}

fn stable_graph_edge_id(from_id: &str, to_id: &str, kind: &str) -> String {
    let raw = serde_json::json!([from_id, kind, to_id]).to_string();
    format!("edge:{}", blake3::hash(raw.as_bytes()).to_hex())
}

fn row_hash<T: serde::Serialize>(value: &T) -> Result<String> {
    let payload = serde_json::to_vec(value)?;
    Ok(blake3::hash(&payload).to_hex().to_string())
}

fn replace_node_properties(
    conn: &libsql::Connection,
    rt: &tokio::runtime::Runtime,
    node_id: &str,
    properties: &BTreeMap<String, String>,
) -> Result<()> {
    block_on(rt, async {
        conn.execute(
            "DELETE FROM graph_node_properties WHERE node_id = ?1",
            [node_id],
        )
        .await?;
        let stmt = conn
            .prepare("INSERT INTO graph_node_properties (node_id, key, value) VALUES (?1, ?2, ?3)")
            .await?;
        for (key, value) in properties {
            stmt.execute(libsql::params![
                node_id.to_string(),
                key.clone(),
                value.clone()
            ])
            .await?;
        }
        Ok::<(), anyhow::Error>(())
    })?;
    Ok(())
}

fn replace_edge_properties(
    conn: &libsql::Connection,
    rt: &tokio::runtime::Runtime,
    edge_key: &str,
    properties: &BTreeMap<String, String>,
) -> Result<()> {
    block_on(rt, async {
        conn.execute(
            "DELETE FROM graph_edge_properties WHERE edge_key = ?1",
            [edge_key.to_string()],
        )
        .await?;
        let stmt = conn
            .prepare("INSERT INTO graph_edge_properties (edge_key, key, value) VALUES (?1, ?2, ?3)")
            .await?;
        for (key, value) in properties {
            stmt.execute(libsql::params![
                edge_key.to_string(),
                key.clone(),
                value.clone()
            ])
            .await?;
        }
        Ok::<(), anyhow::Error>(())
    })?;
    Ok(())
}

impl GraphStore for LibsqlGraphStore {
    fn upsert_node(&self, node: &GraphNode) -> Result<()> {
        let id = node.id.clone();
        block_on(&self.rt, async {
            let properties_json = to_json(&node.properties)?;
            let provenance_json = to_json(&node.provenance)?;
            let freshness_json = optional_to_json(&node.freshness)?;
            let hash = row_hash(node)?;
            self.conn.execute(
                r#"
                INSERT INTO graph_nodes
                    (id, kind, label, properties_json, provenance_json, freshness_json, row_hash, source_watermark)
                VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, NULL)
                ON CONFLICT(id) DO UPDATE SET
                    kind = excluded.kind,
                    label = excluded.label,
                    properties_json = excluded.properties_json,
                    provenance_json = excluded.provenance_json,
                    freshness_json = excluded.freshness_json,
                    row_hash = excluded.row_hash,
                    source_watermark = excluded.source_watermark
                "#,
                libsql::params![node.id.clone(), node.kind.clone(), node.label.clone(), properties_json, provenance_json, freshness_json, hash],
            ).await?;
            Ok::<(), anyhow::Error>(())
        })?;
        replace_node_properties(&self.conn, &self.rt, &id, &node.properties)?;
        Ok(())
    }

    fn upsert_edge(&self, edge: &GraphEdge) -> Result<()> {
        let edge_key = if edge.id.is_empty() {
            stable_graph_edge_id(&edge.from_id, &edge.to_id, &edge.kind)
        } else {
            edge.id.clone()
        };
        let edge_key_for_props = edge_key.clone();
        block_on(&self.rt, async {
            let properties_json = to_json(&edge.properties)?;
            let provenance_json = to_json(&edge.provenance)?;
            let freshness_json = optional_to_json(&edge.freshness)?;
            let hash = row_hash(edge)?;
            self.conn.execute(
                r#"
                INSERT INTO graph_edges
                    (edge_key, from_id, to_id, kind, properties_json, provenance_json, freshness_json, row_hash, source_watermark)
                VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, NULL)
                ON CONFLICT(from_id, to_id, kind) DO UPDATE SET
                    edge_key = excluded.edge_key,
                    properties_json = excluded.properties_json,
                    provenance_json = excluded.provenance_json,
                    freshness_json = excluded.freshness_json,
                    row_hash = excluded.row_hash,
                    source_watermark = excluded.source_watermark
                "#,
                libsql::params![edge_key, edge.from_id.clone(), edge.to_id.clone(), edge.kind.clone(), properties_json, provenance_json, freshness_json, hash],
            ).await?;
            Ok::<(), anyhow::Error>(())
        })?;
        replace_edge_properties(&self.conn, &self.rt, &edge_key_for_props, &edge.properties)?;
        Ok(())
    }

    fn delete_node(&self, id: &str) -> Result<usize> {
        let count = block_on(&self.rt, async {
            let result = self
                .conn
                .execute("DELETE FROM graph_nodes WHERE id = ?1", [id])
                .await?;
            Ok::<u64, anyhow::Error>(result)
        })?;
        Ok(count as usize)
    }

    fn delete_edge(&self, from_id: &str, to_id: &str, kind: &str) -> Result<usize> {
        let count = block_on(&self.rt, async {
            let result = self
                .conn
                .execute(
                    "DELETE FROM graph_edges WHERE from_id = ?1 AND to_id = ?2 AND kind = ?3",
                    libsql::params![from_id, to_id, kind],
                )
                .await?;
            Ok::<u64, anyhow::Error>(result)
        })?;
        Ok(count as usize)
    }

    fn node(&self, id: &str) -> Result<Option<GraphNode>> {
        block_on(&self.rt, async {
            let mut rows = self
                .conn
                .query(
                    r#"
                SELECT id, kind, label, properties_json, provenance_json, freshness_json
                FROM graph_nodes
                WHERE id = ?1
                "#,
                    [id],
                )
                .await?;
            match rows.next().await? {
                Some(row) => Ok(Some(node_from_row(&row)?)),
                None => Ok(None),
            }
        })
    }

    fn all_nodes(&self) -> Result<Vec<GraphNode>> {
        block_on(&self.rt, async {
            let mut rows = self
                .conn
                .query(
                    r#"
                SELECT id, kind, label, properties_json, provenance_json, freshness_json
                FROM graph_nodes
                ORDER BY id
                "#,
                    (),
                )
                .await?;
            let mut nodes = Vec::new();
            while let Some(row) = rows.next().await? {
                nodes.push(node_from_row(&row)?);
            }
            Ok(nodes)
        })
    }

    fn all_edges(&self) -> Result<Vec<GraphEdge>> {
        block_on(&self.rt, async {
            let mut rows = self.conn.query(
                r#"
                SELECT edge_key, from_id, to_id, kind, properties_json, provenance_json, freshness_json
                FROM graph_edges
                ORDER BY from_id, kind, to_id
                "#,
                (),
            ).await?;
            let mut edges = Vec::new();
            while let Some(row) = rows.next().await? {
                edges.push(edge_from_row(&row)?);
            }
            Ok(edges)
        })
    }

    fn edge(&self, edge_id: &str) -> Result<Option<GraphEdge>> {
        block_on(&self.rt, async {
            let mut rows = self
                .conn
                .query(
                    r#"
                    SELECT edge_key, from_id, to_id, kind, properties_json, provenance_json, freshness_json
                    FROM graph_edges
                    WHERE edge_key = ?1
                    "#,
                    [edge_id],
                )
                .await?;
            match rows.next().await? {
                Some(row) => Ok(Some(edge_from_row(&row)?)),
                None => Ok(None),
            }
        })
    }

    fn graph_counts(&self) -> Result<(usize, usize)> {
        let nodes = block_on(&self.rt, async {
            let mut rows = self
                .conn
                .query("SELECT COUNT(*) FROM graph_nodes", ())
                .await?;
            let row = rows.next().await?.context("no row")?;
            let count: u64 = row.get(0)?;
            Ok::<u64, anyhow::Error>(count)
        })?;
        let edges = block_on(&self.rt, async {
            let mut rows = self
                .conn
                .query("SELECT COUNT(*) FROM graph_edges", ())
                .await?;
            let row = rows.next().await?.context("no row")?;
            let count: u64 = row.get(0)?;
            Ok::<u64, anyhow::Error>(count)
        })?;
        Ok((nodes as usize, edges as usize))
    }

    fn nodes_by_kind(&self, kind: &str) -> Result<Vec<GraphNode>> {
        block_on(&self.rt, async {
            let mut rows = self
                .conn
                .query(
                    r#"
                SELECT id, kind, label, properties_json, provenance_json, freshness_json
                FROM graph_nodes
                WHERE kind = ?1
                ORDER BY id
                "#,
                    [kind],
                )
                .await?;
            let mut nodes = Vec::new();
            while let Some(row) = rows.next().await? {
                nodes.push(node_from_row(&row)?);
            }
            Ok(nodes)
        })
    }

    fn outgoing_edges(&self, from_id: &str, kind: Option<&str>) -> Result<Vec<GraphEdge>> {
        block_on(&self.rt, async {
            let mut edges = Vec::new();
            match kind {
                Some(kind) => {
                    let mut rows = self.conn.query(
                        r#"
                        SELECT edge_key, from_id, to_id, kind, properties_json, provenance_json, freshness_json
                        FROM graph_edges
                        WHERE from_id = ?1 AND kind = ?2
                        ORDER BY to_id, kind
                        "#,
                        libsql::params![from_id, kind],
                    ).await?;
                    while let Some(row) = rows.next().await? {
                        edges.push(edge_from_row(&row)?);
                    }
                }
                None => {
                    let mut rows = self.conn.query(
                        r#"
                        SELECT edge_key, from_id, to_id, kind, properties_json, provenance_json, freshness_json
                        FROM graph_edges
                        WHERE from_id = ?1
                        ORDER BY to_id, kind
                        "#,
                        [from_id],
                    ).await?;
                    while let Some(row) = rows.next().await? {
                        edges.push(edge_from_row(&row)?);
                    }
                }
            }
            Ok(edges)
        })
    }

    fn incident_edges(&self, node_id: &str, kind: Option<&str>) -> Result<Vec<GraphEdge>> {
        block_on(&self.rt, async {
            let sql = match kind {
                Some(_) => {
                    r#"
                    SELECT edge_key, from_id, to_id, kind, properties_json, provenance_json, freshness_json
                    FROM (
                        SELECT edge_key, from_id, to_id, kind, properties_json, provenance_json, freshness_json
                        FROM graph_edges
                        WHERE from_id = ?1 AND kind = ?2
                        UNION
                        SELECT edge_key, from_id, to_id, kind, properties_json, provenance_json, freshness_json
                        FROM graph_edges
                        WHERE to_id = ?1 AND kind = ?2
                    ) e
                    ORDER BY e.edge_key
                    "#
                }
                None => {
                    r#"
                    SELECT edge_key, from_id, to_id, kind, properties_json, provenance_json, freshness_json
                    FROM (
                        SELECT edge_key, from_id, to_id, kind, properties_json, provenance_json, freshness_json
                        FROM graph_edges
                        WHERE from_id = ?1
                        UNION
                        SELECT edge_key, from_id, to_id, kind, properties_json, provenance_json, freshness_json
                        FROM graph_edges
                        WHERE to_id = ?1
                    ) e
                    ORDER BY e.edge_key
                    "#
                }
            };
            let mut edges = Vec::new();
            match kind {
                Some(kind) => {
                    let mut rows = self.conn.query(sql, libsql::params![node_id, kind]).await?;
                    while let Some(row) = rows.next().await? {
                        edges.push(edge_from_row(&row)?);
                    }
                }
                None => {
                    let mut rows = self.conn.query(sql, [node_id]).await?;
                    while let Some(row) = rows.next().await? {
                        edges.push(edge_from_row(&row)?);
                    }
                }
            }
            Ok(edges)
        })
    }

    fn edges_between_nodes(&self, node_ids: &BTreeSet<String>) -> Result<Vec<GraphEdge>> {
        if node_ids.is_empty() {
            return Ok(Vec::new());
        }
        block_on(&self.rt, async {
            self.conn
                .execute_batch(
                    r#"
                CREATE TEMP TABLE IF NOT EXISTS _edges_between_ids (id TEXT PRIMARY KEY);
                DELETE FROM _edges_between_ids;
                "#,
                )
                .await?;
            for chunk in node_ids.iter().collect::<Vec<_>>().chunks(450) {
                let row_placeholders: Vec<String> =
                    chunk.iter().map(|_| "(?)".to_string()).collect();
                let placeholders = row_placeholders.join(", ");
                let sql =
                    format!("INSERT OR IGNORE INTO _edges_between_ids (id) VALUES {placeholders}");
                let values: Vec<String> = chunk.iter().map(|id| (*id).clone()).collect();
                self.conn
                    .execute(
                        &sql,
                        libsql::params_from_iter(values.iter().map(|v| v.as_str())),
                    )
                    .await?;
            }
            let mut rows = self
                .conn
                .query(
                    r#"
                SELECT e.edge_key, e.from_id, e.to_id, e.kind, e.properties_json, e.provenance_json, e.freshness_json
                FROM graph_edges e
                WHERE EXISTS (SELECT 1 FROM _edges_between_ids f WHERE f.id = e.from_id)
                  AND EXISTS (SELECT 1 FROM _edges_between_ids t WHERE t.id = e.to_id)
                ORDER BY e.from_id, e.kind, e.to_id
                "#,
                    (),
                )
                .await?;
            let mut edges = Vec::new();
            while let Some(row) = rows.next().await? {
                edges.push(edge_from_row(&row)?);
            }
            Ok::<Vec<GraphEdge>, anyhow::Error>(edges)
        })
    }

    fn shortest_path(
        &self,
        from_id: &str,
        to_id: &str,
        kind: Option<&str>,
    ) -> Result<Option<GraphPath>> {
        self.shortest_path_with_max_hops(from_id, to_id, kind, None)
    }

    fn shortest_path_with_max_hops(
        &self,
        from_id: &str,
        to_id: &str,
        kind: Option<&str>,
        max_hops: Option<usize>,
    ) -> Result<Option<GraphPath>> {
        if from_id == to_id {
            return Ok(Some(GraphPath {
                nodes: vec![from_id.to_string()],
                hops: 0,
            }));
        }
        let hop_limit = max_hops.unwrap_or(usize::MAX);
        if hop_limit == 0 {
            return Ok(None);
        }

        let mut visited = BTreeSet::from([from_id.to_string()]);
        let mut parent = BTreeMap::<String, String>::from([(from_id.to_string(), String::new())]);
        let mut frontier = vec![from_id.to_string()];

        for _depth in 0..hop_limit {
            if frontier.is_empty() {
                break;
            }
            let mut next_frontier = BTreeSet::new();
            for current in &frontier {
                let neighbors = self.outgoing_edges(current, kind)?;
                for edge in neighbors {
                    if !visited.insert(edge.to_id.clone()) {
                        continue;
                    }
                    parent.insert(edge.to_id.clone(), current.clone());
                    if edge.to_id == to_id {
                        let mut nodes = vec![to_id.to_string()];
                        let mut cursor = to_id;
                        while let Some(previous) = parent.get(cursor) {
                            if previous.is_empty() {
                                break;
                            }
                            nodes.push(previous.clone());
                            cursor = previous;
                        }
                        nodes.reverse();
                        return Ok(Some(GraphPath {
                            hops: nodes.len().saturating_sub(1),
                            nodes,
                        }));
                    }
                    next_frontier.insert(edge.to_id);
                }
            }
            frontier = next_frontier.into_iter().collect();
        }
        Ok(None)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tsift_core::{GraphFreshness, GraphProjection, GraphProvenance};

    fn sample_provenance() -> GraphProvenance {
        GraphProvenance::new("fixture", "src/lib.rs:1").with_content_hash("hash-1")
    }

    fn sample_projection() -> GraphProjection {
        let source = sample_provenance();
        GraphProjection {
            nodes: vec![
                GraphNode::new("doc:livekit", "document", "LiveKit guide")
                    .with_property("domain", "livekit")
                    .with_provenance(source.clone())
                    .with_freshness(GraphFreshness::content_hash("node-hash")),
                GraphNode::new("topic:rooms", "topic", "Rooms"),
                GraphNode::new("topic:egress", "topic", "Egress"),
            ],
            edges: vec![
                GraphEdge::new("doc:livekit", "topic:rooms", "mentions")
                    .with_property("confidence", "0.91")
                    .with_provenance(source.clone())
                    .with_freshness(GraphFreshness::content_hash("edge-hash")),
                GraphEdge::new("topic:rooms", "topic:egress", "related_to").with_provenance(source),
            ],
        }
    }

    #[test]
    fn libsql_store_round_trips_generic_nodes_edges() {
        let store = LibsqlGraphStore::in_memory().unwrap();
        let source = sample_provenance();
        let node = GraphNode::new("doc:livekit", "document", "LiveKit guide")
            .with_property("domain", "livekit")
            .with_provenance(source.clone())
            .with_freshness(GraphFreshness::content_hash("node-hash"));
        let topic = GraphNode::new("topic:rooms", "topic", "Rooms");
        let edge = GraphEdge::new("doc:livekit", "topic:rooms", "mentions")
            .with_property("confidence", "0.91")
            .with_provenance(source)
            .with_freshness(GraphFreshness::content_hash("edge-hash"));

        store.upsert_node(&node).unwrap();
        store.upsert_node(&topic).unwrap();
        store.upsert_edge(&edge).unwrap();

        assert_eq!(store.node("doc:livekit").unwrap(), Some(node));
        assert_eq!(store.nodes_by_kind("topic").unwrap(), vec![topic]);
        assert_eq!(store.all_nodes().unwrap().len(), 2);
        assert_eq!(store.all_edges().unwrap().len(), 1);
        assert_eq!(
            store
                .outgoing_edges("doc:livekit", Some("mentions"))
                .unwrap(),
            vec![edge]
        );
    }

    #[test]
    fn libsql_store_supports_projection_upsert() {
        let store = LibsqlGraphStore::in_memory().unwrap();
        let projection = sample_projection();
        projection.upsert_into(&store).unwrap();

        assert_eq!(store.node("doc:livekit").unwrap().unwrap().kind, "document");
        assert_eq!(store.nodes_by_kind("topic").unwrap().len(), 2);
        let mentions = store
            .outgoing_edges("doc:livekit", Some("mentions"))
            .unwrap();
        assert_eq!(mentions.len(), 1);
        assert_eq!(mentions[0].to_id, "topic:rooms");
    }

    #[test]
    fn libsql_store_crud_neighborhood_and_ordering() {
        let store = LibsqlGraphStore::in_memory().unwrap();
        let projection = sample_projection();
        projection.upsert_into(&store).unwrap();

        let neighborhood = store.neighborhood("doc:livekit", 2, None).unwrap().unwrap();
        let node_ids: Vec<&str> = neighborhood.nodes.iter().map(|n| n.id.as_str()).collect();
        assert_eq!(node_ids, vec!["doc:livekit", "topic:egress", "topic:rooms"]);

        assert_eq!(
            store
                .delete_edge("topic:rooms", "topic:egress", "related_to")
                .unwrap(),
            1
        );
        assert!(
            store
                .shortest_path("doc:livekit", "topic:egress", None)
                .unwrap()
                .is_none()
        );
        assert_eq!(store.delete_node("topic:rooms").unwrap(), 1);
        assert!(store.node("topic:rooms").unwrap().is_none());
        assert!(
            store
                .outgoing_edges("doc:livekit", None)
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn libsql_store_shortest_path() {
        let store = LibsqlGraphStore::in_memory().unwrap();
        for id in ["a", "b", "c"] {
            store
                .upsert_node(&GraphNode::new(id, "symbol", id))
                .unwrap();
        }
        store
            .upsert_edge(&GraphEdge::new("a", "b", "calls"))
            .unwrap();
        store
            .upsert_edge(&GraphEdge::new("a", "c", "documents"))
            .unwrap();
        store
            .upsert_edge(&GraphEdge::new("b", "c", "calls"))
            .unwrap();

        let calls = store.outgoing_edges("a", Some("calls")).unwrap();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].to_id, "b");

        let path = store
            .shortest_path("a", "c", Some("calls"))
            .unwrap()
            .unwrap();
        assert_eq!(path.nodes, vec!["a", "b", "c"]);
        assert_eq!(path.hops, 2);

        assert!(
            store
                .shortest_path("c", "a", Some("calls"))
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn libsql_store_open_creates_db_file() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("test-graph.db");
        let store = LibsqlGraphStore::open(&db_path).unwrap();
        store
            .upsert_node(&GraphNode::new("test", "test", "test"))
            .unwrap();
        assert!(db_path.exists());
    }

    #[test]
    fn libsql_graph_counts() {
        let store = LibsqlGraphStore::in_memory().unwrap();
        for id in ["a", "b", "c"] {
            store
                .upsert_node(&GraphNode::new(id, "symbol", id))
                .unwrap();
        }
        store
            .upsert_edge(&GraphEdge::new("a", "b", "calls"))
            .unwrap();
        store
            .upsert_edge(&GraphEdge::new("b", "c", "calls"))
            .unwrap();
        let (nodes, edges) = store.graph_counts().unwrap();
        assert_eq!(nodes, 3);
        assert_eq!(edges, 2);
    }

    #[test]
    fn libsql_incident_edges_pushdown() {
        let store = LibsqlGraphStore::in_memory().unwrap();
        for id in ["a", "b", "c", "d"] {
            store
                .upsert_node(&GraphNode::new(id, "symbol", id))
                .unwrap();
        }
        store
            .upsert_edge(&GraphEdge::new("a", "b", "calls"))
            .unwrap();
        store
            .upsert_edge(&GraphEdge::new("a", "c", "documents"))
            .unwrap();
        store
            .upsert_edge(&GraphEdge::new("d", "b", "calls"))
            .unwrap();
        store
            .upsert_edge(&GraphEdge::new("c", "b", "references"))
            .unwrap();

        let all_incident = store.incident_edges("b", None).unwrap();
        assert_eq!(all_incident.len(), 3);

        let calls_incident = store.incident_edges("b", Some("calls")).unwrap();
        assert_eq!(calls_incident.len(), 2);
        assert!(calls_incident.iter().all(|e| e.kind == "calls"));

        let docs_incident = store.incident_edges("b", Some("documents")).unwrap();
        assert!(docs_incident.is_empty());

        let a_incident = store.incident_edges("a", None).unwrap();
        assert_eq!(a_incident.len(), 2);
        assert!(a_incident.iter().all(|e| e.from_id == "a"));

        let d_incident = store.incident_edges("d", None).unwrap();
        assert_eq!(d_incident.len(), 1);
        assert_eq!(d_incident[0].to_id, "b");
    }

    #[test]
    fn libsql_store_edges_between_nodes_pushdown() {
        let store = LibsqlGraphStore::in_memory().unwrap();
        for id in ["a", "b", "c", "outside"] {
            store
                .upsert_node(&GraphNode::new(id, "symbol", id))
                .unwrap();
        }
        for edge in [
            GraphEdge::new("a", "b", "calls"),
            GraphEdge::new("b", "c", "calls"),
            GraphEdge::new("a", "outside", "calls"),
            GraphEdge::new("outside", "c", "calls"),
        ] {
            store.upsert_edge(&edge).unwrap();
        }

        let scoped = ["a".to_string(), "b".to_string(), "c".to_string()]
            .into_iter()
            .collect::<BTreeSet<_>>();
        let edge_keys = store
            .edges_between_nodes(&scoped)
            .unwrap()
            .into_iter()
            .map(|edge| (edge.from_id, edge.kind, edge.to_id))
            .collect::<Vec<_>>();

        assert_eq!(
            edge_keys,
            vec![
                ("a".to_string(), "calls".to_string(), "b".to_string()),
                ("b".to_string(), "calls".to_string(), "c".to_string()),
            ]
        );

        let empty: BTreeSet<String> = BTreeSet::new();
        assert!(store.edges_between_nodes(&empty).unwrap().is_empty());

        let single = ["a".to_string()].into_iter().collect::<BTreeSet<_>>();
        assert!(store.edges_between_nodes(&single).unwrap().is_empty());
    }
}