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
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
use crate::engine::sparse::btree::SparseEngine;

/// Index path declaration for automatic secondary index extraction.
///
/// Example: `IndexPath::new("$.user.email")` extracts the `user.email` field
/// from each document and indexes it in redb for efficient lookups.
#[derive(Debug, Clone)]
pub struct IndexPath {
    /// JSON-path-like expression (e.g., `$.user.email`, `$.tags[]`).
    pub path: String,
    /// Whether this path extracts array elements individually.
    pub is_array: bool,
}

impl IndexPath {
    pub fn new(path: &str) -> Self {
        let is_array = path.ends_with("[]");
        let path = path.strip_suffix("[]").unwrap_or(path).to_string();
        Self { path, is_array }
    }
}

/// Collection configuration for the Document Engine.
#[derive(Debug, Clone)]
pub struct CollectionConfig {
    pub name: String,
    /// Declared secondary index paths.
    pub index_paths: Vec<IndexPath>,
    /// Whether this collection uses CRDT-backed storage (Loro).
    pub crdt_enabled: bool,
}

impl CollectionConfig {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            index_paths: Vec::new(),
            crdt_enabled: false,
        }
    }

    pub fn with_index(mut self, path: &str) -> Self {
        self.index_paths.push(IndexPath::new(path));
        self
    }

    pub fn with_crdt(mut self) -> Self {
        self.crdt_enabled = true;
        self
    }
}

/// Document Engine.
///
/// NOT a separate storage engine — extends the existing SparseEngine (redb)
/// with MessagePack encoding and automatic secondary index extraction.
///
/// Documents are stored as MessagePack-encoded blobs keyed by
/// `(collection, doc_id)`. On write, declared index paths are extracted
/// and stored in redb B-Trees as `(collection, path, value) -> doc_id`.
pub struct DocumentEngine<'a> {
    sparse: &'a SparseEngine,
    tenant_id: u32,
    configs: std::collections::HashMap<String, CollectionConfig>,
}

impl<'a> DocumentEngine<'a> {
    pub fn new(sparse: &'a SparseEngine, tenant_id: u32) -> Self {
        Self {
            sparse,
            tenant_id,
            configs: std::collections::HashMap::new(),
        }
    }

    /// Register a collection configuration with index paths.
    pub fn register_collection(&mut self, config: CollectionConfig) {
        self.configs.insert(config.name.clone(), config);
    }

    /// Put a document (JSON value) into a collection.
    ///
    /// The document is serialized to MessagePack and stored in redb.
    /// Secondary indexes are extracted and written atomically.
    pub fn put(
        &self,
        collection: &str,
        doc_id: &str,
        document: &serde_json::Value,
    ) -> crate::Result<()> {
        // Serialize to MessagePack.
        let msgpack = json_to_msgpack(document);
        let mut buf = Vec::new();
        rmpv::encode::write_value(&mut buf, &msgpack).map_err(|e| crate::Error::Serialization {
            format: "msgpack".into(),
            detail: format!("encode: {e}"),
        })?;

        // Store the document blob.
        self.sparse.put(self.tenant_id, collection, doc_id, &buf)?;

        // Extract and write secondary indexes.
        if let Some(config) = self.configs.get(collection) {
            for index_path in &config.index_paths {
                let values = extract_index_values(document, &index_path.path, index_path.is_array);
                for value in values {
                    self.sparse.index_put(
                        self.tenant_id,
                        collection,
                        &index_path.path,
                        &value,
                        doc_id,
                    )?;
                }
            }
        }

        Ok(())
    }

    /// Put a document from raw MessagePack bytes.
    ///
    /// Useful when the caller already has MessagePack data (e.g., from network).
    pub fn put_raw(
        &self,
        collection: &str,
        doc_id: &str,
        msgpack_bytes: &[u8],
    ) -> crate::Result<()> {
        self.sparse
            .put(self.tenant_id, collection, doc_id, msgpack_bytes)?;

        // Extract indexes from the MessagePack blob.
        if let Some(config) = self.configs.get(collection)
            && let Ok(value) = rmpv::decode::read_value(&mut &msgpack_bytes[..])
        {
            for index_path in &config.index_paths {
                let values =
                    extract_index_values_rmpv(&value, &index_path.path, index_path.is_array);
                for v in values {
                    self.sparse.index_put(
                        self.tenant_id,
                        collection,
                        &index_path.path,
                        &v,
                        doc_id,
                    )?;
                }
            }
        }

        Ok(())
    }

