nodedb 0.2.0

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
// SPDX-License-Identifier: BUSL-1.1

//! Catalog persistence for the clone CoW subsystem.
//!
//! Three redb tables are managed here:
//!
//! - `clone_copyups`   — maps `(target_collection_key, source_surrogate)` →
//!   `target_surrogate` for rows that have been copy-up'd into the clone.
//! - `clone_tombstones` — records `(target_collection_key, source_surrogate)`
//!   for rows that have been deleted from the clone without ever being
//!   materialised into it.
//! - `clone_lineage`   — maps `source_database_id` → `Vec<child_database_id>`
//!   for orphan-protection checks at DROP DATABASE time.

use nodedb_types::DatabaseId;
use redb::ReadableTable;

use super::types::{
    CLONE_COPYUPS, CLONE_KV_TOMBSTONES, CLONE_LINEAGE, CLONE_TOMBSTONES, SystemCatalog, catalog_err,
};

impl SystemCatalog {
    // ── clone_copyups ─────────────────────────────────────────────────────────

    /// Record a copy-up: `source_surrogate` in `target_collection` is now
    /// represented by `target_surrogate` in target storage.
    pub fn put_clone_copyup(
        &self,
        target_collection_key: &str,
        source_surrogate: u32,
        target_surrogate: u32,
    ) -> crate::Result<()> {
        let txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("clone_copyups begin_write", e))?;
        {
            let mut table = txn
                .open_table(CLONE_COPYUPS)
                .map_err(|e| catalog_err("open clone_copyups", e))?;
            table
                .insert((target_collection_key, source_surrogate), target_surrogate)
                .map_err(|e| catalog_err("insert clone_copyups", e))?;
        }
        txn.commit()
            .map_err(|e| catalog_err("clone_copyups commit", e))
    }

    /// Look up the target surrogate for a copied-up source row.
    /// Returns `None` if no copy-up has been recorded for this surrogate.
    pub fn get_clone_copyup(
        &self,
        target_collection_key: &str,
        source_surrogate: u32,
    ) -> crate::Result<Option<u32>> {
        let txn = self
            .db
            .begin_read()
            .map_err(|e| catalog_err("clone_copyups begin_read", e))?;
        let table = txn
            .open_table(CLONE_COPYUPS)
            .map_err(|e| catalog_err("open clone_copyups read", e))?;
        let val = table
            .get((target_collection_key, source_surrogate))
            .map_err(|e| catalog_err("get clone_copyups", e))?
            .map(|v| v.value());
        Ok(val)
    }

    /// Remove a copy-up record (called when the collection is fully
    /// materialised and the CoW tables are reaped).
    pub fn delete_clone_copyup(
        &self,
        target_collection_key: &str,
        source_surrogate: u32,
    ) -> crate::Result<()> {
        let txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("clone_copyups delete begin_write", e))?;
        {
            let mut table = txn
                .open_table(CLONE_COPYUPS)
                .map_err(|e| catalog_err("open clone_copyups delete", e))?;
            table
                .remove((target_collection_key, source_surrogate))
                .map_err(|e| catalog_err("remove clone_copyups", e))?;
        }
        txn.commit()
            .map_err(|e| catalog_err("clone_copyups delete commit", e))
    }

    /// Delete all copy-up records for a specific `target_collection_key`
    /// (used during post-materialization reap).
    pub fn delete_all_clone_copyups_for_collection(
        &self,
        target_collection_key: &str,
    ) -> crate::Result<u64> {
        let txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("clone_copyups reap begin_write", e))?;
        let removed = {
            let mut table = txn
                .open_table(CLONE_COPYUPS)
                .map_err(|e| catalog_err("open clone_copyups reap", e))?;
            // Collect keys first to avoid borrow issues.
            let keys: Vec<u32> = {
                let iter = table
                    .iter()
                    .map_err(|e| catalog_err("iter clone_copyups reap", e))?;
                let mut acc = Vec::new();
                for row in iter {
                    let (k, _) = row.map_err(|e| catalog_err("iter clone_copyups row", e))?;
                    if k.value().0 == target_collection_key {
                        acc.push(k.value().1);
                    }
                }
                acc
            };
            let count = keys.len() as u64;
            for surrogate in keys {
                table
                    .remove((target_collection_key, surrogate))
                    .map_err(|e| catalog_err("remove clone_copyups reap", e))?;
            }
            count
        };
        txn.commit()
            .map_err(|e| catalog_err("clone_copyups reap commit", e))?;
        Ok(removed)
    }

    // ── clone_tombstones ──────────────────────────────────────────────────────

    /// Record a tombstone: `source_surrogate` was deleted from the clone.
    /// Future reads will consult this before falling back to source storage.
    pub fn put_clone_tombstone(
        &self,
        target_collection_key: &str,
        source_surrogate: u32,
    ) -> crate::Result<()> {
        let txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("clone_tombstones begin_write", e))?;
        {
            let mut table = txn
                .open_table(CLONE_TOMBSTONES)
                .map_err(|e| catalog_err("open clone_tombstones", e))?;
            table
                .insert((target_collection_key, source_surrogate), ())
                .map_err(|e| catalog_err("insert clone_tombstones", e))?;
        }
        txn.commit()
            .map_err(|e| catalog_err("clone_tombstones commit", e))
    }

    /// Returns `true` if `source_surrogate` has been tombstoned in this clone.
    pub fn is_clone_tombstoned(
        &self,
        target_collection_key: &str,
        source_surrogate: u32,
    ) -> crate::Result<bool> {
        let txn = self
            .db
            .begin_read()
            .map_err(|e| catalog_err("clone_tombstones begin_read", e))?;
        let table = txn
            .open_table(CLONE_TOMBSTONES)
            .map_err(|e| catalog_err("open clone_tombstones read", e))?;
        let exists = table
            .get((target_collection_key, source_surrogate))
            .map_err(|e| catalog_err("get clone_tombstones", e))?
            .is_some();
        Ok(exists)
    }

    /// Return the set of all tombstoned source surrogates for a collection.
    ///
    /// Used by the clone read path to filter source rows before merging them
    /// into the target result set.  Returns an empty set when the collection
    /// has no tombstones (common case).
    pub fn list_clone_tombstones(
        &self,
        target_collection_key: &str,
    ) -> crate::Result<std::collections::HashSet<u32>> {
        let txn = self
            .db
            .begin_read()
            .map_err(|e| catalog_err("clone_tombstones list begin_read", e))?;
        let table = txn
            .open_table(CLONE_TOMBSTONES)
            .map_err(|e| catalog_err("open clone_tombstones list", e))?;
        let mut set = std::collections::HashSet::new();
        for row in table
            .iter()
            .map_err(|e| catalog_err("iter clone_tombstones list", e))?
        {
            let (k, _) = row.map_err(|e| catalog_err("iter clone_tombstones list row", e))?;
            if k.value().0 == target_collection_key {
                set.insert(k.value().1);
            }
        }
        Ok(set)
    }

    /// Delete all tombstone records for a specific `target_collection_key`
    /// (used during post-materialization reap).
    pub fn delete_all_clone_tombstones_for_collection(
        &self,
        target_collection_key: &str,
    ) -> crate::Result<u64> {
        let txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("clone_tombstones reap begin_write", e))?;
        let removed = {
            let mut table = txn
                .open_table(CLONE_TOMBSTONES)
                .map_err(|e| catalog_err("open clone_tombstones reap", e))?;
            let keys: Vec<u32> = {
                let iter = table
                    .iter()
                    .map_err(|e| catalog_err("iter clone_tombstones reap", e))?;
                let mut acc = Vec::new();
                for row in iter {
                    let (k, _) = row.map_err(|e| catalog_err("iter clone_tombstones row", e))?;
                    if k.value().0 == target_collection_key {
                        acc.push(k.value().1);
                    }
                }
                acc
            };
            let count = keys.len() as u64;
            for surrogate in keys {
                table
                    .remove((target_collection_key, surrogate))
                    .map_err(|e| catalog_err("remove clone_tombstones reap", e))?;
            }
            count
        };
        txn.commit()
            .map_err(|e| catalog_err("clone_tombstones reap commit", e))?;
        Ok(removed)
    }

    // ── clone_kv_tombstones ───────────────────────────────────────────────────

    /// Record a KV tombstone: `kv_key` was deleted from the KV clone collection.
    /// Future reads will exclude source rows with this key from results.
    pub fn put_kv_clone_tombstone(
        &self,
        target_collection_key: &str,
        kv_key: &str,
    ) -> crate::Result<()> {
        let txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("clone_kv_tombstones begin_write", e))?;
        {
            let mut table = txn
                .open_table(CLONE_KV_TOMBSTONES)
                .map_err(|e| catalog_err("open clone_kv_tombstones", e))?;
            table
                .insert((target_collection_key, kv_key), ())
                .map_err(|e| catalog_err("insert clone_kv_tombstones", e))?;
        }
        txn.commit()
            .map_err(|e| catalog_err("clone_kv_tombstones commit", e))
    }

    /// Return the set of all KV tombstoned keys for a clone collection.
    ///
    /// Used by the clone read path to filter source KV scan results before
    /// merging them into the target result set.
    pub fn list_kv_clone_tombstones(
        &self,
        target_collection_key: &str,
    ) -> crate::Result<std::collections::HashSet<String>> {
        let txn = self
            .db
            .begin_read()
            .map_err(|e| catalog_err("clone_kv_tombstones list begin_read", e))?;
        let table = txn
            .open_table(CLONE_KV_TOMBSTONES)
            .map_err(|e| catalog_err("open clone_kv_tombstones list", e))?;
        let mut set = std::collections::HashSet::new();
        for row in table
            .iter()
            .map_err(|e| catalog_err("iter clone_kv_tombstones list", e))?
        {
            let (k, _) = row.map_err(|e| catalog_err("iter clone_kv_tombstones row", e))?;
            if k.value().0 == target_collection_key {
                set.insert(k.value().1.to_owned());
            }
        }
        Ok(set)
    }

    // ── clone_lineage ─────────────────────────────────────────────────────────

    /// Return the list of child database ids that are clones of `source_db_id`.
    /// Returns an empty vec if no clones exist.
    pub fn get_clone_children(&self, source_db_id: DatabaseId) -> crate::Result<Vec<DatabaseId>> {
        let txn = self
            .db
            .begin_read()
            .map_err(|e| catalog_err("clone_lineage begin_read", e))?;
        let table = txn
            .open_table(CLONE_LINEAGE)
            .map_err(|e| catalog_err("open clone_lineage read", e))?;
        match table
            .get(source_db_id.as_u64())
            .map_err(|e| catalog_err("get clone_lineage", e))?
        {
            None => Ok(Vec::new()),
            Some(v) => {
                let ids: Vec<u64> = zerompk::from_msgpack(v.value())
                    .map_err(|e| catalog_err("deser clone_lineage", e))?;
                Ok(ids.into_iter().map(DatabaseId::new).collect())
            }
        }
    }

    /// Add `child_db_id` as a clone child of `source_db_id`.
    /// Idempotent — safe to call multiple times with the same child.
    pub fn add_clone_child(
        &self,
        source_db_id: DatabaseId,
        child_db_id: DatabaseId,
    ) -> crate::Result<()> {
        let mut children = self.get_clone_children(source_db_id)?;
        if !children.contains(&child_db_id) {
            children.push(child_db_id);
        }
        self.write_clone_children(source_db_id, &children)
    }

    /// Remove `child_db_id` from the clone children of `source_db_id`.
    /// Idempotent — safe to call when the child is already absent.
    pub fn remove_clone_child(
        &self,
        source_db_id: DatabaseId,
        child_db_id: DatabaseId,
    ) -> crate::Result<()> {
        let mut children = self.get_clone_children(source_db_id)?;
        children.retain(|id| *id != child_db_id);
        if children.is_empty() {
            self.delete_clone_lineage_entry(source_db_id)
        } else {
            self.write_clone_children(source_db_id, &children)
        }
    }

    fn write_clone_children(
        &self,
        source_db_id: DatabaseId,
        children: &[DatabaseId],
    ) -> crate::Result<()> {
        let ids: Vec<u64> = children.iter().map(|id| id.as_u64()).collect();
        let bytes =
            zerompk::to_msgpack_vec(&ids).map_err(|e| catalog_err("serialize clone_lineage", e))?;
        let txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("clone_lineage begin_write", e))?;
        {
            let mut table = txn
                .open_table(CLONE_LINEAGE)
                .map_err(|e| catalog_err("open clone_lineage write", e))?;
            table
                .insert(source_db_id.as_u64(), bytes.as_slice())
                .map_err(|e| catalog_err("insert clone_lineage", e))?;
        }
        txn.commit()
            .map_err(|e| catalog_err("clone_lineage commit", e))
    }

    fn delete_clone_lineage_entry(&self, source_db_id: DatabaseId) -> crate::Result<()> {
        let txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("clone_lineage delete begin_write", e))?;
        {
            let mut table = txn
                .open_table(CLONE_LINEAGE)
                .map_err(|e| catalog_err("open clone_lineage delete", e))?;
            table
                .remove(source_db_id.as_u64())
                .map_err(|e| catalog_err("remove clone_lineage", e))?;
        }
        txn.commit()
            .map_err(|e| catalog_err("clone_lineage delete commit", e))
    }
}

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

    fn open_catalog() -> (tempfile::TempDir, SystemCatalog) {
        let dir = tempfile::tempdir().unwrap();
        let cat = SystemCatalog::open(&dir.path().join("system.redb")).unwrap();
        (dir, cat)
    }

    fn db(n: u64) -> DatabaseId {
        DatabaseId::new(n)
    }

    #[test]
    fn copyup_roundtrip() {
        let (_dir, cat) = open_catalog();
        cat.put_clone_copyup("db1:0:users", 42, 99).unwrap();
        assert_eq!(cat.get_clone_copyup("db1:0:users", 42).unwrap(), Some(99));
        assert_eq!(cat.get_clone_copyup("db1:0:users", 43).unwrap(), None);
    }

    #[test]
    fn copyup_delete() {
        let (_dir, cat) = open_catalog();
        cat.put_clone_copyup("db1:0:users", 1, 10).unwrap();
        cat.delete_clone_copyup("db1:0:users", 1).unwrap();
        assert_eq!(cat.get_clone_copyup("db1:0:users", 1).unwrap(), None);
    }

    #[test]
    fn copyup_delete_all_for_collection() {
        let (_dir, cat) = open_catalog();
        cat.put_clone_copyup("db1:0:users", 1, 10).unwrap();
        cat.put_clone_copyup("db1:0:users", 2, 11).unwrap();
        cat.put_clone_copyup("db1:0:posts", 5, 50).unwrap();
        let removed = cat
            .delete_all_clone_copyups_for_collection("db1:0:users")
            .unwrap();
        assert_eq!(removed, 2);
        assert_eq!(cat.get_clone_copyup("db1:0:users", 1).unwrap(), None);
        assert_eq!(cat.get_clone_copyup("db1:0:posts", 5).unwrap(), Some(50));
    }

    #[test]
    fn tombstone_roundtrip() {
        let (_dir, cat) = open_catalog();
        assert!(!cat.is_clone_tombstoned("db1:0:users", 7).unwrap());
        cat.put_clone_tombstone("db1:0:users", 7).unwrap();
        assert!(cat.is_clone_tombstoned("db1:0:users", 7).unwrap());
        assert!(!cat.is_clone_tombstoned("db1:0:users", 8).unwrap());
    }

    #[test]
    fn tombstone_delete_all_for_collection() {
        let (_dir, cat) = open_catalog();
        cat.put_clone_tombstone("db1:0:users", 1).unwrap();
        cat.put_clone_tombstone("db1:0:users", 2).unwrap();
        cat.put_clone_tombstone("db1:0:posts", 9).unwrap();
        let removed = cat
            .delete_all_clone_tombstones_for_collection("db1:0:users")
            .unwrap();
        assert_eq!(removed, 2);
        assert!(!cat.is_clone_tombstoned("db1:0:users", 1).unwrap());
        assert!(cat.is_clone_tombstoned("db1:0:posts", 9).unwrap());
    }

    #[test]
    fn lineage_empty_by_default() {
        let (_dir, cat) = open_catalog();
        assert!(cat.get_clone_children(db(1)).unwrap().is_empty());
    }

    #[test]
    fn lineage_add_and_remove() {
        let (_dir, cat) = open_catalog();
        cat.add_clone_child(db(1), db(2)).unwrap();
        cat.add_clone_child(db(1), db(3)).unwrap();
        let children = cat.get_clone_children(db(1)).unwrap();
        assert!(children.contains(&db(2)));
        assert!(children.contains(&db(3)));

        cat.remove_clone_child(db(1), db(2)).unwrap();
        let children = cat.get_clone_children(db(1)).unwrap();
        assert!(!children.contains(&db(2)));
        assert!(children.contains(&db(3)));
    }

    #[test]
    fn lineage_add_idempotent() {
        let (_dir, cat) = open_catalog();
        cat.add_clone_child(db(1), db(2)).unwrap();
        cat.add_clone_child(db(1), db(2)).unwrap();
        assert_eq!(cat.get_clone_children(db(1)).unwrap().len(), 1);
    }

    #[test]
    fn lineage_remove_last_cleans_up() {
        let (_dir, cat) = open_catalog();
        cat.add_clone_child(db(1), db(2)).unwrap();
        cat.remove_clone_child(db(1), db(2)).unwrap();
        assert!(cat.get_clone_children(db(1)).unwrap().is_empty());
        // Idempotent — removing absent child is fine.
        cat.remove_clone_child(db(1), db(2)).unwrap();
    }
}