code wiki / (root) / nx_imgsearch.nx

nx_imgsearch.nx

buildroot/runtime/nx_imgsearch.nx

43119 B834 linesdepth 11pulls 66 transitivereach 0 importersview sourcekind tooltopic imgsearch
docsdependenciesstructsconstsfunctions

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

nx_imgsearch_engine.nx nx_itoa_lib.nx nx_imgcorpus.nx nx_image_gray.nx nx_dir.nx nx_imgsearch.nx

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

main s_verb_status s_build_engine nx_imgengine_new nx_imgengine_add_tier nx_imgtier_idx_dim ie_tier_is_hash nx_imgtier_idx_dim ↻ nx_imgtier_kind pi_init_children nx_imgtier_new nx_imgtier_copy_init nx_imgtier_orient_init nx_imgtier_similar_init nx_imgtier_local_init kp_pack_dim nx_imgkp_query_dim s_puts s_jkey s_puts ↻ s_num nxi_out nxi_fd sys_mmap ccz_cat_num sys_write sys_munmap s_emit_tiers s_jkey ↻ s_puts ↻ nx_imgengine_ntiers nx_imgengine_tier nx_imgtier_name s_klass_name nx_imgtier_klass s_num ↻ nx_imgtier_threshold nx_imgtier_idx_dim ↻ nx_imgtier_qry_dim nx_imgtier_weight

structs

199struct SwIngest

consts

35const IS_MAGIC_1024: i64 = 1024
36const IS_MAGIC_4096: i64 = 4096
37const IS_MAGIC_99999: i64 = 99999
39const IS_MAGIC0: i64 = 78 // 'N'
40const IS_VERSION: i64 = 2 // v2 adds the LOCAL keypoint pack per record (occlusion-robust tier)
43const IS_STRIDE: i64 = 1256
44const IS_LOCAL_OFF: i64 = 96 // byte offset of the local pack within a record
45const IS_LOCAL_N: i64 = 145 // i64 slots in the local pack (kp_pack_dim)
46const IS_HDR: i64 = 32
47const IS_VDIM: i64 = 80
48const IS_MAXTOPK: i64 = 32
49const IS_RAM_PER_IMG: i64 = 1816 // i64 descriptor footprint per image once expanded for serving
57const IS_WORKDIM: i64 = 512
208const SWINGEST_BYTES: i64 = 56
385const SW_WALK_DEFAULT: i64 = 400
386const SW_WALK_HARDCAP: i64 = 50000

functions

59func 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 }
60func s_putb(p: *u8, n: i64) -> i64 { if n > 0 { sys_write(1, p, n) } return 0 }
65func s_num(v: i64) -> i64 { nxi_out(v); return 0 }
66func s_strlen(p: *u8) -> i64 { var n: i64 = 0; while p[n] != (0 as u8) { n = n + 1 } return n }
67func 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
68func 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
72func s_jstr(p: *u8, n: i64) -> i64
84func s_jkey(k: *u8) -> i64 { s_puts("\"" as *u8); s_puts(k); s_puts("\":" as *u8); return 0 }
87func s_put_i64(b: *u8, off: i64, v: i64) -> i64
92func s_get_i64(b: *u8, off: i64) -> i64
called by 1: s_verb_query
98func s_put_u32(b: *u8, off: i64, v: i64) -> i64
103func s_get_u32(b: *u8, off: i64) -> i64
called by 1: s_verb_query
111func s_build_engine(cap: i64) -> *nx_imgengine
120func s_klass_name(k: i64) -> *u8
125func s_emit_tiers(e: *nx_imgengine) -> i64
148func s_verb_status() -> i64
213func s_downscale_max(gray: *u8, w: i64, h: i64, maxdim: i64, outwh: *i64) -> *u8
237func s_ingest_new() -> *SwIngest
251func s_ingest_file(g: *SwIngest, recs: *u8, nrec: i64, cidb: *u8, cpos: i64, cid: *u8, path: *u8) -> i64
286func s_write_index(outpath: *u8, recs: *u8, nrec: i64, cidb: *u8, cpos: i64) -> i64
306func s_verb_index(manifest: *u8, outpath: *u8) -> i64
388func s_path_join(root: *u8, name: *u8, out: *u8) -> i64
called by 1: s_walkdir_run
399func s_is_image_name(name: *u8, nl: i64) -> i64
called by 1: s_walkdir_run calls 1: nx_dir_name_ends_with
437func s_suffix_path(path: *u8, suf: *u8, out: *u8) -> *u8
447func 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 }
452func s_fwn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 }
called by 1: s_walkdir_run calls 1: nxi_fd
453func s_fwstr(fd: i64, p: *u8) -> i64
called by 1: s_walkdir_run calls 1: s_fw
464func s_walkdir_run(root: *u8, outpath: *u8, maxn: i64, fd: i64) -> i64
526func s_clamp_max(max_in: i64) -> i64
534func s_verb_walkdirsync(root: *u8, outpath: *u8, max_in: i64) -> i64
called by 1: main calls 2: s_walkdir_runs_clamp_max
539func s_verb_walkdir(root: *u8, outpath: *u8, max_in: i64) -> i64
580func s_verb_walkstatus(idxpath: *u8) -> i64
601func s_verb_query(idxpath: *u8, imgpath: *u8, topk_in: i64) -> i64
748func s_verb_selftest() -> i64
785func main(argc: i64, argv: *i64) -> i64