MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / open

Method open

nodedb/src/control/security/catalog/system_catalog.rs:21–64  ·  view source on GitHub ↗

Open or create the system catalog at the given path.

(path: &Path)

Source from the content-addressed store, hash-verified

19impl SystemCatalog {
20 /// Open or create the system catalog at the given path.
21 pub fn open(path: &Path) -> crate::Result<Self> {
22 if let Some(parent) = path.parent() {
23 std::fs::create_dir_all(parent)?;
24 }
25
26 let db = Database::create(path).map_err(|e| catalog_err("open", e))?;
27
28 // Bootstrap every `_system.*` table from the canonical registry —
29 // but only if at least one is actually missing. Probing read-only
30 // first keeps `open` byte-idempotent on an already-bootstrapped
31 // catalog: a write transaction + commit stamps a fresh meta/commit
32 // page on redb every time, so an unconditional bootstrap rewrites
33 // `system.redb` on every boot (changing its size/md5) even when
34 // nothing changed — and a boot that then fails its integrity check
35 // would have mutated persistent catalog state on its way out.
36 // Opening a table in a write transaction creates it if absent; the
37 // registry is the single source of truth, so a table cannot be
38 // read in production code without being bootstrapped here.
39 let needs_bootstrap = match db.begin_read() {
40 Ok(read_txn) => super::bootstrap_tables::BOOTSTRAP_TABLES
41 .iter()
42 .any(|table| (table.probe)(&read_txn).is_err()),
43 // A read transaction on a brand-new database can fail before
44 // the first commit; treat that as "bootstrap needed".
45 Err(_) => true,
46 };
47 if needs_bootstrap {
48 let write_txn = db.begin_write().map_err(|e| catalog_err("init txn", e))?;
49 {
50 for table in super::bootstrap_tables::BOOTSTRAP_TABLES {
51 (table.create)(&write_txn)
52 .map_err(|e| catalog_err(&format!("init {} table", table.label), e))?;
53 }
54 }
55 write_txn
56 .commit()
57 .map_err(|e| catalog_err("init commit", e))?;
58 info!(path = %path.display(), "system catalog opened (bootstrapped)");
59 } else {
60 info!(path = %path.display(), "system catalog opened");
61 }
62
63 Ok(Self { db })
64 }
65
66 /// Execute a write transaction on the WASM_MODULES table.
67 fn wasm_write<F, T>(&self, op: &str, f: F) -> crate::Result<T>

Callers

nothing calls this directly

Calls 5

begin_writeMethod · 0.80
catalog_errFunction · 0.70
createFunction · 0.50
iterMethod · 0.45
commitMethod · 0.45

Tested by

no test coverage detected