code wiki / (root) / nx_galx_vidoff_nas.nx

nx_galx_vidoff_nas.nx source

↩ module page · 36 lines · 2445 B

1// nx_galx_vidoff_nas.nx -- NAS-native rebuild of galx_vid_off.bin (the id->byte-offset index) so the 2// gallery serve does O(1) /vid lookups instead of a full-file scan per request (60 video tiles/page). 3// Runs ON the NAS via nx_nas_run after a catalog reindex. Mirrors gs_vidoff_build in nx_gallery_serve. 4// Header (big-endian): tsv_size(8) n(8) then n*offset(8). tsv_size = the serve's freshness check. No sh. 5// license_tier: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 8 9const TSV: *u8 = "/volume1/ai/galx/knowledge/status/galx_vid_paths.tsv" 10const OFF: *u8 = "/volume1/ai/galx/knowledge/status/galx_vid_off.bin" 11 12func vo_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 14// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 15// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 16// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 17func vo_n(v: i64) -> i64 { nxi_out(v); return 0 } 18func vo_wb64(b: *u8, o: i64, v: i64) -> i64 { b[o]=((v>>56)&0xff) as u8; b[o+1]=((v>>48)&0xff) as u8; b[o+2]=((v>>40)&0xff) as u8; b[o+3]=((v>>32)&0xff) as u8; b[o+4]=((v>>24)&0xff) as u8; b[o+5]=((v>>16)&0xff) as u8; b[o+6]=((v>>8)&0xff) as u8; b[o+7]=(v&0xff) as u8; return o+8 } 19 20func main() -> i64 { 21 let szp: *i64 = sys_mmap(16) as *i64 22 let b: *u8 = sys_read_file(TSV, szp) 23 if (b as i64) == 0 { vo_p("VIDOFF-NAS FAIL: cannot read " as *u8); vo_p(TSV); vo_p("\n" as *u8); sys_exit(1); return 1 } 24 let sz: i64 = szp[0] 25 var n: i64 = 1; var i: i64 = 0 26 while i < sz { if b[i]==(10 as u8) { if i+1 < sz { n=n+1 } } i=i+1 } 27 let ob: *u8 = sys_mmap(16 + n*8 + 64) 28 vo_wb64(ob, 0, sz); vo_wb64(ob, 8, n); vo_wb64(ob, 16, 0) 29 var cnt: i64 = 1; i = 0 30 while i < sz { if b[i]==(10 as u8) { if i+1 < sz { if cnt < n { vo_wb64(ob, 16+cnt*8, i+1); cnt=cnt+1 } } } i=i+1 } 31 let wf: i64 = sys_openat_wr(OFF, 0x1a4) 32 if wf < 0 { vo_p("VIDOFF-NAS FAIL: cannot write " as *u8); vo_p(OFF); vo_p("\n" as *u8); sys_exit(1); return 1 } 33 sys_write(wf, ob, 16 + n*8); sys_close(wf) 34 vo_p("VIDOFF-NAS OK n=" as *u8); vo_n(n); vo_p(" tsv_size=" as *u8); vo_n(sz); vo_p(" -> " as *u8); vo_p(OFF); vo_p("\n" as *u8) 35 sys_exit(0); return 0 36}