store

package
v1.43.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package store implements per-(org, namespace) SQLite shards with optional consensus replication.

Layout on disk:

<rootDir>/<orgID>/<namespace>.db   — one SQLite file per namespace
<rootDir>/_/<namespace>.db         — orgID == "" (embedded/dev)

Schema (single table; key/value blob):

CREATE TABLE kv(
  key   TEXT PRIMARY KEY,
  value BLOB NOT NULL,
  upd   INTEGER NOT NULL
);

Replication:

A Replicator may be attached via WithReplicator. Every put/del is
wrapped in a replication.Frame and Propose'd before the local apply
commits. On Accept the frame is also dispatched to peers via the
driver-internal Subscribe path.

Index

Constants

View Source
const SentinelOrg = "_"

SentinelOrg is the on-disk directory name for the empty org slot. We avoid using "" so the path stays well-formed.

Variables

View Source
var CrossNamespaceKinds = map[string]bool{
	"ns": true,
	"nx": true,
}

CrossNamespaceKinds lists the kinds whose List(prefix) operations must enumerate every shard under the org. Anything else routes to a single shard.

View Source
var ErrClosed = errors.New("store: manager closed")

ErrClosed is returned when a method runs after Close.

View Source
var IdleEvictAfter = 10 * time.Minute

IdleEvictAfter sets how long an open shard may sit unused before the manager closes it. Mutable for tests.

Functions

func CopyFile

func CopyFile(dst, src string) (int64, error)

CopyFile is a helper used by the migration tool. Returns bytes copied.

func IsCrossNamespacePrefix

func IsCrossNamespacePrefix(prefix string) bool

IsCrossNamespacePrefix reports whether prefix is a bare kind/ scan that must fan out across shards.

func NsFromKey

func NsFromKey(key string) (kind, ns, rest string, ok bool)

nsFromKey parses the canonical key layout to derive the namespace segment. Returns ("", false) for keys that are themselves the namespace registry (`ns/<name>`); the caller treats those specially.

Layout reference:

ns/<name>
wf/<ns>/<workflowId>/<runId>
wfh/<ns>/<workflowId>/<runId>/<eventId>
sc/<ns>/<scheduleId>
bt/<ns>/<batchId>
dp/<ns>/<deploymentName>
nx/<ns>/<endpointName>
id/<ns>/<email>
sa/<ns>/<attrName>
idem/<ns>/<workflowId>/<requestId>

func SplitPrefix

func SplitPrefix(prefix string) (kind, ns, suffix string)

SplitPrefix returns (kind, ns, suffix) for a list prefix. ns may be empty when IsCrossNamespacePrefix(prefix) is true.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager owns the on-disk shard layout and the open-shard cache.

func New

func New(rootDir string) (*Manager, error)

New opens the manager rooted at rootDir. The directory is created on demand; errors are returned only for unrecoverable IO problems.

func (*Manager) Close

func (m *Manager) Close() error

Close flushes and closes every open shard. Safe to call twice.

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, org, ns string) (*Shard, error)

Get returns the open shard for (org, ns), creating it on disk if needed. The returned Shard is safe for concurrent use.

func (*Manager) ListShards

func (m *Manager) ListShards(ctx context.Context, org string) ([]*Shard, error)

ListShards enumerates every namespace shard under org. Used by cross-namespace operations like ListNamespaces().

func (*Manager) OpenShardCount

func (m *Manager) OpenShardCount() int

OpenShardCount reports the number of resident shards (for /v1/tasks/cluster).

func (*Manager) Replicator

func (m *Manager) Replicator() replication.Replicator

Replicator returns the currently-installed driver, or nil.

func (*Manager) RootDir

func (m *Manager) RootDir() string

RootDir returns the on-disk root.

func (*Manager) ShardPath

func (m *Manager) ShardPath(org, ns string) string

ShardPath returns the on-disk file for (org, ns).

func (*Manager) WithReplicator

func (m *Manager) WithReplicator(r replication.Replicator)

WithReplicator installs r as the consensus driver for every shard opened from now on, and re-installs it on already-open shards.

type Shard

type Shard struct {
	// contains filtered or unexported fields
}

Shard owns one SQLite file. WAL mode + foreign keys + 5s busy timeout. One writer; readers share the cache. Replicator hooks fire from put/del before local commit so the cluster sees the mutation first.

func (*Shard) Checkpoint

func (s *Shard) Checkpoint() error

Checkpoint forces a WAL truncation without releasing the connection. Used by the migration tool to make the on-disk file fully self-contained before copy.

func (*Shard) Close

func (s *Shard) Close() error

Close flushes WAL and releases the connection. Idempotent.

func (*Shard) Del

func (s *Shard) Del(ctx context.Context, key string) error

Del removes key. No-op if missing.

func (*Shard) Get

func (s *Shard) Get(ctx context.Context, key string) ([]byte, bool, error)

Get reads key.

func (*Shard) List

func (s *Shard) List(ctx context.Context, prefix string, fn func(key string, value []byte) error) error

List walks every kv row whose key starts with prefix in lexicographic order.

func (*Shard) Namespace

func (s *Shard) Namespace() string

Namespace returns the shard's namespace.

func (*Shard) Org

func (s *Shard) Org() string

Org returns the shard's org id ("" for sentinel).

func (*Shard) Path

func (s *Shard) Path() string

Path returns the underlying file path.

func (*Shard) Put

func (s *Shard) Put(ctx context.Context, key string, value []byte) error

Put writes value at key. If a Replicator is installed it runs Propose first; on Accept the local commit happens. On Reject the transaction is dropped and an error is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL