snapshot

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when trying to fetch a Snapshot that doesn't
	// exist.
	ErrNotFound = errors.New("snapshot not found")
)
View Source
var ErrUnimplemented = errors.New("aggregate does not implement (Un)Marshaler, encoding.Binary(Un)Marshaler or encoding.Text(Un)Marshaler")

ErrUnimplemented is returned when trying to marshal or unmarshal a snapshot into an aggregate that does not implement one of the supported marshalers.

Functions

func Marshal

func Marshal(a any) ([]byte, error)

Marshal encodes the given aggregate into a byte slice. If a implements Marshaler, a.MarshalSnapshot() is returned. If a implements encoding.BinaryMarshaler, a.MarshalBinary() is returned and if a implements encoding.TextMarshaler, a.MarshalText is returned. If a implements none of these interfaces, Marshal uses encoding/gob to marshal the snapshot.

func Test added in v0.1.2

func Test(q Query, s Snapshot) bool

Test tests the Snapshot s against the Query q and returns true if q should include s in its results. Test can be used by Store implementations to filter events based on the query.

func Unmarshal

func Unmarshal(s Snapshot, a Target) error

Unmarshal decodes the given snapshot into the given aggregate. If a implements Unmarshaler, a.UnmarshalSnapshot() is returned. If a implements encoding.BinaryMarshaler, a.UnmarshalBinary() is returned and if a implements encoding.TextUnmarshaler, a.UnmarshalText() is returned. If a implements none of these interfaces, encoding/gob is used to unmarshal the snapshot.

Types

type Marshaler

type Marshaler interface {
	MarshalSnapshot() ([]byte, error)
}

A Marshaler can encode itself into bytes. aggregates must implement Marshaler & Unmarshaler for Snapshots to work.

Example using encoding/gob:

type foo struct {
	aggregate.Aggregate
	state
}

type state struct {
	Name string
	Age uint8
}

func (f *foo) MarshalSnapshot() ([]byte, error) {
	var buf bytes.Buffer
	err := gob.NewEncoder(&buf).Encode(f.state)
	return buf.Bytes(), err
}

func (f *foo) UnmarshalSnapshot(p []byte) error {
	return gob.NewDecoder(bytes.NewReader(p)).Decode(&f.state)
}

type Option

type Option func(*snapshot)

Option is an option for creating a snapshot.

func Data

func Data(b []byte) Option

Data returns an Option that overrides the encoded data of a snapshot.

func Time

func Time(t time.Time) Option

Time returns an Option that sets the Time of a snapshot.

type Query

type Query interface {
	aggregate.Query

	Times() time.Constraints
}

Query is a query for snapshots.

type Schedule

type Schedule interface {
	// Test returns true if the given aggregate should be snapshotted.
	Test(aggregate.Aggregate) bool
}

A Schedule determines if an aggregate is scheduled to be snapshotted.

func Every

func Every(n int) Schedule

Every returns a Schedule that instructs to make Snapshots of an aggregate every nth event of that aggregate.

type Snapshot

type Snapshot interface {
	// AggregateName returns the name of the aggregate.
	AggregateName() string

	// AggregateID returns the UUID of the aggregate.
	AggregateID() uuid.UUID

	// AggregateVersion returns the version of the aggregate at the time of the snapshot.
	AggregateVersion() int

	// Time returns the time of the snapshot.
	Time() time.Time

	// State returns the encoded state of the aggregate at the time of the snapshot.
	State() []byte
}

Snapshot is a snapshot of an aggregate.

func New

func New(a aggregate.Aggregate, opts ...Option) (Snapshot, error)

New creates and returns a snapshot of the given aggregate.

func Sort

func Sort(snaps []Snapshot, s aggregate.Sorting, dir aggregate.SortDirection) []Snapshot

Sort sorts Snapshot and returns the sorted Snapshots.

func SortMulti

func SortMulti(snaps []Snapshot, sorts ...aggregate.SortOptions) []Snapshot

SortMulti sorts Snapshots by multiple fields and returns the sorted aggregates.

type Store

type Store interface {
	// Save saves the given Snapshot into the Store.
	Save(context.Context, Snapshot) error

	// Latest returns the latest Snapshot for the aggregate with the given name
	// and UUID.
	Latest(context.Context, string, uuid.UUID) (Snapshot, error)

	// Version returns the Snapshot with the given version for the aggregate
	// with the given name and UUID. Implementations should return an error if
	// the specified Snapshot does not exist in the Store.
	Version(context.Context, string, uuid.UUID, int) (Snapshot, error)

	// Limit returns the latest Snapshot that has a version equal to or lower
	// than the given version. Implementations should return an error if no
	// such Snapshot can be found.
	Limit(context.Context, string, uuid.UUID, int) (Snapshot, error)

	// Query queries the Store for Snapshots that fit the given Query and
	// returns a channel of Snapshots and a channel of errors.
	//
	// Example:
	//
	//	var store Store
	//	snaps, errs, err := store.Query(context.TODO(), query.New())
	//	// handle err
	//	err := streams.Walk(context.TODO(), func(snap Snapshot) {
	//		log.Println(fmt.Sprintf("Queried Snapshot: %v", snap))
	//		foo := newFoo(snap.AggregateID())
	//		err := Unmarshal(snap, foo)
	//		// handle err
	//	}, snaps, errs)
	//	// handle err
	Query(context.Context, Query) (<-chan Snapshot, <-chan error, error)

	// Delete deletes a Snapshot from the Store.
	Delete(context.Context, Snapshot) error
}

Store is a database for aggregate snapshots.

func NewStore added in v0.1.2

func NewStore() Store

NewStore returns an in-memory Snapshot Store.

type Target

type Target interface {
	SetVersion(int)
}

Target is a snapshot target. This should be an aggregate that implements the SetVersion function.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalSnapshot([]byte) error
}

An Unmarshaler can decode itself from bytes.

Directories

Path Synopsis
Package mock_snapshot is a generated GoMock package.
Package mock_snapshot is a generated GoMock package.

Jump to

Keyboard shortcuts

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