def manifest.read.loop
mylsm-lsm-store@0.6.0.1 · src/legacy/ManifestIo.bend:151
import mylsm-lsm-store@0.6.0.1/src/legacy/ManifestIo.bend as ManifestIo
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
151def manifest.read.loop(fuel: Nat, state: ManifestReadState) -> IO(File & Result<&1, &1, U32 & String, Bytes.Bytes>):
152 match fuel:
153 case 0n:
154 match state:
155 case ManifestNeed{file, +remaining, chunks}:
156 match remaining:
157 case 0n:
158 IO.pure(File & Result<&1, &1, U32 & String, Bytes.Bytes>, (file, Done{Bytes.concat(List.reverse(&1, Bytes.Bytes, chunks))}))
159 case 1n+_:
160 IO.pure(File & Result<&1, &1, U32 & String, Bytes.Bytes>, (file, Fail{(U32.from_nat(7n), "bounded manifest read exhausted")}))
161 case ManifestGot{pair, _, _}:
162 match pair:
163 case (file, Fail{error}):
164 IO.pure(File & Result<&1, &1, U32 & String, Bytes.Bytes>, (file, Fail{error}))
165 case (file, Done{_}):
166 IO.pure(File & Result<&1, &1, U32 & String, Bytes.Bytes>, (file, Fail{(U32.from_nat(7n), "bounded manifest read exhausted")}))
167 case 1n+rest:
168 match state:
169 case ManifestNeed{file, +remaining, chunks}:
170 match remaining:
171 case 0n:
172 IO.pure(File & Result<&1, &1, U32 & String, Bytes.Bytes>, (file, Done{Bytes.concat(List.reverse(&1, Bytes.Bytes, chunks))}))
173 case 1n+_:
174 do IO<File & Result<&1, &1, U32 & String, Bytes.Bytes>>:
175 pair : File & Result<&1, &1, U32 & String, Bytes.Bytes> <- Fs.read_bytes(file, U32.from_nat(remaining))
176 manifest.read.loop(rest, ManifestGot{pair, remaining, chunks})
177 case ManifestGot{pair, +remaining, chunks}:
178 match pair:
179 case (file, Fail{error}):
180 IO.pure(File & Result<&1, &1, U32 & String, Bytes.Bytes>, (file, Fail{error}))
181 case (file, Done{Bytes.Bytes{0, _}}):
182 IO.pure(File & Result<&1, &1, U32 & String, Bytes.Bytes>, (file, Fail{(U32.from_nat(8n), "truncated manifest file")}))
183 case (file, Done{Bytes.Bytes{+len, buf}}):
184 manifest.read.loop(rest, ManifestNeed{file, Nat.sub(remaining, U32.to_nat(len)), Con{Bytes.Bytes{len, buf}, chunks}})