commands.bend relies on unsafe/foreign
raw source on the hub · import bend-kit-commands@0.4.0.0/commands.bend as Commands
Retry-safe commands: deduplicate by command id, replay recorded results, and commit keyed rows, record, and outbox in one SQLite transaction. Source: https://github.com/paymog/bend-kit/tree/main/commands
3 imports
import Base import bend-kit-bytes@0.3.3.0/bytes.bend as Bytes import bend-kit-sqlite@0.1.0.1/sqlite.bend as Sqlite
Types
type Outcome source · line 8 · raw
@-R:Type -> Type
Accepted@-R:Type -> @result:R -> Outcome<R>
Refused@-R:Type -> @result:R -> Outcome<R>
type Record source · line 13 · raw
@-P:Type -> @-R:Type -> Type
What the store keeps for one command id: its payload, its outcome, and when it was recorded.
Record@-P:Type -> @-R:Type -> @payload:P -> @outcome:Outcome<R> -> @at:U32 -> Record<P, R>
type Reply source · line 16 · raw
@-R:Type -> Type
Fresh@-R:Type -> @outcome:Outcome<R> -> Reply<R>
Replayed@-R:Type -> @outcome:Outcome<R> -> Reply<R>
Conflict@-R:Type -> Reply<R>
type Decision source · line 22 · raw
@-S:Type -> @-R:Type -> @-E:Type -> Type
The domain's answer to a new command.
Accept@-S:Type -> @-R:Type -> @-E:Type -> @state:S -> @result:R -> @effects:List<&1, E> -> Decision<S, R, E>
Refuse@-S:Type -> @-R:Type -> @-E:Type -> @result:R -> Decision<S, R, E>
type Step source · line 27 · raw
@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> Type
Write: commit the state, the record, and the effects together. Note: commit only the record. Keep: write nothing.
Write@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @state:S -> @record:Record<P, R> -> @reply:Reply<R> -> @effects:List<&1, E> -> Step<S, P, R, E>
Note@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @record:Record<P, R> -> @reply:Reply<R> -> Step<S, P, R, E>
Keep@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @reply:Reply<R> -> Step<S, P, R, E>
type Row source · line 159 · raw
Type
A row of the store's key-value table. Read: Some is the value, None means no row. Written: Some puts the value, None deletes the row.
Row@key:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @value:Maybe<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes> -> Row
type Store source · line 163 · raw
Data
A database and its prepared statements. One Store serves one task at a time.
Store@db:U32 -> @begin:U32 -> @commit:U32 -> @rollback:U32 -> @find:U32 -> @read:U32 -> @save:U32 -> @del:U32 -> @keep:U32 -> @emit:U32 -> Store
type Pending source · line 413 · raw
Type
An undelivered effect: its sequence number, its command id, and the effect.
Pending@seq:U32 -> @id:String -> @effect:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Pending
Definitions
def accepted source · line 32 · raw
@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @rr:Pair(R, R) -> @s2:S -> @p:P -> @at:U32 -> @es:List<&1, E> -> Step<S, P, R, E>
def refused source · line 36 · raw
@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @rr:Pair(R, R) -> @p:P -> @at:U32 -> Step<S, P, R, E>
def replayed source · line 54 · raw
@-R:Type -> @same:Bool -> @o:Outcome<R> -> Reply<R>
def Run source · line 75 · raw
@-A:Type -> Type
def Run.pure source · line 78 · raw
@-A:Type -> @x:A -> Run(A)
def Run.fail source · line 81 · raw
@-A:Type -> @e:Pair(U32, String) -> Run(A)
def Run.next source · line 84 · raw
@-A:Type -> @-B:Type -> @r:Result<&1, &1, Pair(U32, String), A> -> @f:(@_:A -> Run(B)) -> Run(B)
def Run.bind source · line 91 · raw
@-A:Type -> @-B:Type -> @m:Run(A) -> @f:(@_:A -> Run(B)) -> Run(B)
def first source · line 95 · raw
@-A:Type -> @r:Result<&1, &1, Pair(U32, String), A> -> @z:Result<&1, &1, Pair(U32, String), Unit> -> Result<&1, &1, Pair(U32, String), A>
r's value, unless r or then z failed.
def unit source · line 104 · raw
@-A:Type -> @r:Result<&1, &1, Pair(U32, String), A> -> Result<&1, &1, Pair(U32, String), Unit>
def exec source · line 114 · raw
@+stmt:U32 -> Run(Unit)
Steps a statement and ignores any row, then resets it, also after a failed step.
def once.run source · line 120 · raw
@-A:Type -> @+s:U32 -> @body:(@_:U32 -> Run(A)) -> Run(A)
def once source · line 127 · raw
@-A:Type -> @+db:U32 -> @text:String -> @body:(@_:U32 -> Run(A)) -> Run(A)
Prepares one statement, runs body on it, and finalizes it, also after body fails.
def sql source · line 132 · raw
@+db:U32 -> @text:String -> Run(Unit)
def blob.of source · line 135 · raw
@lw:Pair(U32, Array<U32>) -> 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes
def column source · line 139 · raw
@+s:U32 -> @i:U32 -> Run(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes)
def bind source · line 144 · raw
@+s:U32 -> @i:U32 -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Unit)
def same.of source · line 148 · raw
@r:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, Bool)) -> Bool
def same source · line 153 · raw
@a:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Bool
The payload comparison that handle gives step: every byte, NUL included.
def open source · line 168 · raw
@path:String -> @wait:U32 -> Run(Store)
Opens or creates the tables at path. wait is how many ms a write waits for another connection's transaction before it fails with SQLITE_BUSY (5).
def close source · line 187 · raw
@st:Store -> Run(Unit)
def outcome source · line 201 · raw
@accepted:Bool -> @r:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Outcome<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>
def seen.row source · line 208 · raw
@row:Bool -> @+find:U32 -> Run(Maybe<&1, Record<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>>)
def seen.get source · line 221 · raw
@+find:U32 -> @id:String -> Run(Maybe<&1, Record<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>>)
The record for id, if the store holds one.
def row.of source · line 229 · raw
@row:Bool -> @+read:U32 -> Run(Maybe<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)
def row.get source · line 239 · raw
@+read:U32 -> @k:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Maybe<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)
The value at key k, if a row holds it.
def row.at source · line 247 · raw
@+read:U32 -> @kk:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes) -> Run(Row)
def rows.get source · line 254 · raw
@ks:List<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes> -> @+read:U32 -> Run(List<&1, Row>)
One row for each key, in order.
def seen.put.row source · line 264 · raw
@+keep:U32 -> @id:String -> @p:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @kind:U32 -> @r:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @at:U32 -> Run(Unit)
def seen.put.of source · line 273 · raw
@o:Outcome<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes> -> @+keep:U32 -> @id:String -> @p:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @at:U32 -> Run(Unit)
def seen.put source · line 280 · raw
@rec:Record<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes> -> @+keep:U32 -> @id:String -> Run(Unit)
def outbox.put source · line 284 · raw
@es:List<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes> -> @+emit:U32 -> @+id:String -> Run(Unit)
def row.put source · line 295 · raw
@v:Maybe<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes> -> @+save:U32 -> @+del:U32 -> @k:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Unit)
def rows.put source · line 308 · raw
@rs:List<&1, Row> -> @+save:U32 -> @+del:U32 -> Run(Unit)
Writes the rows in order, so a later row for a key wins.
def written source · line 318 · raw
@w:Step<List<&1, Row>, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes> -> @+commit:U32 -> @+save:U32 -> @+del:U32 -> @+keep:U32 -> @+emit:U32 -> @+id:String -> Run(Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)
Applies a step inside the open transaction, then commits it.
def handle.end source · line 374 · raw
@r:Result<&1, &1, Pair(U32, String), Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>> -> @st:Store -> Run(Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)
On failure, resets the read statements and rolls back. Rollback errors are ignored: the transaction may not have started.
def get source · line 398 · raw
@st:Store -> @k:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Maybe<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)
The committed value at key k.
def prune.run source · line 402 · raw
@+s:U32 -> @before:U32 -> Run(Unit)
def prune source · line 408 · raw
@st:Store -> @before:U32 -> Run(Unit)
Forgets every record made before the time before. A later retry of a forgotten id runs as a new command.
def outbox.row source · line 416 · raw
@row:Bool -> @+s:U32 -> Run(Maybe<&1, Pending>)
def outbox.first source · line 427 · raw
@+s:U32 -> Run(Maybe<&1, Pending>)
def outbox.next source · line 433 · raw
@st:Store -> Run(Maybe<&1, Pending>)
The oldest undelivered effect.
def outbox.ack.run source · line 437 · raw
@+s:U32 -> @seq:U32 -> Run(Unit)
def outbox.ack source · line 443 · raw
@st:Store -> @seq:U32 -> Run(Unit)
Removes a delivered effect. Deliver before ack: a crash between them delivers the effect again.
Templates
template decided source · line 40 · raw
@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @-dup_r:(@_:R -> Pair(R, R)) -> @d:Decision<S, R, E> -> @p:P -> @at:U32 -> Step<S, P, R, E>
template fresh source · line 47 · raw
@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @-dup_r:(@_:R -> Pair(R, R)) -> @-decide:(@_:S -> @_:P -> Decision<S, R, E>) -> @pp:Pair(P, P) -> @s:S -> @at:U32 -> Step<S, P, R, E>
template step source · line 63 · raw
@-S:Type -> @-P:Type -> @-R:Type -> @-E:Type -> @-eq:(@_:P -> @_:P -> Bool) -> @-dup_p:(@_:P -> Pair(P, P)) -> @-dup_r:(@_:R -> Pair(R, R)) -> @-decide:(@_:S -> @_:P -> Decision<S, R, E>) -> @found:Maybe<&1, Record<P, R>> -> @s:S -> @p:P -> @at:U32 -> Step<S, P, R, E>
One command, given the record that the store holds for its id and the domain state s. dup_p and dup_r copy a payload and a result: a new command's payload goes to decide and to the record.
template handle.new source · line 339 · raw
@-keys:(@_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> List<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @-decide:(@_:List<&1, Row> -> @_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Decision<List<&1, Row>, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @pp:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes) -> @+read:U32 -> @at:U32 -> @+commit:U32 -> @+save:U32 -> @+del:U32 -> @+keep:U32 -> @+emit:U32 -> @+id:String -> Run(Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)
template handle.found source · line 350 · raw
@-keys:(@_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> List<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @-decide:(@_:List<&1, Row> -> @_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Decision<List<&1, Row>, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @found:Maybe<&1, Record<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>> -> @+read:U32 -> @payload:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @at:U32 -> @+commit:U32 -> @+save:U32 -> @+del:U32 -> @+keep:U32 -> @+emit:U32 -> @+id:String -> Run(Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)
A duplicate reads no rows: step ignores them when found is Some.
template handle.txn source · line 362 · raw
@-keys:(@_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> List<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @-decide:(@_:List<&1, Row> -> @_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Decision<List<&1, Row>, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @st:Store -> @+id:String -> @payload:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @at:U32 -> Run(Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)
template handle source · line 388 · raw
@-keys:(@_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> List<&1, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @-decide:(@_:List<&1, Row> -> @_:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Decision<List<&1, Row>, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> @+st:Store -> @id:String -> @payload:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> @at:U32 -> Run(Reply<0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>)
Runs one command in one transaction: find its record, read the rows that keys names, decide, and commit rows, record, and effects. at is the record's time for prune, in seconds. decide sees the committed rows, one for each key in order, and the payload.