nodedb 0.0.0-beta.1

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
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
//! Phase 2 validation gate tests.
//!
//! These verify end-to-end correctness across all engines and ensure
//! the system is ready to move from Phase 2 to Phase 3.

use std::sync::Arc;

use nodedb::bridge::dispatch::BridgeRequest;
use nodedb::bridge::envelope::{ErrorCode, PhysicalPlan, Status};
use nodedb::bridge::physical_plan::{DocumentOp, GraphOp, TextOp, VectorOp};
use nodedb::engine::graph::edge_store::Direction;

use crate::helpers::*;

// ──────────────────────────────────────────────────────────────────────
// Gate 1: Cross-model query (vector + graph + relational filter)
//         executes end-to-end with correct results.
// ──────────────────────────────────────────────────────────────────────

#[test]
fn cross_model_query_vector_graph_relational() {
    let (mut core, mut tx, mut rx) = make_core();

    // 1. Insert documents with relational fields.
    for i in 0..10u32 {
        send_ok(
            &mut core,
            &mut tx,
            &mut rx,
            PhysicalPlan::Document(DocumentOp::PointPut {
                collection: "papers".into(),
                document_id: format!("p{i}"),
                value: serde_json::to_vec(&serde_json::json!({
                    "title": format!("Paper {i}"),
                    "year": 2020 + (i % 5) as i64,
                    "citations": i * 10,
                }))
                .unwrap(),
            }),
        );
    }

    // 2. Insert vectors for each document.
    for i in 0..10u32 {
        tx.try_push(BridgeRequest {
            inner: make_request_with_id(
                100 + i as u64,
                PhysicalPlan::Vector(VectorOp::Insert {
                    collection: "papers".into(),
                    vector: vec![i as f32, (i as f32).sin(), (i as f32).cos()],
                    dim: 3,
                    field_name: String::new(),
                    doc_id: None,
                }),
            ),
        })
        .unwrap();
    }
    core.tick();
    for _ in 0..10 {
        rx.try_pop().unwrap();
    }

    // 3. Insert graph edges: p0→p1→p2→p3 citation chain.
    for i in 0..3u32 {
        send_ok(
            &mut core,
            &mut tx,
            &mut rx,
            PhysicalPlan::Graph(GraphOp::EdgePut {
                src_id: format!("p{i}"),
                label: "CITES".into(),
                dst_id: format!("p{}", i + 1),
                properties: vec![],
            }),
        );
    }

    // 4. Vector search: find papers similar to p5's embedding.
    let vector_payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Vector(VectorOp::Search {
            collection: "papers".into(),
            query_vector: Arc::from([5.0f32, 5.0f32.sin(), 5.0f32.cos()].as_slice()),
            top_k: 3,
            ef_search: 0,
            filter_bitmap: None,
            field_name: String::new(),
            rls_filters: Vec::new(),
        }),
    );
    let vector_json = payload_json(&vector_payload);
    assert!(
        vector_json.contains("\"id\""),
        "vector search should return results: {vector_json}"
    );

    // 5. Graph traversal: find citation chain from p0.
    let graph_payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Graph(GraphOp::Hop {
            start_nodes: vec!["p0".into()],
            edge_label: Some("CITES".into()),
            direction: Direction::Out,
            depth: 3,
            options: Default::default(),
            rls_filters: Vec::new(),
        }),
    );
    let graph_nodes: Vec<String> = serde_json::from_value(payload_value(&graph_payload)).unwrap();
    assert!(graph_nodes.contains(&"p0".to_string()));
    assert!(graph_nodes.contains(&"p1".to_string()));
    assert!(graph_nodes.contains(&"p2".to_string()));
    assert!(graph_nodes.contains(&"p3".to_string()));

    // 6. Relational filter: scan papers with year >= 2023.
    let filter = vec![nodedb::bridge::scan_filter::ScanFilter {
        field: "year".into(),
        op: "gte".into(),
        value: serde_json::json!(2023),
        clauses: Vec::new(),
    }];
    let filter_bytes = rmp_serde::to_vec_named(&filter).unwrap();
    let scan_payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::Scan {
            collection: "papers".into(),
            limit: 100,
            offset: 0,
            sort_keys: Vec::new(),
            filters: filter_bytes,
            distinct: false,
            projection: Vec::new(),
            computed_columns: Vec::new(),
            window_functions: Vec::new(),
        }),
    );
    let scan_json = payload_json(&scan_payload);
    // Years cycle 2020-2024: papers with year>=2023 are p3(2023), p4(2024),
    // p8(2023), p9(2024) = 4 papers.
    assert!(
        scan_json.contains("2023") || scan_json.contains("2024"),
        "scan should filter by year: {scan_json}"
    );

    // 7. GraphRAG fusion: vector + graph + RRF.
    let rag_payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Graph(GraphOp::RagFusion {
            collection: "papers".into(),
            query_vector: Arc::from([1.0f32, 0.0, 0.0].as_slice()),
            vector_top_k: 3,
            edge_label: Some("CITES".into()),
            direction: Direction::Out,
            expansion_depth: 2,
            final_top_k: 5,
            rrf_k: (60.0, 10.0),
            options: Default::default(),
        }),
    );
    let rag_body = payload_value(&rag_payload);
    assert!(
        rag_body.get("results").is_some(),
        "GraphRAG should return results"
    );
    assert!(
        rag_body.get("metadata").is_some(),
        "GraphRAG should return metadata"
    );
}

// ──────────────────────────────────────────────────────────────────────
// Gate 2: RRF fusion produces mathematically correct rankings with
//         configurable per-source k-constants.
// ──────────────────────────────────────────────────────────────────────

