code wiki / _hdl_build / nx_search_register.nx

nx_search_register.nx source

↩ module page · 70 lines · 4124 B

1// nx_search_register.nx -- INTEGRATION DISCOVERY (loose coupling). Discovers every queryable Nishi index and 2// writes the search-sources MANIFEST (the integration CONTRACT): knowledge/index/search_sources.txt, one line 3// `<label> <idx_path> <docmap_path> <tier>`. Producers (nx_library_harvest_v2, nx_research_grow) just WRITE 4// their indexes -- they do NOT know about the search engine. This registrar DISCOVERS them. The federated 5// searcher CONSUMES the manifest -- it does NOT know about producers. Three decoupled stages talk only through 6// the manifest artifact = proper integration, no tight coupling. Adding an index source = it gets discovered 7// here; the searcher needs zero changes. expect_exit: 0 license_tier: ORIGINAL 8import "nx_search_inverted_persist.nx" // brings syscalls (sys_openat_*/write/mmap); proven to resolve from _hdl_build 9const SR_MAGIC_1024: i64 = 1024 10 11const SR_MAXSHARD: i64 = 4096 12 13func sr_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 14func sr_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 } 15func sr_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[off+i]=s[i];i=i+1} return off+i } 16func sr_catnum(dst: *u8, off: i64, v: i64) -> i64 { 17 if v == 0 { dst[off]=48 as u8; return off+1 } 18 let tmp: *u8=sys_mmap(28); var m: i64=v; var k: i64=0 19 while m>0 { tmp[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } 20 var i: i64=0; while i<k { dst[off+i]=tmp[k-1-i]; i=i+1 } 21 return off+k 22} 23func sr_exists(path: *u8) -> i64 { 24 let fd: i64 = sys_openat_rd(path) 25 if fd < 0 { return 0 } 26 sys_close(fd) 27 return 1 28} 29// write a manifest line iff both idx and docmap exist; returns 1 if written 30func sr_emit(fd: i64, label: *u8, idx: *u8, docmap: *u8, tier: *u8) -> i64 { 31 if sr_exists(idx) == 0 { return 0 } 32 if sr_exists(docmap) == 0 { return 0 } 33 let line: *u8 = sys_mmap(SR_MAGIC_1024); var lp: i64 = 0 34 lp=sr_cat(line,lp,label); line[lp]=32 as u8; lp=lp+1 35 lp=sr_cat(line,lp,idx); line[lp]=32 as u8; lp=lp+1 36 lp=sr_cat(line,lp,docmap); line[lp]=32 as u8; lp=lp+1 37 lp=sr_cat(line,lp,tier); line[lp]=10 as u8; lp=lp+1 38 sys_write(fd, line, lp) 39 return 1 40} 41 42func main() -> i64 { 43 sr_puts("=== nx_search_register: discover indexes -> search-sources manifest (loose-coupled integration) ===\n" as *u8) 44 let man: *u8 = "knowledge/index/search_sources.txt" as *u8 45 let fd: i64 = sys_openat_wr(man, 0x1a4) 46 if fd < 0 { sr_puts(" FAIL open manifest\n" as *u8); sys_exit(1); return 1 } 47 var n: i64 = 0 48 49 // ecosystem-docs index (produced by nx_library_harvest_v2) 50 n = n + sr_emit(fd, "library" as *u8, "knowledge/index/library.idx" as *u8, "knowledge/index/library.docmap" as *u8, "ecosystem" as *u8) 51 52 // researcher growing index shards (produced by nx_research_grow) -- discover contiguously 53 var s: i64 = 0 54 var stop: i64 = 0 55 while stop == 0 { 56 if s >= SR_MAXSHARD { stop = 1 } 57 else { 58 let ip: *u8 = sys_mmap(256); var ic: i64 = sr_cat(ip,0,"knowledge/index/shards/research_" as *u8); ic=sr_catnum(ip,ic,s); ic=sr_cat(ip,ic,".idx" as *u8); ip[ic]=0 as u8 59 let dp: *u8 = sys_mmap(256); var dc: i64 = sr_cat(dp,0,"knowledge/index/shards/research_" as *u8); dc=sr_catnum(dp,dc,s); dc=sr_cat(dp,dc,".docmap" as *u8); dp[dc]=0 as u8 60 let lb: *u8 = sys_mmap(64); var lc: i64 = sr_cat(lb,0,"research_" as *u8); lc=sr_catnum(lb,lc,s); lb[lc]=0 as u8 61 if sr_exists(ip) == 0 { stop = 1 } 62 else { n = n + sr_emit(fd, lb, ip, dp, "research" as *u8); s = s + 1 } 63 } 64 } 65 sys_close(fd) 66 sr_puts(" registered indexes="); sr_num(n); sr_puts(" -> knowledge/index/search_sources.txt\n" as *u8) 67 if n <= 0 { sr_puts(" NO indexes discovered\n" as *u8); sys_exit(1); return 1 } 68 sr_puts(" REGISTER-OK\n" as *u8) 69 sys_exit(0); return 0 70}