state

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package state is where the engine keeps what must survive a restart (a daemon restart natively, a page reload in a browser): containers, their logs and their writable layers. It is a small key-value Store with three implementations — a directory (wctrd's --data-root), memory (tests), and IndexedDB (the playground, outside this module) — and a sealed record format that detects a torn or corrupted value instead of trusting it.

Keys are "<kind>/<name>", kind lower-case letters, name lower-case hex or letters, digits, '.', '_' and '-' (at most 128 bytes in all), so a key is also a safe relative file path.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("state: not found")
	// ErrNoSpace wraps a store's quota failure (QuotaExceededError in a
	// browser, ENOSPC natively). A Put that fails with it wrote nothing.
	ErrNoSpace = errors.New("state: no space left")
	// ErrCorrupt is a value whose seal does not match its content.
	ErrCorrupt = errors.New("state: corrupted record")
	ErrBadKey  = errors.New("state: invalid key")
)

Errors a Store returns.

Functions

func BlobKey

func BlobKey(data []byte) string

BlobKey is the key of content-addressed data: "blob/<sha256 hex>".

func CheckKey

func CheckKey(k string) error

CheckKey refuses keys that are not "<kind>/<name>" as documented.

func ContentKey added in v0.8.0

func ContentKey(kind string, data []byte) string

ContentKey is the key of content-addressed data of a kind of its own: "<kind>/<sha256 hex>" (volumes keep theirs apart from containers', so one's sweep never touches the other's).

func GetBlob

func GetBlob(s Store, key string, max int) ([]byte, error)

GetBlob reads content-addressed data and checks it against its key.

func Seal

func Seal(kind string, payload []byte) []byte

Seal wraps payload as a value of the given kind.

func Unseal

func Unseal(kind string, v []byte, max int) ([]byte, error)

Unseal checks a sealed value of the given kind and returns its payload. Anything else — another kind, a bad digest, a payload over max bytes, a truncated value — is ErrCorrupt.

Types

type Describer

type Describer interface {
	Describe() (kind string, bytes int64)
}

Describer is an optional Store method naming where it keeps values and how many bytes they hold (-1: unknown), for `system df`.

type Dir

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

Dir is a Store in a directory: one file per key, <root>/<kind>/<name>. A Put writes a temporary file, syncs it and renames it over the key, so a crash leaves the old value or the new one.

func OpenDir

func OpenDir(root string) (*Dir, error)

OpenDir opens (creating) a directory store.

func (*Dir) Delete

func (d *Dir) Delete(k string) error

func (*Dir) Describe

func (d *Dir) Describe() (string, int64)

Describe implements Describer.

func (*Dir) Get

func (d *Dir) Get(k string) ([]byte, error)

func (*Dir) Keys

func (d *Dir) Keys(prefix string) ([]string, error)

func (*Dir) Put

func (d *Dir) Put(k string, v []byte) error

type Mem

type Mem struct {
	MaxBytes int64
	// contains filtered or unexported fields
}

Mem is a Store in memory, bounded by MaxBytes (0 = unbounded).

func (*Mem) Delete

func (s *Mem) Delete(k string) error

func (*Mem) Describe

func (s *Mem) Describe() (string, int64)

Describe implements Describer.

func (*Mem) Get

func (s *Mem) Get(k string) ([]byte, error)

func (*Mem) Keys

func (s *Mem) Keys(prefix string) ([]string, error)

func (*Mem) Put

func (s *Mem) Put(k string, v []byte) error

func (*Mem) Size

func (s *Mem) Size() int64

Size is the bytes held.

type Store

type Store interface {
	Get(key string) ([]byte, error)
	Put(key string, value []byte) error
	Delete(key string) error // nil when absent
	Keys(prefix string) ([]string, error)
}

Store keeps values by key. Put is atomic per key: after a crash a key holds the old value or the new one, never a mix. Implementations are safe for concurrent use.

Jump to

Keyboard shortcuts

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