forked from jen20/go-event-sourcing-sample
-
Notifications
You must be signed in to change notification settings - Fork 23
/
snapshotrepository.go
149 lines (126 loc) · 3.99 KB
/
snapshotrepository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package eventsourcing
import (
"context"
"errors"
"reflect"
"github.com/hallgren/eventsourcing/core"
)
// ErrUnsavedEvents aggregate events must be saved before creating snapshot
var ErrUnsavedEvents = errors.New("aggregate holds unsaved events")
type SerializeFunc func(v interface{}) ([]byte, error)
type DeserializeFunc func(data []byte, v interface{}) error
// SnapshotAggregate interface is used to serialize an aggregate that has no exported properties
type SnapshotAggregate interface {
SerializeSnapshot(SerializeFunc) ([]byte, error)
DeserializeSnapshot(DeserializeFunc, []byte) error
}
type SnapshotRepository struct {
eventRepository *EventRepository
snapshotStore core.SnapshotStore
Encoder encoder
}
// NewSnapshotRepository factory function
func NewSnapshotRepository(snapshotStore core.SnapshotStore, eventRepo *EventRepository) *SnapshotRepository {
return &SnapshotRepository{
snapshotStore: snapshotStore,
eventRepository: eventRepo,
Encoder: EncoderJSON{},
}
}
// Register register the aggregate in the event repository
func (s *SnapshotRepository) Register(a aggregate) {
s.eventRepository.Register(a)
}
// EventRepository returns the underlying event repository. If the user wants to operate on the event repository
// and not use snapshot
func (s *SnapshotRepository) EventRepository() *EventRepository {
return s.eventRepository
}
func (s *SnapshotRepository) GetWithContext(ctx context.Context, id string, a aggregate) error {
if reflect.ValueOf(a).Kind() != reflect.Ptr {
return ErrAggregateNeedsToBeAPointer
}
err := s.getSnapshot(ctx, id, a)
if err != nil && !errors.Is(err, core.ErrSnapshotNotFound) {
return err
}
// Append events that could have been saved after the snapshot
return s.eventRepository.GetWithContext(ctx, id, a)
}
// GetSnapshot returns aggregate that is based on the snapshot data
// Beware that it could be more events that has happened after the snapshot was taken
func (s *SnapshotRepository) GetSnapshot(ctx context.Context, id string, a aggregate) error {
if reflect.ValueOf(a).Kind() != reflect.Ptr {
return ErrAggregateNeedsToBeAPointer
}
err := s.getSnapshot(ctx, id, a)
if err != nil && errors.Is(err, core.ErrSnapshotNotFound) {
return ErrAggregateNotFound
}
return err
}
func (s *SnapshotRepository) getSnapshot(ctx context.Context, id string, a aggregate) error {
snapshot, err := s.snapshotStore.Get(ctx, id, aggregateType(a))
if err != nil {
return err
}
// Does the aggregate have specific snapshot handling
sa, ok := a.(SnapshotAggregate)
if ok {
err = sa.DeserializeSnapshot(s.Encoder.Deserialize, snapshot.State)
if err != nil {
return err
}
} else {
err = s.Encoder.Deserialize(snapshot.State, a)
if err != nil {
return err
}
}
// set the internal aggregate properties
root := a.Root()
root.aggregateGlobalVersion = Version(snapshot.GlobalVersion)
root.aggregateVersion = Version(snapshot.Version)
root.aggregateID = snapshot.ID
return nil
}
// Save will save aggregate events and snapshot
func (s *SnapshotRepository) Save(a aggregate) error {
// make sure events are stored
err := s.eventRepository.Save(a)
if err != nil {
return err
}
return s.SaveSnapshot(a)
}
// SaveSnapshot will only store the snapshot and will return an error if there are events that are not stored
func (s *SnapshotRepository) SaveSnapshot(a aggregate) error {
root := a.Root()
if len(root.Events()) > 0 {
return ErrUnsavedEvents
}
state := []byte{}
var err error
// Does the aggregate have specific snapshot handling
sa, ok := a.(SnapshotAggregate)
if ok {
state, err = sa.SerializeSnapshot(s.Encoder.Serialize)
if err != nil {
return err
}
} else {
state, err = s.Encoder.Serialize(a)
if err != nil {
return err
}
}
snapshot := core.Snapshot{
ID: root.ID(),
Type: aggregateType(a),
Version: core.Version(root.Version()),
GlobalVersion: core.Version(root.GlobalVersion()),
State: state,
}
err = s.snapshotStore.Save(snapshot)
return err
}