code wiki / _hdl_build / nx_shard_compact.nx
nx_shard_compact.nx source
↩ module page · 50 lines · 2943 B
1// nx_shard_compact.nx -- fold a domain's PUBLIC shard to ONE segment via the store's own cap-aware
2// compaction (ss_compact_cap: data-driven writer, fail-before-swap crash-safe). This is ALSO the
3// positions-sidecar UPGRADE for pre-phrase shards: the merged segment is rebuilt through ss_write_seg,
4// which now emits seg-<id>.pos -- quoted phrases go EXACT after one compaction.
5// usage: nx_shard_compact <domain>
6// license_tier: ORIGINAL
7import "nx_docportal_search_seg.nx"
8const K_MAGIC_4096: i64 = 4096
9
10func sc_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
11func sc_num(v: i64) -> i64 {
12 let bb: *u8 = sys_mmap(28); var m: i64 = v
13 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
14 let t: *u8 = sys_mmap(28); var k: i64 = 0
15 if m == 0 { t[0] = 48 as u8; k = 1 }
16 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
17 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
18 sys_write(1, bb, k); return 0
19}
20
21func main(argc: i64, argv: *i64) -> i64 {
22 if argc < 2 { sc_puts("usage: nx_shard_compact <domain>\n" as *u8); return 1 }
23 let domain: *u8 = argv[1] as *u8
24 let prefix: *u8 = sys_mmap(512)
25 dss_prefix(domain, prefix)
26 // fresh segid past the manifest max (compaction swaps the manifest to ONLY this segment)
27 let segs: *i64 = sys_mmap(K_MAGIC_4096 * 8) as *i64
28 let ns: i64 = ss_manifest_cap(prefix, segs, K_MAGIC_4096)
29 if ns <= 0 { sc_puts("no shard for domain\n" as *u8); return 2 }
30 var segid: i64 = 1
31 var si: i64 = 0
32 // ROOT FIX seq1730 (id 1785450987): segs[] holds POINTERS to seg-<id> name strings
33 // (nx_seg_store.nx:1234 stores segs[cnt] = name as i64), NOT ids -- so segs[si] + 1 produced
34 // MMAP_ADDRESS+1 (~1.4e14), the pointer-shaped poison that PINS a plane forever: every later
35 // epoch id sorts BELOW it in supersede order and its rows are silently shadowed while rc=0.
36 // Parse the DIGITS, as nx_web_shard_compact.nx:55-58 already does on this SAME array.
37 while si < ns {
38 let sg_nm: *u8 = segs[si] as *u8
39 var sg_v: i64 = 0
40 var sg_ci: i64 = 0
41 while sg_nm[sg_ci] != (0 as u8) { let sg_c: i64 = sg_nm[sg_ci] as i64; if sg_c >= 48 { if sg_c <= 57 { sg_v = sg_v * 10 + (sg_c - 48) } } sg_ci = sg_ci + 1 }
42 if sg_v >= segid { segid = sg_v + 1 }
43 si = si + 1
44 }
45 sc_puts("compacting " as *u8); sc_puts(prefix); sc_puts(" (" as *u8); sc_num(ns); sc_puts(" manifest entries) -> seg-" as *u8); sc_num(segid); sc_puts("\n" as *u8)
46 let rc: i64 = ss_compact_cap(prefix, segid, K_MAGIC_4096)
47 if rc != segid { sc_puts("COMPACT-FAIL rc=" as *u8); sc_num(rc); sc_puts(" (live store untouched -- fail-before-swap)\n" as *u8); return 3 }
48 sc_puts("COMPACTED -> seg-" as *u8); sc_num(segid); sc_puts(" (positions sidecar materialized; phrase queries now EXACT on this shard)\n" as *u8)
49 return 0
50}