~/bend-docscommunity

journal.bend relies on unsafe/foreign

raw source on the hub · import bend-kit-journal@0.3.1.3/journal.bend as Journal

Append-only journal: checksummed frames, fsync'd appends, and crash recovery of the committed prefix. Source: https://github.com/paymog/bend-kit/tree/main/journal

3 imports
import Base
import bend-kit-bytes@0.3.3.1/bytes.bend as Bytes
import bend-kit-hash@0.3.2.2/hash.bend as Hash

Types

type Tail source · line 13 · raw

Data

Clean: the input ends on a frame boundary. Torn: the input ends inside a frame. A crash cut the last append short. Corrupt: a whole header or a whole frame fails its check. Recovery does not go past it.

type Step source · line 19 · raw

@-P:Type -> @-S:Type -> Type

One decoding step. Short, Bad, and End give the input back untouched, at the frame start.

type Recovery source · line 26 · raw

@-a:Quant -> @-P:Kind(a) -> @-S:Type -> Type

The committed records in order, how the input ends, and the input from the first frame not taken.

type Replay source · line 30 · raw

@-A:Type -> @-S:Type -> Type

The state after every committed record, how the input ends, and the input from the first frame not taken.

type Cur source · line 218 · raw

Type

The Bytes instance. A cursor reads b from pos; pos only moves forward, past whole frames.

type Journal source · line 293 · raw

Type

An open journal. max caps a record's length. end is the file's length. ok turns False when an append fails: what reached the disk is then unknown, so appends stop until the journal is reopened.

Definitions

def list.fits source · line 164 · raw

@xs:List<&2, U32> -> @+at:U32 -> @+n:U32 -> Pair(List<&2, U32>, Bool)

def list.field source · line 168 · raw

@xs:List<&2, U32> -> U32

def list.u32 source · line 175 · raw

@xs:List<&2, U32> -> @+at:U32 -> Pair(List<&2, U32>, U32)

def list.cut source · line 179 · raw

@xs:List<&2, U32> -> @+hs:U32 -> @+len:U32 -> Pair(List<&2, U32>, List<&2, U32>)

def list.skip source · line 183 · raw

@xs:List<&2, U32> -> @+hs:U32 -> @+len:U32 -> List<&2, U32>

def list.crc source · line 187 · raw

@xs:List<&2, U32> -> U32

CRC-32 (ISO-HDLC, as zlib and Hash.crc32) of a list of octets.

def list.sum source · line 190 · raw

@xs:List<&2, U32> -> Pair(List<&2, U32>, U32)

def list.next source · line 194 · raw

@+max:U32 -> @xs:List<&2, U32> -> Step<List<&2, U32>, List<&2, U32>>

def list.recover source · line 197 · raw

@n:Nat -> @+max:U32 -> @xs:List<&2, U32> -> Recovery<&2, List<&2, U32>, List<&2, U32>>

def list.head source · line 203 · raw

@+len:U32 -> @+sum:U32 -> List<&2, U32>

def list.frame source · line 206 · raw

@+p:List<&2, U32> -> List<&2, U32>

def list.log source · line 210 · raw

@rs:List<&2, List<&2, U32>> -> List<&2, U32>

The frames of rs, one after another: what appending each record writes.

def cur.fits source · line 221 · raw

@c:Cur -> @+at:U32 -> @+n:U32 -> Pair(Cur, Bool)

def cur.field source · line 225 · raw

@m:Maybe<&2, U32> -> U32

def cur.u32.fin source · line 232 · raw

@+pos:U32 -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Maybe<&2, U32>) -> Pair(Cur, U32)

def cur.u32 source · line 236 · raw

@c:Cur -> @+at:U32 -> Pair(Cur, U32)

def cur.cut.fin source · line 240 · raw

@+pos:U32 -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> Pair(Cur, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes)

def cur.cut source · line 244 · raw

@c:Cur -> @+hs:U32 -> @+len:U32 -> Pair(Cur, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes)

def cur.skip source · line 248 · raw

@c:Cur -> @+hs:U32 -> @+len:U32 -> Cur

def cur.sum source · line 252 · raw

@p:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, U32)

def fuel source · line 256 · raw

@+len:U32 -> Nat

Every frame of b holds at least 12 octets, so len / 12 + 1 steps reach the end.

def frame.of source · line 264 · raw

@+len:U32 -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, U32) -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes

def frame source · line 269 · raw

@p:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes

The frame that appending p writes.

def open.cut source · line 296 · raw

@r:Pair(File, Result<&1, &1, Pair(U32, String), Unit>) -> @+max:U32 -> @+end:U32 -> @-A:Type -> @acc:A -> IO(Result<&1, &1, Pair(U32, String), Pair(Journal, A)>)

