Go · Quickstart

Quickstart

import "github.com/unum-cloud/ustore" 1.0.0-rc.1 · 084197c4

Ask your coding agent about this page, or add the docs server to it

curl -s 'https://www.unum.cloud/docs/ustore/go/quickstart.md' | claude -p "How would this fit my code?"

The program opens a database in memory, writes a blob, a document and an edge in one transaction, and reads them back. Collections are created by name and modality before a transaction writes into them, and every read lands in an arena.

package main

import (
	"errors"
	"fmt"
	"log"

	ustore "github.com/unum-cloud/ustore/golang"
)

func main() {
	database, err := ustore.DatabaseInitRam(nil)
	if err != nil {
		log.Fatal(err)
	}
	defer database.Close()
	avatar, titanic := ustore.KeyFromUint64(1), ustore.KeyFromUint64(2)
	posters, err := database.CollectionCreate("posters", ustore.ModalityBlobs)
	if err != nil {
		log.Fatal(err)
	}
	movies, err := database.CollectionCreate("movies", ustore.ModalityDocs)
	if err != nil {
		log.Fatal(err)
	}
	similar, err := database.CollectionCreate("similar", ustore.ModalityGraph)
	if err != nil {
		log.Fatal(err)
	}

	// One transaction writes a blob, a document and an edge, and commits all three or none.
	transaction, err := database.TransactionInit()
	if err != nil {
		log.Fatal(err)
	}
	defer transaction.Close()
	err = errors.Join(
		transaction.BlobsWrite(posters, []ustore.Key{avatar}, ustore.TapeOf("\x89PNG"), nil),
		transaction.DocsWrite(movies, []ustore.Key{avatar}, ustore.TapeOf(`{"title":"Avatar","year":2009}`), nil),
		transaction.GraphWriteEdges(similar, []ustore.Edge{{Source: avatar, Target: titanic, ID: ustore.EdgeIDDefault}}, nil),
	)
	if err != nil {
		log.Fatal(err)
	}
	if _, err := transaction.Commit(ustore.DurabilityBuffered); err != nil {
		log.Fatal(err)
	}

	// Reads at the head see the commit: the poster, the year and the one edge leaving Avatar.
	arena, err := ustore.NewArena(nil)
	if err != nil {
		log.Fatal(err)
	}
	defer arena.Close()
	poster, err := database.BlobsRead(posters, []ustore.Key{avatar}, arena, nil)
	if err != nil {
		log.Fatal(err)
	}
	value, _ := poster.Get(0)
	fmt.Printf("%q ", value)
	movie, err := database.DocsRead(movies, []ustore.Key{avatar}, arena, &ustore.DocsReadConfig{Fields: ustore.TapeOf("year")})
	if err != nil {
		log.Fatal(err)
	}
	year, _ := movie.Get(0)
	fmt.Printf("%s ", year)
	edges, err := database.GraphReadEdges(similar, []ustore.Key{avatar}, ustore.VertexRoleSource, arena)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(edges.Edges(0)[0].Target == titanic)
}

golang/example_test.go compiles and runs this program on every go test.

API

Types

DataDirectoryDataDirectory is a directory a database spreads its files over, with the bytes they may take in it.
db.go:839 ↗
type DataDirectory struct {
    Path string
    MaxBytes uint64
}

Fields

PathstringPath is the directory, which must exist.
MaxBytesuint64MaxBytes caps the bytes the files may take there, zero being unlimited.
DatabaseInitRamConfigDatabaseInitRamConfig carries the optional arguments of DatabaseInitRam and DatabaseOpenRam.
db.go:777 ↗
type DatabaseInitRamConfig struct {
    Executor *Executor
    Isolation Isolation
    CPUClassCeiling CPUClass
    MemoryLimit uint64
    CommitConflictsLimit uint64
}

Fields

Executor*ExecutorExecutor runs the open, nil the calling goroutine.
IsolationIsolationIsolation is how transactions see each other's writes.
CPUClassCeilingCPUClassCPUClassCeiling is the newest class the database's kernels may run as; zero sets no ceiling.
MemoryLimituint64MemoryLimit caps the bytes of keys and values the database holds, past which a write answers StatusCapacityExhausted; zero takes 16 GiB.
CommitConflictsLimituint64CommitConflictsLimit is the conflicts a commit of the database's own absorbs before it answers StatusWriteConflict; zero derives 12 per logical core.
DatabaseOpenLeveldbConfigDatabaseOpenLeveldbConfig carries the optional arguments of DatabaseOpenLeveldb.
db.go:923 ↗
type DatabaseOpenLeveldbConfig struct {
    Executor *Executor
    MemoryLimit uint64
    BackgroundThreads uint64
}

