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
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
//! Integration tests for KV engine operations via the SPSC bridge.

use nodedb::bridge::envelope::{PhysicalPlan, Status};
use nodedb::bridge::physical_plan::KvOp;

use crate::helpers::*;

// ---------------------------------------------------------------------------
// Basic CRUD
// ---------------------------------------------------------------------------

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

    // PUT
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Put {
            collection: "cache".into(),
            key: b"key1".to_vec(),
            value: b"value1".to_vec(),
            ttl_ms: 0,
        }),
    );

    // GET
    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Get {
            collection: "cache".into(),
            key: b"key1".to_vec(),
            rls_filters: Vec::new(),
        }),
    );
    assert_eq!(payload, b"value1");

    // DELETE
    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Delete {
            collection: "cache".into(),
            keys: vec![b"key1".to_vec()],
        }),
    );
    let json: serde_json::Value = serde_json::from_slice(&payload).unwrap();
    assert_eq!(json["deleted"], 1);

    // GET after DELETE → NotFound
    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Get {
            collection: "cache".into(),
            key: b"key1".to_vec(),
            rls_filters: Vec::new(),
        }),
    );
    assert_eq!(resp.status, Status::Error);
}

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

    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Put {
            collection: "c".into(),
            key: b"k".to_vec(),
            value: b"v1".to_vec(),
            ttl_ms: 0,
        }),
    );

    // Overwrite.
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Put {
            collection: "c".into(),
            key: b"k".to_vec(),
            value: b"v2".to_vec(),
            ttl_ms: 0,
        }),
    );

    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Get {
            collection: "c".into(),
            key: b"k".to_vec(),
            rls_filters: Vec::new(),
        }),
    );
    assert_eq!(payload, b"v2");
}

// ---------------------------------------------------------------------------
// Batch operations
// ---------------------------------------------------------------------------

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

    let entries: Vec<(Vec<u8>, Vec<u8>)> = (0..5u8).map(|i| (vec![i], vec![i * 10])).collect();

    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::BatchPut {
            collection: "c".into(),
            entries,
            ttl_ms: 0,
        }),
    );
    let json: serde_json::Value = serde_json::from_slice(&payload).unwrap();
    assert_eq!(json["inserted"], 5);

    // BatchGet
    let keys: Vec<Vec<u8>> = (0..5u8).map(|i| vec![i]).collect();
    let _payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::BatchGet {
            collection: "c".into(),
            keys,
        }),
    );
}

// ---------------------------------------------------------------------------
// SCAN
// ---------------------------------------------------------------------------

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

    for i in 0..5u32 {
        send_ok(
            &mut core,
            &mut tx,
            &mut rx,
            PhysicalPlan::Kv(KvOp::Put {
                collection: "scantest".into(),
                key: format!("key{i}").into_bytes(),
                value: format!("val{i}").into_bytes(),
                ttl_ms: 0,
            }),
        );
    }

    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Scan {
            collection: "scantest".into(),
            cursor: Vec::new(),
            count: 100,
            filters: Vec::new(),
            match_pattern: None,
        }),
    );

    let json: serde_json::Value = serde_json::from_slice(&payload).unwrap();
    let entries = json["entries"].as_array().unwrap();
    assert_eq!(entries.len(), 5);
}

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

    for prefix in &["user:", "session:", "user:"] {
        for i in 0..3u32 {
            send_ok(
                &mut core,
                &mut tx,
                &mut rx,
                PhysicalPlan::Kv(KvOp::Put {
                    collection: "mixed".into(),
                    key: format!("{prefix}{i}").into_bytes(),
                    value: b"data".to_vec(),
                    ttl_ms: 0,
                }),
            );
        }
    }

    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Scan {
            collection: "mixed".into(),
            cursor: Vec::new(),
            count: 100,
            filters: Vec::new(),
            match_pattern: Some("user:*".into()),
        }),
    );

    let json: serde_json::Value = serde_json::from_slice(&payload).unwrap();
    let entries = json["entries"].as_array().unwrap();
    // "user:0", "user:1", "user:2" — 3 entries (second batch overwrites first).
    assert_eq!(entries.len(), 3);
}

