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 ¶
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) 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 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 ¶
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.
type NeverPolicy ¶
type NeverPolicy struct{}
NeverPolicy is a Snapshot Policy that never signals to take snapshots when queried.
func (NeverPolicy) ShouldRecord ¶
func (NeverPolicy) ShouldRecord(version int64) bool
ShouldRecord always returns false.
type Policy ¶
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.