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 ¶
- Variables
- func Digest(value any) (spec.Digest, error)
- type Scope
- type Store
- func (s *Store) Commit(in spec.StateCommitInput) error
- func (s *Store) Dir() string
- func (s *Store) Get(key spec.StateKey) (spec.Opaque, error)
- func (s *Store) Put(key spec.StateKey, value spec.Opaque) (spec.Digest, error)
- func (s *Store) Revision(identity spec.Ref) (spec.Digest, error)
- func (s *Store) ServeGet(scope *Scope, in *pb.StoreGetRequest) (*pb.StoreGetReply, error)
- func (s *Store) ServePut(scope *Scope, in *pb.StorePutRequest) (*pb.StorePutReply, error)
- func (s *Store) ServeStateCommit(scope *Scope, in *pb.StateCommitRequest) (*pb.Empty, error)
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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) Revision ¶
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 ¶
ServeStateCommit answers a host `state_commit`.