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

//! Collection metadata operations for the system catalog.
//!
//! The storage key is `(database_id: u64, "{tenant_id}:{name}")`.
//! The inner key preserves the legacy `"{tenant_id}:{name}"` encoding so
//! existing catalog-resolver call sites only need to add `database_id`
//! (always `DatabaseId::DEFAULT` in Tier 1).
//!
//! ## Migration
//!
//! On first boot against pre-migration storage, `migrate_collections()`
//! reads all rows from `_system.collections` (the legacy bare-String-keyed
//! table) and rewrites them under `_system.collections_v2` with
//! `DatabaseId::DEFAULT` prepended. The migration is idempotent: if the
//! v2 table already has rows the migration skips; if the legacy table is
//! absent or empty it is also a no-op.

use nodedb_types::DatabaseId;
use redb::{ReadableTable, ReadableTableMetadata};

use super::types::{COLLECTIONS, COLLECTIONS_LEGACY, StoredCollection, SystemCatalog, catalog_err};

impl SystemCatalog {
    /// Store a collection record.
    pub fn put_collection(
        &self,
        database_id: DatabaseId,
        coll: &StoredCollection,
    ) -> crate::Result<()> {
        let inner_key = format!("{}:{}", coll.tenant_id, coll.name);
        let bytes =
            zerompk::to_msgpack_vec(coll).map_err(|e| catalog_err("serialize collection", e))?;
        let write_txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("write txn", e))?;
        {
            let mut table = write_txn
                .open_table(COLLECTIONS)
                .map_err(|e| catalog_err("open collections", e))?;
            table
                .insert((database_id.as_u64(), inner_key.as_str()), bytes.as_slice())
                .map_err(|e| catalog_err("insert collection", e))?;
        }
        write_txn.commit().map_err(|e| catalog_err("commit", e))
    }

    /// Insert a collection only when its catalog key is absent.
    ///
    /// The existence check and insert share one redb write transaction, so
    /// concurrent direct-mode schema announcements cannot overwrite the first
    /// winner after both observed absence.
    pub fn put_collection_if_absent(
        &self,
        database_id: DatabaseId,
        coll: &StoredCollection,
    ) -> crate::Result<bool> {
        let inner_key = format!("{}:{}", coll.tenant_id, coll.name);
        let bytes =
            zerompk::to_msgpack_vec(coll).map_err(|e| catalog_err("serialize collection", e))?;
        let write_txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("write txn", e))?;
        let inserted = {
            let mut table = write_txn
                .open_table(COLLECTIONS)
                .map_err(|e| catalog_err("open collections", e))?;
            if table
                .get((database_id.as_u64(), inner_key.as_str()))
                .map_err(|e| catalog_err("get collection", e))?
                .is_some()
            {
                false
            } else {
                table
                    .insert((database_id.as_u64(), inner_key.as_str()), bytes.as_slice())
                    .map_err(|e| catalog_err("insert collection", e))?;
                true
            }
        };
        write_txn.commit().map_err(|e| catalog_err("commit", e))?;
        Ok(inserted)
    }

    /// Load all collections for a tenant within a database.
    pub fn load_collections_for_tenant(
        &self,
        database_id: DatabaseId,
        tenant_id: u64,
    ) -> crate::Result<Vec<StoredCollection>> {
        let prefix = format!("{tenant_id}:");
        let db_id = database_id.as_u64();
        let read_txn = self
            .db
            .begin_read()
            .map_err(|e| catalog_err("read txn", e))?;
        let table = read_txn
            .open_table(COLLECTIONS)
            .map_err(|e| catalog_err("open collections", e))?;
        let mut colls = Vec::new();
        // Range over all entries with matching database_id prefix.
        let range_start = (db_id, "");
        let range_end = (db_id + 1, "");
        for entry in table
            .range(range_start..range_end)
            .map_err(|e| catalog_err("range collections", e))?
        {
            let (key, value) = entry.map_err(|e| catalog_err("read collection", e))?;
            let (_, inner) = key.value();
            if inner.starts_with(&prefix) {
                let coll: StoredCollection = zerompk::from_msgpack(value.value())
                    .map_err(|e| catalog_err("deser collection", e))?;
                if coll.is_active {
                    colls.push(coll);
                }
            }
        }
        Ok(colls)
    }

    /// Load every soft-deleted collection across all tenants within a database.
    pub fn load_dropped_collections(
        &self,
        database_id: DatabaseId,
    ) -> crate::Result<Vec<StoredCollection>> {
        self.scan_collections_filtered(database_id, |c| !c.is_active)
    }

    /// Load all collections across all tenants within a database.
    pub fn load_all_collections(
        &self,
        database_id: DatabaseId,
    ) -> crate::Result<Vec<StoredCollection>> {
        self.scan_collections_filtered(database_id, |_| true)
    }

    /// Load every collection in every database.
    pub fn load_all_collections_across_databases(&self) -> crate::Result<Vec<StoredCollection>> {
        let read_txn = self
            .db
            .begin_read()
            .map_err(|e| catalog_err("read txn", e))?;
        let table = read_txn
            .open_table(COLLECTIONS)
            .map_err(|e| catalog_err("open collections", e))?;
        let mut collections = Vec::new();
        for entry in table
            .iter()
            .map_err(|e| catalog_err("iterate collections", e))?
        {
            let (_, value) = entry.map_err(|e| catalog_err("read collection", e))?;
            collections.push(
                zerompk::from_msgpack(value.value())
                    .map_err(|e| catalog_err("deser collection", e))?,
            );
        }
        Ok(collections)
    }

    fn scan_collections_filtered(
        &self,
        database_id: DatabaseId,
        predicate: impl Fn(&StoredCollection) -> bool,
    ) -> crate::Result<Vec<StoredCollection>> {
        let db_id = database_id.as_u64();
        let read_txn = self
            .db
            .begin_read()
            .map_err(|e| catalog_err("read txn", e))?;
        let table = read_txn
            .open_table(COLLECTIONS)
            .map_err(|e| catalog_err("open collections", e))?;
        let mut colls = Vec::new();
        let range_start = (db_id, "");
        let range_end = (db_id + 1, "");
        for entry in table
            .range(range_start..range_end)
            .map_err(|e| catalog_err("range collections filter", e))?
        {
            let (_, value) = entry.map_err(|e| catalog_err("read collection", e))?;
            let coll: StoredCollection = zerompk::from_msgpack(value.value())
                .map_err(|e| catalog_err("deser collection", e))?;
            if predicate(&coll) {
                colls.push(coll);
            }
        }
        Ok(colls)
    }

    /// Hard-delete a collection row. Returns `true` if a row was
    /// removed, `false` if the row was already absent (idempotent).
    pub fn delete_collection(
        &self,
        database_id: DatabaseId,
        tenant_id: u64,
        name: &str,
    ) -> crate::Result<bool> {
        let inner_key = format!("{tenant_id}:{name}");
        let write_txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("write txn", e))?;
        let removed;
        {
            let mut table = write_txn
                .open_table(COLLECTIONS)
                .map_err(|e| catalog_err("open collections", e))?;
            removed = table
                .remove((database_id.as_u64(), inner_key.as_str()))
                .map_err(|e| catalog_err("remove collection", e))?
                .is_some();
        }
        write_txn.commit().map_err(|e| catalog_err("commit", e))?;
        Ok(removed)
    }

    /// Get a single collection by database_id + tenant_id + name.
    pub fn get_collection(
        &self,
        database_id: DatabaseId,
        tenant_id: u64,
        name: &str,
    ) -> crate::Result<Option<StoredCollection>> {
        let inner_key = format!("{tenant_id}:{name}");
        let read_txn = self
            .db
            .begin_read()
            .map_err(|e| catalog_err("read txn", e))?;
        let table = read_txn
            .open_table(COLLECTIONS)
            .map_err(|e| catalog_err("open collections", e))?;
        match table.get((database_id.as_u64(), inner_key.as_str())) {
            Ok(Some(value)) => {
                let coll: StoredCollection = zerompk::from_msgpack(value.value())
                    .map_err(|e| catalog_err("deser collection", e))?;
                Ok(Some(coll))
            }
            Ok(None) => Ok(None),
            Err(e) => Err(catalog_err("get collection", e)),
        }
    }

    /// Idempotent migration: reads all rows from the legacy
    /// `_system.collections` table (bare `"{tenant_id}:{name}"` key) and
    /// rewrites them under `_system.collections_v2` with
    /// `DatabaseId::DEFAULT` prepended.
    ///
    /// Safe to call on:
    /// - Fresh boot: legacy table absent or empty → no-op.
    /// - Pre-migration boot: legacy rows present → migrated to v2.
    /// - Already-migrated boot: v2 rows already exist → no-op (skips if
    ///   v2 table is non-empty; any duplicate put is an idempotent
    ///   overwrite because the key+value are identical).
    pub fn migrate_collections(&self) -> crate::Result<()> {
        // Check legacy table existence and emptiness.
        let legacy_rows: Vec<(String, Vec<u8>)> = {
            let txn = self
                .db
                .begin_read()
                .map_err(|e| catalog_err("migrate_collections read txn", e))?;
            match txn.open_table(COLLECTIONS_LEGACY) {
                Ok(table) => {
                    let iter = table
                        .iter()
                        .map_err(|e| catalog_err("migrate_collections iter", e))?;
                    let mut rows = Vec::new();
                    for row in iter {
                        let (k, v) = row.map_err(|e| catalog_err("migrate_collections row", e))?;
                        rows.push((k.value().to_string(), v.value().to_vec()));
                    }
                    rows
                }
                Err(_) => Vec::new(), // legacy table does not exist yet
            }
        };

        if legacy_rows.is_empty() {
            return Ok(());
        }

        // Check if v2 is already populated (already-migrated boot).
        let v2_empty = {
            let txn = self
                .db
                .begin_read()
                .map_err(|e| catalog_err("migrate_collections v2 check txn", e))?;
            match txn.open_table(COLLECTIONS) {
                Ok(table) => table
                    .is_empty()
                    .map_err(|e| catalog_err("migrate_collections v2 is_empty", e))?,
                Err(_) => true,
            }
        };
        if !v2_empty {
            // Already migrated — idempotent no-op.
            return Ok(());
        }

        // Write all legacy rows into v2 under DatabaseId::DEFAULT.
        let db_id = DatabaseId::DEFAULT.as_u64();
        let write_txn = self
            .db
            .begin_write()
            .map_err(|e| catalog_err("migrate_collections write txn", e))?;
        {
            let mut table = write_txn
                .open_table(COLLECTIONS)
                .map_err(|e| catalog_err("migrate_collections open v2", e))?;
            for (inner_key, bytes) in &legacy_rows {
                table
                    .insert((db_id, inner_key.as_str()), bytes.as_slice())
                    .map_err(|e| catalog_err("migrate_collections insert v2", e))?;
            }
        }
        write_txn
            .commit()
            .map_err(|e| catalog_err("migrate_collections commit", e))
    }
}

