nx_doc_catalog.nx source
↩ module page · 47 lines · 2831 B
1// nx_doc_catalog.nx -- R2 of THE NISHI DOCUMENT-INTELLIGENCE arc: the LIBRARIAN -- CATALOG / ORGANIZE /
2// MANAGE documents as content-addressed structured records. Composes the existing nx_asset_record vocab
3// (Dublin Core spine + lifecycle + is_current additive law) -- DRY #15, the document IS just a typed asset
4// record (type="doc"). Content-addressed: same document bytes -> same CID everywhere (the catalog's self-
5// proof of integrity + automatic dedup). Each catalog entry carries the structured extraction summary, so
6// the catalog links a document to its structured data. Commits via seg_store with sys_now_us() (NOT ar_put's
7// sys_now_ms() -- per reference-segstore-segid-use-now-us, per-op commits must not collide). Additive-only
8// (#13): re-cataloging is idempotent by content; history is preserved. license_tier: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_asset_record.nx"
11import "nx_seg_store.nx"
12const K_MAGIC_8192: i64 = 8192
13
14// CATALOG a document: build a content-addressed doc record (Dublin Core + lifecycle + the structured
15// extraction summary), store it keyed by its CID. Writes the CID (NUL-terminated) to out_cid (>=80 bytes).
16// Returns 0 ok, negative on store error. Idempotent by content (same doc -> same CID -> same key).
17func dc_catalog_doc(prefix: *u8, title: *u8, provider: *u8, date: *u8, source: *u8, summary: *u8, out_cid: *u8) -> i64 {
18 let keys: *i64 = sys_mmap(8 * 32) as *i64
19 let vals: *i64 = sys_mmap(8 * 32) as *i64
20 let kv: *i64 = sys_mmap(16) as *i64
21 kv[0] = 0
22 ar_addf(keys, vals, kv, "type\x00" as *u8, "doc\x00" as *u8)
23 ar_addf(keys, vals, kv, "title\x00" as *u8, title)
24 ar_addf(keys, vals, kv, "creator\x00" as *u8, provider)
25 ar_addf(keys, vals, kv, "date\x00" as *u8, date)
26 ar_addf(keys, vals, kv, "identifier\x00" as *u8, source)
27 ar_addf(keys, vals, kv, "subject\x00" as *u8, "medical bill\x00" as *u8)
28 ar_addf(keys, vals, kv, "extract_summary\x00" as *u8, summary)
29 ar_addf(keys, vals, kv, "lifecycle_stage\x00" as *u8, "ingest\x00" as *u8)
30 ar_addf(keys, vals, kv, "is_current\x00" as *u8, "1\x00" as *u8)
31 ar_addf(keys, vals, kv, "classification\x00" as *u8, "private\x00" as *u8)
32 let bytes: *u8 = sys_mmap(K_MAGIC_8192)
33 let blen: i64 = ar_encode(keys, vals, kv[0], bytes)
34 ar_cid(bytes, blen, out_cid)
35 let w: *i64 = ss_begin()
36 if ss_add(w, 1, out_cid, bytes, blen) != 0 { return 0 - 10 }
37 if ss_commit(prefix, w, sys_now_us()) != 0 { return 0 - 11 }
38 return 0
39}
40
41// retrieve a cataloged document's record bytes by CID. ptrout[0]/lenout[0] get bytes+len.
42// returns 1 found, 0 tombstoned, -1 absent.
43func dc_get(prefix: *u8, cid: *u8, ptrout: *i64, lenout: *i64) -> i64 {
44 let h: *i64 = ss_open(prefix)
45 if (h as i64) == 0 { return 0 - 1 }
46 return ss_hget(h, cid, ptrout, lenout)
47}