code wiki / (root) / nx_store_id_lookup_lib.nx

nx_store_id_lookup_lib.nx source

↩ module page · 54 lines · 1954 B

1// Candidate exact row-ID lookup. Read-only; no store mutation or hardware writes. 2import "nx_store_seed_lib.nx" 3 4// A plain row's first column ends at TAB or its actual byte length, never a scratch cap. 5func sid_plain_eq(row: *u8, n: i64, id: *u8, idn: i64) -> i64 { 6 if n < idn { return 0 } 7 var i: i64 = 0 8 while i < idn { 9 if row[i] == (STS_ROWTAB as u8) { return 0 } 10 if row[i] != id[i] { return 0 } 11 i = i + 1 12 } 13 if i == n { return 1 } 14 if row[i] == (STS_ROWTAB as u8) { return 1 } 15 return 0 16} 17 18func sid_row_eq(row: *u8, n: i64, id: *u8, idn: i64, scratch: *u8) -> i64 { 19 if n >= STS_NXR_HDR { 20 if row[0] == (STS_NXR_N as u8) { if row[1] == (STS_NXR_X as u8) { 21 if row[2] == (STS_NXR_R as u8) { if row[3] == (STS_NXR_1 as u8) { 22 let decoded: i64 = sts_emit_row(row,n,scratch,0,STS_FINDCAP) 23 return sid_plain_eq(scratch,decoded,id,idn) 24 } } } } 25 } 26 return sid_plain_eq(row,n,id,idn) 27} 28 29func sid_find_seq(prefix: *u8, id: *u8) -> i64 { 30 let h: *i64 = ss_open_cached(prefix) 31 if (h as i64) == 0 { return 0 - 1 } 32 let slots: *i64 = sts_mm(STS_OUTCAP*2) as *i64 33 let sizes: *i64 = ((slots as i64)+STS_OUTCAP) as *i64 34 var hit: i64 = 0 - 1 35 if ss_hget(h,"q:n" as *u8,slots,sizes) == 1 { 36 let count: i64 = sts_atoi(slots[0] as *u8,sizes[0]) 37 let key: *u8 = sts_mm(STS_KEYCAP) 38 let scratch: *u8 = sts_mm(STS_FINDCAP) 39 var idn: i64 = 0 40 while id[idn] != (0 as u8) { idn = idn + 1 } 41 var seq: i64 = 0 42 while seq < count { 43 sts_rowkey(seq,key) 44 if ss_hget(h,key,slots,sizes) == 1 { 45 if sid_row_eq(slots[0] as *u8,sizes[0],id,idn,scratch) == 1 { hit = seq; seq = count } 46 } 47 seq = seq + 1 48 } 49 sys_munmap(key,STS_KEYCAP) 50 sys_munmap(scratch,STS_FINDCAP) 51 } 52 sys_munmap(slots as *u8,STS_OUTCAP*2) 53 return hit 54}