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 ¶
- type Change
- type CommitHook
- type Op
- type Option
- type Record
- type Store
- func (s *Store) Apply(c Change, typ reflect.Type) (applied bool, err error)
- func (s *Store) Create(ctx context.Context, obj store.Object) error
- func (s *Store) Delete(ctx context.Context, k store.Key) error
- func (s *Store) DeleteAt(ctx context.Context, k store.Key, version uint64) error
- func (s *Store) DeleteIfUnchanged(ctx context.Context, k store.Key, resourceVersion string) error
- func (s *Store) Get(ctx context.Context, k store.Key, out store.Object) error
- func (s *Store) GetTranscript(ctx context.Context, run string) ([]byte, error)
- func (s *Store) List(ctx context.Context, kind string, opts store.ListOptions, out any) (store.ListMeta, error)
- func (s *Store) Lock(ctx context.Context, name string, ttl time.Duration) (store.Unlock, error)
- func (s *Store) PutTranscript(ctx context.Context, run string, data []byte) error
- func (s *Store) Replace(recs []Record, version uint64) error
- func (s *Store) Restore(recs []Record, version uint64) error
- func (s *Store) Snapshot() ([]Record, uint64)
- func (s *Store) Sweep(ctx context.Context) (store.SweepResult, error)
- func (s *Store) Update(ctx context.Context, obj store.Object) error
- func (s *Store) Version() uint64
- func (s *Store) Watch(ctx context.Context, kind string, opts store.WatchOptions) (store.Watcher, error)
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 ¶
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 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 (*Store) Apply ¶ added in v0.2.0
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) DeleteAt ¶ added in v0.2.0
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
DeleteIfUnchanged implements store.UnchangedDeleter: DeleteAt at the resourceVersion read.
func (*Store) GetTranscript ¶
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) PutTranscript ¶
PutTranscript implements store.TranscriptStore.
func (*Store) Replace ¶ added in v0.2.0
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 ¶
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 ¶
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 ¶
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.