def loop

mylsm-lsm-store@0.6.0.1 · src/transfer/BackupCursorIo.bend:218

import mylsm-lsm-store@0.6.0.1/src/transfer/BackupCursorIo.bend as BackupCursorIo
import Base
import mylsm-lsm-store@0.6.0.1/src/Fs.bend as Fs
import mylsm-lsm-store@0.6.0.1/src/transfer/BackupCursor.bend as BackupCursor
import mylsm-lsm-store@0.6.0.1/src/transfer/BackupIo.bend as BackupIo

218# Pulls at most one visible cursor result and writes at most one encoded chunk.
219def loop(
220  fuel: Nat,
221  +destination: String,
222  +temporary: String,
223  state: State
224) -> IO(Result<&1, &1, BackupIo.Error, Unit>):
225  match fuel:
226    case 0n: loop.abort(temporary, state)
227    case 1n+rest:
228      match state:
229        case Need{file, stream}:
230          do IO<Result<&1, &1, BackupIo.Error, Unit>>:
231            step : BackupCursor.Step <- source.step(stream)
232            loop(rest, destination, temporary, Output{file, step})
233        case Output{file, step}:
234          match step:
235            case BackupCursor.Failure{cursor, error}:
236              close.error(file, temporary, cursor, error)
237            case BackupCursor.Continue{stream}:
238              loop(rest, destination, temporary, Need{file, stream})
239            case BackupCursor.Chunk{stream, bytes}:
240              do IO<Result<&1, &1, BackupIo.Error, Unit>>:
241                written : File & Result<&1, &1, U32 & String, Unit> <- Fs.write_bytes(file, bytes)
242                loop(rest, destination, temporary, Written{written, stream})
243            case BackupCursor.End{}:
244              close.file(destination, temporary, file)
245        case Written{pair, stream}:
246          match pair:
247            case (file, Fail{error}):
248              close.stream(file, temporary, stream, error)
249            case (file, Done{Unit{}}):
250              loop(rest, destination, temporary, Need{file, stream})

Uses (19)

Used by (1)