-
Notifications
You must be signed in to change notification settings - Fork 1
/
id.go
267 lines (222 loc) · 5.33 KB
/
id.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
package rdx
import (
"encoding/binary"
"errors"
"strconv"
"github.com/drpcorg/chotki/protocol"
)
/*
ID is an 64-bit locator/identifier.
This is NOT a Lamport timestamp (need more bits for that).
This is *log time*, not *logical time*.
0...............16..............32..............48.............64
+-------+-------+-------+-------+-------+-------+-------+-------
|offset(12)||......sequence.(32.bits)......|..source.(20.bits)..|
|...........progress.(44.bits).............|....................|
*/
type ID uint64
const ID0 ID = 0
const SeqBits = 32
const OffBits = 12
const SrcBits = 20
const ProBits = SeqBits + OffBits
const ProMask = uint64(uint64(1)<<ProBits) - 1
const OffMask = ID(1<<OffBits) - 1
const ProInc = ID(1 << OffBits)
const MaxSrc = (1 << SrcBits) - 1
const SeqOne = 1 << OffBits
const BadId = ID(0xffffffffffffffff)
const ZeroId = ID(0)
func IDfromSrcPro(src, pro uint64) ID {
return ID((src << ProBits) | pro)
}
func IDFromSrcSeqOff(src uint64, seq uint64, off uint16) ID {
ret := uint64(src)
ret <<= SeqBits
ret |= uint64(seq)
ret <<= OffBits
ret |= uint64(off)
return ID(ret)
}
func SrcSeqOff(id ID) (src uint64, seq uint64, off uint16) {
off = uint16(id & OffMask)
id >>= OffBits
seq = uint64(id)
id >>= SeqBits
src = uint64(id)
return
}
func (id ID) ZeroOff() ID {
return id & ID(^OffMask)
}
// Seq is the op sequence number (each replica generates its own
// sequence numbers)
func (id ID) Seq() uint64 {
i := uint64(id)
return (i & ProMask) >> OffBits
}
func (id ID) Pro() uint64 {
i := uint64(id)
return i & ProMask
}
func (id ID) Off() uint64 {
return uint64(id & OffMask)
}
func (id ID) ToOff(newoff uint64) ID {
return (id & ^OffMask) | (ID(newoff) & OffMask)
}
// Src is the replica id. That is normally a small number.
func (id ID) Src() uint64 {
return uint64(id >> ProBits)
}
func (id ID) Bytes() []byte {
var ret [8]byte
binary.BigEndian.PutUint64(ret[:], uint64(id))
return ret[:]
}
func IDFromBytes(by []byte) ID {
return ID(binary.BigEndian.Uint64(by))
}
func (id ID) ZipBytes() []byte {
return ZipUint64Pair(id.Pro(), id.Src())
}
func IDFromZipBytes(zip []byte) ID {
big, lil := UnzipUint64Pair(zip) // todo range check
return ID(big | (lil << ProBits))
}
func (id ID) Feed(into []byte) (res []byte) {
pair := id.ZipBytes()
res = protocol.AppendHeader(into, 'I', len(pair))
res = append(res, pair...)
return res
}
func (id *ID) Drain(from []byte) (rest []byte) { // FIXME
body, rest := protocol.Take('I', from)
seq, orig := UnzipUint64Pair(body)
*id = ID((seq << SrcBits) | orig)
return rest
}
var ErrBadId = errors.New("not an expected id")
func TakeIDWary(lit byte, pack []byte) (id ID, rest []byte, err error) {
idbytes, rest := protocol.Take(lit, pack)
if idbytes == nil {
err = ErrBadId
} else {
id = IDFromZipBytes(idbytes)
}
return
}
func (id ID) String() string {
var buf [64]byte
b := buf[:0]
b = strconv.AppendInt(b, int64(id.Src()), 16)
b = append(b, '-')
b = strconv.AppendInt(b, int64(id.Seq()), 16)
if off := id.Off(); off != 0 {
b = append(b, '-')
b = strconv.AppendInt(b, int64(off), 16)
}
return string(b)
}
const Hex = "0123456789abcdef"
const Hex583Len = 18
func Parse583Off(hex583 []byte) (off uint16) {
return uint16(UnHex(hex583[len(hex583)-3:]))
}
func (id ID) Hex583() []byte {
hex := []byte{'0', '0', '0', '0', '0', '-', '0', '0', '0', '0', '0', '0', '0', '0', '-', '0', '0', '0'}
k := Hex583Len - 1
u := uint64(id)
for k > 14 {
hex[k] = Hex[u&0xf]
u >>= 4
k--
}
k--
for k > 5 {
hex[k] = Hex[u&0xf]
u >>= 4
k--
}
k--
for k >= 0 {
hex[k] = Hex[u&0xf]
u >>= 4
k--
}
return hex
}
func (id ID) String583() string {
return string(id.Hex583())
}
func (id ID) UInt64() uint64 { return uint64(id) }
func UnHex(hex []byte) (num uint64) {
for len(hex) > 0 {
c := hex[0]
if c >= '0' && c <= '9' {
num = (num << 4) | uint64(c-'0')
} else if c >= 'A' && c <= 'F' {
num = (num << 4) | uint64(10+c-'A')
} else if c >= 'a' && c <= 'f' {
num = (num << 4) | uint64(10+c-'a')
} else {
break
}
hex = hex[1:]
}
return
}
func IDFromBracketedString(bid []byte) ID {
if len(bid) < 7 || bid[0] != '{' || bid[len(bid)-1] != '}' {
return BadId
}
return IDFromText(bid[1 : len(bid)-1])
}
func IDFromString(idstr string) (parsed ID) {
parsed, _ = readIDFromString([]byte(idstr))
return
}
func IDFromText(idstr []byte) (parsed ID) {
parsed, _ = readIDFromString(idstr)
return
}
func readIDFromString(idstr []byte) (ID, []byte) {
var parts [3]uint64
i, p := 0, 0
for i < len(idstr) && p < 3 {
c := idstr[i]
if c >= '0' && c <= '9' {
parts[p] = (parts[p] << 4) | uint64(c-'0')
} else if c >= 'A' && c <= 'F' {
parts[p] = (parts[p] << 4) | uint64(10+c-'A')
} else if c >= 'a' && c <= 'f' {
parts[p] = (parts[p] << 4) | uint64(10+c-'a')
} else if c == '-' {
p++
} else {
break
}
i++
}
rest := idstr[i:]
switch p {
case 0: // off
parts[2] = parts[0]
parts[0] = 0
case 1: // Src-seq
case 2: // Src-seq-off
case 3:
return BadId, rest
}
if parts[1] > 0xffffffff || parts[2] > 0xfff || parts[0] > 0xfffff {
return BadId, rest
}
return IDFromSrcSeqOff(parts[0], parts[1], uint16(parts[2])), rest
}
func readIDFromTLV(tlv []byte) (ID, []byte) { //nolint:golint,unused
lit, body, rest := protocol.TakeAny(tlv)
if lit == '-' || lit == 0 {
return BadId, nil
}
return IDFromZipBytes(body), rest
}