-
Notifications
You must be signed in to change notification settings - Fork 2
/
event_store.go
149 lines (116 loc) · 2.9 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
package eventually
import (
"sync"
)
type (
// EventStore for the topicsMap.
EventStore interface {
Load(UUID) (ConsumerCollection, bool)
Store(Descriptor, ...Consumer) bool
DeleteTopic(...Descriptor)
Length() int
Remove(Descriptor, ...Consumer) bool
IsSubscribed(Descriptor, Consumer) bool
Topics() []Descriptor
}
eventStore struct {
sync.RWMutex
topicsConsumersMap map[UUID]ConsumerCollection
topicsMap map[Descriptor]struct{}
topicsMapMux sync.Mutex
}
)
// NewEventStore creates a new EventStore.
func NewEventStore() EventStore {
return &eventStore{
topicsConsumersMap: make(map[UUID]ConsumerCollection),
topicsMap: make(map[Descriptor]struct{}),
}
}
func (es *eventStore) Load(aggregateID UUID) (ConsumerCollection, bool) {
es.RLock()
consumersCol, ok := es.topicsConsumersMap[aggregateID]
es.RUnlock()
return consumersCol, ok
}
func (es *eventStore) Store(topic Descriptor, consumers ...Consumer) bool {
if len(consumers) == 0 {
return false
}
consumersCol, ok := es.Load(topic.AggregateID())
es.Lock()
if !ok {
consumersCol = newConsumerCollection()
es.appendTopic(topic)
}
consumersCol.Append(consumers...)
es.Unlock()
es.updateTopicsMap(topic.AggregateID(), consumersCol)
return true
}
func (es *eventStore) DeleteTopic(topics ...Descriptor) {
es.Lock()
defer es.Unlock()
for _, topic := range topics {
delete(es.topicsConsumersMap, topic.AggregateID())
es.removeTopic(topic)
}
}
func (es *eventStore) Length() int {
es.Lock()
defer es.Unlock()
return len(es.topicsConsumersMap)
}
func (es *eventStore) Remove(topic Descriptor, consumers ...Consumer) bool {
if len(consumers) == 0 {
return false
}
consumersCol, ok := es.Load(topic.AggregateID())
if !ok {
return false
}
consumersCol.(ConsumerCollection).Delete(consumers...)
if consumersCol.Length() == 0 {
es.DeleteTopic(topic)
return true
}
es.updateTopicsMap(topic.AggregateID(), consumersCol)
return true
}
func (es *eventStore) IsSubscribed(topic Descriptor, consumer Consumer) bool {
consumersCol, ok := es.Load(topic.AggregateID())
if ok {
ok = consumersCol.Exists(consumer)
}
return ok
}
func (es *eventStore) updateTopicsMap(aggregateID UUID, consumersCol ConsumerCollection) {
es.Lock()
defer es.Unlock()
es.topicsConsumersMap[aggregateID] = consumersCol
}
func (es *eventStore) Topics() []Descriptor {
es.Lock()
defer es.Unlock()
topics := make([]Descriptor, len(es.topicsMap))
index := 0
for topic := range es.topicsMap {
topics[index] = topic
index++
}
return topics
}
func (es *eventStore) appendTopic(topics ...Descriptor) {
es.topicsMapMux.Lock()
defer es.topicsMapMux.Unlock()
for _, topic := range topics {
es.topicsMap[topic] = struct{}{}
}
}
func (es *eventStore) removeTopic(topics ...Descriptor) {
es.topicsMapMux.Lock()
defer es.topicsMapMux.Unlock()
for _, topic := range topics {
delete(es.topicsMap, topic)
}
}