def read_at.loop

mylsm-lsm-store@0.6.0.1 · src/Fs.bend:282

import mylsm-lsm-store@0.6.0.1/src/Fs.bend as Fs
import Base
import bend-kit-bytes@0.3.2.0/bytes.bend as StorageBytes

282# Assembles a positional read from bounded chunks without moving the file cursor.
283def read_at.loop(
284  fuel: Nat,
285  state: ReadAtState
286) -> IO(
287  File & Result<&1, &1, U32 & String, StorageBytes.Bytes>
288):
289  match fuel:
290    case 0n: read_at.exhausted(state)
291    case 1n+rest:
292      match state:
293        case ReadAtNeed{file, +offset, +remaining, chunks}:
294          match remaining:
295            case 0n: read_at.finish(file, chunks)
296            case 1n+_:
297              do IO<File & Result<&1, &1, U32 & String, StorageBytes.Bytes>>:
298                pair : File & Result<&1, &1, U32 & String, List<&2, U32>> <-
299                  File.read_at(file, U32.from_nat(offset),
300                    U32.from_nat(Nat.min(8192n, remaining)))
301                read_at.loop(rest,
302                  ReadAtRead{pair, offset, remaining, chunks})
303        case ReadAtRead{pair, offset, +remaining, chunks}:
304          match pair:
305            case (file, Fail{error}):
306              IO.pure(File & Result<&1, &1, U32 & String, StorageBytes.Bytes>,
307                (file, Fail{error}))
308            case (file, Done{Nil{}}): read_at.finish(file, chunks)
309            case (file, Done{values}):
310              read_at.loop(rest, ReadAtPacked{file, offset, remaining,
311                chunks, read_at.pack(8192, values)})
312        case ReadAtPacked{file, offset, +remaining, chunks, buffer}:
313          match buffer:
314            case StorageBytes.Bytes{+len, buf}:
315              read_at.loop(rest, ReadAtNeed{file,
316                Nat.add(offset, U32.to_nat(len)),
317                Nat.sub(remaining, U32.to_nat(len)),
318                Con{StorageBytes.Bytes{len, buf}, chunks}})

Uses (22)

Used by (1)