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
omega-mv / core / cortex / om / store.om
| 1 | note store.om: in-memory Cortex state rebuilt from the append log (log.om). |
| 2 | note Strings: small strings (ids, names, types, slugs, json, dates) live in hs (bytes); |
| 3 | note content and embeddings live in hc, a ulong pool used as a little-endian byte space (128 MiB). |
| 4 | note A string reference packs pool and place: bit 63 = on disk (offset into the log file), |
| 5 | note bit 62 = in hc (else hs), bits 25..61 = offset, bits 0..24 = length. NONE = SQL NULL. |
| 6 | const EMAX ulong 262144 |
| 7 | const CMAX ulong 65536 |
| 8 | const SMAX ulong 65536 |
| 9 | const DMAX ulong 65536 |
| 10 | const MIDN ulong 524288 |
| 11 | const MCLN ulong 131072 |
| 12 | const MDIN ulong 131072 |
| 13 | const RDISK ulong 9223372036854775808 |
| 14 | const RHC ulong 4611686018427387904 |
| 15 | const RLENM ulong 33554431 |
| 16 | note hs holds every short string field (ids, names, types, aliases, metadata, timestamps) of every |
| 17 | note logged revision. 2026-10-26: 2022373 bytes for 6641 live entities (about 305 bytes each) at about |
| 18 | note 60-70 new entities an hour, so a single 16 MiB pool would fill (trap 9 when the log is loaded) |
| 19 | note in about a month. One omc pool holds at most 16777216 elements (error 23 beyond), so the 64 MiB |
| 20 | note string space is four pools (hs, hs1, hs2, hs3) read and written only through hsg / hsp. |
| 21 | const HSCAP ulong 67108864 |
| 22 | const HCCAP ulong 134217728 |
| 23 | global hs uchar 16777216 |
| 24 | global hs1 uchar 16777216 |
| 25 | global hs2 uchar 16777216 |
| 26 | global hs3 uchar 16777216 |
| 27 | global hsn ulong 1 |
| 28 | global hc ulong 16777216 |
| 29 | global hcn ulong 1 |
| 30 | note entity slots |
| 31 | global nent ulong 1 |
| 32 | global erow ulong 262144 |
| 33 | global efts ulong 262144 |
| 34 | global eret uchar 262144 |
| 35 | global erev long 262144 |
| 36 | global ecf f64 262144 |
| 37 | global eid ulong 262144 |
| 38 | global esid ulong 262144 |
| 39 | global esl ulong 262144 |
| 40 | global ety ulong 262144 |
| 41 | global enm ulong 262144 |
| 42 | global ect ulong 262144 |
| 43 | global eal ulong 262144 |
| 44 | global emd ulong 262144 |
| 45 | global evf ulong 262144 |
| 46 | global evt ulong 262144 |
| 47 | global eca ulong 262144 |
| 48 | global eem ulong 262144 |
| 49 | global eav uchar 262144 |
| 50 | note claim slots |
| 51 | global nc ulong 1 |
| 52 | global crow ulong 65536 |
| 53 | global cret uchar 65536 |
| 54 | global ccf f64 65536 |
| 55 | global cid ulong 65536 |
| 56 | global csid ulong 65536 |
| 57 | global csl ulong 65536 |
| 58 | global csub ulong 65536 |
| 59 | global cprd ulong 65536 |
| 60 | global cobj ulong 65536 |
| 61 | global clit ulong 65536 |
| 62 | global cmd ulong 65536 |
| 63 | global cca ulong 65536 |
| 64 | note spaces (rows of the spaces table) |
| 65 | global ns ulong 1 |
| 66 | global srow ulong 65536 |
| 67 | global sid ulong 65536 |
| 68 | global sdi ulong 65536 |
| 69 | global sca ulong 65536 |
| 70 | note dictionary of interned strings (slugs, entity types): dref = hs ref, dsp = space slot + 1 |
| 71 | global nd ulong 1 |
| 72 | global dref ulong 65536 |
| 73 | global dsp ulong 65536 |
| 74 | global drank ulong 65536 |
| 75 | note hash tables: slot + 1, 0 empty, NONE deleted |
| 76 | global mid ulong 524288 |
| 77 | global mnm ulong 524288 |
| 78 | global mcl ulong 131072 |
| 79 | global mdi ulong 131072 |
| 80 | note scalar state: 0 max entity rowid, 1 max fts rowid, 2 max claim rowid, 3 max space rowid, 4 rank dirty |
| 81 | global stv ulong 8 |
| 82 | |
| 83 | fn rf_len r ulong to ulong |
| 84 | ret and r RLENM |
| 85 | end |
| 86 | fn rf_off r ulong to ulong |
| 87 | ret and shr r 25 68719476735 |
| 88 | end |
| 89 | fn rf_hs off ulong n ulong to ulong |
| 90 | ret or shl off 25 n |
| 91 | end |
| 92 | fn rf_hc off ulong n ulong to ulong |
| 93 | ret or RHC or shl off 25 n |
| 94 | end |
| 95 | fn rf_disk off ulong n ulong to ulong |
| 96 | ret or RDISK or shl off 25 n |
| 97 | end |
| 98 | fn rf_isdisk r ulong to bool |
| 99 | ret and ne r NONE ne and r RDISK 0 |
| 100 | end |
| 101 | |
| 102 | note hc byte access (little-endian bytes inside ulong cells) |
| 103 | fn hc_get i ulong to uchar |
| 104 | ret uchar and shr get hc shr i 3 mul and i 7 8 255 |
| 105 | end |
| 106 | fn hc_put i ulong c uchar to void |
| 107 | w = shr i 3 |
| 108 | s = mul and i 7 8 |
| 109 | v = and get hc w not shl ulong 255 s |
| 110 | put hc w or v shl ulong c s |
| 111 | end |
| 112 | |
| 113 | note FNV-1a 64 over hs bytes |
| 114 | fn hs_hash r ulong to ulong |
| 115 | h = ulong 14695981039346656037 |
| 116 | o = rf_off r |
| 117 | for i ulong rf_len r |
| 118 | h = mul xor h ulong hsg add o i 1099511628211 |
| 119 | end |
| 120 | ret h |
| 121 | end |
| 122 | fn hs_eq a ulong b ulong to bool |
| 123 | if or eq a NONE eq b NONE |
| 124 | ret eq a b |
| 125 | end |
| 126 | n = rf_len a |
| 127 | if ne n rf_len b |
| 128 | ret false |
| 129 | end |
| 130 | x = rf_off a |
| 131 | y = rf_off b |
| 132 | for i ulong n |
| 133 | if ne hsg add x i hsg add y i |
| 134 | ret false |
| 135 | end |
| 136 | end |
| 137 | ret true |
| 138 | end |
| 139 | note bytewise compare of two hs strings: -1, 0, 1 (memcmp then length, as SQLite BINARY) |
| 140 | fn hs_cmp a ulong b ulong to long |
| 141 | n = rf_len a |
| 142 | m = rf_len b |
| 143 | k = n |
| 144 | if lt m k |
| 145 | k = m |
| 146 | end |
| 147 | x = rf_off a |
| 148 | y = rf_off b |
| 149 | for i ulong k |
| 150 | p = hsg add x i |
| 151 | q = hsg add y i |
| 152 | if lt p q |
| 153 | ret -1 |
| 154 | end |
| 155 | if gt p q |
| 156 | ret 1 |
| 157 | end |
| 158 | end |
| 159 | if lt n m |
| 160 | ret -1 |
| 161 | end |
| 162 | if gt n m |
| 163 | ret 1 |
| 164 | end |
| 165 | ret 0 |
| 166 | end |
| 167 | fn hs_eqtxt r ulong t view uchar * to bool |
| 168 | if eq r NONE |
| 169 | ret false |
| 170 | end |
| 171 | if ne rf_len r len t |
| 172 | ret false |
| 173 | end |
| 174 | o = rf_off r |
| 175 | for i ulong len t |
| 176 | if ne hsg add o i get t i |
| 177 | ret false |
| 178 | end |
| 179 | end |
| 180 | ret true |
| 181 | end |
| 182 | note staging: bytes appended at hsn without committing; hs_commit makes them a string |
| 183 | fn hs_room n ulong to bool |
| 184 | ret le add get hsn 0 n HSCAP |
| 185 | end |
| 186 | fn hs_stage_txt t view uchar * to ulong |
| 187 | o = get hsn 0 |
| 188 | for i ulong len t |
| 189 | hsp add o i get t i |
| 190 | end |
| 191 | ret rf_hs o len t |
| 192 | end |
| 193 | fn hs_commit r ulong to void |
| 194 | if eq r NONE |
| 195 | ret |
| 196 | end |
| 197 | e = add rf_off r rf_len r |
| 198 | if gt e get hsn 0 |
| 199 | put hsn 0 e |
| 200 | end |
| 201 | end |
| 202 | |
| 203 | note dictionary: intern the hs string r (staged); returns dictionary index |
| 204 | fn dict_find r ulong to ulong |
| 205 | h = hs_hash r |
| 206 | m = sub MDIN 1 |
| 207 | i = and h m |
| 208 | for k ulong MDIN |
| 209 | v = get mdi i |
| 210 | if eq v 0 |
| 211 | ret NONE |
| 212 | end |
| 213 | if ne v NONE |
| 214 | d = sub v 1 |
| 215 | if hs_eq get dref d r |
| 216 | ret d |
| 217 | end |
| 218 | end |
| 219 | i = and add i 1 m |
| 220 | end |
| 221 | ret NONE |
| 222 | end |
| 223 | fn dict_intern r ulong to ulong |
| 224 | d = dict_find r |
| 225 | if ne d NONE |
| 226 | ret d |
| 227 | end |
| 228 | hs_commit r |
| 229 | d = get nd 0 |
| 230 | put nd 0 add d 1 |
| 231 | put dref d r |
| 232 | put dsp d 0 |
| 233 | dict_rank d |
| 234 | h = hs_hash r |
| 235 | m = sub MDIN 1 |
| 236 | i = and h m |
| 237 | while ne get mdi i 0 bound 999999 |
| 238 | i = and add i 1 m |
| 239 | end |
| 240 | put mdi i add d 1 |
| 241 | ret d |
| 242 | end |
| 243 | |
| 244 | note id map over entity slots |
| 245 | fn mid_find r ulong to ulong |
| 246 | h = hs_hash r |
| 247 | m = sub MIDN 1 |
| 248 | i = and h m |
| 249 | for k ulong MIDN |
| 250 | v = get mid i |
| 251 | if eq v 0 |
| 252 | ret NONE |
| 253 | end |
| 254 | if ne v NONE |
| 255 | e = sub v 1 |
| 256 | if hs_eq get eid e r |
| 257 | ret e |
| 258 | end |
| 259 | end |
| 260 | i = and add i 1 m |
| 261 | end |
| 262 | ret NONE |
| 263 | end |
| 264 | fn mid_add e ulong to void |
| 265 | h = hs_hash get eid e |
| 266 | m = sub MIDN 1 |
| 267 | i = and h m |
| 268 | while true bound 999999 |
| 269 | v = get mid i |
| 270 | if or eq v 0 eq v NONE |
| 271 | put mid i add e 1 |
| 272 | ret |
| 273 | end |
| 274 | i = and add i 1 m |
| 275 | end |
| 276 | end |
| 277 | fn mid_del e ulong to void |
| 278 | h = hs_hash get eid e |
| 279 | m = sub MIDN 1 |
| 280 | i = and h m |
| 281 | for k ulong MIDN |
| 282 | v = get mid i |
| 283 | if eq v 0 |
| 284 | ret |
| 285 | end |
| 286 | if eq v add e 1 |
| 287 | put mid i NONE |
| 288 | ret |
| 289 | end |
| 290 | i = and add i 1 m |
| 291 | end |
| 292 | end |
| 293 | |
| 294 | note (space slug, name) multimap: key hash mixes the slug dictionary index into the name hash |
| 295 | fn mnm_hash sl ulong r ulong to ulong |
| 296 | ret mul xor hs_hash r mul add sl 1 11400714819323198485 1099511628211 |
| 297 | end |
| 298 | fn mnm_add e ulong to void |
| 299 | h = mnm_hash get esl e get enm e |
| 300 | m = sub MIDN 1 |
| 301 | i = and h m |
| 302 | while true bound 999999 |
| 303 | v = get mnm i |
| 304 | if or eq v 0 eq v NONE |
| 305 | put mnm i add e 1 |
| 306 | ret |
| 307 | end |
| 308 | i = and add i 1 m |
| 309 | end |
| 310 | end |
| 311 | fn mnm_del e ulong to void |
| 312 | h = mnm_hash get esl e get enm e |
| 313 | m = sub MIDN 1 |
| 314 | i = and h m |
| 315 | for k ulong MIDN |
| 316 | v = get mnm i |
| 317 | if eq v 0 |
| 318 | ret |
| 319 | end |
| 320 | if eq v add e 1 |
| 321 | put mnm i NONE |
| 322 | ret |
| 323 | end |
| 324 | i = and add i 1 m |
| 325 | end |
| 326 | end |
| 327 | note lowest-rowid entity with this slug and name (retracted included), as idx_entities_canonical gives it |
| 328 | fn mnm_find sl ulong r ulong to ulong |
| 329 | m = sub MIDN 1 |
| 330 | i = and mnm_hash sl r m |
| 331 | best = NONE |
| 332 | brow = NONE |
| 333 | for k ulong MIDN |
| 334 | v = get mnm i |
| 335 | if eq v 0 |
| 336 | ret best |
| 337 | end |
| 338 | if ne v NONE |
| 339 | e = sub v 1 |
| 340 | if and eq get esl e sl hs_eq get enm e r |
| 341 | if lt get erow e brow |
| 342 | best = e |
| 343 | brow = get erow e |
| 344 | end |
| 345 | end |
| 346 | end |
| 347 | i = and add i 1 m |
| 348 | end |
| 349 | ret best |
| 350 | end |
| 351 | |
| 352 | note claim id map |
| 353 | fn mcl_find r ulong to ulong |
| 354 | h = hs_hash r |
| 355 | m = sub MCLN 1 |
| 356 | i = and h m |
| 357 | for k ulong MCLN |
| 358 | v = get mcl i |
| 359 | if eq v 0 |
| 360 | ret NONE |
| 361 | end |
| 362 | if ne v NONE |
| 363 | c = sub v 1 |
| 364 | if hs_eq get cid c r |
| 365 | ret c |
| 366 | end |
| 367 | end |
| 368 | i = and add i 1 m |
| 369 | end |
| 370 | ret NONE |
| 371 | end |
| 372 | fn mcl_add c ulong to void |
| 373 | h = hs_hash get cid c |
| 374 | m = sub MCLN 1 |
| 375 | i = and h m |
| 376 | while true bound 999999 |
| 377 | v = get mcl i |
| 378 | if or eq v 0 eq v NONE |
| 379 | put mcl i add c 1 |
| 380 | ret |
| 381 | end |
| 382 | i = and add i 1 m |
| 383 | end |
| 384 | end |
| 385 | |
| 386 | note space slot for a slug dictionary index, or NONE |
| 387 | fn sp_of d ulong to ulong |
| 388 | v = get dsp d |
| 389 | if eq v 0 |
| 390 | ret NONE |
| 391 | end |
| 392 | ret sub v 1 |
| 393 | end |
| 394 | fn sp_add row ulong idr ulong d ulong car ulong to ulong |
| 395 | s = get ns 0 |
| 396 | put ns 0 add s 1 |
| 397 | put srow s row |
| 398 | put sid s idr |
| 399 | put sdi s d |
| 400 | put sca s car |
| 401 | put dsp d add s 1 |
| 402 | if gt row get stv 3 |
| 403 | put stv 3 row |
| 404 | end |
| 405 | ret s |
| 406 | end |
| 407 | |
| 408 | note type rank: dord holds dictionary indices in byte order; drank is each index's position. |
| 409 | note A new string is inserted at its place; the order of existing strings never changes. |
| 410 | global dord ulong 65536 |
| 411 | fn dict_rank d ulong to void |
| 412 | n = d |
| 413 | lo = ulong 0 |
| 414 | hi = n |
| 415 | r = get dref d |
| 416 | while lt lo hi bound 64 |
| 417 | mp = div add lo hi 2 |
| 418 | if lt hs_cmp get dref get dord mp r 0 |
| 419 | lo = add mp 1 |
| 420 | else |
| 421 | hi = mp |
| 422 | end |
| 423 | end |
| 424 | i = n |
| 425 | while gt i lo bound 999999 |
| 426 | x = get dord sub i 1 |
| 427 | put dord i x |
| 428 | put drank x i |
| 429 | i = sub i 1 |
| 430 | end |
| 431 | put dord lo d |
| 432 | put drank d lo |
| 433 | end |
| 434 | |
| 435 | note scan order of idx_entities_type: (space slug, entity_type bytes, rowid). eord holds live slots in that |
| 436 | note order; the slug part only groups (dictionary index), the type part uses drank. |
| 437 | global eord ulong 262144 |
| 438 | global neo ulong 1 |
| 439 | global eor ulong 2 |
| 440 | fn eo_lt a ulong b ulong to bool |
| 441 | x = get esl a |
| 442 | y = get esl b |
| 443 | if ne x y |
| 444 | ret lt x y |
| 445 | end |
| 446 | p = get drank get ety a |
| 447 | q = get drank get ety b |
| 448 | if ne p q |
| 449 | ret lt p q |
| 450 | end |
| 451 | ret lt get erow a get erow b |
| 452 | end |
| 453 | note first index whose slot is not less than e |
| 454 | fn eo_lower e ulong to ulong |
| 455 | lo = ulong 0 |
| 456 | hi = get neo 0 |
| 457 | while lt lo hi bound 64 |
| 458 | mp = div add lo hi 2 |
| 459 | if eo_lt get eord mp e |
| 460 | lo = add mp 1 |
| 461 | else |
| 462 | hi = mp |
| 463 | end |
| 464 | end |
| 465 | ret lo |
| 466 | end |
| 467 | fn eo_insert e ulong to void |
| 468 | p = eo_lower e |
| 469 | n = get neo 0 |
| 470 | i = n |
| 471 | while gt i p bound 999999 |
| 472 | put eord i get eord sub i 1 |
| 473 | i = sub i 1 |
| 474 | end |
| 475 | put eord p e |
| 476 | put neo 0 add n 1 |
| 477 | end |
| 478 | fn eo_remove e ulong to void |
| 479 | p = eo_lower e |
| 480 | n = get neo 0 |
| 481 | if or ge p n ne get eord p e |
| 482 | ret |
| 483 | end |
| 484 | i = p |
| 485 | while lt add i 1 n bound 999999 |
| 486 | put eord i get eord add i 1 |
| 487 | i = add i 1 |
| 488 | end |
| 489 | put neo 0 sub n 1 |
| 490 | end |
| 491 | note eord range of slug d into eor 0 (first) and eor 1 (end) |
| 492 | fn eo_range d ulong to void |
| 493 | lo = ulong 0 |
| 494 | hi = get neo 0 |
| 495 | while lt lo hi bound 64 |
| 496 | mp = div add lo hi 2 |
| 497 | if lt get esl get eord mp d |
| 498 | lo = add mp 1 |
| 499 | else |
| 500 | hi = mp |
| 501 | end |
| 502 | end |
| 503 | put eor 0 lo |
| 504 | hi = get neo 0 |
| 505 | a = lo |
| 506 | while lt a hi bound 64 |
| 507 | mp = div add a hi 2 |
| 508 | if le get esl get eord mp d |
| 509 | a = add mp 1 |
| 510 | else |
| 511 | hi = mp |
| 512 | end |
| 513 | end |
| 514 | put eor 1 a |
| 515 | end |
| 516 | |
| 517 | note link/unlink a slot from the name map and scan order (keys: slug, name, type, rowid) |
| 518 | fn ent_link e ulong to void |
| 519 | mnm_add e |
| 520 | eo_insert e |
| 521 | end |
| 522 | fn ent_unlink e ulong to void |
| 523 | mnm_del e |
| 524 | eo_remove e |
| 525 | end |
| 526 | |
| 527 | note max of live fts rows after the current max was removed |
| 528 | fn st_ftsmax to void |
| 529 | m = ulong 0 |
| 530 | for e ulong get nent 0 |
| 531 | if and lt get eret e 2 gt get efts e m |
| 532 | m = get efts e |
| 533 | end |
| 534 | end |
| 535 | put stv 1 m |
| 536 | end |
| 537 | fn st_rowmax to void |
| 538 | m = ulong 0 |
| 539 | for e ulong get nent 0 |
| 540 | if and lt get eret e 2 gt get erow e m |
| 541 | m = get erow e |
| 542 | end |
| 543 | end |
| 544 | put stv 0 m |
| 545 | end |
| 546 | |
| 547 | note copy entity name to the FTS name input, content to the FTS content input, then index (the |
| 548 | note copy loops count to a literal: a variable count stops at 1,000,000 passes, and content can |
| 549 | note be longer, e.g. replayed from an imported database) |
| 550 | fn st_fts_put e ulong to void |
| 551 | r = get enm e |
| 552 | o = rf_off r |
| 553 | n = rf_len r |
| 554 | i = ulong 0 |
| 555 | for a ulong 4611686018427387904 |
| 556 | if ge i n |
| 557 | break |
| 558 | end |
| 559 | put qnm i hsg add o i |
| 560 | i = add i 1 |
| 561 | end |
| 562 | put qnml 0 n |
| 563 | c = get ect e |
| 564 | o = rf_off c |
| 565 | n = rf_len c |
| 566 | k = ulong 0 |
| 567 | for a2 ulong 4611686018427387904 |
| 568 | if ge k n |
| 569 | break |
| 570 | end |
| 571 | put qct k hc_get add o k |
| 572 | k = add k 1 |
| 573 | end |
| 574 | put qctl 0 n |
| 575 | put qgr 0 get esl e |
| 576 | if not fts_put e get efts e |
| 577 | trap 9 |
| 578 | end |
| 579 | end |
| 580 | note remove the FTS row of e (if any) and keep the max fts rowid |
| 581 | fn st_fts_drop e ulong to void |
| 582 | f = get efts e |
| 583 | if eq f 0 |
| 584 | ret |
| 585 | end |
| 586 | if eq get stv 5 0 |
| 587 | fts_del e |
| 588 | end |
| 589 | put efts e 0 |
| 590 | if eq f get stv 1 |
| 591 | st_ftsmax |
| 592 | end |
| 593 | end |
| 594 | note give e a fresh FTS row (max + 1) and index it |
| 595 | fn st_fts_new e ulong to void |
| 596 | f = add get stv 1 1 |
| 597 | put stv 1 f |
| 598 | put efts e f |
| 599 | if eq get stv 5 0 |
| 600 | st_fts_put e |
| 601 | end |
| 602 | end |
| 603 | note byte i of the string space (16 MiB per pool) |
| 604 | fn hsg i ulong to uchar |
| 605 | k = shr i 24 |
| 606 | j = and i 16777215 |
| 607 | if eq k 0 |
| 608 | ret get hs j |
| 609 | end |
| 610 | if eq k 1 |
| 611 | ret get hs1 j |
| 612 | end |
| 613 | if eq k 2 |
| 614 | ret get hs2 j |
| 615 | end |
| 616 | ret get hs3 j |
| 617 | end |
| 618 | note Omega has no elif: `elif eq k 1` parses as an assignment to a local named elif, so an |
| 619 | note if/elif chain runs every later body unconditionally (stored each byte into all later pools). |
| 620 | note Each branch returns on its own instead. tests/big covers it. |
| 621 | fn hsp i ulong v uchar to void |
| 622 | k = shr i 24 |
| 623 | j = and i 16777215 |
| 624 | if eq k 0 |
| 625 | put hs j v |
| 626 | ret |
| 627 | end |
| 628 | if eq k 1 |
| 629 | put hs1 j v |
| 630 | ret |
| 631 | end |
| 632 | if eq k 2 |
| 633 | put hs2 j v |
| 634 | ret |
| 635 | end |
| 636 | put hs3 j v |
| 637 | end |