FileStore

class FileStore(directory: Path, fileSystem: FileSystem = SystemFileSystem, clock: Clock = Clock.System) : Store

A persistent Store that keeps its items in files inside directory: a folder for each part of a namespace, and in it one file of JSON with the items of that namespace. The items of listOf("users", "ana") are in users/ana/items.json.

val store = FileStore(Path(appDirectory, "memories"))
graph.invoke(input, GraphConfig(threadId = "plan-7", store = store.within("users", userId)))

Works on every target that has a file system, as FileCheckpointer does: JVM/Android, Apple, Linux and Windows native, and JS/Wasm on Node.js. It is not available in browsers.

A write is atomic: the file of its namespace is written to a temporary file and then moved into place, so a crash in the middle of a write leaves the items as they were. A write writes the whole file of its namespace again, which is quick for the few hundred items a user has. One instance is safe to share between coroutines; do not point several instances or processes at the same directory.

The names of folders are made from the parts of the namespace, with every character that is not a lowercase letter, a digit, _ or - written as %XX, so that two parts never get the same folder, also on a file system that ignores case.

Parameters

directory

where the items are kept. Created if it does not exist.

fileSystem

the file system to use; replaceable in tests.

clock

gives StoreItem.updatedAt. Two writes of one store never get the same time, so list keeps their order also with a clock that is not precise.

Constructors

Link copied to clipboard
constructor(directory: Path, fileSystem: FileSystem = SystemFileSystem, clock: Clock = Clock.System)

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>