-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathinmemory-eventstreamrepository.go
115 lines (91 loc) · 3.07 KB
/
inmemory-eventstreamrepository.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
package cqrs
import (
"errors"
"sort"
"sync"
)
// InMemoryEventStreamRepository provides an inmemory event sourcing repository
type InMemoryEventStreamRepository struct {
lock sync.Mutex
store map[string][]VersionedEvent
correlation map[string][]VersionedEvent
integrationEvents []VersionedEvent
eventSourcedStore map[string]EventSourced
}
// NewInMemoryEventStreamRepository constructor
func NewInMemoryEventStreamRepository() *InMemoryEventStreamRepository {
store := make(map[string][]VersionedEvent)
correlation := make(map[string][]VersionedEvent)
eventSourcedStore := make(map[string]EventSourced)
return &InMemoryEventStreamRepository{sync.Mutex{}, store, correlation, []VersionedEvent{}, eventSourcedStore}
}
// AllIntegrationEventsEverPublished returns all events ever published
func (r *InMemoryEventStreamRepository) AllIntegrationEventsEverPublished() ([]VersionedEvent, error) {
r.lock.Lock()
defer r.lock.Unlock()
log := r.integrationEvents
sort.Sort(ByCreated(log))
return log, nil
}
// SaveIntegrationEvent persists an integration event
func (r *InMemoryEventStreamRepository) SaveIntegrationEvent(event VersionedEvent) error {
r.lock.Lock()
defer r.lock.Unlock()
r.integrationEvents = append(r.integrationEvents, event)
events := r.correlation[event.CorrelationID]
events = append(events, event)
r.correlation[event.CorrelationID] = events
PackageLogger().Debugf("Saving SaveIntegrationEvent event ", event.CorrelationID, events)
return nil
}
// GetIntegrationEventsByCorrelationID returns all integration events with a matching correlationID
func (r *InMemoryEventStreamRepository) GetIntegrationEventsByCorrelationID(correlationID string) ([]VersionedEvent, error) {
events, _ := r.correlation[correlationID]
return events, nil
}
// Save persists an event sourced object into the repository
func (r *InMemoryEventStreamRepository) Save(id string, newEvents []VersionedEvent) error {
for _, event := range newEvents {
if err := r.SaveIntegrationEvent(event); err != nil {
return err
}
}
r.lock.Lock()
defer r.lock.Unlock()
if events, ok := r.store[id]; ok {
events = append(events, newEvents...)
r.store[id] = events
return nil
}
r.store[id] = newEvents
return nil
}
// Get retrieves events assoicated with an event sourced object by ID
func (r *InMemoryEventStreamRepository) Get(id string, fromVersion int) ([]VersionedEvent, error) {
r.lock.Lock()
defer r.lock.Unlock()
var events []VersionedEvent
if allEvents, ok := r.store[id]; ok {
for _, event := range allEvents {
if event.Version < fromVersion {
continue
}
events = append(events, event)
}
return events, nil
}
return nil, errors.New("not found")
}
// SaveSnapshot ...
func (r *InMemoryEventStreamRepository) SaveSnapshot(eventsourced EventSourced) error {
r.eventSourcedStore[eventsourced.ID()] = eventsourced
return nil
}
// GetSnapshot ...
func (r *InMemoryEventStreamRepository) GetSnapshot(id string) (EventSourced, error) {
value, ok := r.eventSourcedStore[id]
if !ok {
return nil, errors.New("not found")
}
return value, nil
}