def exact.loop

mylsm-lsm-store@0.6.0.1 · src/SstStreamIo.bend:51

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 bend-kit-bytes@0.3.2.0/bytes.bend as Bytes

51def exact.loop(
52  fuel: Nat,
53  state: ExactState
54) -> IO(File & Result<&1, &1, U32 & String, ExactResult>):
55  match fuel:
56    case 0n:
57      match state:
58        case ExactNeed{file, remaining, chunks}:
59          match remaining:
60            case 0n:
61              exact.complete(file, chunks, True{})
62            case 1n+_:
63              IO.pure(File & Result<&1, &1, U32 & String, ExactResult>,
64                (file, Fail{(U32.from_nat(7n), "bounded read exhausted")}))
65        case ExactRead{pair, _, _}:
66          match pair:
67            case (file, Fail{error}):
68              IO.pure(File & Result<&1, &1, U32 & String, ExactResult>, (file, Fail{error}))
69            case (file, Done{_}):
70              IO.pure(File & Result<&1, &1, U32 & String, ExactResult>,
71                (file, Fail{(U32.from_nat(7n), "bounded read exhausted")}))
72    case 1n+rest:
73      match state:
74        case ExactNeed{file, +remaining, chunks}:
75          match remaining:
76            case 0n:
77              exact.complete(file, chunks, True{})
78            case 1n+_:
79              do IO<File & Result<&1, &1, U32 & String, ExactResult>>:
80                next : File & Result<&1, &1, U32 & String, Bytes.Bytes> <-
81                  Fs.read_bytes(file, U32.from_nat(Nat.min(1048576n, remaining)))
82                exact.loop(rest, ExactRead{next, remaining, chunks})
83        case ExactRead{pair, remaining, chunks}:
84          match pair:
85            case (file, Fail{error}):
86              IO.pure(File & Result<&1, &1, U32 & String, ExactResult>, (file, Fail{error}))
87            case (file, Done{Bytes.Bytes{0, _}}):
88              exact.complete(file, chunks, False{})
89            case (file, Done{Bytes.Bytes{+len, buf}}):
90              exact.loop(rest, ExactNeed{file, (remaining - U32.to_nat(len) : Nat),
91                Con{Bytes.Bytes{len, buf}, chunks}})

Uses (23)

Used by (1)