journal.bend relies on unsafe/foreign
raw source on the hub · import bend-kit-journal@0.4.0.0/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 ./pure.bend as Pure
Types
type Journal source · line 26 · 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.
Journal@file:File -> @max:U32 -> @end:U32 -> @ok:Bool -> Journal
Definitions
def open.cut source · line 29 · 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 39 · raw
@t:0x12e0542fb95e35c73c6acd30d8999431/pure.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 53 · raw
@file:File -> @+max:U32 -> @-A:Type -> @r:0x12e0542fb95e35c73c6acd30d8999431/pure.Replay<A, 0x12e0542fb95e35c73c6acd30d8999431/pure.Cur> -> IO(Result<&1, &1, Pair(U32, String), Pair(Journal, A)>)
def append.back source · line 74 · 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 81 · 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 87 · 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 92 · 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 101 · 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 111 · 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 115 · raw
@j:Journal -> IO(Unit)
Templates
template open.read source · line 58 · 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 69 · 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 10 · 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 15 · 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 20 · 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