ruvector-graph 2.3.1

Distributed Neo4j-compatible hypergraph database with SIMD optimization
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
//! Graph database implementation with concurrent access and indexing

use crate::edge::Edge;
use crate::error::Result;
use crate::hyperedge::{Hyperedge, HyperedgeId};
use crate::index::{AdjacencyIndex, EdgeTypeIndex, HyperedgeNodeIndex, LabelIndex, PropertyIndex};
use crate::node::Node;
#[cfg(feature = "storage")]
use crate::storage::GraphStorage;
use crate::types::{EdgeId, NodeId, PropertyValue};
use dashmap::DashMap;
#[cfg(feature = "storage")]
use std::path::Path;
use std::sync::Arc;

/// High-performance graph database with concurrent access
pub struct GraphDB {
    /// In-memory node storage (DashMap for lock-free concurrent reads)
    nodes: Arc<DashMap<NodeId, Node>>,
    /// In-memory edge storage
    edges: Arc<DashMap<EdgeId, Edge>>,
    /// In-memory hyperedge storage
    hyperedges: Arc<DashMap<HyperedgeId, Hyperedge>>,
    /// Label index for fast label-based lookups
    label_index: LabelIndex,
    /// Property index for fast property-based lookups
    property_index: PropertyIndex,
    /// Edge type index
    edge_type_index: EdgeTypeIndex,
    /// Adjacency index for neighbor lookups
    adjacency_index: AdjacencyIndex,
    /// Hyperedge node index
    hyperedge_node_index: HyperedgeNodeIndex,
    /// Optional persistent storage
    #[cfg(feature = "storage")]
    storage: Option<GraphStorage>,
}

impl GraphDB {
    /// Create a new in-memory graph database
    pub fn new() -> Self {
        Self {
            nodes: Arc::new(DashMap::new()),
            edges: Arc::new(DashMap::new()),
            hyperedges: Arc::new(DashMap::new()),
            label_index: LabelIndex::new(),
            property_index: PropertyIndex::new(),
            edge_type_index: EdgeTypeIndex::new(),
            adjacency_index: AdjacencyIndex::new(),
            hyperedge_node_index: HyperedgeNodeIndex::new(),
            #[cfg(feature = "storage")]
            storage: None,
        }
    }

    /// Create a new graph database with persistent storage
    #[cfg(feature = "storage")]
    pub fn with_storage<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
        let storage = GraphStorage::new(path)?;

        let mut db = Self::new();
        db.storage = Some(storage);

        // Load existing data from storage
        db.load_from_storage()?;

