-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverage.html
255 lines (224 loc) · 8.11 KB
/
coverage.html
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>crdt_v2: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">/home/jabb/repos/goodnotes/crdt_v2/clock.go (100.0%)</option>
<option value="file1">/home/jabb/repos/goodnotes/crdt_v2/lww_element_dict.go (100.0%)</option>
<option value="file2">/home/jabb/repos/goodnotes/crdt_v2/timed_value.go (85.7%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package crdt
type Clock interface {
Now() int64
}
var _ Clock = (*FakeClock)(nil)
type FakeClock struct {
time int64
}
func NewFakeClock() *FakeClock <span class="cov8" title="1">{
return &FakeClock{}
}</span>
func (c *FakeClock) SetTime(time int64) <span class="cov8" title="1">{
c.time = time
}</span>
func (c *FakeClock) Now() int64 <span class="cov8" title="1">{
return c.time
}</span>
</pre>
<pre class="file" id="file1" style="display: none">package crdt
type LWWElementDict struct {
id int
clock Clock
adds map[string]TimedValue
removes map[string]TimedValue
}
func NewLWWElementDict(id int, clock Clock) LWWElementDict <span class="cov8" title="1">{
return LWWElementDict{
id: id,
clock: clock,
adds: map[string]TimedValue{},
removes: map[string]TimedValue{},
}
}</span>
// Add-or-updates a key-value pair, and generates a new timestamp.
// Timestamps are assumed unique, totally ordered, consistent with casaul order, and monotonically increasing.
func (l LWWElementDict) Add(key string, value interface{}) <span class="cov8" title="1">{
timedValue := NewTimedValue(value, l.clock.Now())
if add, ok := l.adds[key]; ok && add.Compare(timedValue) >= 0 </span><span class="cov8" title="1">{ // consistent with causal order
return
}</span>
<span class="cov8" title="1">l.adds[key] = timedValue</span>
}
// Removes an existing key-value pair, and generates a new timestamp.
// Timestamps are assumed unique, totally ordered, consistent with casaul order, and monotonically increasing.
func (l LWWElementDict) Remove(key string) <span class="cov8" title="1">{
if !l.Lookup(key) </span><span class="cov8" title="1">{ // an element can only be removed if it is present
return
}</span>
<span class="cov8" title="1">timedValue := NewTimedValue(nil, l.clock.Now())
if remove, ok := l.removes[key]; ok && remove.Compare(timedValue) >= 0 </span><span class="cov8" title="1">{ // consistent with causal order
return
}</span>
<span class="cov8" title="1">l.removes[key] = timedValue</span>
}
// Lookup key-value pair by its key.
// The lookup is biased towards addition.
func (l LWWElementDict) Lookup(key string) bool <span class="cov8" title="1">{
add, ok := l.adds[key]
if !ok </span><span class="cov8" title="1">{
return false
}</span>
<span class="cov8" title="1">remove, ok := l.removes[key]
if ok && remove.Compare(add) > 0 </span><span class="cov8" title="1">{ // biased towards addition
return false
}</span>
<span class="cov8" title="1">return true</span>
}
// Returns the current value:
// A collection of key-value pairs without their underlying add-and-remove timestamps.
func (l LWWElementDict) Values() map[string]interface{} <span class="cov8" title="1">{
values := map[string]interface{}{}
for key, _ := range l.adds </span><span class="cov8" title="1">{
if l.Lookup(key) </span><span class="cov8" title="1">{
values[key] = l.adds[key].value
}</span>
}
<span class="cov8" title="1">return values</span>
}
// Merge commutes the values with the highest timestamp.
// On concurrrent add-or-update, the node with the lowest identifier (address) takes precedence.
// Therefore, concurrent add-or-update operations are deterministic.
// Merge is commutative, associative, and idempotent.
func (l LWWElementDict) Merge(other LWWElementDict) LWWElementDict <span class="cov8" title="1">{
dominant := l.id >= other.id
result := NewLWWElementDict(l.id, l.clock)
result.adds = merge(l.adds, other.adds, dominant)
result.removes = merge(l.removes, other.removes, dominant)
return result
}</span>
func merge(source map[string]TimedValue, replica map[string]TimedValue, dominant bool) map[string]TimedValue <span class="cov8" title="1">{
result := map[string]TimedValue{}
for key, value := range source </span><span class="cov8" title="1">{
result[key] = value
}</span>
<span class="cov8" title="1">for key, replicaValue := range replica </span><span class="cov8" title="1">{
sourceValue, ok := source[key]
if !ok </span>{<span class="cov8" title="1">
}</span> else<span class="cov8" title="1"> if sourceValue.Compare(replicaValue) > 0 ||
(sourceValue.Compare(replicaValue) == 0 && !dominant) </span><span class="cov8" title="1">{
continue</span>
}
<span class="cov8" title="1">result[key] = replicaValue</span>
}
<span class="cov8" title="1">return result</span>
}
</pre>
<pre class="file" id="file2" style="display: none">package crdt
import "fmt"
type TimedValue struct {
value interface{}
timestamp int64
}
func NewTimedValue(value interface{}, timestamp int64) TimedValue <span class="cov8" title="1">{
return TimedValue{value: value, timestamp: timestamp}
}</span>
func (v TimedValue) Compare(other TimedValue) int <span class="cov8" title="1">{
if v.timestamp == other.timestamp </span><span class="cov8" title="1">{
return 0
}</span>
<span class="cov8" title="1">if v.timestamp < other.timestamp </span><span class="cov8" title="1">{
return -1
}</span>
<span class="cov8" title="1">return +1</span>
}
func (v TimedValue) String() string <span class="cov0" title="0">{
return fmt.Sprintf("{value=%v, timestamp=%d}", v.value, v.timestamp)
}</span>
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>