-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimplestore.go
More file actions
179 lines (133 loc) · 3.28 KB
/
simplestore.go
File metadata and controls
179 lines (133 loc) · 3.28 KB
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
179
package main
import (
"crypto/rand"
"fmt"
"sync"
"github.com/Redundancy/upto/store"
)
// SimpleMemoryStore keeps all events in memory
// it's intended to be the shortest path to get upto testable
type SimpleMemoryStore struct {
sync.Mutex
ContextTimelineEventHostMap
latestTimeline map[string]string
}
type TimelineMap map[string]EventHostMap
type ContextTimelineEventHostMap map[string]TimelineMap
func (h ContextTimelineEventHostMap) addEvent(context string, timeline string, event store.TimelineEvent) error {
name := event.EventName
c, contextExists := h[context]
if !contextExists {
c = make(TimelineMap)
h[context] = c
}
t, timelineExists := c[timeline]
if !timelineExists {
t = make(EventHostMap)
c[timeline] = t
}
e, eventNameExists := t[name]
if !eventNameExists {
e = make(HostMap)
t[name] = e
}
_, exists := e[event.Host]
if exists {
return EventInProgressError(name)
}
e[event.Host] = event
return nil
}
func (s *SimpleMemoryStore) CreateContext(name string) {
s.Lock()
defer s.Unlock()
if s.ContextTimelineEventHostMap == nil {
s.ContextTimelineEventHostMap = make(ContextTimelineEventHostMap)
}
s.ContextTimelineEventHostMap[name] = make(TimelineMap)
}
func (s *SimpleMemoryStore) ContextExists(name string) bool {
s.Lock()
defer s.Unlock()
if s.ContextTimelineEventHostMap == nil {
return false
}
_, found := s.ContextTimelineEventHostMap[name]
return found
}
func (s *SimpleMemoryStore) ListContexts() []string {
s.Lock()
defer s.Unlock()
r := make([]string, 0, len(s.ContextTimelineEventHostMap))
for context, _ := range s.ContextTimelineEventHostMap {
r = append(r, context)
}
return r
}
func uuid() string {
b := make([]byte, 16)
rand.Read(b)
b[6] = (b[6] & 0x0f) | 0x40
b[8] = (b[8] & 0x3f) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}
func (s *SimpleMemoryStore) CreateNewTimeline(contextName string) {
s.Lock()
defer s.Unlock()
if s.latestTimeline == nil {
s.latestTimeline = make(map[string]string)
}
// TODO: make more than one timeline
s.latestTimeline[contextName] = uuid()
}
func (s *SimpleMemoryStore) GetLatestTimeline(contextName string) string {
s.Lock()
defer s.Unlock()
if s.latestTimeline == nil {
return ""
}
return s.latestTimeline[contextName]
}
func (s *SimpleMemoryStore) ListContextTimelines(contextName string) []string {
s.Lock()
defer s.Unlock()
if s.ContextTimelineEventHostMap == nil {
return nil
}
m, found := s.ContextTimelineEventHostMap[contextName]
if !found {
return nil
}
r := make([]string, 0, len(m))
for k, _ := range m {
r = append(r, k)
}
return r
}
func (s *SimpleMemoryStore) GetTimelineEvents(contextName, timeline string) []store.TimelineEvent {
s.Lock()
defer s.Unlock()
if s.ContextTimelineEventHostMap == nil {
return nil
}
m, found := s.ContextTimelineEventHostMap[contextName]
if !found {
return nil
}
t, found := m[timeline]
if !found {
return nil
}
r := make([]store.TimelineEvent, 0)
for _, hosts := range t {
for _, event := range hosts {
r = append(r, event)
}
}
return r
}
func (s *SimpleMemoryStore) AddTimelineEvent(contextName, timeline string, event store.TimelineEvent) {
s.Lock()
defer s.Unlock()
s.ContextTimelineEventHostMap.addEvent(contextName, timeline, event)
}