def replay.loop

mylsm-lsm-store@0.6.0.1 · src/transfer/LegacyWal.bend:333

import mylsm-lsm-store@0.6.0.1/src/transfer/LegacyWal.bend as LegacyWal
import Base
import mylsm-lsm-store@0.6.0.1/src/Wal.bend as Wal
import bend-kit-bytes@0.3.2.0/bytes.bend as Bytes

333def replay.loop(fuel: Nat, state: ReplayState) -> ReplayResult:
334  match fuel:
335    case 0n: ReplayInvalid{}
336    case 1n+rest:
337      match state:
338        case ReplayNeed{bytes, offset, batches}:
339          replay.loop(rest, replay.measure(Bytes.length(bytes), offset, batches))
340        case ReplayMeasured{bytes, offset, batches, size}:
341          +total = U32.to_nat(size)
342          replay.loop(rest, replay.bound.state(bytes, offset, batches, total))
343        case ReplayBound{bytes, offset, batches, total, at_end, has_length}:
344              match at_end:
345                case True{}:
346                  ReplayValid{batches}
347                case False{}:
348                  match has_length:
349                    case True{}:
350                      replay.loop(rest,
351                        replay.length.state(bytes, offset, total, batches))
352                    case False{}: ReplayInvalid{}
353        case ReplayLength{bytes, next_offset, total, batches, length}:
354          match length:
355            case None{}: ReplayInvalid{}
356            case Some{frame_length}:
357              replay.loop(rest, replay.frame.state(bytes, next_offset,
358                total, batches, frame_length))
359        case ReplayFrame{bytes, next_offset, frame_end, batches, length, fits}:
360              match fits:
361                case True{}:
362                  replay.loop(rest, replay.frame.decoded(Bytes.slice(bytes,
363                    U32.from_nat(next_offset), length), frame_end, batches))
364                case False{}: ReplayInvalid{}
365        case ReplayDecoded{bytes, next_offset, batches, result}:
366          match result:
367            case Fail{_}: ReplayInvalid{}
368            case Done{batch}:
369              replay.loop(rest, ReplayNeed{bytes, next_offset,
370                List.append(&2, Wal.Batch, batches, Con{batch, Nil{}})})

Uses (18)

Used by (1)