memory

package
v0.1.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 makes it durable.

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(Change) error

CommitHook is called for every Create, Update and Delete (Sweep'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. 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 and transcripts 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) 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) 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) 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) 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