SqlDelightCheckpointer

class SqlDelightCheckpointer<State>(driver: SqlDriver, serializer: StateSerializer<State>, table: String = DEFAULT_TABLE, maxHistory: Int = Int.MAX_VALUE) : Checkpointer<State>

A persistent Checkpointer that saves the checkpoints of each thread in a table of a SQLite database, through a SqlDriver of SQLDelight. One module for every platform an app runs on:

PlatformDriver, from SQLDelight
AndroidAndroidSqliteDriver of android-driver
iOS, macOS, Linux, WindowsNativeSqliteDriver of native-driver
Desktop and servers (JVM)JdbcSqliteDriver of sqlite-driver
Browser (JS and Wasm)WebWorkerDriver of web-worker-driver, with SQLite in a web worker. The worker of SQLDelight keeps the database in memory, so it is gone when the page is reloaded
// Android, with a database file that only holds checkpoints.
val driver = AndroidSqliteDriver(SqlDelightCheckpointer.Schema, context, "runs.db")
val config = GraphConfig(
threadId = "order-42",
checkpointer = SqlDelightCheckpointer(driver, KotlinxStateSerializer<Order>()),
interruptBefore = setOf("pay"),
)

The app creates the driver, and closes it. The checkpointer creates its table when it first needs it, with CREATE TABLE IF NOT EXISTS, so the database may also be the one the app keeps its own data in, with the schema of the app. Schema is a schema for a database that only holds checkpoints, for the drivers that ask for one. The table has one row for each step of a thread:

CREATE TABLE telar_checkpoints (
thread_id TEXT NOT NULL,
step INTEGER NOT NULL,
checkpoint TEXT NOT NULL, -- the checkpoint in the format of CheckpointCodec
PRIMARY KEY (thread_id, step)
)

A save is one transaction: it replaces the checkpoints of the thread with the same step or a later one, adds the new one, and removes the oldest beyond maxHistory. A crash in the middle leaves the checkpoints as they were. SQLite keeps two processes that write the same database from breaking it. One instance is safe to share between coroutines; do not run the same thread in two processes at the same time.

The calls of a driver that blocks, such as those of Android, run on Dispatchers.IO, so the checkpointer can be used from the main thread. An asynchronous driver, such as the one of the browser, is awaited.

Parameters

driver

the database. Its dialect has to be SQLite, which every driver above is.

serializer

converts the graph state to and from a string.

table

the name of the table, letters, digits and _, not starting with a digit.

maxHistory

how many checkpoints a thread keeps, counted from the latest, for CompiledGraph.history and fork. The default keeps all of them; 1 keeps only the latest.

Throws

if table is not a plain name or maxHistory is not positive.

Constructors

Link copied to clipboard
constructor(driver: SqlDriver, serializer: StateSerializer<State>, table: String = DEFAULT_TABLE, maxHistory: Int = Int.MAX_VALUE)

Types

Link copied to clipboard
object Schema : SqlSchema<QueryResult.Value<Unit>>

The schema of a database that only holds checkpoints, in the table of the default name, for a driver that asks for a schema:

Functions

Link copied to clipboard
open suspend override fun delete(threadId: String)
Link copied to clipboard
open suspend override fun history(threadId: String): List<Checkpoint<State>>
Link copied to clipboard
open suspend override fun load(threadId: String): Checkpoint<State>?
Link copied to clipboard
open suspend override fun save(threadId: String, checkpoint: Checkpoint<State>)