-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate_store.go
175 lines (146 loc) · 4.16 KB
/
aggregate_store.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package ehscylla
import (
"context"
"errors"
"fmt"
"github.com/gocql/gocql"
"github.com/google/uuid"
eh "github.com/looplab/eventhorizon"
ehEvents "github.com/looplab/eventhorizon/aggregatestore/events"
)
type Aggregate interface {
ehEvents.VersionedAggregate
SnapshotData() interface{}
}
type aggregateStore struct {
eh.AggregateStore
snapshot AggregateSnapshot
snapshotStrategy func(aggregate eh.Aggregate) bool
store EventStore
}
func NewAggregateStoreWithBoundedContext(
session *gocql.Session,
boundedContext string,
encoder Encoder,
snapshotStrategy func(aggregate eh.Aggregate) bool) (*aggregateStore, error) {
if snapshotStrategy == nil {
snapshotStrategy = StrategySnapshotDefault
}
eventStore, err := NewEventStore(session, encoder, boundedContext)
if err != nil {
return nil, err
}
snapshot, err := NewAggregateSnapshot(session, boundedContext)
if err != nil {
return nil, err
}
return &aggregateStore{
store: eventStore,
snapshot: snapshot,
snapshotStrategy: snapshotStrategy,
}, nil
}
// Load loads the most recent version of an aggregate with a type and id.
func (s *aggregateStore) Load(ctx context.Context, aggType eh.AggregateType, id uuid.UUID) (eh.Aggregate, error) {
agg, err := eh.CreateAggregate(aggType, id)
if err != nil {
return nil, &eh.AggregateStoreError{
Err: err,
Op: eh.AggregateStoreOpLoad,
AggregateType: aggType,
AggregateID: id,
}
}
a, ok := agg.(ehEvents.VersionedAggregate)
if !ok {
return nil, &eh.AggregateStoreError{
Err: ehEvents.ErrAggregateNotVersioned,
Op: eh.AggregateStoreOpLoad,
AggregateType: aggType,
AggregateID: id,
}
}
if aggSnap, support := a.(Aggregate); support {
_, err = s.snapshot.Restore(ctx, aggSnap)
if err != nil {
// Re-create aggregate if error
agg, err = eh.CreateAggregate(aggType, id)
if err != nil {
return nil, err
}
a, _ = agg.(ehEvents.VersionedAggregate)
}
}
events, err := s.store.Load(ctx, a.EntityID(), a.AggregateType(), a.AggregateVersion())
if err != nil && !errors.Is(err, eh.ErrAggregateNotFound) {
return nil, &eh.AggregateStoreError{
Err: err,
Op: eh.AggregateStoreOpLoad,
AggregateType: aggType,
AggregateID: id,
}
}
if err := s.applyEvents(ctx, a, events); err != nil {
return nil, err
}
return a, nil
}
func (s *aggregateStore) applyEvents(ctx context.Context, a ehEvents.VersionedAggregate, events []eh.Event) error {
for _, event := range events {
if event.AggregateType() != a.AggregateType() {
return ehEvents.ErrMismatchedEventType
}
if err := a.ApplyEvent(ctx, event); err != nil {
return fmt.Errorf("could not apply event %s: %w", event, err)
}
a.SetAggregateVersion(event.Version())
}
return nil
}
func (r *aggregateStore) Save(ctx context.Context, agg eh.Aggregate) error {
a, ok := agg.(ehEvents.VersionedAggregate)
if !ok {
return &eh.AggregateStoreError{
Err: ehEvents.ErrAggregateNotVersioned,
Op: eh.AggregateStoreOpSave,
AggregateType: agg.AggregateType(),
AggregateID: agg.EntityID(),
}
}
// Retrieve any new events to store.
events := a.UncommittedEvents()
if len(events) == 0 {
return nil
}
if err := r.store.Save(ctx, events, a.AggregateVersion()); err != nil {
return &eh.AggregateStoreError{
Err: err,
Op: eh.AggregateStoreOpSave,
AggregateType: agg.AggregateType(),
AggregateID: agg.EntityID(),
}
}
a.ClearUncommittedEvents()
// Apply the events in case the aggregate needs to be further used
// after this save. Currently it is not reused.
if err := r.applyEvents(ctx, a, events); err != nil {
return &eh.AggregateStoreError{
Err: err,
Op: eh.AggregateStoreOpSave,
AggregateType: agg.AggregateType(),
AggregateID: agg.EntityID(),
}
}
// Auto snapshot
if r.snapshotStrategy(agg) {
if err := r.snapshot.Store(ctx, agg); err != nil {
return &eh.AggregateStoreError{
Err: err,
Op: eh.AggregateStoreOpSave,
AggregateType: agg.AggregateType(),
AggregateID: agg.EntityID(),
}
}
}
return nil
}