code wiki / (root) / nx_lib_gbif_probe.nx

nx_lib_gbif_probe.nx source

↩ module page · 74 lines · 3319 B

1// nx_lib_gbif_probe.nx -- ingest the REAL library GBIF data from disk into 2// nx_lib_store, index it, and prove taxa are served + full-text searchable. 3import "nx_syscalls.nx" 4import "nx_lib_gbif.nx" 5import "nx_lib_serve.nx" 6import "nx_lib_index.nx" 7const K_MAGIC_262144: i64 = 262144 8const K_MAGIC_2097152: i64 = 2097152 9 10func gp_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 11func gp_wr(p: *u8, n: i64) -> i64 { sys_write(1, p, n); return 0 } 12func gp_putn(v: i64) -> i64 { 13 let bb: *u8 = sys_mmap(28); var m: i64 = v 14 let t: *u8 = sys_mmap(28); var k: i64 = 0 15 if m == 0 { t[0] = 48 as u8; k = 1 } 16 while m > 0 { t[k] = (48 + (m - (m/10)*10)) as u8; m = m / 10; k = k + 1 } 17 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 18} 19 20func main() -> i64 { 21 let lb: *i64 = sys_mmap(16) as *i64 22 let buf: *u8 = sys_read_file("knowledge/fetched/gbif_sample.jsonl\x00" as *u8, lb) 23 if (buf as i64) == 0 { gp_puts("cannot read gbif_sample.jsonl\n"); return 1 } 24 let n: i64 = lb[0] 25 gp_puts("gbif jsonl bytes="); gp_putn(n); gp_puts("\n") 26 27 let count: i64 = nx_lib_gbif_ingest_buf(buf, n) 28 gp_puts("*** ingested REAL GBIF taxa into nx_lib_store: "); gp_putn(count); gp_puts(" ***\n") 29 if count == 0 { return 2 } 30 31 nx_lib_index_build() 32 let out: *u8 = sys_mmap(K_MAGIC_262144) 33 34 // serve the first few GBIF taxa (hk starts "GBIF_") 35 gp_puts("--- sample taxa served from store ---\n") 36 let idxbuf: *u8 = sys_mmap(K_MAGIC_2097152) 37 let inn: i64 = nx_lib_store_index(idxbuf) 38 let hkb: *u8 = sys_mmap(256) 39 var ls: i64 = 0 40 var ii: i64 = 0 41 var shown: i64 = 0 42 while ii <= inn { 43 var eol: i64 = 0 44 if ii == inn { eol = 1 } else { if idxbuf[ii] == 10 as u8 { eol = 1 } } 45 if eol == 1 { 46 if ii > ls { 47 var k: i64 = 0; var j: i64 = ls 48 while j < ii { hkb[k] = idxbuf[j]; k = k + 1; j = j + 1 } 49 hkb[k] = 0 as u8 50 var isg: i64 = 0 51 if k >= 5 { if hkb[0] == 71 as u8 { if hkb[1] == 66 as u8 { if hkb[2] == 73 as u8 { if hkb[3] == 70 as u8 { isg = 1 } } } } } 52 if isg == 1 { if shown < 3 { let jn: i64 = nx_lib_serve_work(hkb, out); if jn > 0 { gp_wr(out, jn); gp_puts("\n") } shown = shown + 1 } } 53 } 54 ls = ii + 1 55 } 56 ii = ii + 1 57 } 58 59 gp_puts("--- FULL-TEXT search over the real taxonomy ---\n") 60 gp_puts("q=Amaryllidaceae -> "); let s1: i64 = nx_lib_index_search("Amaryllidaceae" as *u8, 14, out); op_count(out, s1); gp_puts("\n") 61 gp_puts("q=Plantae -> "); let s2: i64 = nx_lib_index_search("Plantae" as *u8, 7, out); op_count(out, s2); gp_puts("\n") 62 gp_puts("q=Allium -> "); let s3: i64 = nx_lib_index_search("Allium" as *u8, 6, out); gp_wr(out, s3); gp_puts("\n") 63 gp_puts("--- end (REAL GBIF library data, all sovereign) ---\n") 64 return 0 65} 66 67// print how many hks are in a JSON array result (count of commas + 1 if non-empty). 68func op_count(out: *u8, n: i64) -> i64 { 69 if n <= 2 { gp_puts("0 hits"); return 0 } 70 var c: i64 = 1 71 var i: i64 = 0 72 while i < n { if out[i] == 44 as u8 { c = c + 1 } i = i + 1 } 73 gp_putn(c); gp_puts(" hits"); return 0 74}