def write.step
mylsm-lsm-store@0.6.0.1 · src/SstStreamIo.bend:498
import mylsm-lsm-store@0.6.0.1/src/SstStreamIo.bend as SstStreamIo
import Base
import mylsm-lsm-store@0.6.0.1/src/Fs.bend as Fs
import mylsm-lsm-store@0.6.0.1/src/SstFile.bend as SstFile
498def write.step(
499 fuel: Nat,
500 +path: String,
501 state: WriteState
502) -> IO(Result<&1, &1, U32 & String, Unit>):
503 match fuel:
504 case 0n:
505 match state:
506 case WriteNext{file, _}:
507 do IO<Result<&1, &1, U32 & String, Unit>>:
508 _closed : Unit <- File.close(file)
509 return Fail{(U32.from_nat(7n), "table exceeds packed write limit")}
510 case WriteOutput{file, _}:
511 do IO<Result<&1, &1, U32 & String, Unit>>:
512 _closed : Unit <- File.close(file)
513 return Fail{(U32.from_nat(7n), "table exceeds packed write limit")}
514 case WriteResult{pair, _}:
515 match pair:
516 case (file, _):
517 do IO<Result<&1, &1, U32 & String, Unit>>:
518 _closed : Unit <- File.close(file)
519 return Fail{(U32.from_nat(7n), "table exceeds packed write limit")}
520 case 1n+rest:
521 match state:
522 case WriteNext{file, encoder}:
523 write.step(rest, path,
524 WriteOutput{file, SstFile.next_chunk(encoder)})
525 case WriteOutput{file, pair}:
526 match pair:
527 case (_, None{}):
528 write.finish(file, path)
529 case (next, Some{bytes}):
530 do IO<Result<&1, &1, U32 & String, Unit>>:
531 written : File & Result<&1, &1, U32 & String, Unit> <-
532 Fs.write_bytes(file, bytes)
533 write.step(rest, path, WriteResult{written, next})
534 case WriteResult{pair, encoder}:
535 match pair:
536 case (file, Fail{error}):
537 do IO<Result<&1, &1, U32 & String, Unit>>:
538 _closed : Unit <- File.close(file)
539 return Fail{error}
540 case (file, Done{Unit{}}):
541 write.step(rest, path, WriteNext{file, encoder})