nx_lib_index_test.nx source
↩ module page · 72 lines · 2970 B
1// nx_lib_index_test.nx -- gate for the PERSISTED inverted-index search.
2// Ingest real works -> build+save the index (nishi nx_search_inverted engine)
3// -> O(1) query returns the right work_hks (same verdicts as the linear scan,
4// now index-backed). Exit = failed assertion #.
5import "nx_syscalls.nx"
6import "nx_lib_ingest.nx"
7import "nx_lib_ingest_tsv.nx" // nx_lib_ingest_buf
8import "nx_lib_index.nx"
9
10func lit_has(hay: *u8, hayn: i64, needle: *u8) -> i64 {
11 let nn: i64 = ls_strlen(needle)
12 if nn == 0 { return 1 }
13 if hayn < nn { return 0 }
14 let last: i64 = hayn - nn
15 var i: i64 = 0
16 while i <= last {
17 var j: i64 = 0
18 var st: i64 = 0
19 while st == 0 {
20 if j >= nn { st = 2 }
21 if st == 0 { if hay[i+j] != needle[j] { st = 1 } if st == 0 { j = j + 1 } }
22 }
23 if st == 2 { return 1 }
24 i = i + 1
25 }
26 return 0
27}
28
29func main() -> i64 {
30 let tsv: *u8 = "W_ATTN\tAttention Is All You Need\t10.48550/arXiv.1706.03762\t2017\tarxiv-perpetual\nW_ALPHAFOLD\tHighly accurate protein structure prediction with AlphaFold\t10.1038/s41586-021-03819-2\t2021\tcc-by\nW_CRISPR\tA Programmable Dual-RNA-Guided DNA Endonuclease\t10.1126/science.1225829\t2012\tcc-by\n" as *u8
31 nx_lib_ingest_buf(tsv, ls_strlen(tsv))
32
33 // build + persist the inverted index over the whole catalog
34 let nd: i64 = nx_lib_index_build()
35 if nd < 3 { return 1 }
36
37 let out: *u8 = sys_mmap(262144)
38
39 // O(1) single-term queries hit the right work
40 let q1: *u8 = "attention" as *u8
41 let n1: i64 = nx_lib_index_search(q1, ls_strlen(q1), out)
42 if lit_has(out, n1, "\"W_ATTN\"" as *u8) != 1 { return 2 }
43 if lit_has(out, n1, "W_ALPHAFOLD" as *u8) == 1 { return 3 }
44
45 let q2: *u8 = "protein" as *u8
46 let n2: i64 = nx_lib_index_search(q2, ls_strlen(q2), out)
47 if lit_has(out, n2, "\"W_ALPHAFOLD\"" as *u8) != 1 { return 4 }
48
49 let q3: *u8 = "endonuclease" as *u8
50 let n3: i64 = nx_lib_index_search(q3, ls_strlen(q3), out)
51 if lit_has(out, n3, "\"W_CRISPR\"" as *u8) != 1 { return 5 }
52
53 // multi-term AND, both terms in one title
54 let q4: *u8 = "protein structure" as *u8
55 let n4: i64 = nx_lib_index_search(q4, ls_strlen(q4), out)
56 if lit_has(out, n4, "\"W_ALPHAFOLD\"" as *u8) != 1 { return 6 }
57
58 // multi-term AND across titles -> no work has both -> no hit
59 let q5: *u8 = "attention protein" as *u8
60 let n5: i64 = nx_lib_index_search(q5, ls_strlen(q5), out)
61 if lit_has(out, n5, "W_ATTN" as *u8) == 1 { return 7 }
62 if lit_has(out, n5, "W_ALPHAFOLD" as *u8) == 1 { return 8 }
63
64 // no-match -> empty array
65 let q6: *u8 = "zzznomatchzzz" as *u8
66 let n6: i64 = nx_lib_index_search(q6, ls_strlen(q6), out)
67 if n6 != 2 { return 9 }
68
69 let msg: *u8 = "nx_lib_index: 9/9 PERSISTED inverted-index search PASS (O(1) term lookup via nishi nx_search_inverted; retires FTS5 w/ EXCEED)\n" as *u8
70 sys_write(1, msg, ls_strlen(msg))
71 return 0
72}