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