nx_dms_search_store.nx source
↩ module page · 77 lines · 2878 B
1// nx_dms_search_store.nx -- durability for the consent-gated search index.
2// Unlike the fixed-row ledgers, search docs carry variable-length TEXT, so the
3// snapshot is a walk of per-doc records: [asset_id:8 BE][text bytes][0x00].
4// On load, text pointers point into the persistent ss_open blob (mmap'd, never
5// freed), so the reloaded index tokenizes/searches the real bytes.
6// Completes the DMS never-lose story (rights + catalog + offers + search).
7// license_tier: ORIGINAL
8
9import "nx_syscalls.nx"
10import "nx_dms_search.nx"
11import "nx_seg_store.nx"
12
13func nx_dms_search_save(sx: *NxDmsSearchIndex, prefix: *u8, segid: i64) -> i64 {
14 var total: i64 = 0
15 var i: nx_size = 0
16 while i < sx.count {
17 let d: *NxDmsSearchDoc = _sx_at(sx, i)
18 total = total + 8 + (d.text_len as i64) + 1
19 i = i + 1
20 }
21 let blob: *u8 = sys_mmap(total + 16)
22 var o: i64 = 0
23 i = 0
24 while i < sx.count {
25 let d: *NxDmsSearchDoc = _sx_at(sx, i)
26 let aid: i64 = d.asset_id as i64
27 blob[o + 0] = ((aid >> 56) & 255) as u8
28 blob[o + 1] = ((aid >> 48) & 255) as u8
29 blob[o + 2] = ((aid >> 40) & 255) as u8
30 blob[o + 3] = ((aid >> 32) & 255) as u8
31 blob[o + 4] = ((aid >> 24) & 255) as u8
32 blob[o + 5] = ((aid >> 16) & 255) as u8
33 blob[o + 6] = ((aid >> 8) & 255) as u8
34 blob[o + 7] = (aid & 255) as u8
35 o = o + 8
36 let tp: *u8 = (d.text_ptr) as *u8
37 let tl: i64 = d.text_len as i64
38 var t: i64 = 0
39 while t < tl { blob[o] = tp[t]; o = o + 1; t = t + 1 }
40 blob[o] = 0 as u8
41 o = o + 1
42 i = i + 1
43 }
44 let w: *i64 = ss_begin()
45 ss_add(w, 1, "search:snapshot" as *u8, blob, o)
46 return ss_commit(prefix, w, segid)
47}
48
49func nx_dms_search_load(prefix: *u8) -> *NxDmsSearchIndex {
50 let h: *i64 = ss_open(prefix)
51 let ptrout: *i64 = sys_mmap(16) as *i64
52 let lenout: *i64 = sys_mmap(16) as *i64
53 let rc: i64 = ss_hget(h, "search:snapshot" as *u8, ptrout, lenout)
54 if rc != 1 { return nx_dms_search_new(16) }
55 let blob: *u8 = ptrout[0] as *u8
56 let len: i64 = lenout[0]
57 // count docs (walk records)
58 var n: i64 = 0
59 var o: i64 = 0
60 while o < len {
61 o = o + 8
62 while blob[o] != (0 as u8) { o = o + 1 }
63 o = o + 1
64 n = n + 1
65 }
66 let sx: *NxDmsSearchIndex = nx_dms_search_new(n + 16)
67 o = 0
68 while o < len {
69 let aid: i64 = ((blob[o + 0] as i64) << 56) | ((blob[o + 1] as i64) << 48) | ((blob[o + 2] as i64) << 40) | ((blob[o + 3] as i64) << 32) | ((blob[o + 4] as i64) << 24) | ((blob[o + 5] as i64) << 16) | ((blob[o + 6] as i64) << 8) | (blob[o + 7] as i64)
70 o = o + 8
71 let tp: *u8 = (blob as i64 + o) as *u8
72 nx_dms_search_add(sx, aid, tp)
73 while blob[o] != (0 as u8) { o = o + 1 }
74 o = o + 1
75 }
76 return sx
77}