Fields

Executor*ExecutorExecutor runs the open, nil the calling goroutine.
MemoryLimituint64MemoryLimit caps the host memory the engine caches and buffers in, a quarter for the block cache and a quarter for the write buffer; zero takes 1 GiB.
BackgroundThreadsuint64BackgroundThreads is zero or one, as LevelDB compacts on one thread, and more answers StatusUnsupported.
DatabaseOpenNvmeConfigDatabaseOpenNvmeConfig carries the optional arguments of DatabaseOpenNvme, where every zero size or count keeps the engine's default.
db.go:957 ↗
type DatabaseOpenNvmeConfig struct {
    Executor *Executor
    DataDirectories []DataDirectory
    Compactions *Executor
    MemoryLimit uint64
    TransactionMaxBytes uint64
    TransactionMaxDeltas uint64
    CollectionConcurrencyLimit uint64
    CollectionCacheLimit uint64
    CollectionMemoryRunsMaxBytes uint64
    CollectionMemoryRunsMaxDeltas uint64
    CollectionLevelEnlargeFactor uint64
    CollectionValueSize uint64
    CollectionValueMaxSize uint64
    CPUClassCeiling CPUClass
    CommitConflictsLimit uint64
}

Fields

Executor*ExecutorExecutor runs the open, nil the calling goroutine; an NVMe executor moves the bytes.
DataDirectories[]DataDirectoryDataDirectories spread the volumes beyond the directory.
Compactions*ExecutorCompactions is the executor merges run on.
MemoryLimituint64MemoryLimit caps the bytes the engine keeps in memory.
TransactionMaxBytesuint64TransactionMaxBytes caps the bytes one transaction buffers.
TransactionMaxDeltasuint64TransactionMaxDeltas caps the changes one transaction buffers.
CollectionConcurrencyLimituint64CollectionConcurrencyLimit caps the calls one collection serves at once.
CollectionCacheLimituint64CollectionCacheLimit caps the bytes one collection caches.
CollectionMemoryRunsMaxBytesuint64CollectionMemoryRunsMaxBytes caps the bytes of one collection's memory runs.
CollectionMemoryRunsMaxDeltasuint64CollectionMemoryRunsMaxDeltas caps the changes of one collection's memory runs.
CollectionLevelEnlargeFactoruint64CollectionLevelEnlargeFactor is how much larger each level grows than the one above it.
CollectionValueSizeuint64CollectionValueSize is the value size the collections are tuned for.
CollectionValueMaxSizeuint64CollectionValueMaxSize caps the size of one value.
CPUClassCeilingCPUClassCPUClassCeiling is the newest class the database's kernels may run as; zero sets no ceiling.
CommitConflictsLimituint64CommitConflictsLimit is the conflicts a commit of the database's own absorbs before it answers StatusWriteConflict; zero derives 12 per logical core.
DatabaseOpenRocksdbConfigDatabaseOpenRocksdbConfig carries the optional arguments of DatabaseOpenRocksdb and DatabaseViewRocksdb.
db.go:860 ↗
type DatabaseOpenRocksdbConfig struct {
    Executor *Executor
    DataDirectories []DataDirectory
    MemoryLimit uint64
    BackgroundThreads uint64
}

Fields

Executor*ExecutorExecutor runs the open, nil the calling goroutine.
DataDirectories[]DataDirectoryDataDirectories spread the files beyond the directory.
MemoryLimituint64MemoryLimit caps the host memory the engine caches and buffers in, a quarter for one block cache and a quarter for one write buffer manager; zero takes 1 GiB.
BackgroundThreadsuint64BackgroundThreads sizes the pool the database owns for its flushes and compactions; zero takes 2.
DatabaseViewNvmeConfigDatabaseViewNvmeConfig carries the optional arguments of DatabaseViewNvme, where every zero size keeps the engine's default.
db.go:1032 ↗
type DatabaseViewNvmeConfig struct {
    Executor *Executor
    DataDirectories []DataDirectory
    MemoryLimit uint64
    CollectionConcurrencyLimit uint64
    CollectionCacheLimit uint64
    CPUClassCeiling CPUClass
}

