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/cortex-rs/blob/main/om/import_main.om # cortex-rs Pure compiled native Rust memory engine with vector search and SQLite WAL Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `cortex-rs / om / import_main.om` 533 lines ยท 11404 bytes Raw Blame History 1 note import_main.om - program cortex-import SRC.db OUT.log (owner SqliteImport). 2 note Unit: crc32c.om + sqlite.om + import_main.om. Reads the cortex-rs tables spaces, entities, 3 note claims and entities_fts_content through sqlite.om (WAL overlay included) and writes the log 4 note defined in spec/BUILD-CONTRACT.md: CXLOG001, SPACE records (spaces rowid order), ENTITY 5 note records (entities rowid order; ftsrow = rowid of the entities_fts_content row whose c0 equals 6 note the entity id, 0 when absent), CLAIM records (claims rowid order). 7 note Records are staged in klw (crc32c.om) and sealed with crc_klw. A row whose record would not 8 note fit in klw (`8388608` bytes) is refused. OUT must not exist; the log is written to OUT.tmp 9 note (created O_EXCL), fdatasync'd, closed, renamed to OUT with RENAME_NOREPLACE, and the parent 10 note directory is fsync'd. stdout: IMPORT OK spaces=N entities=N claims=N fts=N 11 note (fts = entities that have an FTS row). stderr: wal/dbpages diagnostics and refusals. 12 note Exit: 0 ok, 2 usage, 2..41 sqlite.om refusals (sq_die), 53..79 import refusals (imp_die). 13 text kimsg "cortex-import: \nusage: cortex-import SRC.db OUT.log\nimport: output file already exists; refusing to overwrite\nimport: cannot create OUT.tmp (it exists or the directory is not writable)\nimport: write error\nimport: fdatasync failed\nimport: close failed\nimport: rename OUT.tmp to OUT failed\nimport: a cortex table is missing from sqlite_master\nimport: table schema differs from the cortex-rs schema\nimport: wrong column count in a row\nimport: TEXT column holds a non-text value\nimport: BLOB column holds a non-blob value\nimport: integer out of range for its field\nimport: row too large for the `8388608`-byte log buffer\nimport: duplicate c0 in entities_fts_content\nimport: FTS map capacity exceeded\nimport: directory fsync failed\nimport: OUT path missing or longer than 2000 bytes\nIMPORT OK spaces=\n entities=\n claims=\n fts=\nimport: cannot check whether OUT exists\nimport: entities_fts_content c0 is not text\nimport: rootpage is not an integer\nimport: fts rows without entity=\n" 14 note ksch: whitespace-free schema segments, LF separated: 0 "table", then per table k (0 spaces, 15 note 1 entities, 2 claims, 3 entities_fts_content) name at 1+2k and stripped CREATE sql at 2+2k 16 note (taken from cortex-rs src/db.rs init_schema as stored by SQLite 3.45 in sqlite_master). 17 text ksch "table\nspaces\nCREATETABLEspaces(idTEXTPRIMARYKEY,slugTEXTUNIQUENOTNULL,created_atTEXTNOTNULL)\nentities\nCREATETABLEentities(idTEXTPRIMARYKEY,space_idTEXTNOTNULL,space_slugTEXTNOTNULL,entity_typeTEXTNOTNULL,canonical_nameTEXTNOTNULL,contentTEXTNOTNULL,aliases_jsonTEXTNOTNULLDEFAULT'[]',metadata_jsonTEXTNOTNULLDEFAULT'{}',confidenceREALNOTNULLDEFAULT1.0,revisionINTEGERNOTNULLDEFAULT1,retractedINTEGERNOTNULLDEFAULT0,valid_fromTEXT,valid_toTEXT,created_atTEXTNOTNULL,embeddingBLOB)\nclaims\nCREATETABLEclaims(idTEXTPRIMARYKEY,space_idTEXTNOTNULL,space_slugTEXTNOTNULL,subject_entity_idTEXTNOTNULL,predicateTEXTNOTNULL,object_entity_idTEXT,literal_value_jsonTEXT,confidenceREALNOTNULLDEFAULT1.0,metadata_jsonTEXTNOTNULLDEFAULT'{}',retractedINTEGERNOTNULLDEFAULT0,created_atTEXTNOTNULL)\nentities_fts_content\nCREATETABLE'entities_fts_content'(idINTEGERPRIMARYKEY,c0,c1,c2,c3)\n" 18 text klogmag "CXLOG001" 19 text kdot "." 20 global kopath uchar 4100 21 global kopn ulong 1 22 global kofd int 1 23 global kpl uchar 4096 24 global kroot ulong 4 25 global kwp ulong 1 26 global kcnt ulong 4 27 global kseg ulong 2 28 note FTS map: c0 bytes in arena kfa, entries kfo (offset) kfl (length) kfr (rowid) kfm (matched), 29 note open addressing kfh (entry index + 1) with `2097152` slots for at most `1048576` entries. 30 global kfa uchar `8388608` 31 global kfan ulong 1 32 global kfo ulong `1048576` 33 global kfl uint `1048576` 34 global kfr ulong `1048576` 35 global kfm uchar `1048576` 36 global kfn ulong 1 37 global kfh uint `2097152` 38 fn imp_lmsg k ulong to void 39 ln = ulong 1 40 i = ulong 0 41 n = len kimsg 42 while lt i n bound 999999 43 c = get kimsg i 44 if eq c 10 45 ln = add ln 1 46 else 47 if eq ln k 48 sq_lbyte c 49 end 50 end 51 i = add i 1 52 end 53 end 54 fn imp_die k ulong to void 55 sq_unl 56 put klnn 0 0 57 sq_lmsg 1 58 imp_lmsg k 59 sq_lbyte 10 60 sq_lout 2 61 if eq k 2 62 exit 2 63 end 64 exit int add k 50 65 end 66 fn imp_seg s ulong to void 67 sg = ulong 0 68 st = ulong 0 69 for i ulong len ksch 70 if eq get ksch i 10 71 if eq sg s 72 put kseg 0 st 73 put kseg 1 sub i st 74 ret 75 end 76 sg = add sg 1 77 st = add i 1 78 end 79 end 80 put kseg 0 0 81 put kseg 1 0 82 end 83 fn imp_ws b uchar to bool 84 ret or or eq b 32 eq b 10 or eq b 9 eq b 13 85 end 86 note imp_eqcol: text column c equals segment s (whitespace in the column skipped when strip). 87 fn imp_eqcol c ulong s ulong strip bool to bool 88 if ne sq_ckind c 3 89 ret false 90 end 91 imp_seg s 92 j = get kseg 0 93 e = add j get kseg 1 94 o = get kcof c 95 oe = add o get kcln c 96 while lt o oe bound 999999 97 b = get kpay o 98 o = add o 1 99 if and strip imp_ws b 100 continue 101 end 102 if ge j e 103 ret false 104 end 105 if ne b get ksch j 106 ret false 107 end 108 j = add j 1 109 end 110 ret eq j e 111 end 112 fn imp_master to void 113 for k ulong 4 114 put kroot k 0 115 end 116 sq_first 1 117 while sq_next bound 999999 118 if ne get kncol 0 5 119 imp_die 11 120 end 121 if imp_eqcol 0 0 false 122 for k ulong 4 123 if imp_eqcol 1 add 1 mul 2 k false 124 if ne sq_ckind 3 1 125 imp_die 26 126 end 127 if not imp_eqcol 4 add 2 mul 2 k true 128 imp_die 10 129 end 130 put kroot k ulong sq_cint 3 131 end 132 end 133 end 134 end 135 for k ulong 4 136 if eq get kroot k 0 137 imp_die 9 138 end 139 end 140 end 141 fn imp_u8 v uchar to void 142 w = get kwp 0 143 put klw w v 144 put kwp 0 add w 1 145 end 146 fn imp_put32 o ulong v uint to void 147 put klw o uchar and v 255 148 put klw add o 1 uchar and shr v 8 255 149 put klw add o 2 uchar and shr v 16 255 150 put klw add o 3 uchar shr v 24 151 end 152 fn imp_u32 v uint to void 153 w = get kwp 0 154 imp_put32 w v 155 put kwp 0 add w 4 156 end 157 fn imp_u64 v ulong to void 158 w = get kwp 0 159 imp_put32 w uint and v `4294967295` 160 imp_put32 add w 4 uint shr v 32 161 put kwp 0 add w 8 162 end 163 note imp_bytes: kpay[o..o+n) -> klw[kwp..); n <= `8388608` gives <= 128 chunks of 65536. 164 fn imp_bytes o ulong n ulong to void 165 w = get kwp 0 166 if gt n sub len klw w 167 imp_die 15 168 end 169 i = ulong 0 170 while lt i n bound 200 171 m = sub n i 172 if gt m 65536 173 m = 65536 174 end 175 imp_cp add w i add o i m 176 i = add i m 177 end 178 put kwp 0 add w n 179 end 180 fn imp_cp t ulong s ulong m ulong to void 181 for j ulong m 182 put klw add t j get kpay add s j 183 end 184 end 185 fn imp_str c ulong to void 186 k = sq_ckind c 187 if eq k 0 188 imp_u32 `4294967295` 189 ret 190 end 191 if ne k 3 192 imp_die 12 193 end 194 n = get kcln c 195 imp_u32 uint n 196 imp_bytes get kcof c n 197 end 198 fn imp_blob c ulong to void 199 k = sq_ckind c 200 if eq k 0 201 imp_u32 `4294967295` 202 ret 203 end 204 if ne k 4 205 imp_die 13 206 end 207 n = get kcln c 208 imp_u32 uint n 209 imp_bytes get kcof c n 210 end 211 fn imp_flush to void 212 n = get kwp 0 213 d = ulong 0 214 while lt d n bound 999999 215 k = write get kofd 0 klw d sub n d 216 if le k 0 217 imp_die 5 218 end 219 d = add d ulong k 220 end 221 put kwp 0 0 222 end 223 note imp_begin: room for the whole row (payload bytes + 512 bytes of field overhead; the largest 224 note record, ENTITY, adds 89 bytes of fixed fields to the payload's column bytes). 225 fn imp_begin to ulong 226 need = add get kplen 0 512 227 if gt need len klw 228 imp_die 15 229 end 230 if gt add get kwp 0 need len klw 231 imp_flush 232 end 233 r = get kwp 0 234 put kwp 0 add r 8 235 ret r 236 end 237 fn imp_end r ulong to void 238 pl = sub sub get kwp 0 r 8 239 c = crc_klw add r 8 pl 240 imp_put32 r uint pl 241 imp_put32 add r 4 c 242 end 243 fn imp_fhash c ulong to ulong 244 h = uint `2166136261` 245 o = get kcof c 246 for i ulong get kcln c 247 h = mul xor h uint get kpay add o i `16777619` 248 end 249 ret and ulong h `2097151` 250 end 251 fn imp_feq e ulong c ulong to bool 252 n = get kcln c 253 if ne ulong get kfl e n 254 ret false 255 end 256 a = get kfo e 257 o = get kcof c 258 for i ulong n 259 if ne get kfa add a i get kpay add o i 260 ret false 261 end 262 end 263 ret true 264 end 265 note imp_fslot: slot holding column c's bytes, or the empty slot where they belong. 266 fn imp_fslot c ulong to ulong 267 h = imp_fhash c 268 for i ulong `2097152` 269 e = ulong get kfh h 270 if eq e 0 271 ret h 272 end 273 if imp_feq sub e 1 c 274 ret h 275 end 276 h = and add h 1 `2097151` 277 end 278 imp_die 17 279 ret 0 280 end 281 fn imp_fadd to void 282 k = sq_ckind 1 283 if eq k 0 284 ret 285 end 286 if ne k 3 287 imp_die 25 288 end 289 s = imp_fslot 1 290 if ne get kfh s 0 291 imp_die 16 292 end 293 e = get kfn 0 294 n = get kcln 1 295 a = get kfan 0 296 if or ge e len kfo gt n sub len kfa a 297 imp_die 17 298 end 299 o = get kcof 1 300 for i ulong n 301 put kfa add a i get kpay add o i 302 end 303 put kfo e a 304 put kfl e uint n 305 put kfr e get krowid 0 306 put kfh s uint add e 1 307 put kfn 0 add e 1 308 put kfan 0 add a n 309 end 310 fn imp_fget to ulong 311 if ne sq_ckind 0 3 312 ret 0 313 end 314 e = ulong get kfh imp_fslot 0 315 if eq e 0 316 ret 0 317 end 318 put kfm sub e 1 1 319 put kcnt 3 add get kcnt 3 1 320 ret get kfr sub e 1 321 end 322 fn imp_ncol n ulong to void 323 if ne get kncol 0 n 324 imp_die 11 325 end 326 end 327 fn imp_byte8 c ulong to void 328 v = sq_cint c 329 if or lt v 0 gt v 255 330 imp_die 14 331 end 332 imp_u8 uchar v 333 end 334 fn imp_space to void 335 imp_ncol 3 336 r = imp_begin 337 imp_u8 1 338 imp_u64 get krowid 0 339 imp_str 0 340 imp_str 1 341 imp_str 2 342 imp_end r 343 end 344 fn imp_entity to void 345 imp_ncol 15 346 r = imp_begin 347 imp_u8 2 348 imp_u64 get krowid 0 349 imp_u64 imp_fget 350 for c ulong 8 351 imp_str c 352 end 353 imp_u64 bitcast ulong sq_creal 8 354 imp_u64 bitcast ulong sq_cint 9 355 imp_byte8 10 356 imp_str 11 357 imp_str 12 358 imp_str 13 359 imp_blob 14 360 imp_end r 361 end 362 fn imp_claim to void 363 imp_ncol 11 364 r = imp_begin 365 imp_u8 4 366 imp_u64 get krowid 0 367 for c ulong 7 368 imp_str c 369 end 370 imp_u64 bitcast ulong sq_creal 7 371 imp_str 8 372 imp_byte8 9 373 imp_str 10 374 imp_end r 375 end 376 note imp_walk: every row of table kroot[t] in rowid order; mode t: 0 spaces, 1 entities, 377 note 2 claims, 3 fts content (map). Outer passes of 1000 rows: up to `999999000` rows. 378 fn imp_walk t ulong to ulong 379 sq_first get kroot t 380 n = ulong 0 381 more = true 382 while more bound 999999 383 for q ulong 1000 384 if not sq_next 385 more = false 386 break 387 end 388 if eq t 0 389 imp_space 390 end 391 if eq t 1 392 imp_entity 393 end 394 if eq t 2 395 imp_claim 396 end 397 if eq t 3 398 imp_fadd 399 end 400 n = add n 1 401 end 402 end 403 ret n 404 end 405 fn imp_outpath to void 406 n = argread 2 kopath 0 2000 407 if lt n 1 408 imp_die 19 409 end 410 m = ulong n 411 put kopn 0 m 412 put kopath m 46 413 put kopath add m 1 116 414 put kopath add m 2 109 415 put kopath add m 3 112 416 end 417 note imp_rename: packed list (u16 count 2, u16 len + OUT.tmp, u16 len + OUT), RENAME_NOREPLACE. 418 fn imp_rename to void 419 n = get kopn 0 420 t = add n 4 421 put kpl 0 2 422 put kpl 1 0 423 put kpl 2 uchar and t 255 424 put kpl 3 uchar shr t 8 425 for i ulong t 426 put kpl add 4 i get kopath i 427 end 428 o = add 4 t 429 put kpl o uchar and n 255 430 put kpl add o 1 uchar shr n 8 431 for i ulong n 432 put kpl add add o 2 i get kopath i 433 end 434 r = renames kpl 0 add add o 2 n 1 435 if ne r 0 436 imp_die 8 437 end 438 end 439 fn imp_dirsync to void 440 n = get kopn 0 441 p = n 442 for i ulong n 443 if eq get kopath i 47 444 p = i 445 end 446 end 447 fd = long -1 448 if eq p n 449 fd = fopen kdot 0 1 540672 0 450 else 451 d = p 452 if eq d 0 453 d = 1 454 end 455 fd = fopen kopath 0 d 540672 0 456 end 457 if lt fd 0 458 imp_die 18 459 end 460 if ne fsync int fd 0 461 imp_die 18 462 end 463 r = fclose int fd 464 if ne r 0 465 imp_die 18 466 end 467 end 468 fn imp_summary to void 469 put klnn 0 0 470 imp_lmsg 20 471 sq_ldec get kcnt 0 472 imp_lmsg 21 473 sq_ldec get kcnt 1 474 imp_lmsg 22 475 sq_ldec get kcnt 2 476 imp_lmsg 23 477 sq_ldec get kcnt 3 478 sq_lbyte 10 479 sq_lout 1 480 put klnn 0 0 481 sq_lmsg 1 482 imp_lmsg 27 483 sq_ldec sub get kfn 0 get kcnt 3 484 sq_lbyte 10 485 sq_lout 2 486 end 487 fn main to int 488 if ne argcnt 3 489 imp_die 2 490 end 491 sq_argpath 1 492 imp_outpath 493 r = faccess kopath 0 get kopn 0 0 494 if eq r 0 495 imp_die 3 496 end 497 if ne r -2 498 imp_die 24 499 end 500 sq_open 501 imp_master 502 fd = fopen kopath 0 add get kopn 0 4 524481 420 503 if lt fd 0 504 imp_die 4 505 end 506 put kofd 0 int fd 507 for i ulong add get kopn 0 4 508 put kunl i get kopath i 509 end 510 put kunln 0 add get kopn 0 4 511 for i ulong 8 512 put klw i get klogmag i 513 end 514 put kwp 0 8 515 put kcnt 3 0 516 nfts = imp_walk 3 517 put kcnt 3 0 518 put kcnt 0 imp_walk 0 519 put kcnt 1 imp_walk 1 520 put kcnt 2 imp_walk 2 521 imp_flush 522 if ne fdsync int fd 0 523 imp_die 6 524 end 525 if ne fclose int fd 0 526 imp_die 7 527 end 528 imp_rename 529 put kunln 0 0 530 imp_dirsync 531 imp_summary 532 ret 0 533 end