curl -s 'https://www.unum.cloud/docs/ustore/go/batches-and-arenas.md' | claude -p "How would this fit my code?"
Every output whose length scales with the call lands in an Arena, and the verb answers a view over it. NewArena(executor) places one where that executor computes, and NewArena(nil) on host pages, and a call rewinds it, keeping its high-water mark, so a steady loop allocates once.
A view read after a later call took its arena fails as Contended.
A slice or segment an accessor returned is valid only until the arena’s next call.
A result kept past the next call needs the next call to take another arena.
An input lying inside the arena a call rewinds is refused with StatusInvalidArgument.
A Key is sixteen bytes ordered as their bytes, and KeyFromUint64 spells an integer big-endian, so integer keys order as integers.
The receiver a verb is called on chooses what it sees, where it runs and how it completes, while the verb and its arguments stay.
A Database, a Transaction, a Snapshot and a cursor run their verbs on the calling goroutine, On(executor) blocks on an executor, and Deferred(executor), which cursors lack, answers a *Deferred once the call is submitted, or the refusal the binding or the library made before anything ran.
A deferred call reads its slices and writes its arena until it is final, so neither may change until Wait returns. Wait blocks in ustore_outcome_wait, Status polls through ustore_outcome_status, and a Deferred dropped without either is released by the collector once its call is final.
An Executor is the threads that run calls, shared by any number of databases and receivers, serving one call at a time, so a call submitted while another holds it answers StatusContended. ExecutorForRam spawns threads near the NUMA nodes it names, every node for an empty list, where zero threads take seven eighths of their cores, and a nonzero scratch limit caps each thread’s scratch. ExecutorForNvme spawns the NVMe engine’s rings near the memory domains it names or the media under its Paths, and ExecutorForVram drives the GPUs of one vendor, their positions in GPUDevices or every one for none, one stream each, which it creates and destroys or adopts from the caller and destroys.
Opening a database blocks, on the executor its config names or the calling goroutine, and closing an executor waits for the call holding it.
An arena never outlives the executor that made it, so defer executor.Close() comes before defer arena.Close(), and an arena from NewArena(nil) lives until it is closed.
The converters of c/ustore/arrow.h view an Arrow column as a call’s input in place, and export what a call wrote into an arena as a column in place, and until the column is released the arena’s next call and Close answer StatusContended:
scanned, err := database.BlobsScan(people, 100, found, nil)var schema ustore.ArrowSchemavar array ustore.ArrowDeviceArrayif err := ustore.KeysViewToArrow(scanned.Keys(0), found, &schema, &array); err != nil { return err}defer schema.Release()defer array.Release()keys, err := ustore.KeysViewFromArrow(&schema, array.Array()) // the same keys, in placecolumns, err := database.DocsGather(movies, keys, ustore.TapeOf("title"), []ustore.DocsFieldType{ustore.DocsFieldTypeString}, fetched)var titles ustore.ArrowSchemavar column ustore.ArrowDeviceArrayerr = ustore.DocsColumnViewToArrow(columns.Column(0), ustore.DocsFieldTypeString, fetched, &titles, &column)defer titles.Release()defer column.Release()tape, err := ustore.StringsViewFromArrow(&titles, column.Array()) // the same strings, as a Tapeerr = database.BlobsWrite(people, keys, tape, nil)
KeysViewFromArrow and StringsViewFromArrow read keys and strings, and KeysViewToArrow, StringsViewToArrow and DocsColumnViewToArrow write keys, strings and gathered columns.
API
Types
PhaseProgressPhaseProgress is where a plan stands: its phase, that phase's position in the plan, and its own count, laid out as ustore_phase_progress_t.
Ordinal is this phase's position among the ones the plan runs, from zero.
Completed
uint64
Completed counts the units of this phase done.
Total
uint64
Total counts the units of this phase in all.
ProgressCallbackProgressCallback is called with the units a long call finished so far and the units it finishes if it runs to the end; false stops it, which then fails with StatusCancelled and publishes nothing.
type Arena struct {
// contains filtered or unexported fields
}
Every call given an arena rewinds and refills it, so the views of its previous call end there,
as they do once it is closed: their accessors panic with StatusContended, as a slice bound
does, while a slice already handed out is plain memory the next call overwrites, so it is valid
only until then. To keep one call's views past the next, give that call another arena. An arena
serves one call at a time, and a second overlapping it fails with StatusContended. Deferred
verbs view it as blocking ones do, since Close waits for every deferred call naming it.
NewArenaNewArena makes an empty arena where executor computes, host pages or the memory of its GPU, decided once; a nil executor makes host pages for calls on the calling goroutine.
Nothing is allocated until a call writes into it. An arena must not outlive the executor that made it, so Arena.Close comes before Executor.Close; one made with a nil executor lives until it is closed.
CloseClose releases the memory, waiting for every deferred call naming the arena, and ends every view over it; it comes before the Close of the executor that made the arena.
While an Arrow export of its outputs is unreleased it fails with StatusContended and frees nothing, so a later Close retries. It is idempotent, and a closed arena must not be used again.
DescribeDescribe reports where the arena's outputs live, as the executor that made it decided.
StructBlobsCursorOnBlobsCursorOn is the receiver whose pulls block until their call is final: on the calling goroutine when embedded in a BlobsCursor, and on an executor when BlobsCursorOn.On makes it.
StructChangesCursorChangesCursor is a feed of the changes committed to one collection after a stamp, oldest first, pinning that stamp as a snapshot does until ChangesCursor.Close.3
StructChangesCursorOnChangesCursorOn is the receiver whose pulls block until their call is final: on the calling goroutine when embedded in a ChangesCursor, or on an executor that ChangesCursorOn.On picks.
DatabaseViewNvmeDatabaseViewNvme serves the database a manifest names through the in-tree NVMe engine, read-only, writing nothing to its data directories and refusing every change with StatusReadOnly.
It never flushes: commits made with DurabilityBuffered and not yet flushed are not on disk, so call DatabaseOn.Flush first. It is idempotent, and a closed database must not be used again.
Blobs*BlobsRemove× 4BlobsRemove removes keys with their values, or every key of a range per task given end keys.
ChangesCursor*ChangesCursorInit× 2ChangesCursorInit opens a feed of collection's changes committed after since, reading nothing yet and blocking whatever receiver it is called on; every pull names its own executor through On.
Collection*CollectionCreate× 2CollectionCreate creates a collection under name holding modality; a name starting with an underscore is refused, as the engines keep their own collections there.
CompactPlan*CompactPlanFinish× 2CompactPlanFinish frees a plan whatever the call answers, so another compaction may open on the database, leaving the plan fresh.
GraphRemove*GraphRemoveVertices× 4GraphRemoveVertices removes vertices with every edge they connect to, or every vertex of a range per task given end vertices.
Ingest*IngestInit× 2IngestInit opens an ingest into collection, whose rows arrive in order, blocking on the receiver's executor, whose memory domain holds the ingest.
Pages*PagesInsert× 4PagesInsert parses one fragment per task into elements of the page keys names, reporting the key every element ended up with into arena.
Pages*PagesSplice× 4PagesSplice replaces textLengths bytes of the text of every element from textStarts with values, where a zero length only inserts and an empty value only removes.
StructDatabaseOnDatabaseOn is the receiver whose verbs block until their call is final, reading and writing the head: on the calling goroutine when embedded in a Database, and on an executor when DatabaseOn.On makes it.
type DatabaseOn struct {
// contains filtered or unexported fields
}
StructDeferredDeferred is a submitted call, which holds its outcome and inputs until it is final, owned by the one goroutine that polls or waits on it.3
type Deferred[Result any] struct {
// contains filtered or unexported fields
}
Status polls without blocking, Wait blocks until the call is final and hands back its result, and Close waits the same way and drops it. A Deferred dropped without Wait or Close is released by the collector once the call is final, never blocking on it: until then every later collection checks it again.
CloseClose blocks until the call is final, releases its pins, and reports the call's failure.
StructDocsCursorDocsCursor walks the documents of one collection in batches of rows, every pull resuming past the last key the pull before it handed out.1
StructDocsCursorOnDocsCursorOn is the receiver whose pulls block until their call is final: on the calling goroutine when embedded in a DocsCursor, and on an executor when DocsCursorOn.On makes it.
type Executor struct {
// contains filtered or unexported fields
}
A nil *Executor is the null executor: it owns nothing, and every call handed it runs on the
calling goroutine. An executor serves one call at a time, while databases stay freely concurrent;
a second call finding it held finishes StatusContended, and nothing queues.
ExecutorForNvmeExecutorForNvme spawns threads for the NVMe engine, each submitting and polling its own I/O ring, placed with their staging on memoryDomains, the first placing the threads, or on the domain nvme.Paths share for an empty list.
Zero threads takes the fewest hardware queues of the disks under the paths, capped by the cores, and a zero scratchLimit takes 16 GiB per thread.
ExecutorForRamExecutorForRam spawns host threads for the RAM, RocksDB and LevelDB engines, interleaved across the compute domains local to memoryDomains, every domain the process may allocate on for an empty list.
Zero threads takes seven eighths of the cores those domains carry, and a cpuClassCeiling the CPU lacks narrows the pick rather than refusing it. Every call's staging carves from each thread's scratch, capped at scratchLimit, where zero takes 16 GiB.
ExecutorForVramExecutorForVram opens one non-blocking stream on every named device, its position in the listing of GPUDevices, all of one vendor, created with the executor and destroyed when it closes, all of them driven by the one goroutine that calls the verbs, so that a deferred call on it is final once it returns.
Empty devices take every listed device of the first vendor with a known class. Arenas live on the first device, where every call computes but a filtered scan, which deals its pages across every device.
Streams are empty, or one per device in devices order, a cudaStream_t, hipStream_t or
id<MTLCommandQueue> on that device, which the executor adopts: on success a CUDA or HIP stream
becomes the executor's, which synchronizes and destroys it when it closes, while a Metal queue
is retained, the executor releasing its own reference and the caller keeping theirs; on failure
every stream stays the caller's. A nil or default stream, a device named twice beside streams,
or streams beside empty devices answer StatusInvalidArgument, and a stream on another device
than its entry names answers StatusConfigMismatch. Zero scratchLimit lets each device's
scratch grow, gds is how reads and compactions move bytes between the drives and the devices,
and a gpuClassCeiling some named device loads nothing under answers StatusUnsupported.
CloseClose waits out the call holding the executor, then releases its threads, rings or streams; every arena it made must be closed first, or it fails with StatusContended and frees nothing, so a later Close retries.
StructGraphVerticesCursorGraphVerticesCursor walks the vertices of one graph collection page by page, every pull resuming past the last vertex the pull before it handed out.1
StructGraphVerticesCursorOnGraphVerticesCursorOn is the receiver whose pulls block until their call is final: on the calling goroutine when embedded in a GraphVerticesCursor, or on an executor when GraphVerticesCursorOn.On makes it.
StructIngestIngest lands every row appended to it into one collection as one commit of its own, joining no transaction; its calls run one at a time, on the calling goroutine, or on an executor through IngestOn.On.4
StructIngestOnIngestOn is the receiver whose appends and finish block until their call is final: on the calling goroutine when embedded in an Ingest, and on an executor when IngestOn.On makes it.
StructPagesCursorOnPagesCursorOn is the receiver whose pulls block until their call is final: on the calling goroutine when embedded in a PagesCursor, and on an executor when PagesCursorOn.On makes it.
type PagesCursorOn struct {
// contains filtered or unexported fields
}
StructSnapshotSnapshot pins the state it was taken at until Snapshot.Close, and its own verbs read that state on the calling goroutine; nothing writes through it.32
BlobsCursor*BlobsCursorInit× 2BlobsCursorInit opens a walk over collection from startKey, reading nothing until its first pull and blocking whatever receiver it is called on; every pull names its own executor through On.
Blobs*BlobsMeasure× 2BlobsMeasure estimates the entries and the space inside a range per task into arena, one task per collection collections names, without walking them.
DocsCursor*DocsCursorInit× 2DocsCursorInit opens a walk over collection from startKey, reading nothing until its first pull and blocking whatever receiver it is called on.
Docs*DocsGather× 2DocsGather reads, type-checks and converts one column per field of the documents keys name into arena, converting field i into outputsTypes[i].
GraphMeasure*GraphMeasureVertices× 2GraphMeasureVertices estimates the vertices and the space inside a range per task into arena, one task per collection collections names.
GraphSample*GraphSampleVertices× 2GraphSampleVertices draws up to wanted vertices uniformly at random into arena, one task per collection collections names.
GraphVerticesCursor*GraphVerticesCursorInit× 2GraphVerticesCursorInit opens a walk over the vertices of collection from startKey, reading nothing until its first pull and blocking whatever receiver it is called on.
PagesCursor*PagesCursorInit× 2PagesCursorInit opens a walk over collection from startKey, reading nothing until its first pull and blocking whatever receiver it is called on.
*Save× 2Save writes the snapshot out as a new database of the same engine at path, holding exactly the collections and names it records; a cancelled save removes what it wrote.
StringsCursor*StringsCursorInit× 2StringsCursorInit opens a walk over collection, copying its prefix and bounds, reading nothing until its first pull and blocking whatever receiver it is called on.
Strings*StringsMeasure× 2StringsMeasure estimates the entries and the space inside a range of strings per task into arena, one task per collection collections names.
StructSnapshotDeferredOnSnapshotDeferredOn is the receiver whose verbs submit their calls to an executor, offering the verbs of SnapshotOn with the same arguments.25
StructSnapshotOnSnapshotOn is the receiver whose verbs block until their call is final, reading the state a snapshot pinned; nothing writes through it.
type SnapshotOn struct {
// contains filtered or unexported fields
}
StructStringsCursorStringsCursor walks the strings of one collection page by page, in the order of their text, every pull resuming past the last string the pull before it handed out.2
StructStringsCursorOnStringsCursorOn is the receiver whose pulls block until their call is final: on the calling goroutine when embedded in a StringsCursor, or on an executor that StringsCursorOn.On picks.
type Transaction struct {
TransactionOn
// contains filtered or unexported fields
}
Its own verbs run on the calling goroutine and watch the keys they touch, while Transaction.View picks another watch. It serves one call at a time and holds its database open until Transaction.Close.
CloseClose releases the transaction once every deferred call naming it is final, dropping whatever it did not commit.
StructTransactionOnTransactionOn is the receiver whose verbs block until their call is final, reading and writing inside a transaction under one Watch.