code wiki / (root) / nx_inv_bounded_gate.nx

nx_inv_bounded_gate.nx source

↩ module page · 53 lines · 4040 B

1// nx_inv_bounded_gate.nx -- proves the inverted index works with a BOUNDED, PE/bare-metal-portable footprint 2// (nx_inv_new_bounded) instead of the 4 GiB Linux-lazy-mmap reservation. This removes the #1 friction blocking the 3// index from compiling to a Windows PE / bare-metal target -> the multi-target / platform-independence path. Verified 4// on Linux here; the same bounded index now fits a HeapAlloc/VirtualAlloc HAL. license_tier: ORIGINAL 5import "nx_search_inverted.nx" 6 7func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 8func gn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var w: i64=0; var q: i64=k-1; while q>=0 { o[w]=t[q]; w=w+1; q=q-1 } sys_write(1,o,w); return 0 } 9func bg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 10func bg_has(idx: *NxInvIndex, term: *u8, docid: i64) -> i64 { 11 let h: i64 = nx_inv_hash_bytes_lower(term, bg_slen(term)) 12 let slot: *u8 = nx_inv_lookup_slot(idx, h) 13 if (slot as i64)==0 { return 0 } 14 let wc: i64 = nx_inv_slot_write_cursor(slot); let off: i64 = nx_inv_slot_postings_offset(slot) 15 var k: i64=0 16 while k<wc { let p: *u8=((idx.postings_ptr as i64)+off+k*4) as *u8; let id: i64=(p[0] as i64)|((p[1] as i64)<<8)|((p[2] as i64)<<16)|((p[3] as i64)<<24); if id==docid { return 1 } k=k+1 } 17 return 0 18} 19 20func main() -> i64 { 21 gw("=== nx_inv_bounded_gate: inverted index in a BOUNDED, PE/bare-metal-portable footprint ===\n" as *u8) 22 var pass: i64=0; var tot: i64=0 23 let VOCAB: i64 = 1024 24 let POOL: i64 = 65536 25 let idx: *NxInvIndex = nx_inv_new_bounded(VOCAB, POOL) // ~32KB vocab + 64KB postings = HeapAlloc-friendly 26 27 let d0: *u8 = "sovereign crawler index" as *u8 28 let d1: *u8 = "inverted postings list" as *u8 29 let d2: *u8 = "pagerank damping authority" as *u8 30 nx_inv_index_row(idx, d0, bg_slen(d0), 0); nx_inv_index_row(idx, d1, bg_slen(d1), 1); nx_inv_index_row(idx, d2, bg_slen(d2), 2) 31 nx_inv_finalize_offsets(idx) 32 nx_inv_emit_row(idx, d0, bg_slen(d0), 0); nx_inv_emit_row(idx, d1, bg_slen(d1), 1); nx_inv_emit_row(idx, d2, bg_slen(d2), 2) 33 34 tot=tot+1; if idx.postings_used > 0 { if idx.postings_used <= POOL { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 35 gw("T1 postings fit the bounded pool (" as *u8); gn(idx.postings_used); gw("/" as *u8); gn(POOL); gw(" bytes, no 4GiB reservation)\n" as *u8) 36 37 tot=tot+1; if bg_has(idx,"crawler" as *u8,0)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 38 gw("T2 query 'crawler' -> doc0\n" as *u8) 39 tot=tot+1; if bg_has(idx,"postings" as *u8,1)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 40 gw("T3 query 'postings' -> doc1\n" as *u8) 41 tot=tot+1; if bg_has(idx,"damping" as *u8,2)==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 42 gw("T4 query 'damping' -> doc2\n" as *u8) 43 tot=tot+1; if bg_has(idx,"zznope" as *u8,0)==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 44 gw("T5 absent term -> none\n" as *u8) 45 46 let footprint: i64 = VOCAB*NX_INV_SLOT_BYTES + idx.postings_used 47 tot=tot+1; if footprint < 131072 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 48 gw("T6 total footprint " as *u8); gn(footprint); gw(" bytes < 128KB (fits HeapAlloc/VirtualAlloc HAL -> PE/bare-metal portable)\n" as *u8) 49 50 gw("\n=== nx_inv_bounded_gate " as *u8); gn(pass); gw("/" as *u8); gn(tot); gw(" ===\n" as *u8) 51 if pass==tot { gw("INV-BOUNDED GREEN -- the SAME index engine in a bounded footprint; friction #1 (4GiB mmap) removed -> index is now multi-target portable (Linux ELF / Windows PE / bare-metal)\n" as *u8); sys_exit(0); return 0 } 52 gw("INV-BOUNDED RED\n" as *u8); sys_exit(1); return 1 53}