nx_imgsearch.nx
buildroot/runtime/nx_imgsearch.nx
about
nx_imgsearch.nx -- THE REVERSE-IMAGE SERVICE ORGAN: the MCP/API face of the multi-tier engine.
Every verb emits STRICT JSON on stdout, because the callers are agents and workflows, not humans.
That drives three rules the whole file obeys:
* HONEST ABSENT. "not found" is `{"present":false}` with a reason, never an empty list that a
caller can mistake for "not searched" and never the nearest thing dressed up as a match.
* SELF-DESCRIBING. `status` returns the live tier roster, every threshold, and the MEASURED
per-class robustness with its gaps named, so an agent can decide whether this engine can answer
its question before asking. A tool that cannot state its own limits cannot be trusted with one.
* EVERY RESULT CARRIES ITS PROVENANCE -- which tier matched, at what distance, at which
orientation, and whether that tier was CONFIDENT (inside its own threshold) or merely nearest.
INDEX FORMAT (versioned, fixed-stride, mmap-first -- the scale decision):
header 32B : magic "NXIMGIDX" | u32 version | u32 stride | i64 count | i64 cidblob_off
record 96B : i64 dhash | u8 visdesc[80] | u32 cid_off | u32 cid_len
then : the cid string blob
Fixed stride means O(1) random access and no parsing, so the index is opened with sys_map_file --
the same read-only file-backed map that moved the text shard off the RAM ceiling onto the page
cache. The 80-dimension descriptor is QUANTISED TO u8 AT INGEST (its dimensions are already on a
0..256 scale, so this costs at most one unit on a saturated bin): 96 bytes per image on disk
against 648 bytes unquantised, which is the difference between a 10M-image index fitting a NAS
volume and not. Same integer-quantisation-at-ingest discipline the text engine rides.
HONEST CEILING, reported in the JSON rather than hidden: `query` currently EXPANDS the mmap'd
records into the engine's i64 descriptor arrays, so serving RAM is ~648 bytes/image even though
storage is 96. The named fix is a u8-native similarity tier that computes L1 straight off the
mapped bytes, making serving disk-bound like the text side. Until that lands the ceiling is real
and this tool prints it.
license_tier: ORIGINAL
dependencies 5 imports · 0 importers
imports: nx_imgsearch_engine.nxnx_itoa_lib.nxnx_imgcorpus.nxnx_image_gray.nxnx_dir.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| 199 | struct SwIngest |
consts
| 35 | const IS_MAGIC_1024: i64 = 1024 |
| 36 | const IS_MAGIC_4096: i64 = 4096 |
| 37 | const IS_MAGIC_99999: i64 = 99999 |
| 39 | const IS_MAGIC0: i64 = 78 // 'N' |
| 40 | const IS_VERSION: i64 = 2 // v2 adds the LOCAL keypoint pack per record (occlusion-robust tier) |
| 43 | const IS_STRIDE: i64 = 1256 |
| 44 | const IS_LOCAL_OFF: i64 = 96 // byte offset of the local pack within a record |
| 45 | const IS_LOCAL_N: i64 = 145 // i64 slots in the local pack (kp_pack_dim) |
| 46 | const IS_HDR: i64 = 32 |
| 47 | const IS_VDIM: i64 = 80 |
| 48 | const IS_MAXTOPK: i64 = 32 |
| 49 | const IS_RAM_PER_IMG: i64 = 1816 // i64 descriptor footprint per image once expanded for serving |
| 57 | const IS_WORKDIM: i64 = 512 |
| 208 | const SWINGEST_BYTES: i64 = 56 |
| 385 | const SW_WALK_DEFAULT: i64 = 400 |
| 386 | const SW_WALK_HARDCAP: i64 = 50000 |
functions
| 59 | func s_puts(p: *u8) -> i64 { var n: i64 = 0; while p[n] != (0 as u8) { n = n + 1 } sys_write(1, p, n); return 0 } |
| 60 | func s_putb(p: *u8, n: i64) -> i64 { if n > 0 { sys_write(1, p, n) } return 0 } |
| 65 | func s_num(v: i64) -> i64 { nxi_out(v); return 0 } called by 6: s_emit_tierss_verb_statuss_verb_indexs_verb_walkdirs_verb_querys_verb_selftest calls 1: nxi_out |
| 66 | func s_strlen(p: *u8) -> i64 { var n: i64 = 0; while p[n] != (0 as u8) { n = n + 1 } return n } |
| 67 | func s_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } if b[i] != (0 as u8) { return 0 } return 1 } called by 1: main |
| 68 | func s_atoi(p: *u8) -> i64 { var v: i64 = 0; var i: i64 = 0; while p[i] != (0 as u8) { if p[i] >= (48 as u8) { if p[i] <= (57 as u8) { v = v * 10 + ((p[i] - (48 as u8)) as i64) } } i = i + 1 } return v } called by 1: main |
| 72 | func s_jstr(p: *u8, n: i64) -> i64 |
| 84 | func s_jkey(k: *u8) -> i64 { s_puts("\"" as *u8); s_puts(k); s_puts("\":" as *u8); return 0 } called by 6: s_emit_tierss_verb_statuss_verb_indexs_verb_walkdirs_verb_querys_verb_selftest calls 1: s_puts |
| 87 | func s_put_i64(b: *u8, off: i64, v: i64) -> i64 |
| 92 | func s_get_i64(b: *u8, off: i64) -> i64 called by 1: s_verb_query |
| 98 | func s_put_u32(b: *u8, off: i64, v: i64) -> i64 |
| 103 | func s_get_u32(b: *u8, off: i64) -> i64 called by 1: s_verb_query |
| 111 | func s_build_engine(cap: i64) -> *nx_imgengine called by 3: s_verb_statuss_verb_querys_verb_selftest calls 3: nx_imgengine_newnx_imgengine_add_tiernx_imgtier_new |
| 120 | func s_klass_name(k: i64) -> *u8 |
| 125 | func s_emit_tiers(e: *nx_imgengine) -> i64 called by 1: s_verb_status calls 12: s_jkeys_putsnx_imgengine_ntiersnx_imgengine_tiernx_imgtier_names_klass_name+6 |
| 148 | func s_verb_status() -> i64 |
| 213 | func s_downscale_max(gray: *u8, w: i64, h: i64, maxdim: i64, outwh: *i64) -> *u8 |
| 237 | func s_ingest_new() -> *SwIngest |
| 251 | func s_ingest_file(g: *SwIngest, recs: *u8, nrec: i64, cidb: *u8, cpos: i64, cid: *u8, path: *u8) -> i64 |
| 286 | func s_write_index(outpath: *u8, recs: *u8, nrec: i64, cidb: *u8, cpos: i64) -> i64 |
| 306 | func s_verb_index(manifest: *u8, outpath: *u8) -> i64 |
| 388 | func s_path_join(root: *u8, name: *u8, out: *u8) -> i64 called by 1: s_walkdir_run |
| 399 | func s_is_image_name(name: *u8, nl: i64) -> i64 |
| 437 | func s_suffix_path(path: *u8, suf: *u8, out: *u8) -> *u8 |
| 447 | func s_fw(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } |
| 452 | func s_fwn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 } |
| 453 | func s_fwstr(fd: i64, p: *u8) -> i64 |
| 464 | func s_walkdir_run(root: *u8, outpath: *u8, maxn: i64, fd: i64) -> i64 |
| 526 | func s_clamp_max(max_in: i64) -> i64 |
| 534 | func s_verb_walkdirsync(root: *u8, outpath: *u8, max_in: i64) -> i64 |
| 539 | func s_verb_walkdir(root: *u8, outpath: *u8, max_in: i64) -> i64 |
| 580 | func s_verb_walkstatus(idxpath: *u8) -> i64 |
| 601 | func s_verb_query(idxpath: *u8, imgpath: *u8, topk_in: i64) -> i64 |
| 748 | func s_verb_selftest() -> i64 called by 1: main calls 7: s_build_enginenx_imgengine_add_imagenx_imgcorpus_newnx_imgengine_best_matchs_putss_jkey+1 |
| 785 | func main(argc: i64, argv: *i64) -> i64 |