Fields

Executor*ExecutorExecutor runs the open, nil the calling goroutine; an NVMe executor moves the bytes.
DataDirectories[]DataDirectoryDataDirectories are the volumes beyond the directory, as the database was opened with.
MemoryLimituint64MemoryLimit caps the bytes the engine keeps in memory.
CollectionConcurrencyLimituint64CollectionConcurrencyLimit caps the calls one collection serves at once.
CollectionCacheLimituint64CollectionCacheLimit caps the bytes one collection caches.
CPUClassCeilingCPUClassCPUClassCeiling is the newest class the database's kernels may run as; zero sets no ceiling.
NvmeOptionsNvmeOptions is how the threads of an NVMe executor move bytes between the drives and memory, where every zero size or count keeps the engine's default.
db.go:707 ↗
type NvmeOptions struct {
    Transport Transport
    QueueDepth uint64
    RequestBufferBytes uint64
    RequestBuffersCount uint64
    StreamBufferBytes uint64
    StreamBuffersCount uint64
    Paths []string
}

Fields

TransportTransportTransport is how bytes move between the files and memory.
QueueDepthuint64QueueDepth counts the submissions a channel holds at once.
RequestBufferBytesuint64RequestBufferBytes sizes each request buffer of a channel.
RequestBuffersCountuint64RequestBuffersCount counts the request buffers of each channel.
StreamBufferBytesuint64StreamBufferBytes sizes each streaming buffer of a channel.
StreamBuffersCountuint64StreamBuffersCount counts the streaming buffers of each channel.
Paths[]stringPaths are the data directories, raw media or SPDK namespaces of the databases the executor will open, read only during the call: the memory domain they share places the threads when no memory domain is named, and their fewest hardware queues give the default thread count.
StructDatabaseDeferredOnDatabaseDeferredOn is the receiver whose verbs submit their calls to an executor and answer a Deferred, offering the verbs of DatabaseOn with the same arguments.21
db.go:677 ↗
type DatabaseDeferredOn struct {
    // contains filtered or unexported fields
}

The library reads a deferred call's inputs until the call is final, so the slices a verb is handed must not change until then; the caller keeps its arena until then, as for a blocking verb.

