-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventstore.go
308 lines (256 loc) · 7.6 KB
/
eventstore.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package ehpg
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/go-redis/redis"
"github.com/google/uuid"
eh "github.com/looplab/eventhorizon"
"github.com/looplab/eventhorizon/namespace"
"sort"
"strconv"
"time"
)
// ErrCouldNotClearDB is when the database could not be cleared.
var ErrCouldNotClearDB = errors.New("could not clear database")
// ErrConflictVersion is when a version conflict occurs when saving an aggregate.
var ErrVersionConflict = errors.New("can not create/update aggregate")
// ErrCouldNotMarshalEvent is when an event could not be marshaled into JSON.
var ErrCouldNotMarshalEvent = errors.New("could not marshal event")
// ErrCouldNotUnmarshalEvent is when an event could not be unmarshalled into a concrete type.
var ErrCouldNotUnmarshalEvent = errors.New("could not unmarshal event")
// ErrCouldNotSaveAggregate is when an aggregate could not be saved.
var ErrCouldNotSaveAggregate = errors.New("could not save aggregate")
// EventStore implements an eh.EventStore for PostgreSQL.
type EventStore struct {
db redis.UniversalClient
encoder Encoder
}
var _ = eh.EventStore(&EventStore{})
type AggregateRecord struct {
Namespace string
AggregateID uuid.UUID
Version int
}
type AggregateEvent struct {
EventID uuid.UUID
Namespace string
AggregateID uuid.UUID
AggregateType eh.AggregateType
EventType eh.EventType
RawEventData json.RawMessage
Timestamp time.Time
Version int
MetaData map[string]interface{}
data eh.EventData
RawMetaData json.RawMessage
}
func (a AggregateEvent) MarshalBinary() (data []byte, err error) {
return json.Marshal(a)
}
func (a *AggregateEvent) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, a)
}
// NewUUID for mocking in tests
var NewUUID = uuid.New
// newDBEvent returns a new dbEvent for an event.
func (s *EventStore) newDBEvent(ctx context.Context, event eh.Event) (*AggregateEvent, error) {
ns := namespace.FromContext(ctx)
// Marshal event data if there is any.
rawEventData, err := s.encoder.Marshal(event.Data())
if err != nil {
return nil, eh.EventStoreError{
BaseErr: err,
Err: ErrCouldNotMarshalEvent,
}
}
// Marshal meta data if there is any.
rawMetaData, err := json.Marshal(event.Metadata())
if err != nil {
return nil, eh.EventStoreError{
BaseErr: err,
Err: ErrCouldNotMarshalEvent,
}
}
return &AggregateEvent{
EventID: NewUUID(),
AggregateID: event.AggregateID(),
AggregateType: event.AggregateType(),
EventType: event.EventType(),
Version: event.Version(),
Timestamp: event.Timestamp(),
Namespace: ns,
RawEventData: rawEventData,
RawMetaData: rawMetaData,
}, nil
}
// NewEventStore creates a new EventStore.
func NewEventStore(db redis.UniversalClient) (*EventStore, error) {
if response := db.Ping(); response.Err() != nil {
return nil, response.Err()
}
s := &EventStore{
db: db,
encoder: &jsonEncoder{},
}
return s, nil
}
// Save implements the Save method of the eventhorizon.EventStore interface.
func (s *EventStore) Save(ctx context.Context, events []eh.Event, originalVersion int) error {
ns := namespace.FromContext(ctx)
if len(events) == 0 {
return eh.EventStoreError{
Err: eh.ErrNoEventsToAppend,
}
}
// Build all event records, with incrementing versions starting from the
// original aggregate version.
dbEvents := make(map[string]interface{})
aggregateID := events[0].AggregateID()
version := originalVersion
for _, event := range events {
// Only accept events belonging to the same aggregate.
if event.AggregateID() != aggregateID {
return eh.EventStoreError{
Err: eh.ErrInvalidEvent,
}
}
// Only accept events that apply to the correct aggregate version.
if event.Version() != version+1 {
return eh.EventStoreError{
Err: eh.ErrIncorrectEventVersion,
}
}
// Create the event record for the DB.
e, err := s.newDBEvent(ctx, event)
if err != nil {
return err
}
dbEvents[strconv.Itoa(event.Version())] = *e
version++
}
err := s.db.Watch(func(tx *redis.Tx) error {
for version, event := range dbEvents {
if result := tx.HSetNX(fmt.Sprintf("%s:%s", ns, aggregateID), version, event); result.Val() == false {
return eh.EventStoreError{
BaseErr: result.Err(),
Err: ErrVersionConflict,
}
}
}
return nil
}, fmt.Sprintf("%s:%s", ns, aggregateID))
if err != nil {
return eh.EventStoreError{
BaseErr: err,
Err: ErrCouldNotSaveAggregate,
}
}
return nil
}
// Load implements the Load method of the eventhorizon.EventStore interface.
func (s *EventStore) Load(ctx context.Context, id uuid.UUID) ([]eh.Event, error) {
ns := namespace.FromContext(ctx)
cmd := s.db.HGetAll(fmt.Sprintf("%s:%s", ns, id.String()))
var events []eh.Event
for _, dbEvent := range cmd.Val() {
e := AggregateEvent{}
if err := json.Unmarshal([]byte(dbEvent), &e); err != nil {
return nil, eh.EventStoreError{
BaseErr: err,
Err: ErrCouldNotUnmarshalEvent,
}
}
if e.RawEventData != nil {
if eventData, err := s.encoder.Unmarshal(e.EventType, e.RawEventData); err != nil {
return nil, eh.EventStoreError{
BaseErr: err,
Err: ErrCouldNotUnmarshalEvent,
}
} else {
e.data = eventData
}
}
e.RawEventData = nil
if e.RawMetaData != nil {
if err := json.Unmarshal(e.RawMetaData, &e.MetaData); err != nil {
return nil, eh.EventStoreError{
BaseErr: err,
Err: ErrCouldNotUnmarshalEvent,
}
}
}
e.RawEventData = nil
events = append(events, event{
AggregateEvent: e,
})
}
sort.Slice(events, func(i, j int) bool {
return events[i].Version() < events[j].Version()
})
return events, nil
}
func (s *EventStore) Close() error {
return s.db.Close()
}
// Clear clears the event storage.
func (s *EventStore) Clear(ctx context.Context) error {
ns := namespace.FromContext(ctx)
err := s.db.Watch(func(tx *redis.Tx) error {
iter := tx.Scan(0, fmt.Sprintf("%s:*", ns), 0).Iterator()
for iter.Next() {
err := s.db.Del(iter.Val()).Err()
if err != nil {
return err
}
}
if err := iter.Err(); err != nil {
return err
}
return nil
}, fmt.Sprintf("%s:*", ns))
if err != nil {
return eh.EventStoreError{
BaseErr: err,
Err: ErrCouldNotClearDB,
}
}
return nil
}
// event is the private implementation of the eventhorizon.Event interface
// for a redis event store.
type event struct {
AggregateEvent
}
func (e event) Metadata() map[string]interface{} {
return e.AggregateEvent.MetaData
}
// AggregateID implements the AggregateID method of the eventhorizon.Event interface.
func (e event) AggregateID() uuid.UUID {
return e.AggregateEvent.AggregateID
}
// AggregateType implements the AggregateType method of the eventhorizon.Event interface.
func (e event) AggregateType() eh.AggregateType {
return e.AggregateEvent.AggregateType
}
// EventType implements the EventType method of the eventhorizon.Event interface.
func (e event) EventType() eh.EventType {
return e.AggregateEvent.EventType
}
// Data implements the Data method of the eventhorizon.Event interface.
func (e event) Data() eh.EventData {
return e.AggregateEvent.data
}
// Version implements the Version method of the eventhorizon.Event interface.
func (e event) Version() int {
return e.AggregateEvent.Version
}
// Timestamp implements the Timestamp method of the eventhorizon.Event interface.
func (e event) Timestamp() time.Time {
return e.AggregateEvent.Timestamp
}
// String implements the String method of the eventhorizon.Event interface.
func (e event) String() string {
return fmt.Sprintf("%s@%d", e.AggregateEvent.EventType, e.AggregateEvent.Version)
}