def open.tail source · line 306 · raw

@t:Tail -> @file:File -> @+max:U32 -> @+pos:U32 -> @-A:Type -> @acc:A -> IO(Result<&1, &1, Pair(U32, String), Pair(Journal, A)>)

def open.replayed source · line 320 · raw

@file:File -> @+max:U32 -> @-A:Type -> @r:Replay<A, Cur> -> IO(Result<&1, &1, Pair(U32, String), Pair(Journal, A)>)

def append.back source · line 341 · raw

@+max:U32 -> @+end:U32 -> @+len:U32 -> @r:Pair(File, Result<&1, &1, Pair(U32, String), Unit>) -> Pair(Journal, Result<&1, &1, Pair(U32, String), Unit>)

def append.write source · line 348 · raw

@file:File -> @+max:U32 -> @+end:U32 -> @f:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(Journal, Result<&1, &1, Pair(U32, String), Unit>))

def append.refuse source · line 354 · raw

@file:File -> @+max:U32 -> @+end:U32 -> @+ok:Bool -> @e:Pair(U32, String) -> IO(Pair(Journal, Result<&1, &1, Pair(U32, String), Unit>))

def append.size source · line 359 · raw

@over:Bool -> @big:Bool -> @file:File -> @+max:U32 -> @+end:U32 -> @p:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(Journal, Result<&1, &1, Pair(U32, String), Unit>))

The file stays under 4 GiB, so every offset and the next open's read fit a U32.

def append.ok source · line 368 · raw

@ok:Bool -> @file:File -> @+max:U32 -> @+end:U32 -> @p:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(Journal, Result<&1, &1, Pair(U32, String), Unit>))

def append source · line 378 · raw

@j:Journal -> @p:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(Journal, Result<&1, &1, Pair(U32, String), Unit>))

Appends p as one frame and answers once it is fsynced: Done means durable, under the trust assumptions in README.md. After a Fail the record may or may not survive a reopen.

def close source · line 382 · raw

@j:Journal -> IO(Unit)

Templates

template next.verdict source · line 37 · raw

@-P:Type -> @-S:Type -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @ok:Bool -> @+len:U32 -> @s:S -> @p:P -> Step<P, S>

template next.summed source · line 44 · raw

@-P:Type -> @-S:Type -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @+want:U32 -> @+len:U32 -> @s:S -> @r:Pair(P, U32) -> Step<P, S>

template next.cut source · line 48 · raw

@-P:Type -> @-S:Type -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @+want:U32 -> @+len:U32 -> @r:Pair(S, P) -> Step<P, S>

template next.want source · line 52 · raw

@-P:Type -> @-S:Type -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @+len:U32 -> @r:Pair(S, U32) -> Step<P, S>

template next.body source · line 56 · raw

@-P:Type -> @-S:Type -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @+len:U32 -> @r:Pair(S, Bool) -> Step<P, S>

template next.size source · line 64 · raw

@-P:Type -> @-S:Type -> @-fits:(@_:S -> @+at:U32 -> @+n:U32 -> Pair(S, Bool)) -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @over:Bool -> @+len:U32 -> @s:S -> Step<P, S>

template next.head source · line 71 · raw

@-P:Type -> @-S:Type -> @-fits:(@_:S -> @+at:U32 -> @+n:U32 -> Pair(S, Bool)) -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @ok:Bool -> @+max:U32 -> @+len:U32 -> @s:S -> Step<P, S>

template next.lenx source · line 78 · raw

@-P:Type -> @-S:Type -> @-fits:(@_:S -> @+at:U32 -> @+n:U32 -> Pair(S, Bool)) -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @+max:U32 -> @+len:U32 -> @r:Pair(S, U32) -> Step<P, S>

template next.len source · line 82 · raw

@-P:Type -> @-S:Type -> @-fits:(@_:S -> @+at:U32 -> @+n:U32 -> Pair(S, Bool)) -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @+max:U32 -> @r:Pair(S, U32) -> Step<P, S>

template next.start source · line 88 · raw

@-P:Type -> @-S:Type -> @-fits:(@_:S -> @+at:U32 -> @+n:U32 -> Pair(S, Bool)) -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @+max:U32 -> @r:Pair(S, Bool) -> Step<P, S>

The two length fields are checked before the payload is waited for, so a damaged length is Bad and never a Torn tail that hides the frames after it.

template next.any source · line 96 · raw

@-P:Type -> @-S:Type -> @-fits:(@_:S -> @+at:U32 -> @+n:U32 -> Pair(S, Bool)) -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @+max:U32 -> @r:Pair(S, Bool) -> Step<P, S>

