memory

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package memory is the in-memory store.Store: one map per kind behind a single RWMutex (PoC scale: thousands of objects), JSON deep copies on the way in and out, field indexes, watch fan-out with bounded buffers and a replay history, leased locks, retention and the transcript blobs. A commit hook (WithCommitHook) sees every write before it is applied, which is how store/filestore (and an object store, P3) make it durable; Apply, DeleteAt and Replace (apply.go) serve a store on a shared log.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	Op  Op
	Key store.Key
	// Version is the store version the change gets (the store's counter after it).
	Version uint64
	// At is the write's time on the store's clock: the object's last-write time for create
	// and update (and its creation time for create), the deletion time for delete.
	At time.Time
	// Data is the object's JSON exactly as the store keeps it (resourceVersion and createdAt
	// set when the kind has them); nil for delete. It is shared with the store: don't modify it.
	Data []byte
}

Change is one write as the commit hook sees it, before the store applies it.

type CommitHook

type CommitHook func(ctx context.Context, c Change) error

CommitHook is called for every Create, Update and Delete (Sweep's and DeleteAt's too) once the write has been checked and encoded but before it is applied, under the store's write lock: changes reach it one at a time, in version order. ctx is the write's. When it returns an error the write fails with it (wrapped) and the store is left exactly as it was: no object, version or watch event. It must not call back into the store. Locks, transcripts and Apply don't go through it.

type Op

type Op string

Op is what a Change did.

const (
	OpCreate Op = "create"
	OpUpdate Op = "update"
	OpDelete Op = "delete"
)

Change ops.

type Option

type Option func(*Store)

Option configures New.

func WithCommitHook

func WithCommitHook(h CommitHook) Option

WithCommitHook makes every write go through h first (a durable store journals there).

type Record

type Record struct {
	Key store.Key
	// Type is the object's struct type: the object is a *Type.
	Type reflect.Type
	// Data is the object's JSON as the store keeps it. From Snapshot it is shared with the
	// store: don't modify it.
	Data []byte
	// Version is the object's resourceVersion (kept beside it for kinds without one).
	Version uint64
	// Created and Updated are its creation and last-write times (Updated drives SinceUpdated
	// retention).
	Created, Updated time.Time
}

Record is one stored object, as Snapshot returns it and Restore takes it.

type Store

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

Store is the in-memory store. Use New.

func New

func New(opts store.Options, options ...Option) *Store

New returns an empty store.

func (*Store) Apply added in v0.2.0

func (s *Store) Apply(c Change, typ reflect.Type) (applied bool, err error)

Apply applies a change that was committed elsewhere (another store's write, read back from a shared log), as if it had been written here: the object, its indexes and the version counter change and the watchers are told, in order. The hook isn't called. typ is the object's struct type (nil for a delete). A change at or below the store's version is already here and is skipped (applied is false); one above the next version is an error (a change is missing), as is a change that doesn't fit the state (a create of an object that exists, an update or delete of one that doesn't). The store keeps c.Data: don't modify it afterwards.

func (*Store) Create

func (s *Store) Create(ctx context.Context, obj store.Object) error

Create implements store.Store.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, k store.Key) error

Delete implements store.Store.

func (*Store) DeleteAt added in v0.2.0

func (s *Store) DeleteAt(ctx context.Context, k store.Key, version uint64) error

DeleteAt deletes the object at k only if its resourceVersion is version: ErrConflict when it has changed since, ErrNotFound when there is none. A lock's holder releases its lock record this way, so it never removes a record someone else took over. It goes through the hook as Delete does.

func (*Store) DeleteIfUnchanged added in v0.2.0

func (s *Store) DeleteIfUnchanged(ctx context.Context, k store.Key, resourceVersion string) error

DeleteIfUnchanged implements store.UnchangedDeleter: DeleteAt at the resourceVersion read.

func (*Store) Get

func (s *Store) Get(ctx context.Context, k store.Key, out store.Object) error

Get implements store.Store.

func (*Store) GetTranscript

func (s *Store) GetTranscript(ctx context.Context, run string) ([]byte, error)

GetTranscript implements store.TranscriptStore.

func (*Store) List

func (s *Store) List(ctx context.Context, kind string, opts store.ListOptions, out any) (store.ListMeta, error)

List implements store.Store.

func (*Store) Lock

func (s *Store) Lock(ctx context.Context, name string, ttl time.Duration) (store.Unlock, error)

Lock implements store.Store.

func (*Store) PutTranscript

func (s *Store) PutTranscript(ctx context.Context, run string, data []byte) error

PutTranscript implements store.TranscriptStore.

func (*Store) Replace added in v0.2.0

func (s *Store) Replace(recs []Record, version uint64) error

Replace swaps the whole state for recs at version, which must not be below the store's: a store that fell too far behind a shared log starts again from a snapshot. Every watcher is expired (ErrWatchExpired: it re-lists), and the history starts empty at version, as after Restore. The hook isn't called. The store keeps each record's Data.

func (*Store) Restore

func (s *Store) Restore(recs []Record, version uint64) error

Restore loads recs into an empty store and sets its version counter to version, as a durable store does at start-up. The hook isn't called and watchers aren't told. The watch history starts empty at version: a Watch with an older SinceVersion is ErrWatchExpired, as when the history has been trimmed. The store keeps each record's Data: don't modify it afterwards. Call it before any other use of the store.

func (*Store) Snapshot

func (s *Store) Snapshot() ([]Record, uint64)

Snapshot returns every stored object and the store's version, read at one instant. The version can be above every object's (deletes advance it too).

func (*Store) Sweep

func (s *Store) Sweep(ctx context.Context) (store.SweepResult, error)

Sweep implements store.Sweeper: it deletes the objects and transcripts past retention (store.RetentionOf), telling watchers, and drops expired locks. Run it with store.RunSweeper.

func (*Store) Update

func (s *Store) Update(ctx context.Context, obj store.Object) error

Update implements store.Store.

func (*Store) Version added in v0.2.0

func (s *Store) Version() uint64

Version returns the store's version: the last change applied.

func (*Store) Watch

func (s *Store) Watch(ctx context.Context, kind string, opts store.WatchOptions) (store.Watcher, error)

Watch implements store.Store.

Jump to

Keyboard shortcuts

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