statestore

package
v0.2026336.2016 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package statestore is the ONE store for a deployment's realized state (DESIGN.md D-DEP-5): "resolved ports and images, bindings, leases, reverse records, the kind's own records — lives in the state root (§20), written through `put` with a `state` key". It is what the host's `get`/`put`/`state_commit` (§4.6, opencharly/spec#483) serve for the `state` row of the store table, and what core writes a `bind` handle into (D-CAP-4).

It lives in `spec` for the same reason `needsolver` and `storeroot` do: it is one implementation of a concern the HOST must perform (D-LAW-3: core "reads, links, validates, routes, solves and stores"), and `charly/` core may import `spec/*` only — never an sdk mechanism kit (IMPORT-PURITY). Plugins never import it: they reach the store through the host's `get`/`put`/`state_commit` RPCs.

THE CONTRACT IS NOT DEFINED HERE. spec.StateKey, spec.StateCommitInput, spec.StateRecord and spec.Digest are generated from opencharly/spec's schema/store.cue (the DESIGN.cue:1415-1454 port); this package owns only the WRITE PATH — where a record lives, that a commit replaces a deployment's records together, and that a stale commit is refused.

THE ROWS THAT SHAPE THIS CODE:

  • #StateKey {identity, name}: one record per (deployment, name), stored as <dir>/<identity>/<name>.json. A record's value is the calling kind's own shape (#Opaque, S10): the store never inspects it.
  • P3, "a revision is a digest, not a node": a deployment's REVISION is the digest of its committed record set (name → value), so it needs no counter and no extra field, and two stores holding the same records agree on it.
  • #StateCommitInput {identity, revision, record}: "the transaction `put` cannot be" — the deployment's WHOLE record set is replaced by `record`, atomically, and only when `revision` is the revision the store holds. A stale revision is ErrStaleRevision, never a partial write.

WHERE THE RECORDS LIVE: under the STATE root, resolved by the ONE resolver (storeroot.Dir(storeroot.RootState), the contract's storeroot) — never by hand-building an XDG path. Open takes the directory so a test injects its own root (D-STORE-8); OpenDefault resolves the real one.

THE LOCK: every read-modify-write of one deployment's records serializes on that deployment's file lock (spec/lock, the contract's one advisory-lock primitive — the same auditkit uses), so two processes cannot interleave a put and a commit. This is the STORE's serialization; the command-wide deployment lock of D-DEP-4 is core's and sits above it.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound reports a #StateKey with no committed record.
	ErrNotFound = errors.New("state: no such record")
	// ErrStaleRevision reports a commit whose revision is not the store's current one (D-DEP-4).
	ErrStaleRevision = errors.New("state: stale revision")
	// ErrInvalidKey reports a key or record the store cannot name: an empty identity or name, or one
	// that is not a plain path segment.
	ErrInvalidKey = errors.New("state: invalid key")
)

Functions

func Digest

func Digest(value any) (spec.Digest, error)

Digest is the digest of a value as the store records it: sha256 over its JSON encoding, whose map keys encoding/json writes sorted, so equal values have equal digests.

Types

type Scope

type Scope struct {
	// OwnsKind reports whether the calling plugin owns the kind of the deployment named identity.
	OwnsKind func(identity spec.Ref) bool
}

Scope is what the host knows about the call a host request serves (D-PROTO-12).

type Store

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

Store holds the realized state of every deployment under one directory.

func Open

func Open(dir string) (*Store, error)

Open returns a Store over dir, creating it when absent. Pass an injected directory in a test (D-STORE-8); use OpenDefault in production.

func OpenDefault

func OpenDefault() (*Store, error)

OpenDefault returns the Store over the deployments directory of the STATE root (D-DEP-5).

func (*Store) Commit

func (s *Store) Commit(in spec.StateCommitInput) error

Commit replaces identity's whole record set with in.Record, atomically, when in.Revision is the store's current revision; otherwise it returns ErrStaleRevision and writes nothing.

func (*Store) Dir

func (s *Store) Dir() string

Dir is the directory this Store holds.

func (*Store) Get

func (s *Store) Get(key spec.StateKey) (spec.Opaque, error)

Get returns the record key names, or ErrNotFound.

func (*Store) Put

func (s *Store) Put(key spec.StateKey, value spec.Opaque) (spec.Digest, error)

Put writes one record and returns the digest of its value (#PutOutput).

func (*Store) Revision

func (s *Store) Revision(identity spec.Ref) (spec.Digest, error)

Revision returns the digest of identity's committed record set (P3). A deployment with no records has the revision of the empty set, so a first commit names it like any other.

func (*Store) ServeGet

func (s *Store) ServeGet(scope *Scope, in *pb.StoreGetRequest) (*pb.StoreGetReply, error)

ServeGet answers a host `get`.

func (*Store) ServePut

func (s *Store) ServePut(scope *Scope, in *pb.StorePutRequest) (*pb.StorePutReply, error)

ServePut answers a host `put`.

func (*Store) ServeStateCommit

func (s *Store) ServeStateCommit(scope *Scope, in *pb.StateCommitRequest) (*pb.Empty, error)

ServeStateCommit answers a host `state_commit`.

Jump to

Keyboard shortcuts

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