code wiki / (root) / nx_lib_compact.nx

nx_lib_compact.nx source

↩ module page · 40 lines · 2024 B

1// nx_lib_compact.nx -- compact the library seg_store: merge the many small 2// segments (a killed per-record ingest left ~257) into one (latest-per-key 3// wins -- additive, no data loss), restoring put_work headroom + read speed. 4import "nx_syscalls.nx" 5import "nx_lib_store.nx" 6 7func cp_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 8func cp_putn(v: i64) -> i64 { 9 let bb: *u8 = sys_mmap(28); var m: i64 = v 10 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } 11 let t: *u8 = sys_mmap(28); var k: i64 = 0 12 if m == 0 { t[0] = 48 as u8; k = 1 } 13 while m > 0 { t[k] = (48 + (m - (m/10)*10)) as u8; m = m / 10; k = k + 1 } 14 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 15} 16 17func main() -> i64 { 18 let segp: *i64 = sys_mmap(16) as *i64 19 let ns: i64 = ss_manifest_dyn(LIB_PREFIX, segp) // uncapped TRUE count (legacy ss_manifest tops out at its cap) 20 cp_puts("segments before="); cp_putn(ns); cp_puts("\n") 21 let cnt0: i64 = nx_lib_store_count() 22 cp_puts("works before="); cp_putn(cnt0); cp_puts("\n") 23 24 // fresh max+1 segid: passing the CAPPED count as the new segid could COLLIDE with a live segment 25 // past the cap (overwriting its .docs while the archive still references the name = corrupted history) 26 let r: i64 = ss_compact(LIB_PREFIX, ss_next_segid(LIB_PREFIX)) 27 cp_puts("compact rc="); cp_putn(r); cp_puts("\n") 28 29 let ns2: i64 = ss_manifest_dyn(LIB_PREFIX, segp) 30 cp_puts("segments after="); cp_putn(ns2); cp_puts("\n") 31 let cnt1: i64 = nx_lib_store_count() 32 cp_puts("works after="); cp_putn(cnt1); cp_puts("\n") 33 34 // spot-check a known real taxon still resolves 35 let po: *i64 = sys_mmap(16) as *i64 36 let lo: *i64 = sys_mmap(16) as *i64 37 if nx_lib_store_get_work("GBIF_1000003" as *u8, po, lo) >= 0 { cp_puts("GBIF_1000003 OK: "); sys_write(1, po[0] as *u8, lo[0]); cp_puts("\n") } 38 else { cp_puts("GBIF_1000003 MISSING\n") } 39 return 0 40}