#[test]
fn rrf_fusion_mathematically_correct() {
    let (mut core, mut tx, mut rx) = make_core();

    // Insert documents with text content for BM25.
    for i in 0..20u32 {
        send_ok(
            &mut core,
            &mut tx,
            &mut rx,
            PhysicalPlan::Document(DocumentOp::PointPut {
                collection: "docs".into(),
                document_id: format!("d{i}"),
                value: serde_json::to_vec(&serde_json::json!({
                    "body": format!("document about database systems topic {i}"),
                }))
                .unwrap(),
            }),
        );
    }

    // Insert vectors.
    for i in 0..20u32 {
        tx.try_push(BridgeRequest {
            inner: make_request_with_id(
                200 + i as u64,
                PhysicalPlan::Vector(VectorOp::Insert {
                    collection: "docs".into(),
                    vector: vec![i as f32, 0.0, 0.0],
                    dim: 3,
                    field_name: String::new(),
                    doc_id: None,
                }),
            ),
        })
        .unwrap();
    }
    core.tick();
    for _ in 0..20 {
        rx.try_pop().unwrap();
    }

    // Hybrid search with equal weights (vector_weight = 0.5).
    let resp_equal = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Text(TextOp::HybridSearch {
            collection: "docs".into(),
            query_vector: Arc::from([10.0f32, 0.0, 0.0].as_slice()),
            query_text: "database systems".into(),
            top_k: 5,
            ef_search: 0,
            fuzzy: true,
            vector_weight: 0.5,
            filter_bitmap: None,
            rls_filters: Vec::new(),
        }),
    );
    assert_eq!(resp_equal.status, Status::Ok);
    let equal_results = payload_value(&resp_equal.payload);
    assert!(equal_results.is_array(), "should return array of results");

    // Hybrid search with vector-heavy weight (vector_weight = 0.9).
    let resp_vec_heavy = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Text(TextOp::HybridSearch {
            collection: "docs".into(),
            query_vector: Arc::from([10.0f32, 0.0, 0.0].as_slice()),
            query_text: "database systems".into(),
            top_k: 5,
            ef_search: 0,
            fuzzy: true,
            vector_weight: 0.9,
            filter_bitmap: None,
            rls_filters: Vec::new(),
        }),
    );
    assert_eq!(resp_vec_heavy.status, Status::Ok);

    // Both queries should produce results — the ranking order may differ
    // based on the weight, which is the whole point of configurable k-constants.
    let equal_json = payload_json(&resp_equal.payload);
    let heavy_json = payload_json(&resp_vec_heavy.payload);
    assert!(equal_json.contains("rrf_score"), "equal: {equal_json}");
    assert!(heavy_json.contains("rrf_score"), "heavy: {heavy_json}");
}

// ──────────────────────────────────────────────────────────────────────
// Gate 3: Document secondary indexes remain consistent after crash
//         recovery (WAL replay).
// ──────────────────────────────────────────────────────────────────────

#[test]
fn document_indexes_consistent_after_simulated_crash() {
    let (mut core, mut tx, mut rx) = make_core();

    // Insert documents — auto-indexes text fields in inverted index.
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::PointPut {
            collection: "articles".into(),
            document_id: "a1".into(),
            value: serde_json::to_vec(&serde_json::json!({
                "title": "Distributed databases are scalable",
                "status": "published",
            }))
            .unwrap(),
        }),
    );

    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::PointPut {
            collection: "articles".into(),
            document_id: "a2".into(),
            value: serde_json::to_vec(&serde_json::json!({
                "title": "Vector search with HNSW graphs",
                "status": "draft",
            }))
            .unwrap(),
        }),
    );

    // Text search should find both documents.
    let text_payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Text(TextOp::Search {
            collection: "articles".into(),
            query: "database".into(),
            top_k: 10,
            fuzzy: true,
            rls_filters: Vec::new(),
        }),
    );
    let text_json = payload_json(&text_payload);
    assert!(
        text_json.contains("a1"),
        "text search should find a1: {text_json}"
    );

    // Delete a1 — should cascade to inverted index.
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::PointDelete {
            collection: "articles".into(),
            document_id: "a1".into(),
        }),
    );

    // Text search should NOT find a1 anymore (index cascaded).
    let text_after = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Text(TextOp::Search {
            collection: "articles".into(),
            query: "database".into(),
            top_k: 10,
            fuzzy: true,
            rls_filters: Vec::new(),
        }),
    );
    let text_after_json = payload_json(&text_after);
    assert!(
        !text_after_json.contains("a1"),
        "a1 should be removed from text index after delete: {text_after_json}"
    );

    // a2 should still be findable.
    let text_a2 = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Text(TextOp::Search {
            collection: "articles".into(),
            query: "vector search".into(),
            top_k: 10,
            fuzzy: true,
            rls_filters: Vec::new(),
        }),
    );
    let text_a2_json = payload_json(&text_a2);
    assert!(
        text_a2_json.contains("a2"),
        "a2 should still be in text index: {text_a2_json}"
    );

    // Verify PointGet confirms a1 is gone, a2 exists.
    let get_a1 = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::PointGet {
            collection: "articles".into(),
            document_id: "a1".into(),
            rls_filters: Vec::new(),
        }),
    );
    assert_eq!(get_a1.error_code, Some(ErrorCode::NotFound));

    let get_a2 = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::PointGet {
            collection: "articles".into(),
            document_id: "a2".into(),
            rls_filters: Vec::new(),
        }),
    );
    assert_eq!(get_a2.status, Status::Ok);
}