code wiki / _hdl_build / nx_ad_store.nx
nx_ad_store.nx source
↩ module page · 60 lines · 2711 B
1// nx_ad_store.nx -- LIB: the ad engine's window onto the SOVEREIGN INFORMATION STORE (the team's
2// own substrate, knowledge/store/ad-*), via nx_seg_store. This is the "pure Nishi ecosystem" home
3// for ad config + registry data -- NO TSV flat files, NO SQL. Records are key->bytes, append-only,
4// versioned, content-addressed, term-indexed (operator: "get away from tsv to pure nishi ecosystem").
5//
6// Keys (the ad registry schema, all under prefix knowledge/store/ad-):
7// adcfg:botrules -> invalid-traffic rule lines `field<tab>bad<tab>rule_id` (ADS-019 DATA, rule 11)
8// adcfg:kanon_k -> the k-anonymity floor K (ADS-018 DATA)
9// adgoal:<goal> -> the pick-a-goal menu row (reach/leads/sales; target=census-benchmarked)
10// adpricing:<goal> -> the pricing row (perform_fee/exceed_mult/exceed_bonus; figures=sponsor co-spec)
11// license_tier: ORIGINAL
12import "nx_seg_store.nx"
13import "nx_ad_botfilter.nx"
14import "nx_syscalls.nx"
15
16const ADS_PREFIX: *u8 = "knowledge/store/ad-"
17
18// latest record for `key`: 1=found (sets *ptrout into the loaded segment, *lenout=value bytes),
19// 0=tombstoned, -1=absent. The sovereign-store read path (no SQL, no flat file).
20func ads_get(key: *u8, ptrout: *i64, lenout: *i64) -> i64 {
21 return ss_get(ADS_PREFIX, key, ptrout, lenout)
22}
23
24// next segment id for a commit = 1 + current segment count.
25func ads_seg_next() -> i64 {
26 let segs: *i64 = sys_mmap(8 * 260) as *i64
27 let nseg: i64 = ss_manifest(ADS_PREFIX, segs)
28 if nseg < 0 { return 1 }
29 return 1 + nseg
30}
31
32// parse a leading non-negative int out of the value bytes at adcfg:kanon_k; default 5 (the
33// bootstrap floor, config-hierarchy lowest tier) if the record is absent or unparseable.
34func ads_kanon_k() -> i64 {
35 let pq: *i64 = sys_mmap(16) as *i64
36 let lq: *i64 = sys_mmap(16) as *i64
37 if ads_get("adcfg:kanon_k" as *u8, pq, lq) != 1 { return 5 }
38 let b: *u8 = pq[0] as *u8
39 let n: i64 = lq[0]
40 var v: i64 = 0
41 var i: i64 = 0
42 var go: i64 = 1
43 while go == 1 {
44 if i >= n { go = 0 } else {
45 let c: i64 = b[i] as i64
46 if c >= 48 { if c <= 57 { v = v * 10 + (c - 48); i = i + 1 } else { go = 0 } } else { go = 0 }
47 }
48 }
49 if v <= 0 { return 5 }
50 return v
51}
52
53// load the invalid-traffic rules from adcfg:botrules into rules[] (flat field/bad pairs); returns
54// the pair count, or 0 if the record is absent (fail-closed: a meter with 0 rules counts nothing).
55func ads_botrules(rules: *i64, maxr: i64) -> i64 {
56 let pq: *i64 = sys_mmap(16) as *i64
57 let lq: *i64 = sys_mmap(16) as *i64
58 if ads_get("adcfg:botrules" as *u8, pq, lq) != 1 { return 0 }
59 return bf_parse_rules(pq[0] as *u8, lq[0], rules, maxr)
60}