interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / core / cortex / om / brain.om

2596 lines · 46129 bytes Raw Blame History
1text bx_mown "\"owner\":"
2text bx_csfx "-c"
3note brain.om: the Company Brain (Omega extension; not in cortex-rs and not in the parity corpus).
4note A Brain entry is an entity of the existing store, written as one ENTITY log record:
5note canonicalName = entry id, entityType = "brain." + kind, validFrom = UTC date YYYY-MM-DD,
6note content = body, metadata = {"owner":O} or {"owner":O,"supersedes":S}. Kinds: decision, rule,
7note case, change (free text) and contract, party (structured records, see bn_vline).
8note "Superseded" is derived on every request from the supersedes links of the stored entries
9note (bsup: superseder slot + 1 per entity slot); the superseded entry is never rewritten.
10note Routes (POST, JSON, the same token and space scopes as /api/cortex/*) and the export file
11note format: spec/CORTEX-API.md "Company Brain". No size limit of its own: every scan runs to the
12note end of its input with unguarded literal loop counts; sizes are bounded by the store (EMAX
13note entity slots, hs/hc pools) and the request (2,097,152 bytes), both reported by brain/usage.
14const B_KIND ulong 30
15const B_OWN ulong 31
16const B_DATE ulong 32
17const B_BODY ulong 33
18const B_SUP ulong 34
19const B_DAYS ulong 35
20const B_TAX ulong 36
21const B_REG ulong 37
22const B_NEWID ulong 38
23const B_TEXT ulong 39
24note bsup: superseder slot + 1 per entity slot (0 = not superseded). It is kept live: built once from
25note the replayed log (bn_ensure, on the first request that needs it), then updated by every Brain
26note write (bn_commit). A retract or hard delete marks it stale (bn_stale), and the next request
27note rebuilds it. bvi 1 = valid for every space. bsc is the scratch copy of the consistency check.
28global bsup ulong EMAX
29global bsc ulong EMAX
30global bvi uchar 1
31global bwr uchar 1
32global bmn ulong 2
33global bdy long 1
34global bdh uchar 1
35global bdt uchar 16
36global bvc ulong 16
37global bal long 8
38global bfa ulong 4
39global bfd long 1
40global bhd ulong 4
41global bpb uchar 4096
42global bpbl ulong 1
43global bxo uchar 1
44global bxd uchar 4096
45global bxdl ulong 1
46text pt_bwr "/api/cortex/brain/write"
47text pt_brd "/api/cortex/brain/read"
48text pt_bget "/api/cortex/brain/get"
49text pt_bcf "/api/cortex/brain/confirm"
50text pt_bal "/api/cortex/brain/alerts"
51text pt_brec "/api/cortex/brain/records"
52text pt_bimp "/api/cortex/brain/import"
53text pt_buse "/api/cortex/brain/usage"
54text bs_wr "id|space|kind|owner|date|body|supersedes"
55text by_wr "ccaacac"
56text bn_swr "struct BrainWrite"
57text bs_rd "task|space|limit"
58text by_rd "acf"
59text bn_srd "struct BrainRead"
60text bs_get "id|space"
61text by_get "ac"
62text bn_sget "struct BrainGet"
63text bs_cf "id|space|owner|date|newId"
64text by_cf "acacc"
65text bn_scf "struct BrainConfirm"
66text bs_al "space|date|days|taxId"
67text by_al "cafc"
68text bn_sal "struct BrainAlerts"
69text bs_rec "space|registryId|taxId"
70text by_rec "ccc"
71text bn_srec "struct BrainRecords"
72text bs_imp "text|space|owner|date"
73text by_imp "acac"
74text bn_simp "struct BrainImport"
75text bs_use "space|alarmPercent"
76text by_use "cf"
77text bn_suse "struct BrainUsage"
78text bx_pfx "brain."
79text bx_space "brain"
80text bx_kown "owner"
81text bx_ksup "supersedes"
82text bk_dec "decision"
83text bk_rule "rule"
84text bk_case "case"
85text bk_chg "change"
86text bk_ctr "contract"
87text bk_pty "party"
88text bc_dt "doc_type: "
89text bc_reg "registry_id: "
90text bc_sub "subject: "
91text bc_pty "party: "
92text bc_date "date: "
93text bc_conf "confirmed: "
94text bc_note "note: "
95text bc_name "name: "
96text bc_tax "tax_id: "
97text bx_cyes "confirmed: yes"
98text bx_cno "confirmed: no"
99text bv_yes "yes"
100text bv_no "no"
101text bv_dash "-"
102text bd_1 "contract"
103text bd_2 "amendment"
104text bd_3 "extension"
105text bd_4 "tender"
106text bd_5 "call"
107text bd_6 "framework_agreement"
108text bq_1 "hard_expiry "
109text bq_2 "on_completion "
110text bq_3 "submission_deadline "
111text bq_4 "extension "
112text bq_5 "start "
113text bo_k1 "\"hard_expiry\""
114text bo_k2 "\"on_completion\""
115text bo_k3 "\"submission_deadline\""
116text bo_k4 "\"extension\""
117text bo_k5 "\"start\""
118text bx_err "{\"error\":"
119text bx_line " (line "
120text be_id "id must be non-empty, use only A-Z a-z 0-9 . _ - and not start with ."
121text be_kind "kind must be one of decision, rule, case, change, contract, party"
122text be_own "owner must be non-empty text without control characters"
123text be_date "date must be a calendar date YYYY-MM-DD (proleptic Gregorian, UTC)"
124text be_supid "supersedes must be a valid entry id"
125text be_dup "an entry with this id already exists in this space (entries are append-only; write a new entry that supersedes it): "
126text be_nsup "supersedes names no Brain entry in this space: "
127text be_ssup "that entry is already superseded; supersede the newer entry instead: "
128text be_nf "no Brain entry with this id in this space: "
129text be_nctr "confirm needs a live (not superseded, not retracted) contract record: "
130text be_conf "this contract record is already confirmed: "
131text be_cyes "a contract record is written unconfirmed: confirm it with /api/cortex/brain/confirm or by importing a hand-corrected file"
132text be_rec "contract record lines are `key: value` with key doc_type, registry_id, subject, party, date, confirmed or note; doc_type is contract, amendment, extension, tender, call or framework_agreement; date is `KIND YYYY-MM-DD` with KIND hard_expiry, on_completion, submission_deadline, extension or start (on_completion may give - for no date); party is a tax ID without spaces; confirmed is yes or no"
133text be_rcnt "a contract record needs exactly one doc_type line and at most one registry_id, subject and confirmed line"
134text be_pty "party record lines are `key: value` with key name, tax_id or note; tax_id has no spaces"
135text be_pcnt "a party record needs exactly one name line and at least one tax_id line"
136text be_days "days is required: a whole number of days (alerts cover date .. date + days, both ends included)"
137text be_long "the task gives an FTS query longer than the query buffer of the FTS replica (om/fts.om qq, 1048576 bytes)"
138text be_fcap "the task needs more query terms than the FTS replica holds (om/fts.om capacities)"
139text be_fts "the FTS query failed"
140text be_hdr "an export file starts with header lines (id:, kind:, ...) and a blank line before the body"
141text be_simp "this entry was already corrected by a different entry; correct that entry's file instead: "
142text be_guard "{\"error\":\"Brain entries are append-only: write them through /api/cortex/brain/write\"}"
143text be_big "the answer is larger than the server's 16 MiB output buffer (om/f64.om job); ask for fewer entries (limit) or a narrower window"
144text bo_rec "{\"recorded\":true,\"entry\":"
145text bo_chg "{\"recorded\":true,\"changed\":true,\"entry\":"
146text bo_same "{\"recorded\":false,\"changed\":false,\"entry\":"
147text bo_get "{\"entry\":"
148text bo_id "{\"id\":"
149text bo_sp ",\"space\":"
150text bo_kd ",\"kind\":"
151text bo_dt ",\"date\":"
152text bo_ow ",\"owner\":"
153text bo_su ",\"supersedes\":"
154text bo_sb ",\"supersededBy\":"
155text bo_rt ",\"retracted\":"
156text bo_bd ",\"body\":"
157text bo_rd1 "{\"space\":"
158text bo_rd2 ",\"task\":"
159text bo_rd3 ",\"supported\":"
160text bo_rd4 ",\"entries\":["
161text bo_rd5 "],\"citations\":["
162text bo_nosup ",\"note\":\"no live entry in the Brain matches this task; nothing is cited\""
163text bo_rank "{\"rank\":"
164text bo_sc ",\"score\":"
165text bo_ent ",\"entry\":"
166text bo_cdt ",\"date\":"
167text bo_al1 ",\"date\":"
168text bo_al2 ",\"days\":"
169text bo_al3 ",\"alerts\":["
170text bo_aid "{\"id\":"
171text bo_aed ",\"entryDate\":"
172text bo_adt ",\"docType\":"
173text bo_arg ",\"registryId\":"
174text bo_akd ",\"dateKind\":"
175text bo_akt ",\"keyDate\":"
176text bo_adl ",\"daysLeft\":"
177text bo_apt ",\"parties\":["
178text bo_ptx "{\"taxId\":"
179text bo_pcp ",\"counterparty\":"
180text bo_recs ",\"records\":["
181text bo_cfd "{\"confirmed\":"
182text bo_end "]}"
183text bu_1 "{\"entities\":"
184text bu_2 ",\"entityCapacity\":"
185text bu_3 ",\"claims\":"
186text bu_4 ",\"claimCapacity\":"
187text bu_5 ",\"spaces\":"
188text bu_6 ",\"spaceCapacity\":"
189text bu_7 ",\"dictionary\":"
190text bu_8 ",\"dictionaryCapacity\":"
191text bu_9 ",\"shortStringBytes\":"
192text bu_10 ",\"shortStringCapacity\":"
193text bu_11 ",\"contentBytes\":"
194text bu_12 ",\"contentCapacity\":"
195text bu_13 ",\"ftsTokens\":"
196text bu_14 ",\"ftsTokenCapacity\":"
197text bu_15 ",\"brainEntries\":"
198text bu_16 ",\"ftsRows\":"
199text bu_17 ",\"ftsRowCapacity\":"
200text bu_18 ",\"alarmPercent\":"
201text bu_19 ",\"alarms\":["
202text bu_20 ",\"supersedeIndexMismatches\":"
203text bu_21 ",\"ftsIndexMismatches\":"
204text bn_n1 "entities"
205text bn_n2 "claims"
206text bn_n3 "spaces"
207text bn_n4 "dictionary"
208text bn_n5 "shortStringBytes"
209text bn_n6 "contentBytes"
210text bn_n7 "ftsTokens"
211text bn_n8 "ftsRows"
212text bh_id "id: "
213text bh_sp "space: "
214text bh_kd "kind: "
215text bh_dt "date: "
216text bh_ow "owner: "
217text bh_su "supersedes: "
218text bh_sb "superseded_by: "
219text bh_txt ".txt"
220text bm_ro "cortex: cannot open or replay the log read-only\n"
221text bm_dir "cortex: the export directory must not exist yet and must be creatable\n"
222text bm_bad "cortex: an entry is not exportable (its space, kind, date or id is not usable as a path)\n"
223text bm_wr "cortex: writing an export file failed\n"
224text bm_done "brain-export: files "
225text bm_nl "\n"
226
227note ---- routing (called from api.om) ----
228note route ids 8 write, 9 read, 10 get, 11 confirm, 12 alerts, 13 records, 14 import, 15 usage
229fn bn_route o ulong n ulong to ulong
230if rq_eq o n pt_bwr
231ret 8
232end
233if rq_eq o n pt_brd
234ret 9
235end
236if rq_eq o n pt_bget
237ret 10
238end
239if rq_eq o n pt_bcf
240ret 11
241end
242if rq_eq o n pt_bal
243ret 12
244end
245if rq_eq o n pt_brec
246ret 13
247end
248if rq_eq o n pt_bimp
249ret 14
250end
251if rq_eq o n pt_buse
252ret 15
253end
254ret 0
255end
256note body string field f staged in hc (as entity content is)
257fn bn_shc f ulong slot ulong to void
258n = get dfs f
259if eq n jnone
260ret
261end
262if ne js_kind n 6
263ret
264end
265js_str n
266put ap slot stage_jsv_hc
267end
268note unsigned number field f (u64 max is kept apart from the NONE sentinel)
269fn bn_snum f ulong slot ulong to void
270n = get dfs f
271if eq n jnone
272ret
273end
274if ne js_kind n 3
275ret
276end
277v = js_num n
278if eq v NONE
279v = sub v 1
280end
281put ap slot v
282end
283fn bn_de r ulong to ulong
284if eq r 8
285st = de_plain bs_wr by_wr bn_swr
286if ne st 0
287ret st
288end
289de_sstr 0 A_NAME
290de_sstr 1 A_SPACE
291de_sstr 2 B_KIND
292de_sstr 3 B_OWN
293de_sstr 4 B_DATE
294bn_shc 5 B_BODY
295de_sstr 6 B_SUP
296ret 0
297end
298if eq r 9
299st = de_plain bs_rd by_rd bn_srd
300if ne st 0
301ret st
302end
303de_sstr 0 A_Q
304de_sstr 1 A_SPACE
305bn_snum 2 A_LIMIT
306ret 0
307end
308if eq r 10
309st = de_plain bs_get by_get bn_sget
310if ne st 0
311ret st
312end
313de_sstr 0 A_NAME
314de_sstr 1 A_SPACE
315ret 0
316end
317ret bn_de2 r
318end
319fn bn_de2 r ulong to ulong
320if eq r 11
321st = de_plain bs_cf by_cf bn_scf
322if ne st 0
323ret st
324end
325de_sstr 0 A_NAME
326de_sstr 1 A_SPACE
327de_sstr 2 B_OWN
328de_sstr 3 B_DATE
329de_sstr 4 B_NEWID
330ret 0
331end
332if eq r 12
333st = de_plain bs_al by_al bn_sal
334if ne st 0
335ret st
336end
337de_sstr 0 A_SPACE
338de_sstr 1 B_DATE
339bn_snum 2 B_DAYS
340de_sstr 3 B_TAX
341ret 0
342end
343if eq r 13
344st = de_plain bs_rec by_rec bn_srec
345if ne st 0
346ret st
347end
348de_sstr 0 A_SPACE
349de_sstr 1 B_REG
350de_sstr 2 B_TAX
351ret 0
352end
353if eq r 14
354st = de_plain bs_imp by_imp bn_simp
355if ne st 0
356ret st
357end
358bn_shc 0 B_TEXT
359de_sstr 1 A_SPACE
360de_sstr 2 B_OWN
361de_sstr 3 B_DATE
362ret 0
363end
364st = de_plain bs_use by_use bn_suse
365if ne st 0
366ret st
367end
368de_sstr 0 A_SPACE
369bn_snum 1 A_LIMIT
370ret 0
371end
372note the output buffer job holds 16 MiB and drops bytes past it (jobx = 1): refuse instead of
373note sending a cut answer
374fn bn_run r ulong to ulong
375put bwr 0 0
376put jobx 0 0
377st = bn_run2 r
378if eq get jobx 0 1
379put jobx 0 0
380ret bn_err 500 be_big
381end
382ret st
383end
384fn bn_run2 r ulong to ulong
385if eq r 8
386ret bn_write
387end
388if eq r 9
389ret bn_read
390end
391if eq r 10
392ret bn_get
393end
394if eq r 11
395ret bn_confirm
396end
397if eq r 12
398ret bn_alerts
399end
400if eq r 13
401ret bn_records
402end
403if eq r 14
404ret bn_import
405end
406ret bn_usage
407end
408
409note ---- bytes of a string reference (hs or hc) ----
410fn bn_at r ulong i ulong to uchar
411o = add rf_off r i
412if ne and r RHC 0
413ret hc_get o
414end
415ret hsg o
416end
417note r[a .. a+n) as a reference of the same pool
418fn bn_sub r ulong a ulong n ulong to ulong
419f = and r or RHC RDISK
420ret or f rf_hs add rf_off r a n
421end
422note r holds the bytes of t at a
423fn bn_eqt r ulong a ulong t view uchar * to bool
424if lt rf_len r add a len t
425ret false
426end
427for i ulong len t
428if ne bn_at r add a i get t i
429ret false
430end
431end
432ret true
433end
434fn bn_iseq r ulong t view uchar * to bool
435if eq r NONE
436ret false
437end
438if ne rf_len r len t
439ret false
440end
441ret bn_eqt r 0 t
442end
443note line r[p .. q) starts with t
444fn bn_pfx r ulong p ulong q ulong t view uchar * to bool
445if lt sub q p len t
446ret false
447end
448ret bn_eqt r p t
449end
450note byte-equal references (any pools)
451fn bn_eqr a ulong b ulong to bool
452if or eq a NONE eq b NONE
453ret eq a b
454end
455n = rf_len a
456if ne n rf_len b
457ret false
458end
459for i ulong 4611686018427387904
460if ge i n
461break
462end
463if ne bn_at a i bn_at b i
464ret false
465end
466end
467ret true
468end
469note index of the first newline at or after p, or the length
470fn bn_eol r ulong p ulong to ulong
471n = rf_len r
472for k ulong 4611686018427387904
473i = add p k
474if ge i n
475ret n
476end
477if eq bn_at r i 10
478ret i
479end
480end
481ret n
482end
483note copy r into jes from w0; returns the end
484fn bn_tojes r ulong w0 ulong to ulong
485n = rf_len r
486for i ulong 4611686018427387904
487if ge i n
488break
489end
490put jes add w0 i bn_at r i
491end
492ret add w0 n
493end
494note stage the bytes of r in hs; returns the hs reference
495fn bn_tohs r ulong to ulong
496if eq r NONE
497ret NONE
498end
499n = rf_len r
500o = get hst 0
501if gt add o n HSCAP
502trap 9
503end
504for i ulong 4611686018427387904
505if ge i n
506break
507end
508hsp add o i bn_at r i
509end
510put hst 0 add o n
511ret rf_hs o n
512end
513note stage text t followed by the bytes of r in hs
514fn bn_stcat t view uchar * r ulong to ulong
515o = get hst 0
516x = stage_txt t
517p = add o len t
518n = rf_len r
519for i ulong 4611686018427387904
520if ge i n
521break
522end
523hsp add p i bn_at r i
524end
525put hst 0 add p n
526ret rf_hs o add len t n
527end
528note append the raw bytes of r to job
529fn bn_jraw r ulong to void
530n = rf_len r
531for i ulong 4611686018427387904
532if ge i n
533break
534end
535js_byte bn_at r i
536end
537end
538note JSON string of r
539fn bn_oref r ulong to void
540if eq r NONE
541js_raw tnull
542ret
543end
544n = bn_tojes r 0
545js_esc_buf 0 n
546end
547
548note ---- errors ----
549fn bn_err st ulong t view uchar * to ulong
550put jobl 0 0
551js_raw bx_err
552js_esc_txt t
553js_byte 125
554ret st
555end
556fn bn_errr st ulong t view uchar * r ulong to ulong
557put jobl 0 0
558js_raw bx_err
559for i ulong len t
560put jes i get t i
561end
562n = bn_tojes r len t
563js_esc_buf 0 n
564js_byte 125
565ret st
566end
567fn bn_errn st ulong t view uchar * ln ulong to ulong
568put jobl 0 0
569js_raw bx_err
570for i ulong len t
571put jes i get t i
572end
573n = len t
574for i ulong len bx_line
575put jes add n i get bx_line i
576end
577n = add n len bx_line
578a = get jobl 0
579js_u64 ln
580m = sub get jobl 0 a
581for i ulong m
582put jes add n i get job add a i
583end
584put jobl 0 a
585n = add n m
586put jes n 41
587js_esc_buf 0 add n 1
588js_byte 125
589ret st
590end
591
592note ---- validation ----
593fn bn_kind r ulong to ulong
594if bn_iseq r bk_dec
595ret 1
596end
597if bn_iseq r bk_rule
598ret 2
599end
600if bn_iseq r bk_case
601ret 3
602end
603if bn_iseq r bk_chg
604ret 4
605end
606if bn_iseq r bk_ctr
607ret 5
608end
609if bn_iseq r bk_pty
610ret 6
611end
612ret 0
613end
614fn bn_idok r ulong to bool
615if eq r NONE
616ret false
617end
618n = rf_len r
619if eq n 0
620ret false
621end
622if eq bn_at r 0 46
623ret false
624end
625for i ulong 4611686018427387904
626if ge i n
627break
628end
629if not bn_idc bn_at r i
630ret false
631end
632end
633ret true
634end
635fn bn_idc c uchar to bool
636if and ge c 48 le c 57
637ret true
638end
639if and ge c 65 le c 90
640ret true
641end
642if and ge c 97 le c 122
643ret true
644end
645ret or or eq c 46 eq c 45 eq c 95
646end
647note non-empty, no control bytes; sp: also no space
648fn bn_txtok r ulong sp bool to bool
649if eq r NONE
650ret false
651end
652n = rf_len r
653if eq n 0
654ret false
655end
656for i ulong 4611686018427387904
657if ge i n
658break
659end
660c = bn_at r i
661if or lt c 32 eq c 127
662ret false
663end
664if and sp eq c 32
665ret false
666end
667end
668ret true
669end
670
671note ---- dates: day numbers since 1970-01-01, proleptic Gregorian (Hinnant's civil algorithms) ----
672fn bn_leap y long to bool
673if ne rem y 4 0
674ret false
675end
676if ne rem y 100 0
677ret true
678end
679ret eq rem y 400 0
680end
681fn bn_dim y long m long to long
682if eq m 2
683if bn_leap y
684ret 29
685end
686ret 28
687end
688if or or eq m 4 eq m 6 or eq m 9 eq m 11
689ret 30
690end
691ret 31
692end
693fn bn_dfc y long m long dd long to long
694yy = y
695if le m 2
696yy = sub y 1
697end
698era = div yy 400
699if lt yy 0
700era = div sub yy 399 400
701end
702yoe = sub yy mul era 400
703doe = bn_doe yoe m dd
704ret sub add mul era 146097 doe 719468
705end
706note day of the 400-year era from year of era, month and day
707fn bn_doe yoe long m long dd long to long
708mm = add m 9
709if gt m 2
710mm = sub m 3
711end
712doy = add div add mul 153 mm 2 5 sub dd 1
713t = add mul yoe 365 div yoe 4
714t = sub t div yoe 100
715ret add t doy
716end
717note n decimal digits of r at a, -1 when one is not a digit
718fn bn_dig r ulong a ulong n ulong to long
719v = long 0
720for i ulong n
721c = bn_at r add a i
722if or lt c 48 gt c 57
723ret -1
724end
725v = add mul v 10 sub long c 48
726end
727ret v
728end
729note r is YYYY-MM-DD and a calendar date: its day number into bdy
730fn bn_pdate r ulong to bool
731if eq r NONE
732ret false
733end
734if ne rf_len r 10
735ret false
736end
737if or ne bn_at r 4 45 ne bn_at r 7 45
738ret false
739end
740y = bn_dig r 0 4
741m = bn_dig r 5 2
742dd = bn_dig r 8 2
743if or lt y 0 or lt m 1 gt m 12
744ret false
745end
746if or lt dd 1 gt dd bn_dim y m
747ret false
748end
749put bdy 0 bn_dfc y m dd
750ret true
751end
752fn bn_put2 w0 ulong v long to void
753put bdt w0 uchar add 48 div v 10
754put bdt add w0 1 uchar add 48 rem v 10
755end
756note YYYY-MM-DD of day z into bdt[0 .. 10)
757fn bn_fdate z long to void
758zz = add z 719468
759era = div zz 146097
760if lt zz 0
761era = div sub zz 146096 146097
762end
763doe = sub zz mul era 146097
764yoe = div sub add sub doe div doe 1460 div doe 36524 div doe 146096 365
765t = add mul 365 yoe div yoe 4
766t = sub t div yoe 100
767doy = sub doe t
768mp = div add mul 5 doy 2 153
769dd = add sub doy div add mul 153 mp 2 5 1
770m = add mp 3
771if ge mp 10
772m = sub mp 9
773end
774y = add yoe mul era 400
775if le m 2
776y = add y 1
777end
778bn_put2 0 div y 100
779bn_put2 2 rem y 100
780put bdt 4 45
781bn_put2 5 m
782put bdt 7 45
783bn_put2 8 dd
784end
785fn bn_today to long
786tn = realns
787ret div tn 86400000000000
788end
789fn bn_stdate z long to ulong
790bn_fdate z
791o = get hst 0
792for i ulong 10
793hsp add o i get bdt i
794end
795put hst 0 add o 10
796ret rf_hs o 10
797end
798
799note ---- entries in the store ----
800note entity e is a Brain entry: entityType "brain." + a known kind. The answer depends only on the
801note type's dictionary entry (the dictionary is append-only), so it is kept per entry in bisd
802note (0 not yet known, 1 Brain, 2 not): read before tests it for every row of a space.
803global bisd uchar DMAX
804fn bn_isb e ulong to bool
805t = get ety e
806c = get bisd t
807if eq c 0
808c = uchar 2
809if bn_isb1 t
810c = uchar 1
811end
812put bisd t c
813end
814ret eq c 1
815end
816fn bn_isb1 t ulong to bool
817r = get dref t
818if not bn_eqt r 0 bx_pfx
819ret false
820end
821ret ne bn_kind bn_sub r 6 sub rf_len r 6 0
822end
823fn bn_kref e ulong to ulong
824r = get dref get ety e
825ret bn_sub r 6 sub rf_len r 6
826end
827fn bn_ekind e ulong to ulong
828ret bn_kind bn_kref e
829end
830note a stored, not retracted Brain entry of space d (NONE: any space)
831fn bn_in e ulong d ulong to bool
832if ne get eret e 0
833ret false
834end
835if and ne d NONE ne get esl e d
836ret false
837end
838ret bn_isb e
839end
840note live: stored, not retracted and not superseded (bn_ensure first)
841fn bn_live e ulong d ulong to bool
842if not bn_in e d
843ret false
844end
845ret eq get bsup e 0
846end
847note metadata of e parsed: owner and supersedes string nodes into bmn (jnone when absent)
848fn bn_meta e ulong to void
849put bmn 0 jnone
850put bmn 1 jnone
851r = get emd e
852if eq r NONE
853ret
854end
855n = rf_len r
856o = rf_off r
857for i ulong 4611686018427387904
858if ge i n
859break
860end
861put jin i hsg add o i
862end
863put jinl 0 n
864put jrvm 0 1
865if not js_parse
866ret
867end
868t = get jroot 0
869if ne js_kind t 8
870ret
871end
872a = js_find t bx_kown
873if ne a jnone
874if eq js_kind a 6
875put bmn 0 a
876end
877end
878b = js_find t bx_ksup
879if ne b jnone
880if eq js_kind b 6
881put bmn 1 b
882end
883end
884end
885note the supersedes target of entry e, or NONE
886fn bn_target e ulong to ulong
887bn_meta e
888s = get bmn 1
889if eq s jnone
890ret NONE
891end
892js_str s
893h = get hst 0
894r = stage_jsv
895t = mnm_find get esl e r
896put hst 0 h
897ret t
898end
899note full rebuild of bsup from the stored entries (d: one space; NONE: every space)
900fn bn_index d ulong to void
901n = get nent 0
902for e ulong 4611686018427387904
903if ge e n
904break
905end
906put bsup e 0
907end
908for e ulong 4611686018427387904
909if ge e n
910break
911end
912if bn_in e d
913t = bn_target e
914if and ne t NONE ne t e
915if bn_in t get esl e
916put bsup t add e 1
917end
918end
919end
920end
921end
922note the live index, rebuilt only when it is stale (at start, after a retract or hard delete)
923fn bn_ensure to void
924if eq get bvi 0 1
925ret
926end
927bn_index NONE
928put bvi 0 1
929end
930note a retract or hard delete can make a superseded entry live again or remove slots: rebuild on
931note the next request (called from api3.om)
932fn bn_stale to void
933put bvi 0 0
934end
935note consistency check: mismatches between the live index and a full rebuild (NONE when the live index
936note is stale); the rebuilt index is kept
937fn bn_icheck to ulong
938if ne get bvi 0 1
939ret NONE
940end
941n = get nent 0
942for e ulong 4611686018427387904
943if ge e n
944break
945end
946put bsc e get bsup e
947end
948bn_index NONE
949k = ulong 0
950for e ulong 4611686018427387904
951if ge e n
952break
953end
954if ne get bsc e get bsup e
955k = add k 1
956end
957end
958ret k
959end
960fn bn_space to ulong
961sp = get ap A_SPACE
962if eq sp NONE
963sp = stage_txt bx_space
964put ap A_SPACE sp
965end
966ret sp
967end
968note string node n as JSON (null for jnone)
969fn bn_onode n ulong to void
970if eq n jnone
971js_raw tnull
972ret
973end
974js_str n
975m = get jsvl 0
976for i ulong 4611686018427387904
977if ge i m
978break
979end
980put jes i get jsv i
981end
982js_esc_buf 0 m
983end
984note the entry object of e
985fn bn_oent e ulong to void
986js_raw bo_id
987o_hs get enm e
988js_raw bo_sp
989o_hs get dref get esl e
990js_raw bo_kd
991o_hs bn_kref e
992js_raw bo_dt
993o_hs get evf e
994bn_meta e
995js_raw bo_ow
996bn_onode get bmn 0
997js_raw bo_su
998bn_onode get bmn 1
999js_raw bo_sb
1000s = get bsup e
1001if eq s 0
1002js_raw tnull
1003else
1004o_hs get enm sub s 1
1005end
1006js_raw bo_rt
1007if eq get eret e 0
1008js_raw tfalse
1009js_raw bo_bd
1010bn_oref get ect e
1011else
1012js_raw ttrue
1013js_raw bo_bd
1014js_raw tnull
1015end
1016js_byte 125
1017end
1018
1019note ---- structured records (contract, party): one `key: value` per line ----
1020fn bn_klen c ulong to ulong
1021if eq c 1
1022ret len bc_dt
1023end
1024if eq c 2
1025ret len bc_reg
1026end
1027if eq c 3
1028ret len bc_sub
1029end
1030if eq c 4
1031ret len bc_pty
1032end
1033if eq c 5
1034ret len bc_date
1035end
1036if eq c 6
1037ret len bc_conf
1038end
1039if eq c 7
1040ret len bc_note
1041end
1042if eq c 8
1043ret len bc_name
1044end
1045ret len bc_tax
1046end
1047fn bn_lkey r ulong p ulong q ulong to ulong
1048if bn_pfx r p q bc_dt
1049ret 1
1050end
1051if bn_pfx r p q bc_reg
1052ret 2
1053end
1054if bn_pfx r p q bc_sub
1055ret 3
1056end
1057if bn_pfx r p q bc_pty
1058ret 4
1059end
1060if bn_pfx r p q bc_date
1061ret 5
1062end
1063if bn_pfx r p q bc_conf
1064ret 6
1065end
1066if bn_pfx r p q bc_note
1067ret 7
1068end
1069if bn_pfx r p q bc_name
1070ret 8
1071end
1072if bn_pfx r p q bc_tax
1073ret 9
1074end
1075ret 0
1076end
1077fn bn_doct v ulong to ulong
1078if bn_iseq v bd_1
1079ret 1
1080end
1081if bn_iseq v bd_2
1082ret 2
1083end
1084if bn_iseq v bd_3
1085ret 3
1086end
1087if bn_iseq v bd_4
1088ret 4
1089end
1090if bn_iseq v bd_5
1091ret 5
1092end
1093if bn_iseq v bd_6
1094ret 6
1095end
1096ret 0
1097end
1098note date kind of a date value: 1 hard_expiry, 2 on_completion, 3 submission_deadline, 4 extension, 5 start
1099fn bn_dkind v ulong to ulong
1100if bn_eqt v 0 bq_1
1101ret 1
1102end
1103if bn_eqt v 0 bq_2
1104ret 2
1105end
1106if bn_eqt v 0 bq_3
1107ret 3
1108end
1109if bn_eqt v 0 bq_4
1110ret 4
1111end
1112if bn_eqt v 0 bq_5
1113ret 5
1114end
1115ret 0
1116end
1117fn bn_dklen k ulong to ulong
1118if eq k 1
1119ret len bq_1
1120end
1121if eq k 2
1122ret len bq_2
1123end
1124if eq k 3
1125ret len bq_3
1126end
1127if eq k 4
1128ret len bq_4
1129end
1130ret len bq_5
1131end
1132note a date value "KIND YYYY-MM-DD" (or "on_completion -"): its kind (0 = invalid); bdh/bdy the date
1133fn bn_dline v ulong to ulong
1134k = bn_dkind v
1135if eq k 0
1136ret 0
1137end
1138a = bn_dklen k
1139w = bn_sub v a sub rf_len v a
1140put bdh 0 0
1141if bn_iseq w bv_dash
1142if eq k 2
1143ret k
1144end
1145ret 0
1146end
1147if not bn_pdate w
1148ret 0
1149end
1150put bdh 0 1
1151ret k
1152end
1153fn bn_okind k ulong to void
1154if eq k 1
1155js_raw bo_k1
1156end
1157if eq k 2
1158js_raw bo_k2
1159end
1160if eq k 3
1161js_raw bo_k3
1162end
1163if eq k 4
1164js_raw bo_k4
1165end
1166if eq k 5
1167js_raw bo_k5
1168end
1169end
1170note check one line of a record of kind k (5 contract, 6 party); 0 ok, 1 bad
1171fn bn_vline r ulong p ulong q ulong k ulong to ulong
1172if eq p q
1173ret 0
1174end
1175c = bn_lkey r p q
1176if eq c 0
1177ret 1
1178end
1179if eq k 5
1180if gt c 7
1181ret 1
1182end
1183else
1184if and ne c 7 lt c 8
1185ret 1
1186end
1187end
1188put bvc c add get bvc c 1
1189a = add p bn_klen c
1190if ge a q
1191ret 1
1192end
1193v = bn_sub r a sub q a
1194ret bn_vval c v
1195end
1196fn bn_vval c ulong v ulong to ulong
1197if eq c 1
1198if eq bn_doct v 0
1199ret 1
1200end
1201end
1202if or eq c 4 eq c 9
1203if not bn_txtok v true
1204ret 1
1205end
1206end
1207if eq c 5
1208if eq bn_dline v 0
1209ret 1
1210end
1211end
1212if eq c 6
1213if bn_iseq v bv_yes
1214put bvc 10 1
1215else
1216if not bn_iseq v bv_no
1217ret 1
1218end
1219end
1220end
1221ret 0
1222end
1223note validate a record body; 0 ok, else the error status (422) with the text in job
1224fn bn_vrec r ulong k ulong to ulong
1225for i ulong len bvc
1226put bvc i 0
1227end
1228n = rf_len r
1229p = ulong 0
1230for ln ulong 4611686018427387904
1231if gt p n
1232break
1233end
1234q = bn_eol r p
1235if ne bn_vline r p q k 0
1236if eq k 5
1237ret bn_errn 422 be_rec add ln 1
1238end
1239ret bn_errn 422 be_pty add ln 1
1240end
1241p = add q 1
1242end
1243if eq k 5
1244if not bn_rcnt
1245ret bn_err 422 be_rcnt
1246end
1247ret 0
1248end
1249if or ne get bvc 8 1 eq get bvc 9 0
1250ret bn_err 422 be_pcnt
1251end
1252ret 0
1253end
1254note one doc_type line, at most one registry_id, subject and confirmed line
1255fn bn_rcnt to bool
1256if ne get bvc 1 1
1257ret false
1258end
1259if gt get bvc 2 1
1260ret false
1261end
1262if gt get bvc 3 1
1263ret false
1264end
1265ret le get bvc 6 1
1266end
1267note r with "confirmed: no" appended as a new line (staged in hc)
1268fn bn_addno r ulong to ulong
1269n = rf_len r
1270o = get hct 0
1271m = add add n 1 len bx_cno
1272if gt add o m HCCAP
1273trap 9
1274end
1275for i ulong 4611686018427387904
1276if ge i n
1277break
1278end
1279hc_put add o i bn_at r i
1280end
1281k = n
1282if gt n 0
1283if ne bn_at r sub n 1 10
1284hc_put add o k 10
1285k = add k 1
1286end
1287end
1288for i ulong len bx_cno
1289hc_put add o add k i get bx_cno i
1290end
1291k = add k len bx_cno
1292put hct 0 add o k
1293ret rf_hc o k
1294end
1295note r with its "confirmed: no" line made "confirmed: yes" (staged in hc)
1296fn bn_setconf r ulong to ulong
1297n = rf_len r
1298o = get hct 0
1299if gt add o add n 1 HCCAP
1300trap 9
1301end
1302w = ulong 0
1303p = ulong 0
1304for ln ulong 4611686018427387904
1305if gt p n
1306break
1307end
1308q = bn_eol r p
1309if bn_iseq bn_sub r p sub q p bx_cno
1310for i ulong len bx_cyes
1311hc_put add o add w i get bx_cyes i
1312end
1313w = add w len bx_cyes
1314else
1315for i ulong 4611686018427387904
1316if ge i sub q p
1317break
1318end
1319hc_put add o add w i bn_at r add p i
1320end
1321w = add w sub q p
1322end
1323if lt q n
1324hc_put add o w 10
1325w = add w 1
1326end
1327p = add q 1
1328end
1329put hct 0 add o w
1330ret rf_hc o w
1331end
1332note the record body r has a "confirmed: yes" line
1333fn bn_conf r ulong to bool
1334n = rf_len r
1335p = ulong 0
1336for ln ulong 4611686018427387904
1337if gt p n
1338break
1339end
1340q = bn_eol r p
1341if bn_iseq bn_sub r p sub q p bx_cyes
1342ret true
1343end
1344p = add q 1
1345end
1346ret false
1347end
1348note value of the first line with key t, or NONE
1349fn bn_fval r ulong t view uchar * to ulong
1350n = rf_len r
1351p = ulong 0
1352for ln ulong 4611686018427387904
1353if gt p n
1354break
1355end
1356q = bn_eol r p
1357if bn_pfx r p q t
1358a = add p len t
1359ret bn_sub r a sub q a
1360end
1361p = add q 1
1362end
1363ret NONE
1364end
1365note the party body r lists tax ID v
1366fn bn_hastax r ulong v ulong to bool
1367n = rf_len r
1368p = ulong 0
1369for ln ulong 4611686018427387904
1370if gt p n
1371break
1372end
1373q = bn_eol r p
1374if bn_pfx r p q bc_tax
1375a = add p len bc_tax
1376if bn_eqr bn_sub r a sub q a v
1377ret true
1378end
1379end
1380p = add q 1
1381end
1382ret false
1383end
1384note the live party record of space d that lists tax ID v (first by slot), or NONE
1385fn bn_cpty v ulong d ulong to ulong
1386for e ulong 4611686018427387904
1387if ge e get nent 0
1388break
1389end
1390if bn_live e d
1391if eq bn_ekind e 6
1392if bn_hastax get ect e v
1393ret e
1394end
1395end
1396end
1397end
1398ret NONE
1399end
1400note a party of contract body r is tax ID t or belongs to the same counterparty (party slot cp)
1401fn bn_taxm r ulong cp ulong t ulong to bool
1402n = rf_len r
1403p = ulong 0
1404for ln ulong 4611686018427387904
1405if gt p n
1406break
1407end
1408q = bn_eol r p
1409if bn_pfx r p q bc_pty
1410a = add p len bc_pty
1411v = bn_sub r a sub q a
1412if bn_eqr v t
1413ret true
1414end
1415if ne cp NONE
1416if bn_hastax get ect cp v
1417ret true
1418end
1419end
1420end
1421p = add q 1
1422end
1423ret false
1424end
1425note structured kinds: validate the body; contract: append "confirmed: no" when absent and refuse
1426note "confirmed: yes" unless conf (a person's import)
1427fn bn_body k ulong conf bool to ulong
1428if lt k 5
1429ret 0
1430end
1431st = bn_vrec get ap B_BODY k
1432if ne st 0
1433ret st
1434end
1435if eq k 6
1436ret 0
1437end
1438if eq get bvc 6 0
1439put ap B_BODY bn_addno get ap B_BODY
1440ret 0
1441end
1442if and not conf eq get bvc 10 1
1443ret bn_err 422 be_cyes
1444end
1445ret 0
1446end
1447
1448note ---- write after ----
1449note the id of a correction of entry id r in space d: "<r>-c<k>" with the smallest k >= 1 not in use
1450fn bn_nextid r ulong d ulong to ulong
1451for k ulong 4611686018427387904
1452h = get hst 0
1453c = bn_tohs r
1454x = stage_txt bx_csfx
1455bn_stdec add k 1
1456c = rf_hs rf_off c sub get hst 0 rf_off c
1457if eq mnm_find d c NONE
1458ret c
1459end
1460put hst 0 h
1461end
1462ret NONE
1463end
1464note stage the decimal digits of v in hs
1465fn bn_stdec v ulong to void
1466a = get jobl 0
1467js_u64 v
1468x = stage_job a
1469end
1470note entry fields of a new entry (A_NAME id, B_KIND, B_OWN, B_DATE, B_BODY, B_SUP); conf: import
1471fn bn_wchk conf bool to ulong
1472if eq get ap A_NAME NONE
1473put ap A_NAME stage_uuid
1474end
1475if not bn_idok get ap A_NAME
1476ret bn_err 422 be_id
1477end
1478k = bn_kind get ap B_KIND
1479if eq k 0
1480ret bn_err 422 be_kind
1481end
1482if not bn_txtok get ap B_OWN false
1483ret bn_err 422 be_own
1484end
1485if eq get ap B_DATE NONE
1486put ap B_DATE bn_stdate bn_today
1487end
1488if not bn_pdate get ap B_DATE
1489ret bn_err 422 be_date
1490end
1491if ne get ap B_SUP NONE
1492if not bn_idok get ap B_SUP
1493ret bn_err 422 be_supid
1494end
1495end
1496ret bn_body k conf
1497end
1498note the id is free and the supersedes target is a live entry of space d (bn_index d first)
1499fn bn_wlink d ulong to ulong
1500if ne d NONE
1501if ne mnm_find d get ap A_NAME NONE
1502ret bn_errr 409 be_dup get ap A_NAME
1503end
1504end
1505s = get ap B_SUP
1506if eq s NONE
1507ret 0
1508end
1509t = NONE
1510if ne d NONE
1511t = mnm_find d s
1512end
1513if eq t NONE
1514ret bn_errr 404 be_nsup s
1515end
1516if not bn_in t d
1517ret bn_errr 404 be_nsup s
1518end
1519if ne get bsup t 0
1520ret bn_errr 409 be_ssup s
1521end
1522ret 0
1523end
1524note append the entry through the entity upsert (one ENTITY record, flushed before the answer);
1525note returns its slot
1526fn bn_commit to ulong
1527put ap A_TYPE bn_stcat bx_pfx get ap B_KIND
1528put ap A_CONT get ap B_BODY
1529put ap A_ALIAS stage_txt tarr0
1530a = get jobl 0
1531js_byte 123
1532js_raw bx_mown
1533o_hs get ap B_OWN
1534if ne get ap B_SUP NONE
1535js_raw bo_su
1536o_hs get ap B_SUP
1537end
1538js_byte 125
1539put ap A_META stage_job a
1540put ap A_CONF bitcast ulong f64 1.0
1541put ap A_VF get ap B_DATE
1542put ap A_VT NONE
1543put ap A_ID NONE
1544put bwr 0 1
1545st = api_upsert
1546put bwr 0 0
1547put jobl 0 0
1548if ne st 201
1549trap 9
1550end
1551d = dict_find get ap A_SPACE
1552e = mnm_find d get ap A_NAME
1553put bsup e 0
1554note incremental update of the live index: the new entry supersedes its target
1555s = get ap B_SUP
1556if ne s NONE
1557t = mnm_find d s
1558put bsup t add e 1
1559end
1560ret e
1561end
1562fn bn_write to ulong
1563sp = bn_space
1564if not scope_ok sp
1565js_raw e403
1566ret 403
1567end
1568st = bn_wchk false
1569if ne st 0
1570ret st
1571end
1572d = dict_find sp
1573if ne d NONE
1574bn_ensure
1575end
1576st = bn_wlink d
1577if ne st 0
1578ret st
1579end
1580e = bn_commit
1581js_raw bo_rec
1582bn_oent e
1583js_byte 125
1584ret 201
1585end
1586note api_upsert guard: entities of a Brain type, or named like a stored entry, are written only here
1587fn bn_guard sp ulong to bool
1588if eq get bwr 0 1
1589ret false
1590end
1591x = false
1592if bn_eqt get ap A_TYPE 0 bx_pfx
1593x = true
1594end
1595d = dict_find sp
1596if ne d NONE
1597e = mnm_find d get ap A_NAME
1598if ne e NONE
1599if bn_isb e
1600x = true
1601end
1602end
1603end
1604i = get ap A_ID
1605if ne i NONE
1606f = mid_find i
1607if ne f NONE
1608if bn_isb f
1609x = true
1610end
1611end
1612end
1613if x
1614js_raw be_guard
1615end
1616ret x
1617end
1618
1619note ---- read before ----
1620fn bn_tokc c uchar to bool
1621if ge c 128
1622ret true
1623end
1624ret bn_idc c
1625end
1626fn bn_qput c uchar to bool
1627n = get qql 0
1628if ge n len qq
1629ret false
1630end
1631put qq n c
1632put qql 0 add n 1
1633ret true
1634end
1635note FTS5 query: every token of the task as a quoted string, joined by OR
1636fn bn_match to bool
1637put qql 0 0
1638q = get ap A_Q
1639n = rf_len q
1640w = false
1641for i ulong 4611686018427387904
1642if ge i n
1643break
1644end
1645c = bn_at q i
1646ok = true
1647if bn_tokc c
1648if not w
1649ok = bn_qopen
1650w = true
1651end
1652ok = and ok bn_qput c
1653else
1654if w
1655ok = bn_qput 34
1656w = false
1657end
1658end
1659if not ok
1660ret false
1661end
1662end
1663if w
1664ret bn_qput 34
1665end
1666ret true
1667end
1668fn bn_qopen to bool
1669ok = true
1670if gt get qql 0 0
1671ok = and ok bn_qput 32
1672ok = and ok bn_qput 79
1673ok = and ok bn_qput 82
1674ok = and ok bn_qput 32
1675end
1676ret and ok bn_qput 34
1677end
1678fn bn_read to ulong
1679sp = bn_space
1680if not scope_ok sp
1681js_raw e403
1682ret 403
1683end
1684lim = get ap A_LIMIT
1685if eq lim NONE
1686lim = 10
1687end
1688n = ulong 0
1689d = dict_find sp
1690if ne d NONE
1691if not bn_match
1692ret bn_err 413 be_long
1693end
1694if gt get qql 0 0
1695n = bn_fts d lim
1696if eq n NONE
1697if eq get qerr 0 2
1698ret bn_err 413 be_fcap
1699end
1700ret bn_err 500 be_fts
1701end
1702end
1703end
1704put jobl 0 0
1705js_raw bo_rd1
1706o_hs sp
1707js_raw bo_rd2
1708o_hs get ap A_Q
1709js_raw bo_rd3
1710if gt n 0
1711js_raw ttrue
1712else
1713js_raw tfalse
1714end
1715js_raw bo_rd4
1716bn_orows n
1717js_raw bo_rd5
1718bn_ocite n
1719js_byte 93
1720if eq n 0
1721js_raw bo_nosup
1722end
1723js_byte 125
1724ret 200
1725end
1726note ranked FTS rows over the live entries of d; NONE when the query failed (qerr)
1727fn bn_fts d ulong lim ulong to ulong
1728bn_ensure
1729e = fts_gfirst d
1730for a ulong 4611686018427387904
1731if eq e NONE
1732break
1733end
1734v = uchar 0
1735if bn_live e d
1736v = 1
1737end
1738put qok e v
1739e = fts_gnext e
1740end
1741put qgsel 0 d
1742put qlim 0 lim
1743n = fts_search
1744if ne get qerr 0 0
1745ret NONE
1746end
1747ret n
1748end
1749fn bn_orows n ulong to void
1750for i ulong n
1751if gt i 0
1752js_byte 44
1753end
1754js_raw bo_rank
1755js_u64 add i 1
1756js_raw bo_sc
1757js_f64 bitcast ulong neg get qrb i
1758js_raw bo_ent
1759bn_oent get qrd i
1760js_byte 125
1761end
1762end
1763note citations: the id and date of every entry returned, in rank order
1764fn bn_ocite n ulong to void
1765for i ulong n
1766e = get qrd i
1767if gt i 0
1768js_byte 44
1769end
1770js_raw bo_id
1771o_hs get enm e
1772js_raw bo_cdt
1773o_hs get evf e
1774js_byte 125
1775end
1776end
1777
1778note ---- get, confirm ----
1779fn bn_get to ulong
1780sp = bn_space
1781if not scope_ok sp
1782js_raw e403
1783ret 403
1784end
1785d = dict_find sp
1786e = NONE
1787if ne d NONE
1788e = mnm_find d get ap A_NAME
1789end
1790if eq e NONE
1791ret bn_errr 404 be_nf get ap A_NAME
1792end
1793if not bn_isb e
1794ret bn_errr 404 be_nf get ap A_NAME
1795end
1796bn_ensure
1797put jobl 0 0
1798js_raw bo_get
1799bn_oent e
1800js_byte 125
1801ret 200
1802end
1803fn bn_confirm to ulong
1804sp = bn_space
1805if not scope_ok sp
1806js_raw e403
1807ret 403
1808end
1809d = dict_find sp
1810t = NONE
1811if ne d NONE
1812bn_ensure
1813t = mnm_find d get ap A_NAME
1814end
1815if eq t NONE
1816ret bn_errr 404 be_nf get ap A_NAME
1817end
1818if not bn_live t d
1819ret bn_errr 409 be_nctr get ap A_NAME
1820end
1821if ne bn_ekind t 5
1822ret bn_errr 409 be_nctr get ap A_NAME
1823end
1824if bn_conf get ect t
1825ret bn_errr 409 be_conf get ap A_NAME
1826end
1827put ap B_SUP get ap A_NAME
1828put ap A_NAME get ap B_NEWID
1829if eq get ap A_NAME NONE
1830put ap A_NAME bn_nextid get ap B_SUP d
1831end
1832put ap B_KIND stage_txt bk_ctr
1833put ap B_BODY bn_setconf get ect t
1834st = bn_wchk true
1835if ne st 0
1836ret st
1837end
1838st = bn_wlink d
1839if ne st 0
1840ret st
1841end
1842e = bn_commit
1843js_raw bo_rec
1844bn_oent e
1845js_byte 125
1846ret 201
1847end
1848
1849note ---- contract alerts and records ----
1850note alert window: day y with date <= y <= date + days (integer day numbers, both ends included)
1851fn bn_inwin y long to bool
1852dd = get bfd 0
1853if lt y dd
1854ret false
1855end
1856ret le ulong sub y dd get bfa 3
1857end
1858note key (y, name of e, p) greater than the key stored at bal[b .. b+2]
1859fn bn_kgt y long e ulong p ulong b ulong to bool
1860sy = get bal b
1861if ne y sy
1862ret gt y sy
1863end
1864se = ulong get bal add b 1
1865if ne e se
1866ret gt hs_cmp get enm e get enm se 0
1867end
1868ret gt long p get bal add b 2
1869end
1870fn bn_kput b ulong y long e ulong p ulong to void
1871put bal b y
1872put bal add b 1 long e
1873put bal add b 2 long p
1874end
1875note e is a live, confirmed contract record of d that matches the tax filter
1876fn bn_aok e ulong d ulong to bool
1877if not bn_live e d
1878ret false
1879end
1880if ne bn_ekind e 5
1881ret false
1882end
1883if not bn_conf get ect e
1884ret false
1885end
1886t = get bfa 0
1887if eq t NONE
1888ret true
1889end
1890ret bn_taxm get ect e get bfa 1 t
1891end
1892note candidate line r[p .. q) of contract e: a dated key date (not start) inside the window,
1893note after the last key emitted and before the best key of this pass
1894fn bn_acand e ulong r ulong p ulong q ulong to void
1895if not bn_pfx r p q bc_date
1896ret
1897end
1898a = add p len bc_date
1899k = bn_dline bn_sub r a sub q a
1900if or eq k 0 eq k 5
1901ret
1902end
1903if eq get bdh 0 0
1904ret
1905end
1906y = get bdy 0
1907if not bn_inwin y
1908ret
1909end
1910if eq get bal 3 1
1911if not bn_kgt y e p 0
1912ret
1913end
1914end
1915if eq get bal 7 1
1916if bn_kgt y e p 4
1917ret
1918end
1919end
1920bn_kput 4 y e p
1921put bal 7 1
1922end
1923fn bn_ascan e ulong to void
1924r = get ect e
1925n = rf_len r
1926p = ulong 0
1927for ln ulong 4611686018427387904
1928if gt p n
1929ret
1930end
1931q = bn_eol r p
1932bn_acand e r p q
1933p = add q 1
1934end
1935end
1936note one selection pass: the smallest alert key after the last one; false when none is left
1937fn bn_apass d ulong to bool
1938put bal 7 0
1939for e ulong 4611686018427387904
1940if ge e get nent 0
1941break
1942end
1943if bn_aok e d
1944bn_ascan e
1945end
1946end
1947if eq get bal 7 0
1948ret false
1949end
1950bn_kput 0 get bal 4 ulong get bal 5 ulong get bal 6
1951put bal 3 1
1952ret true
1953end
1954fn bn_oparties r ulong d ulong to void
1955js_raw bo_apt
1956n = rf_len r
1957p = ulong 0
1958w = false
1959for ln ulong 4611686018427387904
1960if gt p n
1961break
1962end
1963q = bn_eol r p
1964if bn_pfx r p q bc_pty
1965a = add p len bc_pty
1966v = bn_sub r a sub q a
1967if w
1968js_byte 44
1969end
1970w = true
1971js_raw bo_ptx
1972bn_oref v
1973js_raw bo_pcp
1974c = bn_cpty v d
1975if eq c NONE
1976js_raw tnull
1977else
1978o_hs get enm c
1979end
1980js_byte 125
1981end
1982p = add q 1
1983end
1984js_byte 93
1985end
1986fn bn_oalert d ulong to void
1987e = ulong get bal 1
1988p = ulong get bal 2
1989r = get ect e
1990q = bn_eol r p
1991a = add p len bc_date
1992v = bn_sub r a sub q a
1993k = bn_dline v
1994y = get bdy 0
1995js_raw bo_aid
1996o_hs get enm e
1997js_raw bo_aed
1998o_hs get evf e
1999bn_meta e
2000js_raw bo_ow
2001bn_onode get bmn 0
2002js_raw bo_adt
2003bn_oref bn_fval r bc_dt
2004js_raw bo_arg
2005bn_oref bn_fval r bc_reg
2006js_raw bo_akd
2007bn_okind k
2008js_raw bo_akt
2009bn_oref bn_sub v bn_dklen k 10
2010js_raw bo_adl
2011js_u64 ulong sub y get bfd 0
2012bn_oparties r d
2013js_byte 125
2014end
2015fn bn_aloop d ulong to void
2016k = ulong 0
2017for i ulong 4611686018427387904
2018if not bn_apass d
2019ret
2020end
2021if gt k 0
2022js_byte 44
2023end
2024bn_oalert d
2025k = add k 1
2026end
2027end
2028fn bn_alerts to ulong
2029sp = bn_space
2030if not scope_ok sp
2031js_raw e403
2032ret 403
2033end
2034if not bn_pdate get ap B_DATE
2035ret bn_err 422 be_date
2036end
2037put bfd 0 get bdy 0
2038dd = get ap B_DAYS
2039if eq dd NONE
2040ret bn_err 422 be_days
2041end
2042put bfa 3 dd
2043put bfa 0 get ap B_TAX
2044put bfa 1 NONE
2045put bal 3 0
2046put jobl 0 0
2047js_raw bo_rd1
2048o_hs sp
2049js_raw bo_al1
2050o_hs get ap B_DATE
2051js_raw bo_al2
2052js_u64 dd
2053js_raw bo_al3
2054d = dict_find sp
2055if ne d NONE
2056bn_ensure
2057if ne get bfa 0 NONE
2058put bfa 1 bn_cpty get bfa 0 d
2059end
2060bn_aloop d
2061end
2062js_raw bo_end
2063ret 200
2064end
2065note e is a live contract record of d matching the registry and tax filters
2066fn bn_rok e ulong d ulong to bool
2067if not bn_live e d
2068ret false
2069end
2070if ne bn_ekind e 5
2071ret false
2072end
2073g = get ap B_REG
2074if ne g NONE
2075if not bn_eqr bn_fval get ect e bc_reg g
2076ret false
2077end
2078end
2079t = get bfa 0
2080if eq t NONE
2081ret true
2082end
2083ret bn_taxm get ect e get bfa 1 t
2084end
2085note one pass: the smallest (entry date, id) after the last one
2086fn bn_rpass d ulong to bool
2087put bal 7 0
2088for e ulong 4611686018427387904
2089if ge e get nent 0
2090break
2091end
2092if bn_rok e d
2093x = bn_pdate get evf e
2094y = get bdy 0
2095ok = true
2096if eq get bal 3 1
2097ok = bn_kgt y e 0 0
2098end
2099if eq get bal 7 1
2100if bn_kgt y e 0 4
2101ok = false
2102end
2103end
2104if ok
2105bn_kput 4 y e 0
2106put bal 7 1
2107end
2108end
2109end
2110if eq get bal 7 0
2111ret false
2112end
2113bn_kput 0 get bal 4 ulong get bal 5 0
2114put bal 3 1
2115ret true
2116end
2117fn bn_records to ulong
2118sp = bn_space
2119if not scope_ok sp
2120js_raw e403
2121ret 403
2122end
2123put bfa 0 get ap B_TAX
2124put bfa 1 NONE
2125put bal 3 0
2126put jobl 0 0
2127js_raw bo_rd1
2128o_hs sp
2129js_raw bo_recs
2130d = dict_find sp
2131if ne d NONE
2132bn_ensure
2133if ne get bfa 0 NONE
2134put bfa 1 bn_cpty get bfa 0 d
2135end
2136k = ulong 0
2137for i ulong 4611686018427387904
2138if not bn_rpass d
2139break
2140end
2141if gt k 0
2142js_byte 44
2143end
2144e = ulong get bal 1
2145js_raw bo_cfd
2146if bn_conf get ect e
2147js_raw ttrue
2148else
2149js_raw tfalse
2150end
2151js_raw bo_ent
2152bn_oent e
2153js_byte 125
2154k = add k 1
2155end
2156end
2157js_raw bo_end
2158ret 200
2159end
2160
2161note ---- import of a hand-corrected export file ----
2162note header lines up to the first blank line: bhd 0 id, 1 space, 2 kind, 3 body (NONE when absent)
2163fn bn_hdr r ulong to bool
2164for i ulong 4
2165put bhd i NONE
2166end
2167n = rf_len r
2168p = ulong 0
2169for ln ulong 4611686018427387904
2170if ge p n
2171ret false
2172end
2173q = bn_eol r p
2174if eq p q
2175put bhd 3 bn_sub r add q 1 sub sub n q 1
2176ret and ne get bhd 0 NONE ne get bhd 2 NONE
2177end
2178bn_hline r p q
2179p = add q 1
2180end
2181ret false
2182end
2183fn bn_hline r ulong p ulong q ulong to void
2184if bn_pfx r p q bh_id
2185a = add p len bh_id
2186put bhd 0 bn_sub r a sub q a
2187end
2188if bn_pfx r p q bh_sp
2189a = add p len bh_sp
2190put bhd 1 bn_sub r a sub q a
2191end
2192if bn_pfx r p q bh_kd
2193a = add p len bh_kd
2194put bhd 2 bn_sub r a sub q a
2195end
2196end
2197note kind k and body b equal those of entry e
2198fn bn_same e ulong k ulong b ulong to bool
2199if ne bn_ekind e k
2200ret false
2201end
2202ret bn_eqr get ect e b
2203end
2204fn bn_import to ulong
2205if not bn_hdr get ap B_TEXT
2206ret bn_err 422 be_hdr
2207end
2208if ne get bhd 1 NONE
2209put ap A_SPACE bn_tohs get bhd 1
2210end
2211sp = bn_space
2212if not scope_ok sp
2213js_raw e403
2214ret 403
2215end
2216idr = bn_tohs get bhd 0
2217d = dict_find sp
2218e = NONE
2219if ne d NONE
2220e = mnm_find d idr
2221end
2222if eq e NONE
2223ret bn_errr 404 be_nf idr
2224end
2225if not bn_in e d
2226ret bn_errr 404 be_nf idr
2227end
2228kr = bn_tohs get bhd 2
2229k = bn_kind kr
2230if eq k 0
2231ret bn_err 422 be_kind
2232end
2233b = get bhd 3
2234bn_ensure
2235if bn_same e k b
2236ret bn_isame e
2237end
2238s = get bsup e
2239if ne s 0
2240if bn_same sub s 1 k b
2241ret bn_isame sub s 1
2242end
2243ret bn_errr 409 be_simp get enm sub s 1
2244end
2245put ap A_NAME bn_nextid idr d
2246put ap B_KIND kr
2247put ap B_BODY b
2248put ap B_SUP idr
2249st = bn_wchk true
2250if ne st 0
2251ret st
2252end
2253st = bn_wlink d
2254if ne st 0
2255ret st
2256end
2257f = bn_commit
2258js_raw bo_chg
2259bn_oent f
2260js_byte 125
2261ret 201
2262end
2263fn bn_isame e ulong to ulong
2264put jobl 0 0
2265js_raw bo_same
2266bn_oent e
2267js_byte 125
2268ret 200
2269end
2270
2271note ---- usage: the store's fixed capacities beside their fill, and the alarm list ----
2272note alarms names every quantity whose fill is at or above alarmPercent (default 50) of its capacity;
2273note the gate (tests/brain/load.sh) fails when the list is not empty. Nothing is refused here.
2274fn bn_usage to ulong
2275p = get ap A_LIMIT
2276if eq p NONE
2277p = 50
2278end
2279put jobl 0 0
2280js_raw bu_1
2281js_u64 get nent 0
2282js_raw bu_2
2283js_u64 EMAX
2284js_raw bu_3
2285js_u64 get nc 0
2286js_raw bu_4
2287js_u64 CMAX
2288js_raw bu_5
2289js_u64 get ns 0
2290js_raw bu_6
2291js_u64 SMAX
2292js_raw bu_7
2293js_u64 get nd 0
2294js_raw bu_8
2295js_u64 DMAX
2296js_raw bu_9
2297js_u64 get hsn 0
2298js_raw bu_10
2299js_u64 HSCAP
2300js_raw bu_11
2301js_u64 get hcn 0
2302js_raw bu_12
2303js_u64 HCCAP
2304js_raw bu_13
2305js_u64 fts_tokens
2306js_raw bu_14
2307js_u64 QTKN
2308js_raw bu_16
2309js_u64 fts_rows
2310js_raw bu_17
2311js_u64 QD
2312js_raw bu_15
2313k = ulong 0
2314for e ulong 4611686018427387904
2315if ge e get nent 0
2316break
2317end
2318if bn_in e NONE
2319k = add k 1
2320end
2321end
2322js_u64 k
2323js_raw bu_20
2324m = bn_icheck
2325if eq m NONE
2326js_raw tnull
2327else
2328js_u64 m
2329end
2330js_raw bu_21
2331js_u64 fts_icheck
2332js_raw bu_18
2333js_u64 p
2334js_raw bu_19
2335bn_alarms p
2336js_raw bo_end
2337ret 200
2338end
2339fn bn_alarms p ulong to void
2340put bfa 2 0
2341bn_alarm bn_n1 get nent 0 EMAX p
2342bn_alarm bn_n2 get nc 0 CMAX p
2343bn_alarm bn_n3 get ns 0 SMAX p
2344bn_alarm bn_n4 get nd 0 DMAX p
2345bn_alarm bn_n5 get hsn 0 HSCAP p
2346bn_alarm bn_n6 get hcn 0 HCCAP p
2347bn_alarm bn_n7 fts_tokens QTKN p
2348bn_alarm bn_n8 fts_rows QD p
2349end
2350note name t when used * 100 >= cap * p (bfa 2 counts the names written)
2351fn bn_alarm t view uchar * u ulong c ulong p ulong to void
2352if lt mul u 100 mul c p
2353ret
2354end
2355if gt get bfa 2 0
2356js_byte 44
2357end
2358js_esc_txt t
2359put bfa 2 add get bfa 2 1
2360end
2361
2362note ---- plain-file export: cortex --log LOG --brain-export DIR (read-only, offline) ----
2363note DIR/<space>/<kind>/<YYYY-MM-DD>/<id>.txt for every stored (not retracted) entry, superseded ones
2364note included. The log is opened read-only and replayed without cutting a torn tail or compacting,
2365note so it is safe beside a running server (it sees the records complete when it starts).
2366fn bn_ropen to int
2367fd = fopen lpath 0 get lpl 0 524288 0
2368if lt fd 0
2369ret -1
2370end
2371sz = fseek int fd 0 2
2372if lt sz 8
2373ret -2
2374end
2375r = pread int fd klr 0 8 0
2376if ne r 8
2377ret -1
2378end
2379for i ulong 8
2380if ne get klr i get lmagic i
2381ret -2
2382end
2383end
2384put lgs 0 ulong fd
2385put lgs 1 ulong sz
2386put lgs 3 ulong fd
2387ret 0
2388end
2389note a path segment: non-empty, not . or .., no / and no NUL
2390fn bn_segok r ulong to bool
2391n = rf_len r
2392if eq n 0
2393ret false
2394end
2395if and le n 2 eq bn_at r 0 46
2396if eq n 1
2397ret false
2398end
2399if eq bn_at r 1 46
2400ret false
2401end
2402end
2403for i ulong 4611686018427387904
2404if ge i n
2405break
2406end
2407c = bn_at r i
2408if or eq c 47 eq c 0
2409ret false
2410end
2411end
2412ret true
2413end
2414note append "/" and r to the path in bpb (false past PATH_MAX, a limit of the kernel)
2415fn bn_pcat r ulong to bool
2416n = rf_len r
2417o = get bpbl 0
2418if ge add add o n 1 len bpb
2419ret false
2420end
2421put bpb o 47
2422for i ulong n
2423put bpb add add o 1 i bn_at r i
2424end
2425put bpbl 0 add add o 1 n
2426ret true
2427end
2428fn bn_mkd to bool
2429x = makedir bpb 0 get bpbl 0 493
2430ret or eq x 0 eq x -17
2431end
2432fn bn_xpath e ulong to bool
2433for i ulong get bxdl 0
2434put bpb i get bxd i
2435end
2436put bpbl 0 get bxdl 0
2437s = get dref get esl e
2438k = bn_kref e
2439v = get evf e
2440if not and and bn_segok s bn_idok get enm e bn_pdate v
2441ret false
2442end
2443ok = bn_pcat s
2444if ok
2445ok = bn_mkd
2446end
2447if ok
2448ok = bn_pcat k
2449end
2450if ok
2451ok = bn_mkd
2452end
2453if ok
2454ok = bn_pcat v
2455end
2456if ok
2457ok = bn_mkd
2458end
2459if ok
2460ok = bn_pcat get enm e
2461end
2462if not ok
2463ret false
2464end
2465n = get bpbl 0
2466if ge add n len bh_txt len bpb
2467ret false
2468end
2469for i ulong len bh_txt
2470put bpb add n i get bh_txt i
2471end
2472put bpbl 0 add n len bh_txt
2473ret true
2474end
2475note the export file of entry e into job
2476fn bn_xtext e ulong to void
2477put jobl 0 0
2478js_raw bh_id
2479bn_jraw get enm e
2480js_byte 10
2481js_raw bh_sp
2482bn_jraw get dref get esl e
2483js_byte 10
2484js_raw bh_kd
2485bn_jraw bn_kref e
2486js_byte 10
2487js_raw bh_dt
2488bn_jraw get evf e
2489js_byte 10
2490bn_meta e
2491js_raw bh_ow
2492bn_jnode get bmn 0
2493js_byte 10
2494if ne get bmn 1 jnone
2495js_raw bh_su
2496bn_jnode get bmn 1
2497js_byte 10
2498end
2499s = get bsup e
2500if ne s 0
2501js_raw bh_sb
2502bn_jraw get enm sub s 1
2503js_byte 10
2504end
2505js_byte 10
2506bn_jraw get ect e
2507end
2508fn bn_jnode n ulong to void
2509if eq n jnone
2510ret
2511end
2512js_str n
2513m = get jsvl 0
2514for i ulong 4611686018427387904
2515if ge i m
2516break
2517end
2518js_byte get jsv i
2519end
2520end
2521fn bn_xone e ulong to int
2522if not bn_xpath e
2523say bm_bad
2524put jobl 0 0
2525js_raw bh_id
2526bn_jraw get enm e
2527js_byte 10
2528w = write 2 job 0 get jobl 0
2529ret 3
2530end
2531bn_xtext e
2532fd = fopen bpb 0 get bpbl 0 524481 420
2533if lt fd 0
2534say bm_wr
2535ret 4
2536end
2537n = get jobl 0
2538o = ulong 0
2539for i ulong 4611686018427387904
2540if ge o n
2541break
2542end
2543w = write int fd job o sub n o
2544if le w 0
2545x = fclose int fd
2546say bm_wr
2547ret 4
2548end
2549o = add o ulong w
2550end
2551x = fclose int fd
2552if ne x 0
2553say bm_wr
2554ret 4
2555end
2556ret 0
2557end
2558fn bn_export to int
2559fts_init
2560if ne bn_ropen 0
2561say bm_ro
2562ret 3
2563end
2564put stv 5 1
2565x = lg_replay int get lgs 0 get lgs 1
2566st_rowmax
2567st_ftsmax
2568st_spmax
2569st_materialize
2570put stv 5 0
2571m = makedir bxd 0 get bxdl 0 493
2572if ne m 0
2573say bm_dir
2574ret 2
2575end
2576bn_index NONE
2577k = ulong 0
2578for e ulong 4611686018427387904
2579if ge e get nent 0
2580break
2581end
2582if bn_in e NONE
2583r = bn_xone e
2584if ne r 0
2585ret r
2586end
2587k = add k 1
2588end
2589end
2590put jobl 0 0
2591js_raw bm_done
2592js_u64 k
2593js_byte 10
2594w = write 1 job 0 get jobl 0
2595ret 0
2596end