def exact.loop
mylsm-lsm-store@0.6.0.1 · src/coordinator/Io.bend:423
import mylsm-lsm-store@0.6.0.1/src/coordinator/Io.bend as Io
import Base
import bend-kit-bytes@0.3.2.0/bytes.bend as Bytes
423def exact.loop(
424 fuel: Nat,
425 state: ExactState
426) -> IO(
427 File & Result<&1, &1, U32 & String, ExactResult>
428):
429 match fuel:
430 case 0n:
431 match state:
432 case Need{file, remaining, _}:
433 match remaining:
434 case 0n: exact.complete(file, Nil{}, True{})
435 case 1n+_: IO.pure(File & Result<&1, &1, U32 & String, ExactResult>,
436 (file, Fail{(7, "socket frame read exhausted its bound")}))
437 case Read{pair, _, _}:
438 match pair:
439 case (file, Fail{error}):
440 IO.pure(File & Result<&1, &1, U32 & String, ExactResult>,
441 (file, Fail{error}))
442 case (file, Done{_}):
443 IO.pure(File & Result<&1, &1, U32 & String, ExactResult>,
444 (file, Fail{(7, "socket frame read exhausted its bound")}))
445 case 1n+rest:
446 match state:
447 case Need{file, remaining, chunks}:
448 match remaining:
449 case 0n: exact.complete(file, chunks, True{})
450 case 1n+rest_remaining:
451 +total = Nat.add(1n, rest_remaining)
452 do IO<File & Result<&1, &1, U32 & String, ExactResult>>:
453 pair : File & Result<&1, &1, U32 & String, Bytes.Bytes> <-
454 read.bytes(file,
455 U32.from_nat(Nat.min(65536n, total)))
456 exact.loop(rest, Read{pair, total, chunks})
457 case Read{pair, remaining, chunks}:
458 match pair:
459 case (file, Fail{error}):
460 IO.pure(File & Result<&1, &1, U32 & String, ExactResult>,
461 (file, Fail{error}))
462 case (file, Done{Bytes.Bytes{0, _}}):
463 exact.complete(file, chunks, False{})
464 case (file, Done{Bytes.Bytes{+length, words}}):
465 exact.loop(rest, Need{file,
466 Nat.sub(remaining, U32.to_nat(length)),
467 Con{Bytes.Bytes{length, words}, chunks}})