-
Notifications
You must be signed in to change notification settings - Fork 6
/
namespace.go
525 lines (400 loc) · 10.1 KB
/
namespace.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package crdt
import (
"fmt"
"sort"
"github.com/johnny-morrice/godless/crypto"
"github.com/johnny-morrice/godless/log"
"github.com/pkg/errors"
)
type TableName string
type RowName string
type EntryName string
type byTableName []TableName
func (by byTableName) Len() int {
return len(by)
}
func (by byTableName) Swap(i, j int) {
by[i], by[j] = by[j], by[i]
}
func (by byTableName) Less(i, j int) bool {
return by[i] < by[j]
}
func JoinStreamEntries(stream []NamespaceStreamEntry) ([]NamespaceStreamEntry, []InvalidNamespaceEntry) {
ns, readInvalid := ReadNamespaceStream(stream)
stream, makeInvalid := MakeNamespaceStream(ns)
invalidEntries := append(readInvalid, makeInvalid...)
return stream, invalidEntries
}
func FilterSignedEntries(stream []NamespaceStreamEntry, keys []crypto.PublicKey) ([]NamespaceStreamEntry, []InvalidNamespaceEntry) {
unsigned, readInvalid := ReadNamespaceStream(stream)
signed := unsigned.FilterVerified(keys)
signedStream, makeInvalid := MakeNamespaceStream(signed)
invalidEntries := append(readInvalid, makeInvalid...)
return signedStream, invalidEntries
}
// Semi-lattice type that implements our storage
type Namespace struct {
Tables map[TableName]Table
}
func EmptyNamespace() Namespace {
return MakeNamespace(map[TableName]Table{})
}
func MakeNamespace(tables map[TableName]Table) Namespace {
out := Namespace{
Tables: map[TableName]Table{},
}
for k, v := range tables {
out.Tables[k] = v
}
return out
}
func (ns Namespace) FilterVerified(keys []crypto.PublicKey) Namespace {
verified := EmptyNamespace()
ns.ForeachEntry(func(t TableName, r RowName, e EntryName, entry Entry) {
signed := entry.FilterVerified(keys)
verified.addEntry(t, r, e, signed)
})
return verified
}
func (ns Namespace) addEntry(t TableName, r RowName, e EntryName, entry Entry) {
table := EmptyTable()
row := EmptyRow()
row.addEntry(e, entry)
table.addRow(r, row)
ns.addTable(t, table)
}
// Strip removes empty tables and rows that would not be saved to the backing store.
func (ns Namespace) Strip() (Namespace, []InvalidNamespaceEntry) {
const failMsg = "Namespace.Strip failed"
stream, invalid := MakeNamespaceStream(ns)
invalidCount := len(invalid)
if invalidCount > 0 {
log.Warn("Stripped %d invalid points", invalidCount)
}
namespace, invalid := ReadNamespaceStream(stream)
return namespace, invalid
}
func (ns Namespace) GetTableNames() []TableName {
tableNames := make([]TableName, 0, len(ns.Tables))
for name := range ns.Tables {
tableNames = append(tableNames, name)
}
sort.Sort(byTableName(tableNames))
return tableNames
}
// The batch should correspond to a single point.
func (ns Namespace) addPointBatch(stream []NamespaceStreamEntry) ([]InvalidNamespaceEntry, error) {
const failMsg = "Namespace.addPointBatch failed"
if len(stream) == 0 {
log.Warn("Namespace.addPointBatch of length 0")
return nil, nil
}
point, invalid, err := readStreamPoint(stream)
if err != nil {
return invalid, errors.Wrap(err, failMsg)
}
first := stream[0]
tableName := first.Table
rowName := first.Row
entryName := first.Entry
ns.addPoint(tableName, rowName, entryName, point)
return invalid, nil
}
func (ns Namespace) addStreamEntry(entry NamespaceStreamEntry) error {
const failMsg = "Namespace.addStreamEntry failed"
var sigs []crypto.Signature
if !crypto.IsNilSignature(entry.Point.Signature) {
sig, err := crypto.ParseSignature(entry.Point.Signature)
if err != nil {
return errors.Wrap(err, failMsg)
}
sigs = []crypto.Signature{sig}
}
point := PresignedPoint(entry.Point.Text, sigs)
ns.addPoint(entry.Table, entry.Row, entry.Entry, point)
return nil
}
func (ns Namespace) addPoint(tableName TableName, rowName RowName, entryName EntryName, point Point) {
table := MakeTable(map[RowName]Row{
rowName: MakeRow(map[EntryName]Entry{
entryName: MakeEntry([]Point{point}),
}),
})
ns.addTable(tableName, table)
}
func (ns Namespace) IsEmpty() bool {
return len(ns.Tables) == 0
}
func (ns Namespace) Copy() Namespace {
cpy := EmptyNamespace()
for k, table := range ns.Tables {
cpy.Tables[k] = table.Copy()
}
return cpy
}
func (ns Namespace) JoinNamespace(other Namespace) Namespace {
joined := ns.Copy()
for otherk, otherTable := range other.Tables {
joined.addTable(otherk, otherTable)
}
return joined
}
func (ns Namespace) addTable(key TableName, table Table) {
current, present := ns.Tables[key]
if present {
current.addTable(table)
} else {
ns.Tables[key] = table
}
}
func (ns Namespace) JoinTable(key TableName, table Table) Namespace {
joined := ns.Copy()
joined.addTable(key, table)
return joined
}
func (ns Namespace) GetTable(key TableName) (Table, error) {
if table, present := ns.Tables[key]; present {
return table, nil
} else {
return Table{}, fmt.Errorf("No such Table in Namespace '%s'", key)
}
}
func (ns Namespace) Equals(other Namespace) bool {
if len(ns.Tables) != len(other.Tables) {
return false
}
for k, v := range ns.Tables {
otherv, present := other.Tables[k]
if !present {
return false
}
if !v.Equals(otherv) {
return false
}
}
return true
}
// Iterate through all entries
func (ns Namespace) ForeachEntry(f func(t TableName, r RowName, e EntryName, entry Entry)) {
ns.ForeachRow(func(t TableName, r RowName, row Row) {
for e, entry := range row.Entries {
f(t, r, e, entry)
}
})
}
func (ns Namespace) ForeachRow(f func(t TableName, r RowName, row Row)) {
for tableName, table := range ns.Tables {
for rowName, row := range table.Rows {
f(tableName, rowName, row)
}
}
}
// TODO improved type validation
type Table struct {
Rows map[RowName]Row
}
func EmptyTable() Table {
return MakeTable(map[RowName]Row{})
}
func MakeTable(rows map[RowName]Row) Table {
out := Table{
Rows: map[RowName]Row{},
}
for k, v := range rows {
out.Rows[k] = v
}
return out
}
func (t Table) ForeachRow(f func(rowName RowName, r Row)) {
for k, r := range t.Rows {
f(k, r)
}
}
func (t Table) ForeachEntry(f func(rowName RowName, entryName EntryName, entry Entry)) {
for rowName, row := range t.Rows {
for entryName, entry := range row.Entries {
f(rowName, entryName, entry)
}
}
}
func (t Table) Copy() Table {
cpy := EmptyTable()
for k, row := range t.Rows {
cpy.Rows[k] = row.Copy()
}
return cpy
}
func (t Table) AllRows() []Row {
rows := []Row{}
for _, v := range t.Rows {
rows = append(rows, v)
}
return rows
}
func (t Table) JoinTable(other Table) Table {
joined := t.Copy()
joined.addTable(other)
return joined
}
func (t Table) addTable(other Table) {
for otherk, otherRow := range other.Rows {
t.addRow(otherk, otherRow)
}
}
func (t Table) JoinRow(rowKey RowName, row Row) Table {
joined := t.Copy()
joined.addRow(rowKey, row)
return joined
}
func (t Table) GetRow(rowKey RowName) (Row, error) {
if row, present := t.Rows[rowKey]; present {
return row, nil
} else {
return Row{}, fmt.Errorf("No such Row in Table '%s'", rowKey)
}
}
// Destructive.
func (t Table) addRow(rowKey RowName, row Row) {
if current, present := t.Rows[rowKey]; present {
current.addRow(row)
} else {
t.Rows[rowKey] = row
}
}
func (t Table) Equals(other Table) bool {
if len(t.Rows) != len(other.Rows) {
return false
}
for k, v := range t.Rows {
otherv, present := other.Rows[k]
if !present {
return false
}
if !v.Equals(otherv) {
return false
}
}
return true
}
type Row struct {
Entries map[EntryName]Entry
}
func EmptyRow() Row {
return MakeRow(map[EntryName]Entry{})
}
func MakeRow(entries map[EntryName]Entry) Row {
out := Row{
Entries: map[EntryName]Entry{},
}
for k, v := range entries {
out.Entries[k] = v
}
return out
}
func (row Row) ForeachEntry(f func(entryName EntryName, entry Entry)) {
for name, entry := range row.Entries {
f(name, entry)
}
}
func (row Row) Copy() Row {
cpy := Row{Entries: map[EntryName]Entry{}}
for k, entry := range row.Entries {
cpy.Entries[k] = entry.Copy()
}
return cpy
}
func (row Row) addRow(other Row) {
for otherk, otherEntry := range other.Entries {
if joinEntry, present := row.Entries[otherk]; present {
row.Entries[otherk] = joinEntry.JoinEntry(otherEntry)
} else {
row.Entries[otherk] = otherEntry
}
}
}
func (row Row) JoinRow(other Row) Row {
joined := row.Copy()
joined.addRow(other)
return joined
}
func (row Row) GetEntry(entryKey EntryName) (Entry, error) {
if entry, present := row.Entries[entryKey]; present {
return entry, nil
} else {
return Entry{}, fmt.Errorf("No such Entry in Row '%s'", entryKey)
}
}
func (row Row) addEntry(entryKey EntryName, other Entry) {
if entry, present := row.Entries[entryKey]; present {
row.Entries[entryKey] = entry.JoinEntry(other)
} else {
row.Entries[entryKey] = other
}
}
func (row Row) JoinEntry(entryKey EntryName, entry Entry) Row {
cpy := row.Copy()
cpy.addEntry(entryKey, entry)
return cpy
}
func (row Row) Equals(other Row) bool {
if len(row.Entries) != len(other.Entries) {
return false
}
for k, v := range row.Entries {
otherv, present := other.Entries[k]
if !present {
return false
}
if !v.Equals(otherv) {
return false
}
}
return true
}
type Entry struct {
Set []Point
}
func EmptyEntry() Entry {
return MakeEntry([]Point{})
}
func MakeEntry(set []Point) Entry {
sort.Sort(byPointValue(set))
undupes := uniqPointSorted(set)
return Entry{Set: undupes}
}
func (e Entry) FilterVerified(keys []crypto.PublicKey) Entry {
verified := make([]Point, 0, len(e.Set))
for _, p := range e.Set {
if p.IsVerifiedByAny(keys) {
verified = append(verified, p)
}
}
verifiedEntry := Entry{Set: verified}
return verifiedEntry
}
func (e Entry) Copy() Entry {
return MakeEntry(e.Set)
}
func (e Entry) JoinEntry(other Entry) Entry {
return MakeEntry(append(e.Set, other.Set...))
}
func (e Entry) Equals(other Entry) bool {
// Easy because Entry.set is deduplicated and sorted
if len(e.Set) != len(other.Set) {
return false
}
for i, myPoint := range e.Set {
theirPoint := other.Set[i]
if !myPoint.Equals(theirPoint) {
return false
}
}
return true
}
func (e Entry) GetValues() []Point {
cpy := make([]Point, len(e.Set))
for i, p := range e.Set {
cpy[i] = p
}
return cpy
}