looplab / eventhorizon

Event Sourcing for Go!
Apache License 2.0
1.57k stars 196 forks source link

Eventstore snapshot #396

Closed klowdo closed 1 year ago

klowdo commented 1 year ago

Description

This adds snapshot capability to eventstore and aggregatestore. With given strategy saves a snapshot of aggregate and loads next time and remaining events wich can increase load times of long-living aggregates

Affected Components

Related Issues

None

Solution and Design

A eventstore implemtation can now implement eh.SnapshotStore an now store snaphots of current state in aggregate.

every aggregate that want to be snapshotted needs to implement eh.Snapshotable

type Snapshotable interface {
    CreateSnapshot() *Snapshot
    ApplySnapshot(snapshot *Snapshot)
}

Loading

When aggretate i requested to be loaded the store checks if aggretate implements eh.Snapshotable and tries to find latest snapshot for aggregate. Loads and applies snapshot then loads all events that is not applies in current snapshot.

Saving

When aggretate i requested to be saved the store checks if aggretate implements eh.Snapshotable and if the strategy return true we create a snapshot ans saves it alongside the events.

Steps to test and verify

  1. Add Strategy to aggregatestore
    • EveryNumberEventSnapshotStrateg <- set to every 1
    • PeriodSnapshotStrategy
    • NoSnapshotStrategy
  2. Implement ehSnapshotable in longliving aggregate
  3. check mongo db snapshot collection
coveralls commented 1 year ago

Coverage Status

Coverage decreased (-1.06%) to 67.542% when pulling 4f802e8b9c0f07c7a76b61325be0a1ddf1d9a72e on klowdo:eventstore-snapshot into f8fe1ad7457f209f888d14a641813a7c104b89ab on looplab:main.