    /// Get a document and deserialize from MessagePack to JSON.
    pub fn get(&self, collection: &str, doc_id: &str) -> crate::Result<Option<serde_json::Value>> {
        match self.sparse.get(self.tenant_id, collection, doc_id)? {
            Some(bytes) => {
                let rmpv_val = rmpv::decode::read_value(&mut bytes.as_slice()).map_err(|e| {
                    crate::Error::Serialization {
                        format: "msgpack".into(),
                        detail: format!("decode: {e}"),
                    }
                })?;
                Ok(Some(rmpv_to_json(&rmpv_val)))
            }
            None => Ok(None),
        }
    }

    /// Get raw MessagePack bytes (zero-copy path for DataFusion UDFs).
    pub fn get_raw(&self, collection: &str, doc_id: &str) -> crate::Result<Option<Vec<u8>>> {
        self.sparse.get(self.tenant_id, collection, doc_id)
    }

    /// Delete a document and its secondary index entries.
    pub fn delete(&self, collection: &str, doc_id: &str) -> crate::Result<bool> {
        // Clean up secondary index entries for this document before deleting it.
        self.sparse
            .delete_indexes_for_document(self.tenant_id, collection, doc_id)?;
        self.sparse.delete(self.tenant_id, collection, doc_id)
    }

    /// Drop all secondary index entries for a field across the entire collection.
    ///
    /// Used by ALTER COLLECTION DROP INDEX. Returns the number of entries removed.
    pub fn drop_field_index(&self, collection: &str, field: &str) -> crate::Result<usize> {
        self.sparse
            .delete_index_entries_for_field(self.tenant_id, collection, field)
    }

    /// Lookup documents by a secondary index value.
    ///
    /// Returns document IDs matching `path = value` in the given collection.
    pub fn index_lookup(
        &self,
        collection: &str,
        path: &str,
        value: &str,
    ) -> crate::Result<Vec<String>> {
        let prefix_with_value = format!("{value}:");
        let results = self.sparse.range_scan(
            self.tenant_id,
            collection,
            path,
            Some(prefix_with_value.as_bytes()),
            None,
            1000,
        )?;

        let mut doc_ids = Vec::new();
        for (key, _) in results {
            // Key format: "{tenant_id}:{collection}:{path}:{value}:{doc_id}"
            // After range_scan, we get the full key; extract doc_id.
            if let Some(doc_id) = key.rsplit(':').next() {
                // Verify the value portion matches exactly (not just prefix).
                let expected_prefix = format!("{}:{collection}:{path}:{value}:", self.tenant_id);
                if key.starts_with(&expected_prefix) {
                    doc_ids.push(doc_id.to_string());
                }
            }
        }
        Ok(doc_ids)
    }
}

/// Strip the leading `$.` or `$` prefix from a JSON path expression.
pub(crate) fn normalize_path(path: &str) -> &str {
    path.strip_prefix("$.")
        .or_else(|| path.strip_prefix('$'))
        .unwrap_or(path)
}

/// Extract scalar values at a JSON path for secondary indexing.
pub(crate) fn extract_index_values(
    doc: &serde_json::Value,
    path: &str,
    is_array: bool,
) -> Vec<String> {
    let path = normalize_path(path);
    let target = navigate_json(doc, path);

    match target {
        Some(serde_json::Value::Array(arr)) if is_array => {
            arr.iter().filter_map(json_scalar_to_string).collect()
        }
        Some(val) => json_scalar_to_string(val).into_iter().collect(),
        None => Vec::new(),
    }
}

/// Extract values from an rmpv::Value at a path.
fn extract_index_values_rmpv(value: &rmpv::Value, path: &str, is_array: bool) -> Vec<String> {
    let path = normalize_path(path);
    let target = navigate_rmpv(value, path);

    match target {
        Some(rmpv::Value::Array(arr)) if is_array => {
            arr.iter().filter_map(rmpv_scalar_to_string).collect()
        }
        Some(val) => rmpv_scalar_to_string(val).into_iter().collect(),
        None => Vec::new(),
    }
}

