DocumentStore

A durable, ordered store of JSON documents keyed by byte string.

A commit is appended to a checksummed write-ahead log and then applied to an in-memory sorted table; when that table reaches StoreOptions.memtableMaxBytes it is sealed and a new log begins, and a background pass writes it out as an immutable sorted segment. Segments are merged downwards through levels as they accumulate. A read consults the memtables newest first, then level 0's segments newest first, then one segment per level below that — stopping at the first version it finds, including a tombstone, which is an answer rather than a reason to keep looking.

The guarantee. After any interruption, reopening the store yields exactly the acknowledged prefix of the commits: every commit whose write returned is present, no commit that had not returned is present, and nothing in between is missing. Under Durability.SYNC that holds across power loss; under Durability.BUFFERED it holds across process death and sync is what extends it to the machine.

Concurrency: one writer, many readers. Writes are serialised on an internal lock — the engine assumes a single writing thread, and the lock is there so that a mistaken second one gets contention rather than a corrupt log. Reads take no lock and may run on any number of threads while a write is in progress.

A batch is atomic to a reader as well as to recovery: the sequence a read is bounded by is published only once every operation in the batch is in the memtable, so one view of the store never shows part of a batch. The unit is the view, not the call — a run of separate get calls is a run of separate reads, each at whatever sequence the store had reached. Take a Snapshot to read several keys as one.

DocumentStore.open(Path.of("data")).use { store ->
store.put(Key.of("user:1"), Variant.fromJson("""{"name":"ada"}"""))
store.get(Key.of("user:1"))?.select("$.name")?.stringValue() // "ada"

store.snapshot().use { snapshot ->
store.scan(from = Key.of("user:"), snapshot = snapshot).use { cursor ->
while (cursor.next()) println(cursor.key)
}
}
}

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

The directory this store owns.

Link copied to clipboard

The segments the live version names, right now.

Link copied to clipboard

The options it was opened with.

Link copied to clipboard

Sequence number of the last committed operation; 0 for a store nothing has been written to.

Link copied to clipboard

A snapshot of the store's current sizes.

Functions

Link copied to clipboard
fun backfill(observer: SegmentObserver)

Feeds every document of every live segment through observer.

Link copied to clipboard
open override fun close()

Forces and closes the log, then releases the directory.

Link copied to clipboard
fun compact()

Flushes, then compacts until no level is over its budget. Returns when the tree is in shape.

Link copied to clipboard
fun delete(key: Key)

Commits a deletion of key. Deleting an absent key is legal and writes a tombstone.

Link copied to clipboard
fun flush()

Seals the active memtable and writes every sealed one out as a segment.

Link copied to clipboard
fun get(key: Key): Variant?

The current version of key, or null if it is absent or deleted.

fun get(key: Key, snapshot: Snapshot): Variant?

The version of key that snapshot sees, or null if it was absent or deleted then.

Link copied to clipboard
fun put(key: Key, document: Variant)

Commits document under key, replacing any current version.

Link copied to clipboard
fun rotate()

Seals the active memtable and starts a new log.

Link copied to clipboard
fun scan(from: Key? = null, to: Key? = null, snapshot: Snapshot? = null): DocumentCursor

An ordered walk over the documents in [from, to], both bounds inclusive and both optional.

Link copied to clipboard
fun scanSegments(segmentNumbers: Set<Long>, snapshot: Snapshot, includeUnflushed: Boolean = true, from: Key? = null, to: Key? = null): DocumentCursor

An ordered walk over the documents in [from, to] held by the named segments only.

Link copied to clipboard

A fixed view of the store as it is now.

Link copied to clipboard
fun sync()

Forces every commit so far to stable storage.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun write(batch: WriteBatch)

Commits batch as one record. An empty batch does nothing.