#[cfg(test)]
mod tests {
    use nodedb_types::CollectionType;

    use super::*;
    use crate::control::security::catalog::types::{COLLECTIONS_LEGACY, StoredCollection};

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

    fn make_coll(tenant_id: u64, name: &str) -> StoredCollection {
        let mut c = StoredCollection::new(tenant_id, name, "admin");
        c.collection_type = CollectionType::document();
        c
    }

    #[test]
    fn put_get_roundtrip() {
        let (_dir, cat) = open_catalog();
        let coll = make_coll(1, "users");
        cat.put_collection(DatabaseId::DEFAULT, &coll).unwrap();
        let fetched = cat.get_collection(DatabaseId::DEFAULT, 1, "users").unwrap();
        assert!(fetched.is_some());
        assert_eq!(fetched.unwrap().name, "users");
    }

    #[test]
    fn missing_returns_none() {
        let (_dir, cat) = open_catalog();
        assert!(
            cat.get_collection(DatabaseId::DEFAULT, 1, "ghost")
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn delete_is_idempotent() {
        let (_dir, cat) = open_catalog();
        cat.put_collection(DatabaseId::DEFAULT, &make_coll(1, "users"))
            .unwrap();
        assert!(
            cat.delete_collection(DatabaseId::DEFAULT, 1, "users")
                .unwrap()
        );
        assert!(
            !cat.delete_collection(DatabaseId::DEFAULT, 1, "users")
                .unwrap()
        );
    }

    #[test]
    fn load_for_tenant_filters_correctly() {
        let (_dir, cat) = open_catalog();
        cat.put_collection(DatabaseId::DEFAULT, &make_coll(1, "a"))
            .unwrap();
        cat.put_collection(DatabaseId::DEFAULT, &make_coll(1, "b"))
            .unwrap();
        cat.put_collection(DatabaseId::DEFAULT, &make_coll(2, "c"))
            .unwrap();
        let t1 = cat
            .load_collections_for_tenant(DatabaseId::DEFAULT, 1)
            .unwrap();
        assert_eq!(t1.len(), 2);
        let t2 = cat
            .load_collections_for_tenant(DatabaseId::DEFAULT, 2)
            .unwrap();
        assert_eq!(t2.len(), 1);
    }

    // ── Migration tests ──────────────────────────────────────────────────

    /// Helper: write a legacy (bare string key) row directly so we can
    /// test the migration without going through put_collection.
    fn insert_legacy_row(cat: &SystemCatalog, coll: &StoredCollection) {
        let key = format!("{}:{}", coll.tenant_id, coll.name);
        let bytes = zerompk::to_msgpack_vec(coll).unwrap();
        let txn = cat.db.begin_write().unwrap();
        {
            let mut table = txn.open_table(COLLECTIONS_LEGACY).unwrap();
            table.insert(key.as_str(), bytes.as_slice()).unwrap();
        }
        txn.commit().unwrap();
    }

    #[test]
    fn fresh_boot_migration_is_noop() {
        let (_dir, cat) = open_catalog();
        // No legacy rows → migration is a no-op.
        cat.migrate_collections().unwrap();
        assert!(
            cat.load_all_collections(DatabaseId::DEFAULT)
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn pre_migration_boot_migrates_all_rows() {
        let (_dir, cat) = open_catalog();
        let coll1 = make_coll(1, "widgets");
        let coll2 = make_coll(2, "orders");
        insert_legacy_row(&cat, &coll1);
        insert_legacy_row(&cat, &coll2);

        cat.migrate_collections().unwrap();

        let w = cat
            .get_collection(DatabaseId::DEFAULT, 1, "widgets")
            .unwrap();
        assert!(w.is_some(), "widgets must be accessible after migration");
        let o = cat
            .get_collection(DatabaseId::DEFAULT, 2, "orders")
            .unwrap();
        assert!(o.is_some(), "orders must be accessible after migration");
    }

    #[test]
    fn already_migrated_boot_is_idempotent() {
        let (_dir, cat) = open_catalog();
        // Write a v2 row directly (simulating already-migrated).
        cat.put_collection(DatabaseId::DEFAULT, &make_coll(1, "existing"))
            .unwrap();

        // Also insert a legacy row that would conflict if re-migrated.
        let coll_legacy = make_coll(1, "existing");
        insert_legacy_row(&cat, &coll_legacy);

        // Migration should be a no-op (v2 non-empty).
        cat.migrate_collections().unwrap();

        let all = cat.load_all_collections(DatabaseId::DEFAULT).unwrap();
        assert_eq!(all.len(), 1, "should still be 1 row, not duplicated");
    }
}