fn navigate_json<'a>(value: &'a serde_json::Value, path: &str) -> Option<&'a serde_json::Value> {
    if path.is_empty() {
        return Some(value);
    }
    let mut current = value;
    for segment in path.split('.') {
        current = current.get(segment)?;
    }
    Some(current)
}

fn navigate_rmpv<'a>(value: &'a rmpv::Value, path: &str) -> Option<&'a rmpv::Value> {
    if path.is_empty() {
        return Some(value);
    }
    let mut current = value;
    for segment in path.split('.') {
        let map = current.as_map()?;
        let mut found = false;
        for (k, v) in map {
            if k.as_str() == Some(segment) {
                current = v;
                found = true;
                break;
            }
        }
        if !found {
            return None;
        }
    }
    Some(current)
}

fn json_scalar_to_string(val: &serde_json::Value) -> Option<String> {
    match val {
        serde_json::Value::String(s) => Some(s.clone()),
        serde_json::Value::Number(n) => Some(n.to_string()),
        serde_json::Value::Bool(b) => Some(b.to_string()),
        _ => None,
    }
}

fn rmpv_scalar_to_string(val: &rmpv::Value) -> Option<String> {
    match val {
        rmpv::Value::String(s) => Some(s.as_str()?.to_string()),
        rmpv::Value::Integer(i) => Some(i.to_string()),
        rmpv::Value::Boolean(b) => Some(b.to_string()),
        rmpv::Value::F32(f) => Some(f.to_string()),
        rmpv::Value::F64(f) => Some(f.to_string()),
        _ => None,
    }
}

/// Convert serde_json::Value to rmpv::Value for MessagePack serialization.
pub(crate) fn json_to_msgpack(val: &serde_json::Value) -> rmpv::Value {
    match val {
        serde_json::Value::Null => rmpv::Value::Nil,
        serde_json::Value::Bool(b) => rmpv::Value::Boolean(*b),
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                rmpv::Value::Integer(rmpv::Integer::from(i))
            } else if let Some(u) = n.as_u64() {
                rmpv::Value::Integer(rmpv::Integer::from(u))
            } else {
                rmpv::Value::F64(n.as_f64().unwrap_or(0.0))
            }
        }
        serde_json::Value::String(s) => rmpv::Value::String(rmpv::Utf8String::from(s.as_str())),
        serde_json::Value::Array(arr) => {
            rmpv::Value::Array(arr.iter().map(json_to_msgpack).collect())
        }
        serde_json::Value::Object(obj) => rmpv::Value::Map(
            obj.iter()
                .map(|(k, v)| {
                    (
                        rmpv::Value::String(rmpv::Utf8String::from(k.as_str())),
                        json_to_msgpack(v),
                    )
                })
                .collect(),
        ),
    }
}

/// Convert rmpv::Value back to serde_json::Value.
fn rmpv_to_json(val: &rmpv::Value) -> serde_json::Value {
    match val {
        rmpv::Value::Nil => serde_json::Value::Null,
        rmpv::Value::Boolean(b) => serde_json::Value::Bool(*b),
        rmpv::Value::Integer(i) => {
            if let Some(n) = i.as_i64() {
                serde_json::json!(n)
            } else if let Some(n) = i.as_u64() {
                serde_json::json!(n)
            } else {
                serde_json::Value::Null
            }
        }
        rmpv::Value::F32(f) => serde_json::json!(*f),
        rmpv::Value::F64(f) => serde_json::json!(*f),
        rmpv::Value::String(s) => serde_json::Value::String(s.as_str().unwrap_or("").to_string()),
        rmpv::Value::Binary(b) => serde_json::Value::String(base64_encode(b)),
        rmpv::Value::Array(arr) => serde_json::Value::Array(arr.iter().map(rmpv_to_json).collect()),
        rmpv::Value::Map(entries) => {
            let mut obj = serde_json::Map::new();
            for (k, v) in entries {
                let key = match k {
                    rmpv::Value::String(s) => s.as_str().unwrap_or("").to_string(),
                    other => format!("{other}"),
                };
                obj.insert(key, rmpv_to_json(v));
            }
            serde_json::Value::Object(obj)
        }
        rmpv::Value::Ext(_, _) => serde_json::Value::Null,
    }
}

