-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
97 lines (73 loc) · 2.09 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
89
90
91
92
93
94
95
96
97
package eventsource
import (
"context"
"errors"
"fmt"
"sort"
"sync"
)
var ErrSnapNotFound = errors.New("snapshot not found")
func NewInmemEventStore() EventStore {
return &inmemEventStore{persistence: make(map[string]History)}
}
// inmemEventStore is an implementation of eventsource.EventStore
// can be useful for testing or examples.
type inmemEventStore struct {
mu sync.Mutex
persistence map[string]History
}
func (s *inmemEventStore) SaveEvents(ctx context.Context, agrID string, models History, version int) error {
s.mu.Lock()
defer s.mu.Unlock()
i := version
var history History
for _, m := range models {
m.Version = i
history = append(s.persistence[agrID], m)
i++
}
sort.Slice(history, func(i, j int) bool { return history[i].Version < history[j].Version })
s.persistence[agrID] = history
return nil
}
func (s *inmemEventStore) GetEventsForAggregate(ctx context.Context, agrID string, version int) (History, error) {
history, ok := s.persistence[agrID]
if !ok {
return nil, fmt.Errorf("no aggregate found with id: %s", agrID)
}
if version != 0 {
var h History
for _, m := range history {
if m.Version > version {
h = append(h, m)
}
}
return h, nil
}
return history, nil
}
func NewInmemSnapStore() SnapshotStore {
return &inmemSnapStore{persistence: make(map[string][]SnapshotModel)}
}
type inmemSnapStore struct {
mu sync.Mutex
persistence map[string][]SnapshotModel
}
func (r *inmemSnapStore) SaveSnapshot(ctx context.Context, agrID string, model SnapshotModel, version int) error {
r.mu.Lock()
defer r.mu.Unlock()
history := append(r.persistence[model.ID], model)
sort.Slice(history, func(i, j int) bool { return history[i].Version < history[j].Version })
r.persistence[model.ID] = history
return nil
}
func (s *inmemSnapStore) GetSnapshotForAggregate(ctx context.Context, agrID string, version int) (SnapshotModel, error) {
history, ok := s.persistence[agrID]
if !ok {
return SnapshotModel{}, ErrSnapNotFound
}
if len(history) == 0 {
return SnapshotModel{}, ErrSnapNotFound
}
return history[len(history)-1], nil
}