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 / src / tensor / omega_tensor_store.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note omega_tensor_store.om -- Pure seeded OM-2 M20 crash-safe tensor storage lifetime. |
| 3 | note Composed with tensor_shape.om, tg_sha256.om, omega_tensor_cpu.om, and omega_tensor.om. |
| 4 | |
| 5 | const OTS_ERR_IO long -20 |
| 6 | const OTS_ERR_TORN long -21 |
| 7 | const OTS_ERR_CORRUPT long -22 |
| 8 | const OTS_ERR_BUSY long -23 |
| 9 | |
| 10 | const OTS_HDR_BYTES ulong 56 |
| 11 | const OTS_HDR_SUM ulong 24 |
| 12 | const OTS_REC_BYTES ulong 200 |
| 13 | const OTS_REC_SUM ulong 168 |
| 14 | const OTS_REC_PUT ulong 1 |
| 15 | const OTS_REC_RELEASE ulong 2 |
| 16 | const OTS_MAX_CAP ulong 64 |
| 17 | const OTS_JBUF_MAX ulong 65536 |
| 18 | |
| 19 | const OTS_CRASH_NONE ulong 0 |
| 20 | const OTS_CRASH_PAYLOAD_PARTIAL ulong 1 |
| 21 | const OTS_CRASH_PAYLOAD_WRITTEN ulong 2 |
| 22 | const OTS_CRASH_PAYLOAD_RENAMED ulong 3 |
| 23 | const OTS_CRASH_PAYLOAD_SYNCED ulong 4 |
| 24 | const OTS_CRASH_JOURNAL_PARTIAL ulong 5 |
| 25 | const OTS_CRASH_JOURNAL_WRITTEN ulong 6 |
| 26 | const OTS_CRASH_JOURNAL_RENAMED ulong 7 |
| 27 | const OTS_CRASH_JOURNAL_SYNCED ulong 8 |
| 28 | const OTS_CRASH_COUNT ulong 9 |
| 29 | const OTS_CRASH_EXIT int 86 |
| 30 | |
| 31 | const OTS_O_RDONLY int 0 |
| 32 | const OTS_O_WRONLY int 1 |
| 33 | const OTS_O_CREAT int 64 |
| 34 | const OTS_O_TRUNC int 512 |
| 35 | const OTS_O_DIRECTORY int 16384 |
| 36 | const OTS_LOCK_EX_NB int 6 |
| 37 | |
| 38 | global ots_dir uchar 512 |
| 39 | global ots_meta ulong 8 |
| 40 | note ots_meta: 0=open, 1=dlen, 2=lock_fd+1, 3=cap, 4=jlen, 5=count, 6=crash_step |
| 41 | global ots_sl_meta ulong 256 |
| 42 | note per slot (stride 4): 0=live, 1=gen, 2=dtype, 3=rank |
| 43 | global ots_sl_shape ulong 512 |
| 44 | note per slot (stride 8) |
| 45 | global ots_sl_vid uchar 2048 |
| 46 | note per slot (stride 32) |
| 47 | global ots_sl_psha uchar 2048 |
| 48 | note per slot (stride 32) |
| 49 | |
| 50 | global ots_jbuf uchar 65536 |
| 51 | global ots_pbuf uchar 16384 |
| 52 | global ots_pa uchar 600 |
| 53 | global ots_pb uchar 600 |
| 54 | global ots_plen ulong 2 |
| 55 | global ots_dents uchar 16384 |
| 56 | |
| 57 | text ots_txt_ptmp "payload.tmp" |
| 58 | text ots_txt_jtmp "journal.tmp" |
| 59 | text ots_txt_jrn "journal" |
| 60 | |
| 61 | fn ots_set_crash_step step ulong to long |
| 62 | if ge step OTS_CRASH_COUNT |
| 63 | ret OT_ERR_BAD_ARGS |
| 64 | end |
| 65 | put ots_meta 6 step |
| 66 | ret OT_OK |
| 67 | end |
| 68 | |
| 69 | fn ots_crash_hit step ulong to void |
| 70 | if eq get ots_meta 6 step |
| 71 | exit OTS_CRASH_EXIT |
| 72 | end |
| 73 | end |
| 74 | |
| 75 | fn ots_mk_path slot ulong name view uchar * to ulong |
| 76 | dl = get ots_meta 1 |
| 77 | nl = len name |
| 78 | if ge add add dl 1 nl 600 |
| 79 | put ots_plen slot 0 |
| 80 | ret 0 |
| 81 | end |
| 82 | if eq slot 0 |
| 83 | for i ulong dl |
| 84 | put ots_pa i get ots_dir i |
| 85 | end |
| 86 | put ots_pa dl 47 |
| 87 | for i ulong nl |
| 88 | put ots_pa add add dl 1 i get name i |
| 89 | end |
| 90 | else |
| 91 | for i ulong dl |
| 92 | put ots_pb i get ots_dir i |
| 93 | end |
| 94 | put ots_pb dl 47 |
| 95 | for i ulong nl |
| 96 | put ots_pb add add dl 1 i get name i |
| 97 | end |
| 98 | end |
| 99 | tot = add add dl 1 nl |
| 100 | put ots_plen slot tot |
| 101 | ret tot |
| 102 | end |
| 103 | |
| 104 | fn ots_rename_pa_to_pb to long |
| 105 | ol = get ots_plen 0 |
| 106 | nl = get ots_plen 1 |
| 107 | pkt = new uchar 1216 0 |
| 108 | put pkt 0 2 |
| 109 | put pkt 1 0 |
| 110 | put pkt 2 uchar and ol 255 |
| 111 | put pkt 3 uchar and shr ol 8 255 |
| 112 | for i ulong ol |
| 113 | put pkt add 4 i get ots_pa i |
| 114 | end |
| 115 | o2 = add 4 ol |
| 116 | put pkt o2 uchar and nl 255 |
| 117 | put pkt add o2 1 uchar and shr nl 8 255 |
| 118 | for i ulong nl |
| 119 | put pkt add add o2 2 i get ots_pb i |
| 120 | end |
| 121 | ret renames pkt 0 add add o2 2 nl 0 |
| 122 | end |
| 123 | |
| 124 | fn ots_fsync_dir to long |
| 125 | fd = fopen ots_dir 0 get ots_meta 1 or OTS_O_RDONLY OTS_O_DIRECTORY 0 |
| 126 | if lt fd 0 |
| 127 | ret -1 |
| 128 | end |
| 129 | r = fsync int fd |
| 130 | c = fclose int fd |
| 131 | if ne r 0 |
| 132 | ret -1 |
| 133 | end |
| 134 | ret c |
| 135 | end |
| 136 | |
| 137 | fn ots_write_global fd int is_jbuf ulong n ulong to long |
| 138 | off = ulong 0 |
| 139 | while lt off n bound 10000 |
| 140 | w = long 0 |
| 141 | if ne is_jbuf 0 |
| 142 | w = write fd ots_jbuf off sub n off |
| 143 | else |
| 144 | w = write fd ots_pbuf off sub n off |
| 145 | end |
| 146 | if lt w 0 |
| 147 | if eq w -4 |
| 148 | continue |
| 149 | end |
| 150 | ret -1 |
| 151 | end |
| 152 | off = add off ulong w |
| 153 | end |
| 154 | ret 0 |
| 155 | end |
| 156 | |
| 157 | fn ots_commit_pa_pb is_jbuf ulong n ulong step0 ulong to long |
| 158 | fd = fopen ots_pa 0 get ots_plen 0 or or OTS_O_WRONLY OTS_O_CREAT OTS_O_TRUNC 420 |
| 159 | if lt fd 0 |
| 160 | ret OTS_ERR_IO |
| 161 | end |
| 162 | if eq get ots_meta 6 step0 |
| 163 | half = div n 2 |
| 164 | if and eq half 0 gt n 0 |
| 165 | half = 1 |
| 166 | end |
| 167 | if eq ots_write_global int fd is_jbuf half 0 |
| 168 | fs = fsync int fd |
| 169 | end |
| 170 | cc = fclose int fd |
| 171 | exit OTS_CRASH_EXIT |
| 172 | end |
| 173 | if ne ots_write_global int fd is_jbuf n 0 |
| 174 | cc = fclose int fd |
| 175 | ret OTS_ERR_IO |
| 176 | end |
| 177 | if ne fsync int fd 0 |
| 178 | cc = fclose int fd |
| 179 | ret OTS_ERR_IO |
| 180 | end |
| 181 | if ne fclose int fd 0 |
| 182 | ret OTS_ERR_IO |
| 183 | end |
| 184 | ots_crash_hit add step0 1 |
| 185 | if ne ots_rename_pa_to_pb 0 |
| 186 | ret OTS_ERR_IO |
| 187 | end |
| 188 | ots_crash_hit add step0 2 |
| 189 | if ne ots_fsync_dir 0 |
| 190 | ret OTS_ERR_IO |
| 191 | end |
| 192 | ots_crash_hit add step0 3 |
| 193 | ret OT_OK |
| 194 | end |
| 195 | |
| 196 | fn ots_encode_hdr cap ulong count ulong to void |
| 197 | h24 = new uchar 24 0 |
| 198 | tstore_encode_header cap count h24 |
| 199 | dig = new uchar 32 0 |
| 200 | tg_sha_hash h24 24 dig |
| 201 | for i ulong 24 |
| 202 | put ots_jbuf i get h24 i |
| 203 | end |
| 204 | for i ulong 32 |
| 205 | put ots_jbuf add 24 i get dig i |
| 206 | end |
| 207 | end |
| 208 | |
| 209 | fn ots_encode_rec_at off ulong meta view ulong 6 sh view ulong 8 vid view uchar 32 psha view uchar 32 to void |
| 210 | r168 = new uchar 168 0 |
| 211 | tstore_encode_record meta sh vid psha r168 |
| 212 | dig = new uchar 32 0 |
| 213 | tg_sha_hash r168 168 dig |
| 214 | for i ulong 168 |
| 215 | put ots_jbuf add off i get r168 i |
| 216 | end |
| 217 | for i ulong 32 |
| 218 | put ots_jbuf add add off 168 i get dig i |
| 219 | end |
| 220 | end |
| 221 | |
| 222 | fn ots_check_put_shape rk ulong sh view ulong 8 to long |
| 223 | el = new ulong 1 0 |
| 224 | if ne ot_shape_check rk sh el OT_OK |
| 225 | ret OTS_ERR_TORN |
| 226 | end |
| 227 | for d ulong sub OT_MAX_RANK rk |
| 228 | if ne get sh add rk d 0 |
| 229 | ret OTS_ERR_TORN |
| 230 | end |
| 231 | end |
| 232 | ret OT_OK |
| 233 | end |
| 234 | |
| 235 | fn ots_store_put_slot sl ulong dt ulong rk ulong sh view ulong 8 vid view uchar 32 psha view uchar 32 to void |
| 236 | sb = mul sl 4 |
| 237 | put ots_sl_meta sb 1 |
| 238 | put ots_sl_meta add sb 2 dt |
| 239 | put ots_sl_meta add sb 3 rk |
| 240 | for d ulong 8 |
| 241 | put ots_sl_shape add mul sl 8 d get sh d |
| 242 | end |
| 243 | for i ulong 32 |
| 244 | put ots_sl_vid add mul sl 32 i get vid i |
| 245 | put ots_sl_psha add mul sl 32 i get psha i |
| 246 | end |
| 247 | end |
| 248 | |
| 249 | fn ots_apply_put sl ulong gen ulong meta view ulong 6 sh view ulong 8 vid view uchar 32 psha view uchar 32 to long |
| 250 | sb = mul sl 4 |
| 251 | if ne get ots_sl_meta sb 0 |
| 252 | ret OTS_ERR_TORN |
| 253 | end |
| 254 | cur_g = get ots_sl_meta add sb 1 |
| 255 | if or eq cur_g OT_U64_MAX ne gen cur_g |
| 256 | ret OTS_ERR_TORN |
| 257 | end |
| 258 | dt = get meta 4 |
| 259 | rk = get meta 5 |
| 260 | if or eq ot_dtype_size dt 0 gt rk OT_MAX_RANK |
| 261 | ret OTS_ERR_TORN |
| 262 | end |
| 263 | if ne ots_check_put_shape rk sh OT_OK |
| 264 | ret OTS_ERR_TORN |
| 265 | end |
| 266 | ots_store_put_slot sl dt rk sh vid psha |
| 267 | ret OT_OK |
| 268 | end |
| 269 | |
| 270 | fn ots_apply_release sl ulong gen ulong to long |
| 271 | sb = mul sl 4 |
| 272 | if eq get ots_sl_meta sb 0 |
| 273 | ret OTS_ERR_TORN |
| 274 | end |
| 275 | if ne gen get ots_sl_meta add sb 1 |
| 276 | ret OTS_ERR_TORN |
| 277 | end |
| 278 | put ots_sl_meta sb 0 |
| 279 | put ots_sl_meta add sb 1 add gen 1 |
| 280 | ret OT_OK |
| 281 | end |
| 282 | |
| 283 | fn ots_apply_record meta view ulong 6 sh view ulong 8 vid view uchar 32 psha view uchar 32 expect_seq ulong to long |
| 284 | kind = get meta 0 |
| 285 | sl = get meta 1 |
| 286 | gen = get meta 2 |
| 287 | if or ne get meta 3 expect_seq ge sl get ots_meta 3 |
| 288 | ret OTS_ERR_TORN |
| 289 | end |
| 290 | if eq kind OTS_REC_PUT |
| 291 | ret ots_apply_put sl gen meta sh vid psha |
| 292 | end |
| 293 | if eq kind OTS_REC_RELEASE |
| 294 | ret ots_apply_release sl gen |
| 295 | end |
| 296 | ret OTS_ERR_TORN |
| 297 | end |
| 298 | |
| 299 | fn ots_replay_one i ulong to long |
| 300 | off = add OTS_HDR_BYTES mul i OTS_REC_BYTES |
| 301 | r168 = new uchar 168 0 |
| 302 | for k ulong 168 |
| 303 | put r168 k get ots_jbuf add off k |
| 304 | end |
| 305 | meta = new ulong 6 0 |
| 306 | sh = new ulong 8 0 |
| 307 | vid = new uchar 32 0 |
| 308 | psha = new uchar 32 0 |
| 309 | if eq tstore_decode_record r168 meta sh vid psha 0 |
| 310 | ret OTS_ERR_TORN |
| 311 | end |
| 312 | sum = new uchar 32 0 |
| 313 | tg_sha_hash r168 168 sum |
| 314 | for k ulong 32 |
| 315 | if ne get sum k get ots_jbuf add add off 168 k |
| 316 | ret OTS_ERR_TORN |
| 317 | end |
| 318 | end |
| 319 | ret ots_apply_record meta sh vid psha i |
| 320 | end |
| 321 | |
| 322 | fn ots_read_payload_fd fd int expect ulong to long |
| 323 | n = ulong 0 |
| 324 | extra = new uchar 1 0 |
| 325 | longer = ulong 0 |
| 326 | while true bound 1000 |
| 327 | if lt n expect |
| 328 | r = read fd ots_pbuf n sub expect n |
| 329 | if lt r 0 |
| 330 | if eq r -4 |
| 331 | continue |
| 332 | end |
| 333 | ret OTS_ERR_IO |
| 334 | end |
| 335 | if eq r 0 |
| 336 | break |
| 337 | end |
| 338 | n = add n ulong r |
| 339 | else |
| 340 | r = read fd extra 0 1 |
| 341 | if lt r 0 |
| 342 | if eq r -4 |
| 343 | continue |
| 344 | end |
| 345 | ret OTS_ERR_IO |
| 346 | end |
| 347 | if gt r 0 |
| 348 | longer = 1 |
| 349 | end |
| 350 | break |
| 351 | end |
| 352 | end |
| 353 | if or ne longer 0 ne n expect |
| 354 | ret OTS_ERR_CORRUPT |
| 355 | end |
| 356 | ret OT_OK |
| 357 | end |
| 358 | |
| 359 | fn ots_load_payload sl ulong to long |
| 360 | sb = mul sl 4 |
| 361 | dt = get ots_sl_meta add sb 2 |
| 362 | rk = get ots_sl_meta add sb 3 |
| 363 | sh = new ulong 8 0 |
| 364 | for d ulong 8 |
| 365 | put sh d get ots_sl_shape add mul sl 8 d |
| 366 | end |
| 367 | el = new ulong 1 0 |
| 368 | if ne ot_shape_check rk sh el OT_OK |
| 369 | ret OTS_ERR_CORRUPT |
| 370 | end |
| 371 | expect = mul get el 0 ot_dtype_size dt |
| 372 | if gt expect 16384 |
| 373 | ret OT_ERR_CAPACITY |
| 374 | end |
| 375 | vid = new uchar 32 0 |
| 376 | for i ulong 32 |
| 377 | put vid i get ots_sl_vid add mul sl 32 i |
| 378 | end |
| 379 | nm = new uchar 68 0 |
| 380 | tstore_hex_name vid nm |
| 381 | if eq ots_mk_path 0 nm 0 |
| 382 | ret OTS_ERR_IO |
| 383 | end |
| 384 | fd = fopen ots_pa 0 get ots_plen 0 OTS_O_RDONLY 0 |
| 385 | if lt fd 0 |
| 386 | if eq fd -2 |
| 387 | ret OTS_ERR_CORRUPT |
| 388 | end |
| 389 | ret OTS_ERR_IO |
| 390 | end |
| 391 | rc = ots_read_payload_fd int fd expect |
| 392 | cc = fclose int fd |
| 393 | if ne rc OT_OK |
| 394 | ret rc |
| 395 | end |
| 396 | ret ots_verify_payload sl expect |
| 397 | end |
| 398 | |
| 399 | fn ots_verify_payload sl ulong expect ulong to long |
| 400 | loc = new uchar 16384 0 |
| 401 | for i ulong expect bound 16385 |
| 402 | put loc i get ots_pbuf i |
| 403 | end |
| 404 | dig = new uchar 32 0 |
| 405 | tg_sha_hash part loc 0 expect expect dig |
| 406 | for i ulong 32 |
| 407 | if ne get dig i get ots_sl_psha add mul sl 32 i |
| 408 | ret OTS_ERR_CORRUPT |
| 409 | end |
| 410 | end |
| 411 | ret OT_OK |
| 412 | end |
| 413 | |
| 414 | fn ots_payload_ref nm view uchar 68 to ulong |
| 415 | cap = get ots_meta 3 |
| 416 | vid = new uchar 32 0 |
| 417 | cand = new uchar 68 0 |
| 418 | for i ulong cap |
| 419 | if eq get ots_sl_meta mul i 4 0 |
| 420 | continue |
| 421 | end |
| 422 | for k ulong 32 |
| 423 | put vid k get ots_sl_vid add mul i 32 k |
| 424 | end |
| 425 | tstore_hex_name vid cand |
| 426 | same = ulong 1 |
| 427 | for k ulong 68 |
| 428 | if ne get cand k get nm k |
| 429 | same = 0 |
| 430 | end |
| 431 | end |
| 432 | if eq same 1 |
| 433 | ret 1 |
| 434 | end |
| 435 | end |
| 436 | ret 0 |
| 437 | end |
| 438 | |
| 439 | fn ots_name_eq nm view uchar 256 nlen ulong txt view uchar * to ulong |
| 440 | if ne nlen len txt |
| 441 | ret 0 |
| 442 | end |
| 443 | for i ulong nlen |
| 444 | if ne get nm i get txt i |
| 445 | ret 0 |
| 446 | end |
| 447 | end |
| 448 | ret 1 |
| 449 | end |
| 450 | |
| 451 | fn ots_sweep_one pos ulong nru ulong only_chk ulong any_out edit ulong 1 to ulong |
| 452 | nm = new uchar 256 0 |
| 453 | p68 = new uchar 68 0 |
| 454 | nl = ulong 0 |
| 455 | noff = add pos 19 |
| 456 | while lt nl 255 bound 256 |
| 457 | if ge add noff nl nru |
| 458 | break |
| 459 | end |
| 460 | ch = get ots_dents add noff nl |
| 461 | if eq ch 0 |
| 462 | break |
| 463 | end |
| 464 | put nm nl ch |
| 465 | nl = add nl 1 |
| 466 | end |
| 467 | is_tmp = or ne ots_name_eq nm nl ots_txt_ptmp 0 ne ots_name_eq nm nl ots_txt_jtmp 0 |
| 468 | is_pay = ne tstore_is_payload_name part nm 0 nl 0 |
| 469 | if is_pay |
| 470 | put any_out 0 1 |
| 471 | end |
| 472 | if ne only_chk 0 |
| 473 | ret 0 |
| 474 | end |
| 475 | do_rm = is_tmp |
| 476 | if is_pay |
| 477 | for k ulong 68 |
| 478 | put p68 k get nm k |
| 479 | end |
| 480 | if eq ots_payload_ref p68 0 |
| 481 | do_rm = true |
| 482 | end |
| 483 | end |
| 484 | if do_rm |
| 485 | pl = ots_mk_path 0 part nm 0 nl |
| 486 | if gt pl 0 |
| 487 | if eq unlink ots_pa 0 pl 0 |
| 488 | ret 1 |
| 489 | end |
| 490 | end |
| 491 | end |
| 492 | ret 0 |
| 493 | end |
| 494 | |
| 495 | fn ots_sweep_dir only_chk ulong any_out edit ulong 1 to long |
| 496 | put any_out 0 0 |
| 497 | dfd = fopen ots_dir 0 get ots_meta 1 or OTS_O_RDONLY OTS_O_DIRECTORY 0 |
| 498 | if lt dfd 0 |
| 499 | ret OTS_ERR_IO |
| 500 | end |
| 501 | rm = ulong 0 |
| 502 | while true bound 512 |
| 503 | nr = getdnts int dfd ots_dents 0 16384 |
| 504 | if lt nr 0 |
| 505 | cc = fclose int dfd |
| 506 | ret OTS_ERR_IO |
| 507 | end |
| 508 | if eq nr 0 |
| 509 | break |
| 510 | end |
| 511 | pos = ulong 0 |
| 512 | nru = ulong nr |
| 513 | while lt pos nru bound 4096 |
| 514 | lo = ulong get ots_dents add pos 16 |
| 515 | hi = shl ulong get ots_dents add pos 17 8 |
| 516 | rl = or lo hi |
| 517 | if eq rl 0 |
| 518 | break |
| 519 | end |
| 520 | if ne ots_sweep_one pos nru only_chk any_out 0 |
| 521 | rm = 1 |
| 522 | end |
| 523 | pos = add pos rl |
| 524 | end |
| 525 | end |
| 526 | cc = fclose int dfd |
| 527 | if ne rm 0 |
| 528 | if ne ots_fsync_dir 0 |
| 529 | ret OTS_ERR_IO |
| 530 | end |
| 531 | end |
| 532 | ret OT_OK |
| 533 | end |
| 534 | |
| 535 | fn ots_load_journal cap ulong to long |
| 536 | if eq ots_mk_path 0 ots_txt_jrn 0 |
| 537 | ret OTS_ERR_IO |
| 538 | end |
| 539 | fd = fopen ots_pa 0 get ots_plen 0 OTS_O_RDONLY 0 |
| 540 | if lt fd 0 |
| 541 | ret OTS_ERR_IO |
| 542 | end |
| 543 | sb = new uchar 128 0 |
| 544 | if lt fstat int fd sb 0 128 0 |
| 545 | cc = fclose int fd |
| 546 | ret OTS_ERR_IO |
| 547 | end |
| 548 | fl = ulong 0 |
| 549 | for i ulong 8 |
| 550 | fl = or fl shl ulong get sb add 48 i mul i 8 |
| 551 | end |
| 552 | if gt fl OTS_JBUF_MAX |
| 553 | cc = fclose int fd |
| 554 | ret OT_ERR_CAPACITY |
| 555 | end |
| 556 | got = ulong 0 |
| 557 | while lt got fl bound 1000 |
| 558 | r = read int fd ots_jbuf got sub fl got |
| 559 | if lt r 0 |
| 560 | if eq r -4 |
| 561 | continue |
| 562 | end |
| 563 | break |
| 564 | end |
| 565 | if eq r 0 |
| 566 | break |
| 567 | end |
| 568 | got = add got ulong r |
| 569 | end |
| 570 | cc = fclose int fd |
| 571 | if lt got OTS_HDR_BYTES |
| 572 | ret OTS_ERR_TORN |
| 573 | end |
| 574 | ret ots_verify_journal cap got |
| 575 | end |
| 576 | |
| 577 | fn ots_verify_journal cap ulong jlen ulong to long |
| 578 | h24 = new uchar 24 0 |
| 579 | for i ulong 24 |
| 580 | put h24 i get ots_jbuf i |
| 581 | end |
| 582 | cnt_a = new ulong 1 0 |
| 583 | hst = tstore_decode_header h24 cap cnt_a |
| 584 | if eq hst 0 |
| 585 | ret OTS_ERR_TORN |
| 586 | end |
| 587 | sum = new uchar 32 0 |
| 588 | tg_sha_hash h24 24 sum |
| 589 | for i ulong 32 |
| 590 | if ne get sum i get ots_jbuf add 24 i |
| 591 | ret OTS_ERR_TORN |
| 592 | end |
| 593 | end |
| 594 | if eq hst 2 |
| 595 | ret OT_ERR_BAD_ARGS |
| 596 | end |
| 597 | cnt = get cnt_a 0 |
| 598 | if ne jlen add OTS_HDR_BYTES mul cnt OTS_REC_BYTES |
| 599 | ret OTS_ERR_TORN |
| 600 | end |
| 601 | for i ulong cnt bound 512 |
| 602 | rc = ots_replay_one i |
| 603 | if ne rc OT_OK |
| 604 | ret rc |
| 605 | end |
| 606 | end |
| 607 | put ots_meta 5 cnt |
| 608 | put ots_meta 4 jlen |
| 609 | ret OT_OK |
| 610 | end |
| 611 | |
| 612 | fn ots_close to void |
| 613 | if eq get ots_meta 0 0 |
| 614 | ret |
| 615 | end |
| 616 | if gt get ots_meta 2 0 |
| 617 | cc = fclose int sub get ots_meta 2 1 |
| 618 | put ots_meta 2 0 |
| 619 | end |
| 620 | put ots_meta 0 0 |
| 621 | end |
| 622 | |
| 623 | fn ots_open dir view uchar * dlen ulong cap ulong to long |
| 624 | if or eq dlen 0 or ge dlen 450 or eq cap 0 gt cap OTS_MAX_CAP |
| 625 | ret OT_ERR_BAD_ARGS |
| 626 | end |
| 627 | for i ulong 8 |
| 628 | put ots_meta i 0 |
| 629 | end |
| 630 | for i ulong dlen |
| 631 | put ots_dir i get dir i |
| 632 | end |
| 633 | put ots_meta 1 dlen |
| 634 | put ots_meta 3 cap |
| 635 | mr = makedir ots_dir 0 dlen 493 |
| 636 | if and lt mr 0 ne mr -17 |
| 637 | ret OTS_ERR_IO |
| 638 | end |
| 639 | for i ulong cap |
| 640 | put ots_sl_meta mul i 4 0 |
| 641 | put ots_sl_meta add mul i 4 1 1 |
| 642 | end |
| 643 | lfd = fopen ots_dir 0 dlen or OTS_O_RDONLY OTS_O_DIRECTORY 0 |
| 644 | if lt lfd 0 |
| 645 | ret OTS_ERR_IO |
| 646 | end |
| 647 | put ots_meta 0 1 |
| 648 | put ots_meta 2 add ulong lfd 1 |
| 649 | fl = flock int lfd OTS_LOCK_EX_NB |
| 650 | if ne fl 0 |
| 651 | ots_close |
| 652 | if eq fl -11 |
| 653 | ret OTS_ERR_BUSY |
| 654 | end |
| 655 | ret OTS_ERR_IO |
| 656 | end |
| 657 | if eq ots_mk_path 0 ots_txt_jrn 0 |
| 658 | ots_close |
| 659 | ret OTS_ERR_IO |
| 660 | end |
| 661 | rc = OT_OK |
| 662 | any = new ulong 1 0 |
| 663 | if ne faccess ots_pa 0 get ots_plen 0 0 0 |
| 664 | rc = ots_sweep_dir 1 any |
| 665 | if and eq rc OT_OK ne get any 0 0 |
| 666 | rc = OTS_ERR_TORN |
| 667 | end |
| 668 | if eq rc OT_OK |
| 669 | ots_encode_hdr cap 0 |
| 670 | put ots_meta 4 OTS_HDR_BYTES |
| 671 | p1 = ots_mk_path 0 ots_txt_jtmp |
| 672 | p2 = ots_mk_path 1 ots_txt_jrn |
| 673 | rc = ots_commit_pa_pb 1 OTS_HDR_BYTES OTS_CRASH_COUNT |
| 674 | end |
| 675 | else |
| 676 | rc = ots_load_journal cap |
| 677 | if eq rc OT_OK |
| 678 | for i ulong cap |
| 679 | if ne get ots_sl_meta mul i 4 0 |
| 680 | rc = ots_load_payload i |
| 681 | if ne rc OT_OK |
| 682 | break |
| 683 | end |
| 684 | end |
| 685 | end |
| 686 | end |
| 687 | end |
| 688 | if eq rc OT_OK |
| 689 | rc = ots_sweep_dir 0 any |
| 690 | end |
| 691 | if ne rc OT_OK |
| 692 | ots_close |
| 693 | ret rc |
| 694 | end |
| 695 | ret OT_OK |
| 696 | end |
| 697 | |
| 698 | fn ots_valid sl ulong gen ulong to bool |
| 699 | if or eq get ots_meta 0 0 ge sl get ots_meta 3 |
| 700 | ret false |
| 701 | end |
| 702 | sb = mul sl 4 |
| 703 | if eq get ots_sl_meta sb 0 |
| 704 | ret false |
| 705 | end |
| 706 | ret eq get ots_sl_meta add sb 1 gen |
| 707 | end |
| 708 | |
| 709 | fn ots_live_count to ulong |
| 710 | if eq get ots_meta 0 0 |
| 711 | ret 0 |
| 712 | end |
| 713 | n = ulong 0 |
| 714 | cap = get ots_meta 3 |
| 715 | for i ulong cap |
| 716 | if ne get ots_sl_meta mul i 4 0 |
| 717 | n = add n 1 |
| 718 | end |
| 719 | end |
| 720 | ret n |
| 721 | end |
| 722 | |
| 723 | fn ots_find_free_slot to ulong |
| 724 | cap = get ots_meta 3 |
| 725 | for i ulong cap |
| 726 | sb = mul i 4 |
| 727 | if and eq get ots_sl_meta sb 0 ne get ots_sl_meta add sb 1 OT_U64_MAX |
| 728 | ret i |
| 729 | end |
| 730 | end |
| 731 | ret cap |
| 732 | end |
| 733 | |
| 734 | fn ots_put_payload ts_s ulong ts_g ulong vid edit uchar 32 psha edit uchar 32 to long |
| 735 | tb = mul ts_s 10 |
| 736 | dt = get ot_ts_meta add tb 3 |
| 737 | elems = get ot_ts_meta add tb 6 |
| 738 | bytes = mul elems ot_dtype_size dt |
| 739 | ot_gather_slot ts_s 0 |
| 740 | loc = new uchar 16384 0 |
| 741 | for i ulong bytes bound 16385 |
| 742 | put loc i get ot_buf_a i |
| 743 | end |
| 744 | tensor_value_bytes part loc 0 bytes dt 1 |
| 745 | for i ulong bytes bound 16385 |
| 746 | put ots_pbuf i get loc i |
| 747 | end |
| 748 | rc = ot_value_id ts_s ts_g vid |
| 749 | if ne rc OT_OK |
| 750 | ret rc |
| 751 | end |
| 752 | tg_sha_hash part loc 0 bytes bytes psha |
| 753 | nm = new uchar 68 0 |
| 754 | tstore_hex_name vid nm |
| 755 | p1 = ots_mk_path 0 ots_txt_ptmp |
| 756 | p2 = ots_mk_path 1 nm |
| 757 | ret ots_commit_pa_pb 0 bytes OTS_CRASH_PAYLOAD_PARTIAL |
| 758 | end |
| 759 | |
| 760 | fn ots_put ts_s ulong ts_g ulong out_h edit ulong 2 to long |
| 761 | if eq get ots_meta 0 0 |
| 762 | ret OT_ERR_BAD_ARGS |
| 763 | end |
| 764 | rc = ot_ts_check ts_s ts_g |
| 765 | if ne rc OT_OK |
| 766 | ret rc |
| 767 | end |
| 768 | sl = ots_find_free_slot |
| 769 | if eq sl get ots_meta 3 |
| 770 | ret OT_ERR_CAPACITY |
| 771 | end |
| 772 | vid = new uchar 32 0 |
| 773 | psha = new uchar 32 0 |
| 774 | rc = ots_put_payload ts_s ts_g vid psha |
| 775 | if ne rc OT_OK |
| 776 | ret rc |
| 777 | end |
| 778 | ret ots_put_journal sl ts_s vid psha out_h |
| 779 | end |
| 780 | |
| 781 | fn ots_fill_put_meta sl ulong ts_s ulong meta edit ulong 6 sh edit ulong 8 to void |
| 782 | tb = mul ts_s 10 |
| 783 | put meta 0 OTS_REC_PUT |
| 784 | put meta 1 sl |
| 785 | put meta 2 get ots_sl_meta add mul sl 4 1 |
| 786 | put meta 3 get ots_meta 5 |
| 787 | put meta 4 get ot_ts_meta add tb 3 |
| 788 | rk = get ot_ts_meta add tb 4 |
| 789 | put meta 5 rk |
| 790 | for d ulong rk |
| 791 | put sh d get ot_ts_shape add mul ts_s 8 d |
| 792 | end |
| 793 | end |
| 794 | |
| 795 | fn ots_put_journal sl ulong ts_s ulong vid view uchar 32 psha view uchar 32 out_h edit ulong 2 to long |
| 796 | meta = new ulong 6 0 |
| 797 | sh = new ulong 8 0 |
| 798 | ots_fill_put_meta sl ts_s meta sh |
| 799 | seq = get meta 3 |
| 800 | rc = ots_apply_record meta sh vid psha seq |
| 801 | if ne rc OT_OK |
| 802 | ret rc |
| 803 | end |
| 804 | jlen = get ots_meta 4 |
| 805 | if gt add jlen OTS_REC_BYTES OTS_JBUF_MAX |
| 806 | ret OT_ERR_CAPACITY |
| 807 | end |
| 808 | ots_encode_rec_at jlen meta sh vid psha |
| 809 | ots_encode_hdr get ots_meta 3 add seq 1 |
| 810 | p1 = ots_mk_path 0 ots_txt_jtmp |
| 811 | p2 = ots_mk_path 1 ots_txt_jrn |
| 812 | rc = ots_commit_pa_pb 1 add jlen OTS_REC_BYTES OTS_CRASH_JOURNAL_PARTIAL |
| 813 | if ne rc OT_OK |
| 814 | ots_encode_hdr get ots_meta 3 seq |
| 815 | put ots_sl_meta mul sl 4 0 |
| 816 | ret rc |
| 817 | end |
| 818 | put ots_meta 4 add jlen OTS_REC_BYTES |
| 819 | put ots_meta 5 add seq 1 |
| 820 | put out_h 0 sl |
| 821 | put out_h 1 get meta 2 |
| 822 | ret OT_OK |
| 823 | end |
| 824 | |
| 825 | fn ots_copy_pbuf_to_slot slot ulong nb ulong to void |
| 826 | dbase = ot_slot_dbase slot |
| 827 | for i ulong nb bound 16385 |
| 828 | put ot_arena add dbase i get ots_pbuf i |
| 829 | end |
| 830 | end |
| 831 | |
| 832 | fn ots_get sl ulong gen ulong out_h edit ulong 2 to long |
| 833 | if not ots_valid sl gen |
| 834 | ret OT_ERR_STALE |
| 835 | end |
| 836 | rc = ots_load_payload sl |
| 837 | if ne rc OT_OK |
| 838 | ret rc |
| 839 | end |
| 840 | sb = mul sl 4 |
| 841 | dt = get ots_sl_meta add sb 2 |
| 842 | rk = get ots_sl_meta add sb 3 |
| 843 | sh = new ulong 8 0 |
| 844 | for d ulong rk |
| 845 | put sh d get ots_sl_shape add mul sl 8 d |
| 846 | end |
| 847 | rc = ot_new_dense dt rk sh out_h |
| 848 | if ne rc OT_OK |
| 849 | ret rc |
| 850 | end |
| 851 | nb = mul get ot_ts_meta add mul get out_h 0 10 6 ot_dtype_size dt |
| 852 | ots_copy_pbuf_to_slot get out_h 0 nb |
| 853 | ret OT_OK |
| 854 | end |
| 855 | |
| 856 | fn ots_gc_payload vid view uchar 32 to void |
| 857 | nm = new uchar 68 0 |
| 858 | tstore_hex_name vid nm |
| 859 | if eq ots_payload_ref nm 0 |
| 860 | pl = ots_mk_path 0 nm |
| 861 | if gt pl 0 |
| 862 | u = unlink ots_pa 0 pl |
| 863 | end |
| 864 | end |
| 865 | end |
| 866 | |
| 867 | fn ots_release sl ulong gen ulong to long |
| 868 | if not ots_valid sl gen |
| 869 | ret OT_ERR_STALE |
| 870 | end |
| 871 | vid = new uchar 32 0 |
| 872 | for i ulong 32 |
| 873 | put vid i get ots_sl_vid add mul sl 32 i |
| 874 | end |
| 875 | meta = new ulong 6 0 |
| 876 | seq = get ots_meta 5 |
| 877 | put meta 0 OTS_REC_RELEASE |
| 878 | put meta 1 sl |
| 879 | put meta 2 gen |
| 880 | put meta 3 seq |
| 881 | sh = new ulong 8 0 |
| 882 | zero1 = new uchar 32 0 |
| 883 | zero2 = new uchar 32 0 |
| 884 | rc = ots_apply_record meta sh zero1 zero2 seq |
| 885 | if ne rc OT_OK |
| 886 | ret rc |
| 887 | end |
| 888 | jlen = get ots_meta 4 |
| 889 | ots_encode_rec_at jlen meta sh zero1 zero2 |
| 890 | ots_encode_hdr get ots_meta 3 add seq 1 |
| 891 | p1 = ots_mk_path 0 ots_txt_jtmp |
| 892 | p2 = ots_mk_path 1 ots_txt_jrn |
| 893 | rc = ots_commit_pa_pb 1 add jlen OTS_REC_BYTES OTS_CRASH_JOURNAL_PARTIAL |
| 894 | if ne rc OT_OK |
| 895 | ots_encode_hdr get ots_meta 3 seq |
| 896 | put ots_sl_meta mul sl 4 1 |
| 897 | put ots_sl_meta add mul sl 4 1 gen |
| 898 | ret rc |
| 899 | end |
| 900 | put ots_meta 4 add jlen OTS_REC_BYTES |
| 901 | put ots_meta 5 add seq 1 |
| 902 | ots_gc_payload vid |
| 903 | ret OT_OK |
| 904 | end |