fn base64_encode(data: &[u8]) -> String {
    // Simple base64 encoding without pulling in a dependency.
    const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut result = String::with_capacity(data.len().div_ceil(3) * 4);
    for chunk in data.chunks(3) {
        let b0 = chunk[0] as u32;
        let b1 = if chunk.len() > 1 { chunk[1] as u32 } else { 0 };
        let b2 = if chunk.len() > 2 { chunk[2] as u32 } else { 0 };
        let n = (b0 << 16) | (b1 << 8) | b2;
        result.push(CHARS[((n >> 18) & 0x3F) as usize] as char);
        result.push(CHARS[((n >> 12) & 0x3F) as usize] as char);
        if chunk.len() > 1 {
            result.push(CHARS[((n >> 6) & 0x3F) as usize] as char);
        } else {
            result.push('=');
        }
        if chunk.len() > 2 {
            result.push(CHARS[(n & 0x3F) as usize] as char);
        } else {
            result.push('=');
        }
    }
    result
}

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

    fn make_engine() -> (SparseEngine, tempfile::TempDir) {
        let dir = tempfile::tempdir().unwrap();
        let engine = SparseEngine::open(&dir.path().join("doc.redb")).unwrap();
        (engine, dir)
    }

    #[test]
    fn put_and_get_document() {
        let (sparse, _dir) = make_engine();
        let doc_engine = DocumentEngine::new(&sparse, 1);

        let doc = serde_json::json!({
            "name": "Alice",
            "email": "alice@example.com",
            "age": 30
        });

        doc_engine.put("users", "u1", &doc).unwrap();
        let retrieved = doc_engine.get("users", "u1").unwrap().unwrap();

        assert_eq!(retrieved["name"], "Alice");
        assert_eq!(retrieved["email"], "alice@example.com");
        assert_eq!(retrieved["age"], 30);
    }

    #[test]
    fn get_nonexistent_returns_none() {
        let (sparse, _dir) = make_engine();
        let doc_engine = DocumentEngine::new(&sparse, 1);
        assert!(doc_engine.get("users", "missing").unwrap().is_none());
    }

    #[test]
    fn delete_document() {
        let (sparse, _dir) = make_engine();
        let doc_engine = DocumentEngine::new(&sparse, 1);

        let doc = serde_json::json!({"name": "Bob"});
        doc_engine.put("users", "u1", &doc).unwrap();
        assert!(doc_engine.delete("users", "u1").unwrap());
        assert!(doc_engine.get("users", "u1").unwrap().is_none());
    }

    #[test]
    fn overwrite_document() {
        let (sparse, _dir) = make_engine();
        let doc_engine = DocumentEngine::new(&sparse, 1);

        doc_engine
            .put("users", "u1", &serde_json::json!({"v": 1}))
            .unwrap();
        doc_engine
            .put("users", "u1", &serde_json::json!({"v": 2}))
            .unwrap();

        let doc = doc_engine.get("users", "u1").unwrap().unwrap();
        assert_eq!(doc["v"], 2);
    }

    #[test]
    fn secondary_index_extraction() {
        let (sparse, _dir) = make_engine();
        let mut doc_engine = DocumentEngine::new(&sparse, 1);

        doc_engine.register_collection(CollectionConfig::new("users").with_index("$.email"));

        doc_engine
            .put(
                "users",
                "u1",
                &serde_json::json!({"name": "Alice", "email": "alice@example.com"}),
            )
            .unwrap();
        doc_engine
            .put(
                "users",
                "u2",
                &serde_json::json!({"name": "Bob", "email": "bob@example.com"}),
            )
            .unwrap();

        // Lookup by email index.
        let results = doc_engine
            .index_lookup("users", "$.email", "alice@example.com")
            .unwrap();
        assert_eq!(results, vec!["u1"]);
    }

    #[test]
    fn array_index_extraction() {
        let (sparse, _dir) = make_engine();
        let mut doc_engine = DocumentEngine::new(&sparse, 1);

        doc_engine.register_collection(CollectionConfig::new("users").with_index("$.tags[]"));

        doc_engine
            .put(
                "users",
                "u1",
                &serde_json::json!({"name": "Alice", "tags": ["admin", "editor"]}),
            )
            .unwrap();

        let results = doc_engine.index_lookup("users", "$.tags", "admin").unwrap();
        assert_eq!(results, vec!["u1"]);

        let results = doc_engine
            .index_lookup("users", "$.tags", "editor")
            .unwrap();
        assert_eq!(results, vec!["u1"]);
    }

    #[test]
    fn nested_field_index() {
        let (sparse, _dir) = make_engine();
        let mut doc_engine = DocumentEngine::new(&sparse, 1);

        doc_engine.register_collection(CollectionConfig::new("docs").with_index("$.metadata.lang"));

        doc_engine
            .put(
                "docs",
                "d1",
                &serde_json::json!({"title": "Hello", "metadata": {"lang": "en"}}),
            )
            .unwrap();

        let results = doc_engine
            .index_lookup("docs", "$.metadata.lang", "en")
            .unwrap();
        assert_eq!(results, vec!["d1"]);
    }

    #[test]
    fn raw_msgpack_roundtrip() {
        let (sparse, _dir) = make_engine();
        let doc_engine = DocumentEngine::new(&sparse, 1);

        let doc = serde_json::json!({"key": "value", "num": 42});
        let rmpv_val = json_to_msgpack(&doc);
        let mut buf = Vec::new();
        rmpv::encode::write_value(&mut buf, &rmpv_val).unwrap();

        doc_engine.put_raw("col", "id1", &buf).unwrap();

        let raw = doc_engine.get_raw("col", "id1").unwrap().unwrap();
        assert_eq!(raw, buf);

        let decoded = doc_engine.get("col", "id1").unwrap().unwrap();
        assert_eq!(decoded["key"], "value");
        assert_eq!(decoded["num"], 42);
    }

    #[test]
    fn msgpack_is_compact() {
        let doc = serde_json::json!({
            "name": "Alice Wonderland",
            "age": 30,
            "tags": ["admin", "user"],
            "active": true
        });

        let json_bytes = serde_json::to_vec(&doc).unwrap();
        let rmpv_val = json_to_msgpack(&doc);
        let mut msgpack_bytes = Vec::new();
        rmpv::encode::write_value(&mut msgpack_bytes, &rmpv_val).unwrap();

        // MessagePack should be more compact than JSON.
        assert!(
            msgpack_bytes.len() < json_bytes.len(),
            "msgpack {} bytes vs json {} bytes",
            msgpack_bytes.len(),
            json_bytes.len()
        );
    }

    #[test]
    fn collections_are_isolated() {
        let (sparse, _dir) = make_engine();
        let doc_engine = DocumentEngine::new(&sparse, 1);

        doc_engine
            .put("users", "id1", &serde_json::json!({"type": "user"}))
            .unwrap();
        doc_engine
            .put("orders", "id1", &serde_json::json!({"type": "order"}))
            .unwrap();

        let user = doc_engine.get("users", "id1").unwrap().unwrap();
        let order = doc_engine.get("orders", "id1").unwrap().unwrap();
        assert_eq!(user["type"], "user");
        assert_eq!(order["type"], "order");
    }

    #[test]
    fn put_raw_with_index_extraction() {
        let (sparse, _dir) = make_engine();
        let mut doc_engine = DocumentEngine::new(&sparse, 1);

        doc_engine.register_collection(CollectionConfig::new("items").with_index("$.category"));

        let doc = serde_json::json!({"name": "Widget", "category": "tools"});
        let rmpv_val = json_to_msgpack(&doc);
        let mut buf = Vec::new();
        rmpv::encode::write_value(&mut buf, &rmpv_val).unwrap();

        doc_engine.put_raw("items", "i1", &buf).unwrap();

        let results = doc_engine
            .index_lookup("items", "$.category", "tools")
            .unwrap();
        assert_eq!(results, vec!["i1"]);
    }

    #[test]
    fn json_msgpack_roundtrip_preserves_types() {
        let doc = serde_json::json!({
            "string": "hello",
            "int": 42,
            "float": 1.234,
            "bool": true,
            "null": null,
            "array": [1, "two", false],
            "nested": {"a": {"b": 3}}
        });

        let rmpv_val = json_to_msgpack(&doc);
        let recovered = rmpv_to_json(&rmpv_val);

        assert_eq!(recovered["string"], "hello");
        assert_eq!(recovered["int"], 42);
        assert_eq!(recovered["bool"], true);
        assert!(recovered["null"].is_null());
        assert_eq!(recovered["array"][0], 1);
        assert_eq!(recovered["array"][1], "two");
        assert_eq!(recovered["nested"]["a"]["b"], 3);
    }
}