-
Notifications
You must be signed in to change notification settings - Fork 712
/
latest_map.go
219 lines (192 loc) · 4.94 KB
/
latest_map.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package report
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"sort"
"time"
"github.com/mndrix/ps"
)
// LatestMap is a persitent map which support latest-win merges. We have to
// embed ps.Map as its an interface. LatestMaps are immutable.
type LatestMap struct {
ps.Map
}
// LatestEntry represents a timestamped value inside the LatestMap.
type LatestEntry struct {
Timestamp time.Time `json:"timestamp"`
Value string `json:"value"`
}
func (e LatestEntry) String() string {
return fmt.Sprintf("\"%s\" (%s)", e.Value, e.Timestamp.String())
}
// Equal returns true if the supplied LatestEntry is equal to this one.
func (e LatestEntry) Equal(e2 LatestEntry) bool {
return e.Timestamp.Equal(e2.Timestamp) && e.Value == e2.Value
}
// EmptyLatestMap is an empty LatestMap. Start with this.
var EmptyLatestMap = LatestMap{ps.NewMap()}
// MakeLatestMap makes an empty LatestMap
func MakeLatestMap() LatestMap {
return EmptyLatestMap
}
// Copy is a noop, as LatestMaps are immutable.
func (m LatestMap) Copy() LatestMap {
return m
}
// Size returns the number of elements
func (m LatestMap) Size() int {
if m.Map == nil {
return 0
}
return m.Map.Size()
}
// Merge produces a fresh LatestMap, container the kers from both inputs. When
// both inputs container the same key, the latter value is used.
func (m LatestMap) Merge(other LatestMap) LatestMap {
var (
mSize = m.Size()
otherSize = other.Size()
output = m.Map
iter = other.Map
)
switch {
case mSize == 0:
return other
case otherSize == 0:
return m
case mSize < otherSize:
output, iter = iter, output
}
iter.ForEach(func(key string, iterVal interface{}) {
if existingVal, ok := output.Lookup(key); ok {
if existingVal.(LatestEntry).Timestamp.Before(iterVal.(LatestEntry).Timestamp) {
output = output.Set(key, iterVal)
}
} else {
output = output.Set(key, iterVal)
}
})
return LatestMap{output}
}
// Lookup the value for the given key.
func (m LatestMap) Lookup(key string) (string, bool) {
if m.Map == nil {
return "", false
}
value, ok := m.Map.Lookup(key)
if !ok {
return "", false
}
return value.(LatestEntry).Value, true
}
// Set the value for the given key.
func (m LatestMap) Set(key string, timestamp time.Time, value string) LatestMap {
if m.Map == nil {
m = EmptyLatestMap
}
return LatestMap{m.Map.Set(key, LatestEntry{timestamp, value})}
}
// Delete the value for the given key.
func (m LatestMap) Delete(key string) LatestMap {
if m.Map == nil {
m = EmptyLatestMap
}
return LatestMap{m.Map.Delete(key)}
}
// ForEach executes f on each key value pair in the map
func (m LatestMap) ForEach(fn func(k, v string)) {
if m.Map == nil {
return
}
m.Map.ForEach(func(key string, value interface{}) {
fn(key, value.(LatestEntry).Value)
})
}
func (m LatestMap) String() string {
keys := []string{}
if m.Map == nil {
m = EmptyLatestMap
}
for _, k := range m.Map.Keys() {
keys = append(keys, k)
}
sort.Strings(keys)
buf := bytes.NewBufferString("{")
for _, key := range keys {
val, _ := m.Map.Lookup(key)
fmt.Fprintf(buf, "%s: %s,\n", key, val)
}
fmt.Fprintf(buf, "}\n")
return buf.String()
}
// DeepEqual tests equality with other LatestMap
func (m LatestMap) DeepEqual(n LatestMap) bool {
if m.Size() != n.Size() {
return false
}
if m.Size() == 0 {
return true
}
equal := true
m.Map.ForEach(func(k string, val interface{}) {
if otherValue, ok := n.Map.Lookup(k); !ok {
equal = false
} else {
equal = equal && val.(LatestEntry).Equal(otherValue.(LatestEntry))
}
})
return equal
}
func (m LatestMap) toIntermediate() map[string]LatestEntry {
intermediate := map[string]LatestEntry{}
if m.Map != nil {
m.Map.ForEach(func(key string, val interface{}) {
intermediate[key] = val.(LatestEntry)
})
}
return intermediate
}
func (m LatestMap) fromIntermediate(in map[string]LatestEntry) LatestMap {
out := ps.NewMap()
for k, v := range in {
out = out.Set(k, v)
}
return LatestMap{out}
}
// MarshalJSON implements json.Marshaller
func (m LatestMap) MarshalJSON() ([]byte, error) {
buf := bytes.Buffer{}
var err error
if m.Map != nil {
err = json.NewEncoder(&buf).Encode(m.toIntermediate())
} else {
err = json.NewEncoder(&buf).Encode(nil)
}
return buf.Bytes(), err
}
// UnmarshalJSON implements json.Unmarshaler
func (m *LatestMap) UnmarshalJSON(input []byte) error {
in := map[string]LatestEntry{}
if err := json.NewDecoder(bytes.NewBuffer(input)).Decode(&in); err != nil {
return err
}
*m = LatestMap{}.fromIntermediate(in)
return nil
}
// GobEncode implements gob.Marshaller
func (m LatestMap) GobEncode() ([]byte, error) {
buf := bytes.Buffer{}
err := gob.NewEncoder(&buf).Encode(m.toIntermediate())
return buf.Bytes(), err
}
// GobDecode implements gob.Unmarshaller
func (m *LatestMap) GobDecode(input []byte) error {
in := map[string]LatestEntry{}
if err := gob.NewDecoder(bytes.NewBuffer(input)).Decode(&in); err != nil {
return err
}
*m = LatestMap{}.fromIntermediate(in)
return nil
}