template next source · line 105 · raw

@-P:Type -> @-S:Type -> @-fits:(@_:S -> @+at:U32 -> @+n:U32 -> Pair(S, Bool)) -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @+max:U32 -> @s:S -> Step<P, S>

One frame from s. max caps a payload's length.

template recover.con source · line 108 · raw

@-a:Quant -> @-P:Kind(a) -> @-S:Type -> @p:P -> @r:Recovery<a, P, S> -> Recovery<a, P, S>

template recover.go source · line 114 · raw

@-a:Quant -> @-P:Kind(a) -> @-S:Type -> @-fits:(@_:S -> @+at:U32 -> @+n:U32 -> Pair(S, Bool)) -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @n:Nat -> @+max:U32 -> @st:Step<P, S> -> Recovery<a, P, S>

Every frame from st on. Once n frames are taken, the next one is the last: the tail is Corrupt, and rest starts after it. Callers give n at least the frame count, so this never happens.

template recover source · line 134 · raw

@-a:Quant -> @-P:Kind(a) -> @-S:Type -> @-fits:(@_:S -> @+at:U32 -> @+n:U32 -> Pair(S, Bool)) -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @n:Nat -> @+max:U32 -> @s:S -> Recovery<a, P, S>

Every committed record of s, in order.

template replay.go source · line 138 · raw

@-P:Type -> @-S:Type -> @-A:Type -> @-fits:(@_:S -> @+at:U32 -> @+n:U32 -> Pair(S, Bool)) -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @-step:(@_:A -> @_:P -> A) -> @n:Nat -> @+max:U32 -> @st:Step<P, S> -> @acc:A -> Replay<A, S>

step folded over every frame from st on, without a list of the records. n as in recover.go.

template replay source · line 157 · raw

@-P:Type -> @-S:Type -> @-A:Type -> @-fits:(@_:S -> @+at:U32 -> @+n:U32 -> Pair(S, Bool)) -> @-u32:(@_:S -> @+at:U32 -> Pair(S, U32)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, P)) -> @-skip:(@_:S -> @+hs:U32 -> @+len:U32 -> S) -> @-sum:(@_:P -> Pair(P, U32)) -> @-step:(@_:A -> @_:P -> A) -> @n:Nat -> @+max:U32 -> @s:S -> @acc:A -> Replay<A, S>

template list.replay source · line 200 · raw

@-A:Type -> @-step:(@_:A -> @_:List<&2, U32> -> A) -> @n:Nat -> @+max:U32 -> @xs:List<&2, U32> -> @acc:A -> Replay<A, List<&2, U32>>

template replay.bytes source · line 260 · raw

@-A:Type -> @-step:(@_:A -> @_:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> A) -> @+max:U32 -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @acc:A -> Replay<A, Cur>

step folded over every committed record of b, in order. rest.pos is where the committed prefix ends.

template open.read source · line 325 · raw

@-A:Type -> @-step:(@_:A -> @_:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> A) -> @r:Result<&1, &1, Pair(U32, String), Pair(File, Pair(U32, Array<U32>))> -> @+max:U32 -> @acc:A -> IO(Result<&1, &1, Pair(U32, String), Pair(Journal, A)>)

template open source · line 336 · raw

@-A:Type -> @-step:(@_:A -> @_:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> A) -> @path:String -> @+max:U32 -> @limit:U32 -> @acc:A -> IO(Result<&1, &1, Pair(U32, String), Pair(Journal, A)>)

Opens the journal at path and folds step over its committed records, from acc. A torn tail is cut off and fsynced before open answers. A corrupt frame fails with EINVAL and its offset; the file is left as it is. max caps a record's length; limit caps the file's size.

Effects (foreign code)

effect open.words source · line 277 · raw

@path:String -> @limit:U32 -> IO(Result<&1, &1, Pair(U32, String), Pair(File, Pair(U32, Array<U32>))>)

Opens path for reading and appending, creating it, and fsyncs its directory. Answers the file and its whole contents. Fails with EFBIG when the file holds more than limit octets.

foreign: effs/journal.c, effs/journal.js

effect append.words source · line 282 · raw

@file:File -> @len:U32 -> @words:Array<U32> -> IO(Pair(File, Result<&1, &1, Pair(U32, String), Unit>))

Writes len octets of words at the end of file, then fsyncs it.

foreign: effs/journal.c, effs/journal.js

effect truncate source · line 287 · raw

@file:File -> @len:U32 -> IO(Pair(File, Result<&1, &1, Pair(U32, String), Unit>))

Cuts file to len octets, then fsyncs it.

foreign: effs/journal.c, effs/journal.js