// ---------------------------------------------------------------------------
// TTL / Expiry
// ---------------------------------------------------------------------------

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

    // PUT without TTL.
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Put {
            collection: "c".into(),
            key: b"k".to_vec(),
            value: b"v".to_vec(),
            ttl_ms: 0,
        }),
    );

    // Set EXPIRE.
    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Expire {
            collection: "c".into(),
            key: b"k".to_vec(),
            ttl_ms: 60_000,
        }),
    );
    assert_eq!(resp.status, Status::Ok);

    // PERSIST removes TTL.
    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Persist {
            collection: "c".into(),
            key: b"k".to_vec(),
        }),
    );
    assert_eq!(resp.status, Status::Ok);

    // Key should still be accessible.
    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Get {
            collection: "c".into(),
            key: b"k".to_vec(),
            rls_filters: Vec::new(),
        }),
    );
    assert_eq!(payload, b"v");
}

// ---------------------------------------------------------------------------
// Secondary indexes
// ---------------------------------------------------------------------------

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

    // Insert entries first.
    let doc1 =
        rmp_serde::to_vec(&serde_json::json!({"region": "us-east", "status": "active"})).unwrap();
    let doc2 =
        rmp_serde::to_vec(&serde_json::json!({"region": "eu-west", "status": "active"})).unwrap();

    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Put {
            collection: "sessions".into(),
            key: b"s1".to_vec(),
            value: doc1,
            ttl_ms: 0,
        }),
    );
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Put {
            collection: "sessions".into(),
            key: b"s2".to_vec(),
            value: doc2,
            ttl_ms: 0,
        }),
    );

    // Register index with backfill.
    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::RegisterIndex {
            collection: "sessions".into(),
            field: "region".into(),
            field_position: 0,
            backfill: true,
        }),
    );
    let json: serde_json::Value = serde_json::from_slice(&payload).unwrap();
    assert_eq!(json["backfilled"], 2);
}

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

    // Register index.
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::RegisterIndex {
            collection: "c".into(),
            field: "status".into(),
            field_position: 0,
            backfill: false,
        }),
    );

    // Insert entry (will be indexed).
    let doc = rmp_serde::to_vec(&serde_json::json!({"status": "active"})).unwrap();
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::Put {
            collection: "c".into(),
            key: b"k1".to_vec(),
            value: doc,
            ttl_ms: 0,
        }),
    );

    // Drop index.
    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Kv(KvOp::DropIndex {
            collection: "c".into(),
            field: "status".into(),
        }),
    );
    let json: serde_json::Value = serde_json::from_slice(&payload).unwrap();
    assert_eq!(json["entries_removed"], 1);
}

// ---------------------------------------------------------------------------
// Tenant isolation
// ---------------------------------------------------------------------------

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

    // Tenant 1 writes.
    let req = nodedb::bridge::envelope::Request {
        tenant_id: nodedb::types::TenantId::new(1),
        ..make_request(PhysicalPlan::Kv(KvOp::Put {
            collection: "shared".into(),
            key: b"k".to_vec(),
            value: b"tenant1".to_vec(),
            ttl_ms: 0,
        }))
    };
    tx.try_push(nodedb::bridge::dispatch::BridgeRequest { inner: req })
        .unwrap();
    core.tick();
    let resp = rx.try_pop().unwrap();
    assert_eq!(resp.inner.status, Status::Ok);

    // Tenant 2 writes same key.
    let req = nodedb::bridge::envelope::Request {
        tenant_id: nodedb::types::TenantId::new(2),
        ..make_request(PhysicalPlan::Kv(KvOp::Put {
            collection: "shared".into(),
            key: b"k".to_vec(),
            value: b"tenant2".to_vec(),
            ttl_ms: 0,
        }))
    };
    tx.try_push(nodedb::bridge::dispatch::BridgeRequest { inner: req })
        .unwrap();
    core.tick();
    let resp = rx.try_pop().unwrap();
    assert_eq!(resp.inner.status, Status::Ok);

    // Tenant 1 reads — should get "tenant1", not "tenant2".
    let req = nodedb::bridge::envelope::Request {
        tenant_id: nodedb::types::TenantId::new(1),
        ..make_request(PhysicalPlan::Kv(KvOp::Get {
            collection: "shared".into(),
            key: b"k".to_vec(),
            rls_filters: Vec::new(),
        }))
    };
    tx.try_push(nodedb::bridge::dispatch::BridgeRequest { inner: req })
        .unwrap();
    core.tick();
    let resp = rx.try_pop().unwrap();
    assert_eq!(resp.inner.status, Status::Ok);
    assert_eq!(resp.inner.payload.to_vec(), b"tenant1");
}