PostgresStore

class PostgresStore(dataSource: DataSource, table: String = DEFAULT_TABLE, clock: Clock = Clock.System, createTable: Boolean = true) : Store

A persistent Store that keeps its items in a table of a PostgreSQL database, through the DataSource of the app, so that every instance of a server remembers what an agent learned:

val config = GraphConfig(
threadId = "chat-7",
checkpointer = PostgresCheckpointer(dataSource, KotlinxStateSerializer<AgentState>()),
store = PostgresStore(dataSource).within("users", userId),
)

The app creates the data source, with the JDBC driver of PostgreSQL and the pool it likes, and closes it. The store creates its table when it first needs it, with CREATE TABLE IF NOT EXISTS, unless createTable = false, for an app whose migrations create it. The table has one row for each item:

CREATE TABLE telar_store (
namespace TEXT COLLATE "C" NOT NULL, -- each part followed by '/', with '%' and '/' in a part written as %25 and %2F
key TEXT NOT NULL,
value TEXT NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY (namespace, key)
)

The namespace compares byte by byte (COLLATE "C"), whatever the locale of the database, so the primary key finds the items under a namespace.

Each write is one statement, so a crash leaves the items as they were or with the write done. Two writes of one instance never get the same time, so list keeps their order also with a clock that is not precise or that went back. Writes from several instances are ordered by their clocks. One instance of the class is safe to share between coroutines. JDBC blocks, so every call runs on Dispatchers.IO.

Parameters

dataSource

the database. It has to be PostgreSQL, version 12 or later.

table

the name of the table, letters, digits and _, not starting with a digit, with a schema in front or not.

clock

gives StoreItem.updatedAt, to the microsecond, the precision of PostgreSQL.

createTable

whether the store creates table when it does not exist.

Throws

if table is not a plain name.

Constructors

Link copied to clipboard
constructor(dataSource: DataSource, table: String = DEFAULT_TABLE, clock: Clock = Clock.System, createTable: Boolean = true)

Functions

Link copied to clipboard
open suspend override fun delete(namespace: List<String>, key: String)
Link copied to clipboard
open suspend override fun get(namespace: List<String>, key: String): StoreItem?
Link copied to clipboard
open suspend override fun list(namespace: List<String>, limit: Int): List<StoreItem>
Link copied to clipboard
open suspend override fun put(namespace: List<String>, key: String, value: String)
Link copied to clipboard
open suspend fun search(namespace: List<String>, query: String, limit: Int): List<StoreItem>