-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvv.go
219 lines (189 loc) · 3.81 KB
/
vv.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 rdx
import (
"errors"
"slices"
"github.com/drpcorg/chotki/protocol"
)
// VV is a version vector, max ids seen from each known replica.
type VV map[uint64]uint64
func (vv VV) Get(src uint64) (pro uint64) {
return vv[src]
}
// Set the progress for the specified source
func (vv VV) Set(src, pro uint64) {
vv[src] = pro
}
// Put the Src-pro pair to the VV, returns whether it was
// unseen (i.e. made any difference)
func (vv VV) Put(src, pro uint64) bool {
pre, ok := vv[src]
if ok && pre >= pro {
return false
}
vv[src] = pro
return true
}
// Adds the id to the VV, returns whether it was unseen
func (vv VV) PutID(id ID) bool {
return vv.Put(id.Src(), id.Pro())
}
var ErrSeen = errors.New("previously seen id")
var ErrGap = errors.New("id sequence gap")
// Whether this VV overlaps with another one (have common non-zero entry)
func (vv VV) ProgressedOver(b VV) bool {
for src, pro := range vv {
bpro, ok := b[src]
if !ok || pro > bpro {
return true
}
}
return false
}
func (vv VV) InterestOver(b VV) VV {
ahead := make(VV)
for src, pro := range vv {
bpro, ok := b[src]
if !ok || pro > bpro {
ahead[src] = bpro
}
}
return ahead
}
func (vv VV) IDs() (ids []ID) {
for src, pro := range vv {
ids = append(ids, IDfromSrcPro(src, pro))
}
slices.Sort(ids)
return
}
// TLV Vv record, nil for empty
func (vv VV) TLV() (ret []byte) {
ids := vv.IDs()
for _, id := range ids {
ret = protocol.Append(ret, 'V', id.ZipBytes())
}
return
}
const ( // todo
VvSeen = -1
VvNext = 0
VvGap = 1
)
var ErrBadVRecord = errors.New("bad V record")
var ErrBadV0Record = errors.New("bad V0 record")
// consumes: Vv record
func (vv VV) PutTLV(rec []byte) (err error) {
rest := rec
for len(rest) > 0 {
var val []byte
val, rest, err = protocol.TakeWary('V', rest)
if err != nil {
break
}
id := IDFromZipBytes(val)
vv.PutID(id)
}
return
}
func VValid(tlv []byte) bool {
for len(tlv) > 0 {
var body []byte
body, tlv = protocol.Take('V', tlv)
if body == nil || len(body) > 16 { //todo valid pair len
return false
}
}
return true
}
func (vv VV) Seen(bb VV) bool {
for src, pro := range bb {
seq := vv[src]
if pro > seq {
return false
}
}
return true
}
func (vv VV) GetID(src uint64) ID {
return IDfromSrcPro(src, vv[src])
}
func (vv VV) String() string {
ids := vv.IDs()
ret := make([]byte, 0, len(vv)*32)
for i := 0; i+1 < len(ids); i++ {
ret = append(ret, ids[i].String()...)
ret = append(ret, ',')
}
if len(ids) > 0 {
ret = append(ret, ids[len(ids)-1].String()...)
}
return string(ret)
}
func VVFromString(vvs string) (vv VV) {
vv = make(VV)
rest := []byte(vvs)
var id ID
for len(rest) > 0 && id != BadId {
id, rest = readIDFromString(rest)
vv.PutID(id)
}
return
}
func VVFromTLV(tlv []byte) (vv VV) {
vv = make(VV)
_ = vv.PutTLV(tlv)
return
}
func Vstring(tlv []byte) (txt string) {
vv := make(VV)
_ = vv.PutTLV(tlv)
return vv.String()
}
func Vparse(txt string) (tlv []byte) {
vv := VVFromString(txt)
return Vtlv(vv)
}
func Vtlv(vv VV) (tlv []byte) {
return vv.TLV()
}
func Vplain(tlv []byte) VV {
return VVFromTLV(tlv)
}
func Vmerge(tlvs [][]byte) (tlv []byte) {
vv := make(VV)
for _, v := range tlvs {
_ = vv.PutTLV(v)
}
tlv = vv.TLV()
return
}
func VVdelta(old VV, new_val VV) (tlv_delta []byte) {
delta := make(VV)
for src, pro := range new_val {
pre := old[src]
if pro != pre {
delta.Put(src, pro)
}
}
return delta.TLV()
}
func Vdelta(tlv []byte, new_val VV) (tlv_delta []byte) {
old := make(VV)
_ = old.PutTLV(tlv)
return VVdelta(old, new_val)
}
func Vvalid(tlv []byte) bool {
return VValid(tlv)
}
func Vdiff(tlv []byte, vvdiff VV) (diff_tlv []byte) {
old := make(VV)
diff := make(VV)
_ = old.PutTLV(tlv)
for src, pro := range old {
pre, ok := vvdiff[src]
if ok && pro > pre {
diff[src] = pro
}
}
return diff.TLV()
}