-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_store.go
178 lines (153 loc) · 4.1 KB
/
event_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
176
177
178
package ehscylla
import (
"context"
"errors"
"time"
"github.com/gocql/gocql"
"github.com/google/uuid"
eh "github.com/looplab/eventhorizon"
)
var ErrInvalidEventVersion = errors.New("invalid event version")
// ErrCouldNotMarshalEvent is when an event could not be marshaled into JSON.
var ErrCouldNotMarshalEvent = errors.New("could not marshal event")
// EventStore is an interface for an event sourcing event store.
type EventStore interface {
// Save appends all events in the event stream to the store.
Save(ctx context.Context, events []eh.Event, originalVersion int) error
// Load loads all events for the aggregate id from the store.
Load(ctx context.Context, aggregateId uuid.UUID, aggregateType eh.AggregateType, fromVersion int) ([]eh.Event, error)
}
type eventStore struct {
session *gocql.Session
encoder Encoder
boundedContext string
}
func NewEventStore(session *gocql.Session, encoder Encoder, boundedContext string) (EventStore, error) {
if encoder == nil {
encoder = jsonEncoder{}
}
return &eventStore{
session: session,
boundedContext: boundedContext,
encoder: encoder,
}, nil
}
func (e *eventStore) Save(ctx context.Context, events []eh.Event, originalVersion int) error {
batch := e.session.NewBatch(gocql.LoggedBatch)
for _, event := range events {
// Marshal event data if there is any.
data, err := e.encoder.Marshal(event.Data())
if err != nil {
return &eh.EventStoreError{
Err: err,
}
}
metadata, err := e.encoder.Marshal(event.Metadata())
if err != nil {
return &eh.EventStoreError{
Err: err,
}
}
batch.Query(`INSERT INTO event_store (
bounded_context,
aggregate_id,
aggregate_type,
event_id,
event_type,
event_version,
event_data,
event_timestamp,
event_metadata)
VALUES (?,?,?,?,?,?,?,?,?) IF NOT EXISTS;`, e.boundedContext, event.AggregateID().String(),
event.AggregateType(), uuid.New().String(), event.EventType(), event.Version(), data, event.Timestamp(), metadata)
}
cas := make(map[string]interface{})
applied, _, err := e.session.MapExecuteBatchCAS(batch, cas)
if err != nil {
return &eh.EventStoreError{
Err: err,
}
}
if !applied {
return &eh.EventStoreError{
Err: ErrInvalidEventVersion,
}
}
return nil
}
// Load loads all events for the aggregate id from the store.
func (e *eventStore) Load(ctx context.Context, aggregateId uuid.UUID, aggregateType eh.AggregateType, fromVersion int) ([]eh.Event, error) {
var events []eh.Event
scanner := e.session.Query(`
SELECT
aggregate_id,
aggregate_type,
event_id,
event_type,
event_version,
event_data,
event_timestamp,
event_metadata
FROM event_store WHERE
bounded_context = ?
AND aggregate_id = ?
AND aggregate_type = ?
AND event_version > ?`,
e.boundedContext, aggregateId.String(), aggregateType, fromVersion).Consistency(gocql.One).WithContext(ctx).Iter().Scanner()
for scanner.Next() {
var aggregateId gocql.UUID
var aggregateType eh.AggregateType
var eventId gocql.UUID
var eventType eh.EventType
var eventVersion int
var eventTimeStamp time.Time
var eventData []byte
var eventMetaData []byte
err := scanner.Scan(
&aggregateId,
&aggregateType,
&eventId,
&eventType,
&eventVersion,
&eventData,
&eventTimeStamp,
&eventMetaData)
if err != nil {
return nil, &eh.EventStoreError{
Err: err,
}
}
eventDataUnmarshal, err := e.encoder.UnmarshalEventData(eventType, eventData)
if err != nil {
return nil, &eh.EventStoreError{
Err: err,
}
}
eventMetaDataUnmarshal, err := e.encoder.UnmarshalEventMetaData(eventMetaData)
if err != nil {
return nil, &eh.EventStoreError{
Err: err,
}
}
event := eh.NewEventForAggregate(
eventType,
eventDataUnmarshal,
eventTimeStamp,
aggregateType,
uuid.UUID(aggregateId),
eventVersion,
eh.WithMetadata(eventMetaDataUnmarshal))
events = append(events, event)
}
if err := scanner.Err(); err != nil {
return nil, &eh.EventStoreError{
Err: err,
}
}
return events, nil
}
// Close closes the EventStore.
func (e *eventStore) Close() error {
e.session.Close()
return nil
}