        Ok(db)
    }

    /// Load all data from storage into memory
    #[cfg(feature = "storage")]
    fn load_from_storage(&mut self) -> anyhow::Result<()> {
        if let Some(storage) = &self.storage {
            // Load nodes
            for node_id in storage.all_node_ids()? {
                if let Some(node) = storage.get_node(&node_id)? {
                    self.nodes.insert(node_id.clone(), node.clone());
                    self.label_index.add_node(&node);
                    self.property_index.add_node(&node);
                }
            }

            // Load edges
            for edge_id in storage.all_edge_ids()? {
                if let Some(edge) = storage.get_edge(&edge_id)? {
                    self.edges.insert(edge_id.clone(), edge.clone());
                    self.edge_type_index.add_edge(&edge);
                    self.adjacency_index.add_edge(&edge);
                }
            }

            // Load hyperedges
            for hyperedge_id in storage.all_hyperedge_ids()? {
                if let Some(hyperedge) = storage.get_hyperedge(&hyperedge_id)? {
                    self.hyperedges
                        .insert(hyperedge_id.clone(), hyperedge.clone());
                    self.hyperedge_node_index.add_hyperedge(&hyperedge);
                }
            }
        }
        Ok(())
    }

    // Node operations

    /// Create a node
    pub fn create_node(&self, node: Node) -> Result<NodeId> {
        let id = node.id.clone();

        // Update indexes
        self.label_index.add_node(&node);
        self.property_index.add_node(&node);

        // Insert into memory
        self.nodes.insert(id.clone(), node.clone());

        // Persist to storage if available
        #[cfg(feature = "storage")]
        if let Some(storage) = &self.storage {
            storage.insert_node(&node)?;
        }

        Ok(id)
    }

    /// Get a node by ID
    pub fn get_node(&self, id: impl AsRef<str>) -> Option<Node> {
        self.nodes.get(id.as_ref()).map(|entry| entry.clone())
    }

    /// Borrow a node and apply `f` without cloning it.
    ///
    /// Hot-path accessor for scans that only need to read a node (e.g. vector
    /// scoring). Avoids the full `Node` + embedding clone that `get_node`
    /// incurs. Returns `None` if the node is absent.
    pub fn with_node<R>(&self, id: &str, f: impl FnOnce(&Node) -> R) -> Option<R> {
        self.nodes.get(id).map(|entry| f(entry.value()))
    }

    /// Node ids carrying `label`, straight from the label index (no node clones).
    pub fn node_ids_by_label(&self, label: &str) -> Vec<NodeId> {
        self.label_index.get_nodes_by_label(label)
    }

    /// Delete a node
    pub fn delete_node(&self, id: impl AsRef<str>) -> Result<bool> {
        if let Some((_, node)) = self.nodes.remove(id.as_ref()) {
            // Update indexes
            self.label_index.remove_node(&node);
            self.property_index.remove_node(&node);

            // Delete from storage if available
            #[cfg(feature = "storage")]
            if let Some(storage) = &self.storage {
                storage.delete_node(id.as_ref())?;
            }

            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Atomically update an existing node.
    ///
    /// Applies `f` to a clone of the current node, persists the new value, and
    /// refreshes the label and property indexes. Concurrent updates to the same
    /// node are serialized, so one update cannot silently overwrite another.
    /// The node ID is immutable and changing it returns a constraint error.
    ///
    /// Returns `Ok(false)` if the node was not found (no error), `Ok(true)` if
    /// updated successfully. This is the counterpart to `create_node` and
    /// enables SUPERSEDES-style versioning where a prior node is marked
    /// `deprecated` without deleting it.
    ///
    /// # Example
    ///
    /// ```ignore
    /// graph.update_node(&node_id, |n| {
    ///     n.set_property("status", PropertyValue::from("deprecated"));
    ///     n.set_property("deprecated_at", PropertyValue::from(now_iso8601));
    /// })?;
    /// ```
    ///
    /// The callback runs while the node's map shard is write-locked. It must
    /// not call back into this `GraphDB`, because doing so may deadlock.
    pub fn update_node<F>(&self, id: impl AsRef<str>, f: F) -> Result<bool>
    where
        F: FnOnce(&mut Node),
    {
        let id_ref = id.as_ref();
        let Some(mut entry) = self.nodes.get_mut(id_ref) else {
            return Ok(false);
        };
        let old_node = entry.value().clone();
        let mut new_node = old_node.clone();
        f(&mut new_node);

        if new_node.id != old_node.id {
            return Err(crate::error::GraphError::ConstraintViolation(
                "A node's ID cannot be changed by update_node".to_string(),
            ));
        }

        // Persist before changing memory so a storage error leaves the live
        // node and its indexes untouched.
        #[cfg(feature = "storage")]
        if let Some(storage) = &self.storage {
            storage.insert_node(&new_node)?;
        }

        self.label_index.remove_node(&old_node);
        self.property_index.remove_node(&old_node);
        *entry.value_mut() = new_node.clone();
        self.label_index.add_node(&new_node);
        self.property_index.add_node(&new_node);

        Ok(true)
    }

    /// Keyword (BM25) search over a node text property.
    ///
    /// Builds a transient `Bm25Index` from the `text_field` property of all
    /// nodes carrying `label`, and returns the top-`k` node IDs by BM25 score.
    /// This is the keyword arm of hybrid search — pair with vector ANN for
    /// reciprocal rank fusion.
    ///
    /// For large graphs, build the index once and reuse it; this method
    /// rebuilds on every call (suitable for small-to-medium graphs or
    /// one-shot queries). A cached variant can be added behind a feature
    /// flag if needed.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let hits = graph.keyword_search("Memory", "content", "vector search", 10)?;
    /// ```
    pub fn keyword_search(
        &self,
        label: &str,
        text_field: &str,
        query: &str,
        k: usize,
    ) -> Result<Vec<(NodeId, f32)>> {
        let docs: Vec<(NodeId, String)> = self
            .node_ids_by_label(label)
            .into_iter()
            .filter_map(|id| {
                self.with_node(&id, |node| {
                    node.get_property(text_field).and_then(|value| match value {
                        PropertyValue::String(s) => Some(s.clone()),
                        _ => None,
                    })
                })
                .flatten()
                .map(|text| (id, text))
            })
            .collect();

        if docs.is_empty() {
            return Ok(Vec::new());
        }

        let index = crate::bm25::Bm25Index::build(docs, crate::bm25::Bm25Params::default());
        Ok(index.search(query, k))
    }

    /// Get nodes by label
    pub fn get_nodes_by_label(&self, label: &str) -> Vec<Node> {
        self.label_index
            .get_nodes_by_label(label)
            .into_iter()
            .filter_map(|id| self.get_node(&id))
            .collect()
    }

    /// Get nodes by property
    pub fn get_nodes_by_property(&self, key: &str, value: &PropertyValue) -> Vec<Node> {
        self.property_index
            .get_nodes_by_property(key, value)
            .into_iter()
            .filter_map(|id| self.get_node(&id))
            .collect()
    }

    // Edge operations

    /// Create an edge
    pub fn create_edge(&self, edge: Edge) -> Result<EdgeId> {
        let id = edge.id.clone();

        // Verify nodes exist
        if !self.nodes.contains_key(&edge.from) || !self.nodes.contains_key(&edge.to) {
            return Err(crate::error::GraphError::NodeNotFound(
                "Source or target node not found".to_string(),
            ));
        }

        // Update indexes
        self.edge_type_index.add_edge(&edge);
        self.adjacency_index.add_edge(&edge);

        // Insert into memory
        self.edges.insert(id.clone(), edge.clone());

        // Persist to storage if available
        #[cfg(feature = "storage")]
        if let Some(storage) = &self.storage {
            storage.insert_edge(&edge)?;
        }

        Ok(id)
    }

    /// Get an edge by ID
    pub fn get_edge(&self, id: impl AsRef<str>) -> Option<Edge> {
        self.edges.get(id.as_ref()).map(|entry| entry.clone())
    }

    /// Delete an edge
    pub fn delete_edge(&self, id: impl AsRef<str>) -> Result<bool> {
        if let Some((_, edge)) = self.edges.remove(id.as_ref()) {
            // Update indexes
            self.edge_type_index.remove_edge(&edge);
            self.adjacency_index.remove_edge(&edge);

            // Delete from storage if available
            #[cfg(feature = "storage")]
            if let Some(storage) = &self.storage {
                storage.delete_edge(id.as_ref())?;
            }

            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Delete multiple edges (batch)
    pub fn delete_edges_batch(&self, ids: &[impl AsRef<str>]) -> Result<usize> {
        let mut deleted = 0;
        let mut edges_to_update = Vec::with_capacity(ids.len());

        for id in ids {
            let key: &str = id.as_ref();
            if let Some((_, edge)) = self.edges.remove(key) {
                edges_to_update.push(edge);
                deleted += 1;
            }
        }

        for edge in &edges_to_update {
            self.edge_type_index.remove_edge(edge);
            self.adjacency_index.remove_edge(edge);
        }

        #[cfg(feature = "storage")]
        if let Some(storage) = &self.storage {
            let str_ids = ids.iter().map(|id| id.as_ref()).collect::<Vec<_>>();
            storage.delete_edges_batch(&str_ids)?;
        }

        Ok(deleted)
    }

    /// Get edges by type
    pub fn get_edges_by_type(&self, edge_type: &str) -> Vec<Edge> {
        self.edge_type_index
            .get_edges_by_type(edge_type)
            .into_iter()
            .filter_map(|id| self.get_edge(&id))
            .collect()
    }

    /// Get outgoing edges from a node
    pub fn get_outgoing_edges(&self, node_id: &NodeId) -> Vec<Edge> {
        self.adjacency_index
            .get_outgoing_edges(node_id)
            .into_iter()
            .filter_map(|id| self.get_edge(&id))
            .collect()
    }

    /// Get incoming edges to a node
    pub fn get_incoming_edges(&self, node_id: &NodeId) -> Vec<Edge> {
        self.adjacency_index
            .get_incoming_edges(node_id)
            .into_iter()
            .filter_map(|id| self.get_edge(&id))
            .collect()
    }

    /// Checks whether an edge exists from `from` → `to` with type `edge_type`.
    /// Returns true if found, false otherwise.
    ///
    /// Fast path: avoids cloning `Edge` by reading fields through the `DashMap`
    /// reference guard and short-circuits on first match.
    pub fn has_edge(&self, from: &NodeId, to: &NodeId, edge_type: &str) -> bool {
        self.adjacency_index
            .get_outgoing_edges(from)
            .into_iter()
            .any(|id| {
                self.edges
                    .get(&id)
                    .is_some_and(|e| e.to == *to && e.edge_type == edge_type)
            })
    }

    /// Get outgoing edges for multiple nodes in one call (O(k×avg_degree) vs O(E) for full scan).
    pub fn get_edges_for_nodes(&self, node_ids: &[NodeId]) -> Vec<Edge> {
        let mut result = Vec::with_capacity(node_ids.len() * 4);
        self.adjacency_index
            .for_each_outgoing_edge(node_ids, |edge_id| {
                if let Some(edge) = self.edges.get(edge_id.as_str()) {
                    result.push(edge.clone());
                }
            });

        result
    }

    // Hyperedge operations

    /// Create a hyperedge
    pub fn create_hyperedge(&self, hyperedge: Hyperedge) -> Result<HyperedgeId> {
        let id = hyperedge.id.clone();

        // Verify all nodes exist
        for node_id in &hyperedge.nodes {
            if !self.nodes.contains_key(node_id) {
                return Err(crate::error::GraphError::NodeNotFound(format!(
                    "Node {} not found",
                    node_id
                )));
            }
        }

        // Update index
        self.hyperedge_node_index.add_hyperedge(&hyperedge);

        // Insert into memory
        self.hyperedges.insert(id.clone(), hyperedge.clone());

        // Persist to storage if available
        #[cfg(feature = "storage")]
        if let Some(storage) = &self.storage {
            storage.insert_hyperedge(&hyperedge)?;
        }

        Ok(id)
    }

    /// Get a hyperedge by ID
    pub fn get_hyperedge(&self, id: &HyperedgeId) -> Option<Hyperedge> {
        self.hyperedges.get(id).map(|entry| entry.clone())
    }

    /// Get hyperedges containing a node
    pub fn get_hyperedges_by_node(&self, node_id: &NodeId) -> Vec<Hyperedge> {
        self.hyperedge_node_index
            .get_hyperedges_by_node(node_id)
            .into_iter()
            .filter_map(|id| self.get_hyperedge(&id))
            .collect()
    }

    /// Delete a hyperedge by ID
    pub fn delete_hyperedge(&self, id: &HyperedgeId) -> Result<bool> {
        if let Some((_, hyperedge)) = self.hyperedges.remove(id) {
            self.hyperedge_node_index.remove_hyperedge(&hyperedge);

            #[cfg(feature = "storage")]
            if let Some(storage) = &self.storage {
                storage.delete_hyperedge(id)?;
            }

            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Delete all hyperedges that contain a given node
    pub fn delete_hyperedges_by_node(&self, node_id: &NodeId) -> Result<usize> {
        let ids: Vec<HyperedgeId> = self.hyperedge_node_index.get_hyperedges_by_node(node_id);
        let mut deleted = 0;
        for id in &ids {
            if self.delete_hyperedge(id)? {
                deleted += 1;
            }
        }
        Ok(deleted)
    }

    // Statistics

    /// Get the number of nodes
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Get the number of edges
    pub fn edge_count(&self) -> usize {
        self.edges.len()
    }

    /// Get the number of hyperedges
    pub fn hyperedge_count(&self) -> usize {
        self.hyperedges.len()
    }
}

impl Default for GraphDB {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::edge::EdgeBuilder;
    use crate::hyperedge::HyperedgeBuilder;
    use crate::node::NodeBuilder;
    use std::sync::{Arc, Barrier};

    #[test]
    fn test_graph_creation() {
        let db = GraphDB::new();
        assert_eq!(db.node_count(), 0);
        assert_eq!(db.edge_count(), 0);
    }

    #[test]
    fn test_node_operations() {
        let db = GraphDB::new();

        let node = NodeBuilder::new()
            .label("Person")
            .property("name", "Alice")
            .build();

        let id = db.create_node(node.clone()).unwrap();
        assert_eq!(db.node_count(), 1);

        let retrieved = db.get_node(&id);
        assert!(retrieved.is_some());

        let deleted = db.delete_node(&id).unwrap();
        assert!(deleted);
        assert_eq!(db.node_count(), 0);
    }

    #[test]
    fn test_edge_operations() {
        let db = GraphDB::new();

        let node1 = NodeBuilder::new().build();
        let node2 = NodeBuilder::new().build();

        let id1 = db.create_node(node1.clone()).unwrap();
        let id2 = db.create_node(node2.clone()).unwrap();

        let edge = EdgeBuilder::new(id1.clone(), id2.clone(), "KNOWS")
            .property("since", 2020i64)
            .build();

        let edge_id = db.create_edge(edge).unwrap();
        assert_eq!(db.edge_count(), 1);

        let retrieved = db.get_edge(&edge_id);
        assert!(retrieved.is_some());
    }

    #[test]
    fn test_label_index() {
        let db = GraphDB::new();

        let node1 = NodeBuilder::new().label("Person").build();
        let node2 = NodeBuilder::new().label("Person").build();
        let node3 = NodeBuilder::new().label("Organization").build();

        db.create_node(node1).unwrap();
        db.create_node(node2).unwrap();
        db.create_node(node3).unwrap();

        let people = db.get_nodes_by_label("Person");
        assert_eq!(people.len(), 2);

        let orgs = db.get_nodes_by_label("Organization");
        assert_eq!(orgs.len(), 1);
    }

    #[test]
    fn test_hyperedge_operations() {
        let db = GraphDB::new();

        let node1 = NodeBuilder::new().build();
        let node2 = NodeBuilder::new().build();
        let node3 = NodeBuilder::new().build();

        let id1 = db.create_node(node1).unwrap();
        let id2 = db.create_node(node2).unwrap();
        let id3 = db.create_node(node3).unwrap();

        let hyperedge =
            HyperedgeBuilder::new(vec![id1.clone(), id2.clone(), id3.clone()], "MEETING")
                .description("Team meeting")
                .build();

        let hedge_id = db.create_hyperedge(hyperedge).unwrap();
        assert_eq!(db.hyperedge_count(), 1);

        let hedges = db.get_hyperedges_by_node(&id1);
        assert_eq!(hedges.len(), 1);
    }

    #[test]
    fn test_update_node() {
        let db = GraphDB::new();

        let node = NodeBuilder::new()
            .id("mem-001")
            .label("Memory")
            .property("content", "original content")
            .property("status", "active")
            .build();

        db.create_node(node).unwrap();

        // Update: mark as deprecated
        let updated = db
            .update_node("mem-001", |n| {
                n.set_property("status", PropertyValue::from("deprecated"));
                n.set_property("deprecated_at", PropertyValue::from("2026-07-12T12:00:00Z"));
            })
            .unwrap();
        assert!(updated);

        let retrieved = db.get_node("mem-001").unwrap();
        assert_eq!(
            retrieved.get_property("status").unwrap(),
            &PropertyValue::from("deprecated")
        );
        assert!(retrieved.get_property("deprecated_at").is_some());

        assert!(db
            .get_nodes_by_property("status", &PropertyValue::from("active"))
            .is_empty());
        assert_eq!(
            db.get_nodes_by_property("status", &PropertyValue::from("deprecated"))
                .len(),
            1
        );
    }

    #[test]
    fn test_update_node_refreshes_label_and_property_indexes() {
        let db = GraphDB::new();
        db.create_node(
            NodeBuilder::new()
                .id("indexed")
                .label("OldLabel")
                .property("state", "old")
                .property("removed", true)
                .build(),
        )
        .unwrap();

        db.update_node("indexed", |node| {
            node.remove_label("OldLabel");
            node.add_label("NewLabel");
            node.set_property("state", PropertyValue::from("new"));
            node.properties.remove("removed");
        })
        .unwrap();

        assert!(db.get_nodes_by_label("OldLabel").is_empty());
        assert_eq!(db.get_nodes_by_label("NewLabel").len(), 1);
        assert!(db
            .get_nodes_by_property("state", &PropertyValue::from("old"))
            .is_empty());
        assert_eq!(
            db.get_nodes_by_property("state", &PropertyValue::from("new"))
                .len(),
            1
        );
        assert!(db
            .get_nodes_by_property("removed", &PropertyValue::from(true))
            .is_empty());
    }

    #[test]
    fn test_update_node_rejects_id_changes_without_side_effects() {
        let db = GraphDB::new();
        db.create_node(NodeBuilder::new().id("original").label("Old").build())
            .unwrap();

        let error = db
            .update_node("original", |node| {
                node.id = "replacement".to_string();
                node.add_label("New");
            })
            .unwrap_err();

        assert!(matches!(
            error,
            crate::error::GraphError::ConstraintViolation(_)
        ));
        assert!(db.get_node("replacement").is_none());
        assert!(db.get_node("original").unwrap().has_label("Old"));
        assert!(db.get_nodes_by_label("New").is_empty());
    }

    #[test]
    fn test_concurrent_updates_do_not_lose_writes() {
        const THREADS: usize = 8;
        const UPDATES_PER_THREAD: usize = 100;

        let db = Arc::new(GraphDB::new());
        db.create_node(
            NodeBuilder::new()
                .id("counter")
                .property("value", 0_i64)
                .build(),
        )
        .unwrap();
        let barrier = Arc::new(Barrier::new(THREADS));
        let mut handles = Vec::new();

        for _ in 0..THREADS {
            let db = Arc::clone(&db);
            let barrier = Arc::clone(&barrier);
            handles.push(std::thread::spawn(move || {
                barrier.wait();
                for _ in 0..UPDATES_PER_THREAD {
                    db.update_node("counter", |node| {
                        let value = match node.get_property("value") {
                            Some(PropertyValue::Integer(value)) => *value,
                            _ => panic!("counter property is missing"),
                        };
                        node.set_property("value", PropertyValue::Integer(value + 1));
                    })
                    .unwrap();
                }
            }));
        }

        for handle in handles {
            handle.join().unwrap();
        }
        assert_eq!(
            db.get_node("counter")
                .unwrap()
                .get_property("value")
                .cloned(),
            Some(PropertyValue::Integer(
                (THREADS * UPDATES_PER_THREAD) as i64
            ))
        );
    }

    #[test]
    fn test_update_node_not_found() {
        let db = GraphDB::new();
        let result = db.update_node("nonexistent", |_| {}).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_keyword_search() {
        let db = GraphDB::new();

        let docs = vec![
            ("mem-1", "the quick brown fox jumps over the lazy dog"),
            ("mem-2", "machine learning models for vector search"),
            ("mem-3", "vector databases enable semantic search at scale"),
            ("mem-4", "a recipe for italian pasta with tomato sauce"),
        ];

        for (id, text) in docs {
            let node = NodeBuilder::new()
                .id(id)
                .label("Memory")
                .property("content", text)
                .build();
            db.create_node(node).unwrap();
        }

        let hits = db
            .keyword_search("Memory", "content", "vector search", 4)
            .unwrap();

        assert!(!hits.is_empty());
        // mem-2 and mem-3 both mention "vector" and "search"; pasta doc must not lead.
        assert!(hits[0].0 == "mem-2" || hits[0].0 == "mem-3");
        assert!(hits.iter().all(|(id, _)| id != "mem-4") || hits.last().unwrap().0 == "mem-4");
    }

    #[test]
    fn test_keyword_search_empty_label() {
        let db = GraphDB::new();
        let hits = db
            .keyword_search("Nonexistent", "content", "anything", 5)
            .unwrap();
        assert!(hits.is_empty());
    }

    #[test]
    fn test_keyword_search_ignores_other_labels_and_non_string_fields() {
        let db = GraphDB::new();
        for node in [
            NodeBuilder::new()
                .id("wanted")
                .label("Memory")
                .property("content", "unique needle")
                .build(),
            NodeBuilder::new()
                .id("wrong-label")
                .label("Other")
                .property("content", "unique needle")
                .build(),
            NodeBuilder::new()
                .id("wrong-type")
                .label("Memory")
                .property("content", 42_i64)
                .build(),
        ] {
            db.create_node(node).unwrap();
        }

        let hits = db
            .keyword_search("Memory", "content", "needle", 10)
            .unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].0, "wanted");
        assert!(db
            .keyword_search("Memory", "content", "needle", 0)
            .unwrap()
            .is_empty());
    }

    #[cfg(feature = "storage")]
    #[test]
    fn test_update_node_persists() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("graph.redb");

        {
            let db = GraphDB::with_storage(&path).unwrap();
            db.create_node(
                NodeBuilder::new()
                    .id("persistent")
                    .label("Old")
                    .property("state", "old")
                    .build(),
            )
            .unwrap();
            db.update_node("persistent", |node| {
                node.remove_label("Old");
                node.add_label("New");
                node.set_property("state", PropertyValue::from("new"));
            })
            .unwrap();
        }

        let reopened = GraphDB::with_storage(&path).unwrap();
        let node = reopened.get_node("persistent").unwrap();
        assert!(node.has_label("New"));
        assert_eq!(
            node.get_property("state"),
            Some(&PropertyValue::from("new"))
        );
        assert_eq!(reopened.get_nodes_by_label("New").len(), 1);
        assert_eq!(
            reopened
                .get_nodes_by_property("state", &PropertyValue::from("new"))
                .len(),
            1
        );
    }
}