-
Notifications
You must be signed in to change notification settings - Fork 1
/
FIRST.go
541 lines (468 loc) · 11.7 KB
/
FIRST.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package rdx
import (
"bytes"
"errors"
"fmt"
"strconv"
"github.com/drpcorg/chotki/protocol"
)
// Common LWW functions
// for bad format, value==nil (an empty value is an empty slice)
func ParseFIRST(bulk []byte) (rev int64, src uint64, value []byte) {
lit, hlen, blen := protocol.ProbeHeader(bulk)
if lit != 'T' && lit != '0' || hlen+blen > len(bulk) {
return
}
tsb := bulk[hlen : hlen+blen]
rev, src = UnzipIntUint64Pair(tsb)
value = bulk[hlen+blen:]
return
}
// same as ParseFIRST, but the Rev number is zigzagged
func FIRSTparsez(bulk []byte) (zrev uint64, src uint64, value []byte) {
lit, hlen, blen := protocol.ProbeHeader(bulk)
if lit != 'T' && lit != '0' || hlen+blen > len(bulk) {
return
}
tsb := bulk[hlen : hlen+blen]
zrev, src = UnzipUint64Pair(tsb)
value = bulk[hlen+blen:]
return
}
// Parses an enveloped FIRST record
func ParseEnvelopedFIRST(data []byte) (lit byte, t Time, value, rest []byte, err error) {
var hlen, blen int
lit, hlen, blen = protocol.ProbeHeader(data)
if lit == 0 || hlen+blen > len(data) {
err = protocol.ErrIncomplete
return
}
rec := data[hlen : hlen+blen]
rest = data[hlen+blen:]
tlit, thlen, tblen := protocol.ProbeHeader(rec)
tlen := thlen + tblen
if (tlit != 'T' && tlit != '0') || (tlen > len(rec)) {
err = ErrBadFIRST
return
}
tsb := rec[thlen:tlen]
t.Rev, t.Src = UnzipIntUint64Pair(tsb)
value = rec[tlen:]
return
}
func IsFirst(c byte) bool {
return c == Float || c == Integer || c == Reference || c == String || c == Term
}
func FIRSTtlv(rev int64, src uint64, value []byte) (bulk []byte) {
time := ZipIntUint64Pair(rev, src)
bulk = make([]byte, 0, len(time)+len(value)+2)
bulk = protocol.AppendTiny(bulk, 'T', time)
bulk = append(bulk, value...)
return
}
func FIRSTdefault(rdt byte) []byte {
return FIRSTtlv(0, 0, []byte{})
}
func MergeFIRST(tlvs [][]byte) (tlv []byte) {
var winrec []byte
var winrev int64
var winsrc uint64
var winhlbl int
for _, rec := range tlvs {
l, hlen, blen := protocol.ProbeHeader(rec)
hlbl := hlen + blen
tsb := rec[hlen:hlbl]
rev, src := UnzipIntUint64Pair(tsb)
if rev < 0 {
rev = -rev
}
if l != 'T' && l != '0' {
continue
}
if rev < winrev {
continue
}
if rev == winrev {
valtie := bytes.Compare(rec[hlbl:], winrec[winhlbl:])
if valtie < 0 {
continue
}
if valtie == 0 && src < winsrc {
continue
}
}
winrev = rev
winrec = rec
winsrc = src
winhlbl = hlbl
}
return winrec
}
func DiffFIRST(tlv []byte, vvdiff VV) []byte {
_, src, _ := ParseFIRST(tlv)
_, ok := vvdiff[src]
if ok {
return tlv
} else {
return nil
}
}
var ErrBadPacket = errors.New("bad packet")
func SetTimeFIRST(bare []byte, t Time) (res []byte) {
_, _, value := ParseFIRST(bare)
res = FIRSTtlv(t.Rev, t.Src, value)
return
}
func SetSourceFIRST(bare []byte, src uint64) (res []byte, err error) {
time, s, value := ParseFIRST(bare)
if value == nil {
return nil, ErrBadPacket
}
if s != src { // ensure correct attribution
res = FIRSTtlv(time, src, value)
} else {
res = bare
}
return
}
type FIRSTIterator struct {
TLV []byte
one []byte
bare []byte
val []byte
src uint64
revz uint64
lit byte
}
func (a *FIRSTIterator) ParsedValue() (rdt byte, time Time, value []byte) {
return a.lit, Time{ZagZigUint64(a.revz), a.src}, a.val
}
func (a *FIRSTIterator) Next() bool {
if len(a.TLV) == 0 {
return false
}
var hlen, blen, rlen int
a.lit, hlen, blen = protocol.ProbeHeader(a.TLV)
rlen = hlen + blen
if a.lit < 'A' || len(a.TLV) < rlen {
return false
}
a.bare = a.TLV[hlen:rlen]
a.revz, a.src, a.val = FIRSTparsez(a.bare)
a.one = a.TLV[:rlen]
a.TLV = a.TLV[rlen:]
return true
}
func (a *FIRSTIterator) Value() []byte {
return a.one
}
func FIRSTrdx2tlv(a *RDX) (tlv []byte) {
if a == nil || !a.FIRST() {
return nil
}
str := string(a.Text)
switch a.RdxType {
case Float:
tlv = Fparse(str)
case Integer:
tlv = Iparse(str)
case Reference:
tlv = Rparse(str)
case String:
tlv = Sparse(str)
case Term:
tlv = Tparse(str)
case Natural:
tlv = Nparse(str)
case ZCounter:
tlv = Zparse(str)
default:
return nil
}
return
}
func FIRSTrdxs2tlv(a []RDX) (tlv []byte) {
for i := 0; i < len(a); i++ {
tlv = append(tlv, FIRSTrdx2tlv(&a[i])...)
}
return
}
func FIRSTrdxs2tlvs(a []RDX) (tlv protocol.Records) {
for i := 0; i < len(a); i++ {
if !a[i].FIRST() {
return nil
}
first := protocol.Record(a[i].RdxType, FIRSTrdx2tlv(&a[i]))
tlv = append(tlv, first)
}
return
}
func FIRSTcompare(a, b []byte) int {
alit, ahlen, ablen := protocol.ProbeHeader(a)
blit, bhlen, bblen := protocol.ProbeHeader(b)
if alit != blit {
return int(alit) - int(blit)
}
_, _, av := FIRSTparsez(a[ahlen : ahlen+ablen])
_, _, bv := FIRSTparsez(b[bhlen : bhlen+bblen])
return bytes.Compare(av, bv)
}
// I is a last-write-wins int64
func Idefault() []byte { return FIRSTdefault('I') }
// produce a text form (for REPL mostly)
func Istring(tlv []byte) (txt string) {
return fmt.Sprintf("%d", Inative(tlv))
}
// parse a text form into a TLV value
func Iparse(txt string) (tlv []byte) {
i, err := strconv.ParseInt(txt, 10, 64)
if err != nil {
return nil
}
return Itlv(i)
}
// convert native golang value into a TLV form
func Itlv(i int64) (tlv []byte) {
return FIRSTtlv(0, 0, ZipInt64(i))
}
// Enveloped I TLV
func Itlve(rev int64, src uint64, inc int64) []byte {
return protocol.Record('I',
protocol.TinyRecord('T', ZipIntUint64Pair(rev, src)),
ZipInt64(inc),
)
}
// convert a TLV value to a native golang value
func Inative(tlv []byte) int64 {
_, _, val := ParseFIRST(tlv)
return UnzipInt64(val)
}
// merge TLV values
func Imerge(tlvs [][]byte) (tlv []byte) {
return MergeFIRST(tlvs)
}
// produce an op that turns the old value into the new one
func Idelta(tlv []byte, new_val int64, clock Clock) (tlv_delta []byte) {
rev, _, val := ParseFIRST(tlv)
if rev < 0 {
rev = -rev
}
nv := ZipInt64(new_val)
if !bytes.Equal(val, nv) {
tlv_delta = FIRSTtlv(rev+1, 0, nv)
}
return
}
// checks a TLV value for validity (format violations)
func Ivalid(tlv []byte) bool {
_, src, val := ParseFIRST(tlv)
return val != nil && len(val) <= 8 && src <= MaxSrc
}
func Idiff(tlv []byte, vvdiff VV) []byte {
return DiffFIRST(tlv, vvdiff)
}
// S is a last-write-wins UTF-8 string
const hex = "0123456789abcdef"
// produce a text form (for REPL mostly)
func Sstring(tlv []byte) (txt string) {
dst := make([]byte, 0, len(tlv)*2)
_, _, val := ParseFIRST(tlv)
dst = append(dst, '"')
for _, b := range val {
switch b {
case '\\', '"':
dst = append(dst, '\\', b)
case '\n':
dst = append(dst, '\\', 'n')
case '\r':
dst = append(dst, '\\', 'r')
case '\t':
dst = append(dst, '\\', 't')
case 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0xb, 0xc, 0xe, 0xf,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a,
0x1b, 0x1c, 0x1d, 0x1e, 0x1f:
dst = append(dst, '\\', 'u', '0', '0', hex[b>>4], hex[b&0xF])
default:
dst = append(dst, b)
}
}
dst = append(dst, '"')
return string(dst)
}
func Sdefault() []byte { return FIRSTdefault('S') }
func Sparset(txt string, t Time) (tlv []byte) {
if len(txt) < 2 || txt[0] != '"' || txt[len(txt)-1] != '"' {
return nil
}
unq := txt[1 : len(txt)-1]
unesc, _ := Unescape([]byte(unq), nil)
return FIRSTtlv(t.Rev, t.Src, unesc)
}
// parse a text form into a TLV value; nil on error
func Sparse(txt string) (tlv []byte) {
return Sparset(txt, Time{0, 0})
}
// convert native golang value into a TLV form
func Stlv(s string) (tlv []byte) {
return FIRSTtlv(0, 0, []byte(s))
}
// convert a TLV value to a native golang value
func Snative(tlv []byte) string {
_, _, val := ParseFIRST(tlv)
return string(val)
}
// merge TLV values
func Smerge(tlvs [][]byte) (tlv []byte) {
return MergeFIRST(tlvs)
}
// produce an op that turns the old value into the new one
func Sdelta(tlv []byte, new_val string, clock Clock) (tlv_delta []byte) {
rev, _, val := ParseFIRST(tlv)
if rev < 0 {
rev = -rev
}
nv := []byte(new_val)
if !bytes.Equal(val, nv) {
tlv_delta = FIRSTtlv(rev+1, 0, nv)
}
return
}
// checks a TLV value for validity (format violations)
func Svalid(tlv []byte) bool {
_, src, val := ParseFIRST(tlv)
return val != nil && src <= MaxSrc
}
func Sdiff(tlv []byte, vvdiff VV) []byte {
return DiffFIRST(tlv, vvdiff)
}
// R is a last-write-wins ID
// produce a text form (for REPL mostly)
func Rstring(tlv []byte) (txt string) {
return Rnative(tlv).String()
}
// parse a text form into a TLV value
func Rparse(txt string) (tlv []byte) {
id := IDFromString(txt)
return Rtlv(id)
}
// convert native golang value into a TLV form
func Rtlv(i ID) (tlv []byte) {
return FIRSTtlv(0, 0, i.ZipBytes())
}
func Rdefault() []byte { return FIRSTdefault('R') }
// convert a TLV value to a native golang value
func Rnative(tlv []byte) ID {
_, _, val := ParseFIRST(tlv)
return IDFromZipBytes(val)
}
// merge TLV values
func Rmerge(tlvs [][]byte) (tlv []byte) {
return MergeFIRST(tlvs)
}
// produce an op that turns the old value into the new one
func Rdelta(tlv []byte, new_val ID, clock Clock) (tlv_delta []byte) {
rev, _, val := ParseFIRST(tlv)
if rev < 0 {
rev = -rev
}
nv := new_val.ZipBytes()
if !bytes.Equal(val, nv) {
tlv_delta = FIRSTtlv(rev+1, 0, nv)
}
return
}
// checks a TLV value for validity (format violations)
func Rvalid(tlv []byte) bool {
_, src, val := ParseFIRST(tlv)
return val != nil && src <= MaxSrc
// todo correct sizes
}
func Rdiff(tlv []byte, vvdiff VV) []byte {
return DiffFIRST(tlv, vvdiff)
}
// F is a last-write-wins float64
// produce a text form (for REPL mostly)
func Fstring(tlv []byte) (txt string) {
return fmt.Sprintf("%f", Fnative(tlv))
}
// parse a text form into a TLV value
func Fparse(txt string) (tlv []byte) {
var i float64
_, _ = fmt.Sscanf(txt, "%f", &i)
return Ftlv(i)
}
// convert native golang value into a TLV form
func Ftlv(i float64) (tlv []byte) {
return FIRSTtlv(0, 0, ZipFloat64(i))
}
// convert a TLV value to a native golang value
func Fnative(tlv []byte) float64 {
_, _, val := ParseFIRST(tlv)
return UnzipFloat64(val)
}
// merge TLV values
func Fmerge(tlvs [][]byte) (tlv []byte) {
return MergeFIRST(tlvs)
}
// produce an op that turns the old value into the new one
func Fdelta(tlv []byte, new_val float64, clock Clock) (tlv_delta []byte) {
rev, _, val := ParseFIRST(tlv)
if rev < 0 {
rev = -rev
}
nv := ZipFloat64(new_val)
if !bytes.Equal(val, nv) {
tlv_delta = FIRSTtlv(rev+1, 0, nv)
}
return
}
// checks a TLV value for validity (format violations)
func Fvalid(tlv []byte) bool {
_, src, val := ParseFIRST(tlv)
return val != nil && src <= MaxSrc && len(val) <= 8
}
func Fdiff(tlv []byte, vvdiff VV) []byte {
return DiffFIRST(tlv, vvdiff)
}
// I is a last-write-wins int64
// produce a text form (for REPL mostly)
func Tstring(tlv []byte) (txt string) {
_, _, term := FIRSTparsez(tlv)
if len(term) == 0 {
return "null"
} else {
return string(term)
}
}
// parse a text form into a TLV value
func Tparse(txt string) (tlv []byte) {
return Ttlv(txt)
}
// convert native golang value into a TLV form
func Ttlv(term string) (tlv []byte) {
return FIRSTtlv(0, 0, []byte(term))
}
// merge TLV values
func Tmerge(tlvs [][]byte) (tlv []byte) {
return MergeFIRST(tlvs)
}
// produce an op that turns the old value into the new one
func Tdelta(tlv []byte, clock Clock) (tlv_delta []byte) {
return nil
}
// checks a TLV value for validity (format violations)
func Tvalid(tlv []byte) bool {
_, src, val := ParseFIRST(tlv)
return val != nil && len(val) == 0 && src <= MaxSrc
}
func Tdiff(tlv []byte, vvdiff VV) []byte {
return DiffFIRST(tlv, vvdiff)
}
func Restamp(first []byte, time Time) (stamped []byte) {
it := FIRSTIterator{TLV: first}
stamp := protocol.TinyRecord('T', time.ZipBytes())
for it.Next() {
stamped = protocol.Append(stamped, it.lit, stamp, it.val)
}
return
}