*× 4Clear submits DatabaseOn.Clear.
db.go:1141 ↗
func (on DatabaseDeferredOn) Clear(
    mode DropMode,
) (*Deferred[struct{}], error)
Symbol
Clear
Compact
Describe
Flush
CollectionCreateCollectionCreate submits DatabaseOn.CollectionCreate.
db.go:1386 ↗
func (on DatabaseDeferredOn) CollectionCreate(
    name string,
    modality Modality,
) (*Deferred[CollectionID], error)
CollectionDropCollectionDrop submits DatabaseOn.CollectionDrop.
db.go:1420 ↗
func (on DatabaseDeferredOn) CollectionDrop(
    collection CollectionID,
    mode DropMode,
) (*Deferred[struct{}], error)
CompactPlanFinishCompactPlanFinish submits DatabaseOn.CompactPlanFinish.
db.go:1228 ↗
func (on DatabaseDeferredOn) CompactPlanFinish(
    plan *CompactPlan,
) (*Deferred[struct{}], error)
BlobsRemoveBlobsRemove submits DatabaseOn.BlobsRemove.
db.go:209 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) BlobsRemove(
    collections Collections,
    keys []Key,
    config *RemoveConfig,
) (*Deferred[struct{}], error)
BlobsWriteBlobsWrite submits DatabaseOn.BlobsWrite.
db.go:169 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) BlobsWrite(
    collections Collections,
    keys []Key,
    values Tape,
    config *WriteConfig,
) (*Deferred[struct{}], error)
DocsRemoveDocsRemove submits DatabaseOn.DocsRemove.
db.go:194 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) DocsRemove(
    collections Collections,
    keys []Key,
    config *RemoveConfig,
) (*Deferred[struct{}], error)
DocsWriteDocsWrite submits DatabaseOn.DocsWrite.
db.go:149 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) DocsWrite(
    collections Collections,
    keys []Key,
    values Tape,
    config *DocsWriteConfig,
) (*Deferred[struct{}], error)
GraphRemoveEdgesGraphRemoveEdges submits DatabaseOn.GraphRemoveEdges.
db.go:261 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) GraphRemoveEdges(
    collections Collections,
    edges []Edge,
    config *WriteConfig,
) (*Deferred[struct{}], error)
GraphRemoveVerticesGraphRemoveVertices submits DatabaseOn.GraphRemoveVertices.
db.go:358 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) GraphRemoveVertices(
    collections Collections,
    vertices []Key,
    config *GraphRemoveVerticesConfig,
) (*Deferred[struct{}], error)
GraphWriteEdgesGraphWriteEdges submits DatabaseOn.GraphWriteEdges.
db.go:219 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) GraphWriteEdges(
    collections Collections,
    edges []Edge,
    config *WriteConfig,
) (*Deferred[struct{}], error)
GraphWriteVerticesGraphWriteVertices submits DatabaseOn.GraphWriteVertices.
db.go:307 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) GraphWriteVertices(
    collections Collections,
    vertices []Key,
    config *WriteConfig,
) (*Deferred[struct{}], error)
PagesImportPagesImport submits DatabaseOn.PagesImport.
db.go:91 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) PagesImport(
    collections Collections,
    keys []Key,
    values Tape,
    config *PagesImportConfig,
) (*Deferred[struct{}], error)
PagesInsertPagesInsert submits DatabaseOn.PagesInsert.
db.go:167 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) PagesInsert(
    collections Collections,
    keys []Key,
    values Tape,
    arena *Arena,
    config *PagesInsertConfig,
) (*Deferred[Inserted], error)
PagesMovePagesMove submits DatabaseOn.PagesMove.
db.go:280 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) PagesMove(
    collections Collections,
    keys, elements []Key,
    config *PagesMoveConfig,
) (*Deferred[struct{}], error)
PagesRemovePagesRemove submits DatabaseOn.PagesRemove.
db.go:227 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) PagesRemove(
    collections Collections,
    keys []Key,
    config *PagesRemoveConfig,
) (*Deferred[struct{}], error)
PagesRemoveAttributesPagesRemoveAttributes submits DatabaseOn.PagesRemoveAttributes.
db.go:408 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) PagesRemoveAttributes(
    collections Collections,
    keys, elements []Key,
    fields Tape,
    config *WriteConfig,
) (*Deferred[struct{}], error)
PagesSetAttributesPagesSetAttributes submits DatabaseOn.PagesSetAttributes.
db.go:366 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) PagesSetAttributes(
    collections Collections,
    keys, elements []Key,
    fields, values Tape,
    config *WriteConfig,
) (*Deferred[struct{}], error)
PagesSplicePagesSplice submits DatabaseOn.PagesSplice.
db.go:323 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) PagesSplice(
    collections Collections,
    keys, elements []Key,
    textStarts, textLengths []uint64,
    values Tape,
    config *WriteConfig,
) (*Deferred[struct{}], error)
StringsRemoveStringsRemove submits DatabaseOn.StringsRemove.
db.go:91 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) StringsRemove(
    collections Collections,
    strings Tape,
    config *StringsRemoveConfig,
) (*Deferred[struct{}], error)
StringsWriteStringsWrite submits DatabaseOn.StringsWrite.
db.go:40 ↗

Inherited from deferredWrites[Handle]

func (on DatabaseDeferredOn) StringsWrite(
    collections Collections,
    strings Tape,
    values []Key,
    config *WriteConfig,
) (*Deferred[struct{}], error)
EnumEngineEngine is the engine serving a database.2
db.go:366 ↗
type Engine int32

Values

EngineRamEngineRam keeps everything in memory, imaged into a directory on demand.
EngineRocksDBEngineRocksDB serves a RocksDB directory.
EngineLevelDBEngineLevelDB serves a LevelDB directory.
EngineNvmeEngineNvme is the in-tree NVMe engine.
EngineFromNameEngineFromName reads an engine back from the spelling Engine.String renders.
db.go:385 ↗
func EngineFromName(name string) (Engine, error)
StringString renders the engine as the library spells it.
db.go:380 ↗
func (engine Engine) String() string