nx_lib_openalex_probe.nx source
↩ module page · 90 lines · 4296 B
1// nx_lib_openalex_probe.nx -- LIVE: fetch real works from OpenAlex over
2// sovereign TLS-1.3, ingest into nx_lib_store, build the index, and prove they
3// are served + searchable. The whole sovereign library on real academic data.
4import "nx_syscalls.nx"
5import "nx_lib_fetch.nx"
6import "nx_lib_openalex.nx"
7import "nx_lib_serve.nx"
8import "nx_lib_index.nx"
9const K_MAGIC_4194304: i64 = 4194304
10const K_MAGIC_262144: i64 = 262144
11const K_MAGIC_1048576: i64 = 1048576
12
13func op_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 op_wr(p: *u8, n: i64) -> i64 { sys_write(1, p, n); return 0 }
15func op_putn(v: i64) -> i64 {
16 let bb: *u8 = sys_mmap(28); var m: i64 = v
17 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) }
18 let t: *u8 = sys_mmap(28); var k: i64 = 0
19 if m == 0 { t[0] = 48 as u8; k = 1 }
20 while m > 0 { t[k] = (48 + (m - (m/10)*10)) as u8; m = m / 10; k = k + 1 }
21 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0
22}
23func op_body(buf: *u8, n: i64) -> i64 {
24 var bi: i64 = 0
25 while bi + 3 < n {
26 if buf[bi] == 13 as u8 { if buf[bi+1] == 10 as u8 { if buf[bi+2] == 13 as u8 { if buf[bi+3] == 10 as u8 { return bi + 4 } } } }
27 bi = bi + 1
28 }
29 return 0 - 1
30}
31
32func main() -> i64 {
33 let store: *TrustStore = nx_lib_fetch_store()
34 if store == 0 as *TrustStore { op_puts("no CA store\n"); return 1 }
35
36 let url: *u8 = "https://api.openalex.org/works?filter=publication_year:2023,cited_by_count:>2000&per-page=4&select=id,title,doi,publication_year,authorships,abstract_inverted_index&mailto=elderwesto@gmail.com\x00"
37 let cap: i64 = K_MAGIC_4194304
38 let buf: *u8 = sys_mmap(cap)
39 op_puts("fetching real works from OpenAlex over sovereign TLS-1.3...\n")
40 let gc: i64 = nx_lib_fetch(store, url, buf, cap)
41 op_puts("fetch bytes="); op_putn(gc); op_puts("\n")
42 if gc < 0 { return 2 }
43 let bs: i64 = op_body(buf, gc)
44 if bs < 0 { op_puts("no body\n"); return 3 }
45 let bp: *u8 = ((buf as i64) + bs) as *u8
46 let bl: i64 = gc - bs
47
48 let count: i64 = nx_lib_openalex_ingest_buf(bp, bl)
49 op_puts("*** ingested REAL works into nx_lib_store: "); op_putn(count); op_puts(" ***\n")
50 if count == 0 { op_puts("--- first 300B of body (debug) ---\n"); var d: i64 = 300; if bl < 300 { d = bl } op_wr(bp, d); op_puts("\n"); return 4 }
51
52 nx_lib_index_build()
53 let out: *u8 = sys_mmap(K_MAGIC_262144)
54
55 // serve each REAL OpenAlex work (hk = W<digit>...) with its true title/doi/year
56 op_puts("--- REAL works served from nx_lib_store (fetched sovereignly) ---\n")
57 let idxbuf: *u8 = sys_mmap(K_MAGIC_1048576)
58 let inn: i64 = nx_lib_store_index(idxbuf)
59 let hkb: *u8 = sys_mmap(256)
60 var ls: i64 = 0
61 var ii: i64 = 0
62 var shown: i64 = 0
63 while ii <= inn {
64 var eol: i64 = 0
65 if ii == inn { eol = 1 } else { if idxbuf[ii] == 10 as u8 { eol = 1 } }
66 if eol == 1 {
67 if ii > ls {
68 var k: i64 = 0
69 var j: i64 = ls
70 while j < ii { hkb[k] = idxbuf[j]; k = k + 1; j = j + 1 }
71 hkb[k] = 0 as u8
72 var isreal: i64 = 0
73 if k >= 2 { if (hkb[0] as i64) == 87 { let c2: i64 = hkb[1] as i64; if c2 >= 0x30 { if c2 <= 0x39 { isreal = 1 } } } }
74 if isreal == 1 { if shown < 8 {
75 let jn: i64 = nx_lib_serve_work(hkb, out)
76 if jn > 0 { op_wr(out, jn); op_puts("\n") }
77 shown = shown + 1
78 } }
79 }
80 ls = ii + 1
81 }
82 ii = ii + 1
83 }
84 op_puts("--- FULL-TEXT search (index over title+abstract+authors, O(1)) ---\n")
85 op_puts("q=cancer -> "); let l1: i64 = nx_lib_index_search("cancer" as *u8, 6, out); op_wr(out, l1); op_puts("\n")
86 op_puts("q=individual -> "); let l2: i64 = nx_lib_index_search("individual" as *u8, 10, out); op_wr(out, l2); op_puts(" (abstract-only word)\n")
87 op_puts("q=mortality -> "); let l3: i64 = nx_lib_index_search("mortality" as *u8, 9, out); op_wr(out, l3); op_puts(" (abstract-only word)\n")
88 op_puts("--- end (REAL OpenAlex data, all sovereign: TLS fetch -> parse -> store -> index -> serve) ---\n")
89 return 0
90}