-
Notifications
You must be signed in to change notification settings - Fork 39
/
store.go
88 lines (71 loc) · 2.15 KB
/
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
package eventsource
import (
"context"
"sort"
"sync"
)
// Record provides the serialized representation of the event
type Record struct {
// Version contains the version associated with the serialized event
Version int
// Data contains the event in serialized form
Data []byte
}
// History represents
type History []Record
// Len implements sort.Interface
func (h History) Len() int {
return len(h)
}
// Swap implements sort.Interface
func (h History) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
// Less implements sort.Interface
func (h History) Less(i, j int) bool {
return h[i].Version < h[j].Version
}
// Store provides an abstraction for the Repository to save data
type Store interface {
// Save the provided serialized records to the store
Save(ctx context.Context, aggregateID string, records ...Record) error
// Load the history of events up to the version specified.
// When toVersion is 0, all events will be loaded.
// To start at the beginning, fromVersion should be set to 0
Load(ctx context.Context, aggregateID string, fromVersion, toVersion int) (History, error)
}
// memoryStore provides an in-memory implementation of Store
type memoryStore struct {
mux *sync.Mutex
eventsByID map[string]History
}
func newMemoryStore() *memoryStore {
return &memoryStore{
mux: &sync.Mutex{},
eventsByID: map[string]History{},
}
}
func (m *memoryStore) Save(ctx context.Context, aggregateID string, records ...Record) error {
if _, ok := m.eventsByID[aggregateID]; !ok {
m.eventsByID[aggregateID] = History{}
}
history := append(m.eventsByID[aggregateID], records...)
sort.Sort(history)
m.eventsByID[aggregateID] = history
return nil
}
func (m *memoryStore) Load(ctx context.Context, aggregateID string, fromVersion, toVersion int) (History, error) {
all, ok := m.eventsByID[aggregateID]
if !ok {
return nil, NewError(nil, ErrAggregateNotFound, "no aggregate found with id, %v", aggregateID)
}
history := make(History, 0, len(all))
if len(all) > 0 {
for _, record := range all {
if v := record.Version; v >= fromVersion && (toVersion == 0 || v <= toVersion) {
history = append(history, record)
}
}
}
return all, nil
}