snapshot

package
v0.1.0-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package snapshot provides support for Aggregate Root snapshots, useful where the size of your Aggregate Roots is expected to considerably grow in size and number of events.

Snapshots are used by an Event-sourced Aggregate Repository as an optimization technique to speed up the Aggregate state rehydration process, by saving the state of the Aggregate Root at a particular point in time in a durable store.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = fmt.Errorf("snapshot: entry not found")

ErrNotFound is returned by a snapshot.Getter when no recent snapshot has been found in the store.

Functions

This section is empty.

Types

type AlwaysPolicy

type AlwaysPolicy struct{}

AlwaysPolicy is a Snapshot Policy that always signals to take snapshots when queried.

func (AlwaysPolicy) Record

func (AlwaysPolicy) Record(version int64)

Record is a no-op.

func (AlwaysPolicy) ShouldRecord

func (AlwaysPolicy) ShouldRecord(version int64) bool

ShouldRecord always returns true.

type AtFixedIntervalsPolicy

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

AtFixedIntervalsPolicy is a Snapshot Policy that signals to take snapshots at a fixed, specified time interval (e.g. every 1 hour, etc.)

Please note: the time interval is calculated from the start of the application, not from the last Snapshot inserted in the Snapshot store. This is important to keep in mind while debugging your application and the snapshot behavior.

func NewAtFixedIntervalsPolicy

func NewAtFixedIntervalsPolicy(interval time.Duration) *AtFixedIntervalsPolicy

NewAtFixedIntervalsPolicy creates an AtFixedIntervalsPolicy instance with the specified time interval for Snapshot recordings.

func (*AtFixedIntervalsPolicy) Record

func (p *AtFixedIntervalsPolicy) Record(version int64)

Record updates the internal state of the Policy with the current timestamp.

func (*AtFixedIntervalsPolicy) ShouldRecord

func (p *AtFixedIntervalsPolicy) ShouldRecord(version int64) bool

ShouldRecord returns true on the first query, then after every interval specified during construction.

type EveryVersionIncrementPolicy

type EveryVersionIncrementPolicy int64

EveryVersionIncrementPolicy is a Snapshot Policy that signals to take snapshots every version increment specified by this value.

If the number used is EveryVersionIncrementPolicy(10), it means this policy will signal to record a snapshot at version 10, 20, 30 and so on.

func (EveryVersionIncrementPolicy) Record

func (EveryVersionIncrementPolicy) Record(version int64)

Record is a no-op, as the policy uses a stateless function.

func (EveryVersionIncrementPolicy) ShouldRecord

func (v EveryVersionIncrementPolicy) ShouldRecord(version int64) bool

ShouldRecord returns true when the current version modulo the increment specified in this policy equals to zero.

type Getter

type Getter interface {
	Get(ctx context.Context, id string) (Snapshot, error)
}

Getter is used to retrieve the most-recent Snapshot from a durable store.

type InMemoryStore

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

InMemoryStore is a map-based, thread-safe inmemory Snapshot store that can be used for storing long-lived Aggregate Roots.

Since there is no entry eviction, it is suggested to use this store only for test scenarios.

func NewInMemoryStore

func NewInMemoryStore() *InMemoryStore

NewInMemoryStore returns a fresh new instance of an the InMemoryStore snapshot store.

func (*InMemoryStore) Get

func (s *InMemoryStore) Get(ctx context.Context, id string) (Snapshot, error)

Get returns the latest version of the Aggregate Root recorded by its Aggregate id. ErrNotFound is returned if no Aggregate Root state has been committed to the store.

func (*InMemoryStore) MarshalJSON

func (s *InMemoryStore) MarshalJSON() ([]byte, error)

MarshalJSON serializes the internal state of the store for debugging purposes.

When relying on this functionality, make sure that the fields of your Aggregate Root state are correctly exported, or that your Aggregate Root implements json.Unmarshaler and json.Marshaler interfaces, for correct (de)-serialization.

func (*InMemoryStore) Record

func (s *InMemoryStore) Record(ctx context.Context, id string, version int64, state interface{}) error

Record adds or overwrites the previous Aggregate Root state in the store internal state. This operation cannot fail.

type NeverPolicy

type NeverPolicy struct{}

NeverPolicy is a Snapshot Policy that never signals to take snapshots when queried.

func (NeverPolicy) Record

func (NeverPolicy) Record(version int64)

Record is a no-op.

func (NeverPolicy) ShouldRecord

func (NeverPolicy) ShouldRecord(version int64) bool

ShouldRecord always returns false.

type Policy

type Policy interface {
	ShouldRecord(version int64) bool
	Record(version int64)
}

Policy represents the behavior of the Snapshot functionality, advising on the frequency of the snapshots to take.

Choose the best Policy among the ones provided in this package, considering your needs and the rate of updates of the Aggregate Root you're trying to optimize.

type Recorder

type Recorder interface {
	Record(ctx context.Context, id string, version int64, state interface{}) error
}

Recorder is used to record Snapshots to a durable store.

type Snapshot

type Snapshot struct {
	Version    int64       `json:"version"`
	State      interface{} `json:"state"`
	RecordedAt time.Time   `json:"recorded_at"`
}

Snapshot represents the value of a snapshot found in the store.

Jump to

Keyboard shortcuts

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