forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.go
284 lines (256 loc) · 9.42 KB
/
number.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
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package codec
import (
"encoding/binary"
"math"
"github.com/juju/errors"
)
const signMask uint64 = 0x8000000000000000
// EncodeIntToCmpUint make int v to comparable uint type
func EncodeIntToCmpUint(v int64) uint64 {
return uint64(v) ^ signMask
}
// DecodeCmpUintToInt decodes the u that encoded by EncodeIntToCmpUint
func DecodeCmpUintToInt(u uint64) int64 {
return int64(u ^ signMask)
}
// EncodeInt appends the encoded value to slice b and returns the appended slice.
// EncodeInt guarantees that the encoded value is in ascending order for comparison.
func EncodeInt(b []byte, v int64) []byte {
var data [8]byte
u := EncodeIntToCmpUint(v)
binary.BigEndian.PutUint64(data[:], u)
return append(b, data[:]...)
}
// EncodeIntDesc appends the encoded value to slice b and returns the appended slice.
// EncodeIntDesc guarantees that the encoded value is in descending order for comparison.
func EncodeIntDesc(b []byte, v int64) []byte {
var data [8]byte
u := EncodeIntToCmpUint(v)
binary.BigEndian.PutUint64(data[:], ^u)
return append(b, data[:]...)
}
// DecodeInt decodes value encoded by EncodeInt before.
// It returns the leftover un-decoded slice, decoded value if no error.
func DecodeInt(b []byte) ([]byte, int64, error) {
if len(b) < 8 {
return nil, 0, errors.New("insufficient bytes to decode value")
}
u := binary.BigEndian.Uint64(b[:8])
v := DecodeCmpUintToInt(u)
b = b[8:]
return b, v, nil
}
// DecodeIntDesc decodes value encoded by EncodeInt before.
// It returns the leftover un-decoded slice, decoded value if no error.
func DecodeIntDesc(b []byte) ([]byte, int64, error) {
if len(b) < 8 {
return nil, 0, errors.New("insufficient bytes to decode value")
}
u := binary.BigEndian.Uint64(b[:8])
v := DecodeCmpUintToInt(^u)
b = b[8:]
return b, v, nil
}
// EncodeUint appends the encoded value to slice b and returns the appended slice.
// EncodeUint guarantees that the encoded value is in ascending order for comparison.
func EncodeUint(b []byte, v uint64) []byte {
var data [8]byte
binary.BigEndian.PutUint64(data[:], v)
return append(b, data[:]...)
}
// EncodeUintDesc appends the encoded value to slice b and returns the appended slice.
// EncodeUintDesc guarantees that the encoded value is in descending order for comparison.
func EncodeUintDesc(b []byte, v uint64) []byte {
var data [8]byte
binary.BigEndian.PutUint64(data[:], ^v)
return append(b, data[:]...)
}
// DecodeUint decodes value encoded by EncodeUint before.
// It returns the leftover un-decoded slice, decoded value if no error.
func DecodeUint(b []byte) ([]byte, uint64, error) {
if len(b) < 8 {
return nil, 0, errors.New("insufficient bytes to decode value")
}
v := binary.BigEndian.Uint64(b[:8])
b = b[8:]
return b, v, nil
}
// DecodeUintDesc decodes value encoded by EncodeInt before.
// It returns the leftover un-decoded slice, decoded value if no error.
func DecodeUintDesc(b []byte) ([]byte, uint64, error) {
if len(b) < 8 {
return nil, 0, errors.New("insufficient bytes to decode value")
}
data := b[:8]
v := binary.BigEndian.Uint64(data)
b = b[8:]
return b, ^v, nil
}
// EncodeVarint appends the encoded value to slice b and returns the appended slice.
// Note that the encoded result is not memcomparable.
func EncodeVarint(b []byte, v int64) []byte {
var data [binary.MaxVarintLen64]byte
n := binary.PutVarint(data[:], v)
return append(b, data[:n]...)
}
// DecodeVarint decodes value encoded by EncodeVarint before.
// It returns the leftover un-decoded slice, decoded value if no error.
func DecodeVarint(b []byte) ([]byte, int64, error) {
v, n := binary.Varint(b)
if n > 0 {
return b[n:], v, nil
}
if n < 0 {
return nil, 0, errors.New("value larger than 64 bits")
}
return nil, 0, errors.New("insufficient bytes to decode value")
}
// EncodeUvarint appends the encoded value to slice b and returns the appended slice.
// Note that the encoded result is not memcomparable.
func EncodeUvarint(b []byte, v uint64) []byte {
var data [binary.MaxVarintLen64]byte
n := binary.PutUvarint(data[:], v)
return append(b, data[:n]...)
}
// DecodeUvarint decodes value encoded by EncodeUvarint before.
// It returns the leftover un-decoded slice, decoded value if no error.
func DecodeUvarint(b []byte) ([]byte, uint64, error) {
v, n := binary.Uvarint(b)
if n > 0 {
return b[n:], v, nil
}
if n < 0 {
return nil, 0, errors.New("value larger than 64 bits")
}
return nil, 0, errors.New("insufficient bytes to decode value")
}
const (
negativeTagEnd = 8 // negative tag is (negativeTagEnd - length).
positiveTagStart = 0xff - 8 // Positive tag is (positiveTagStart + length).
)
// EncodeComparableVarint encodes an int64 to a mem-comparable bytes.
func EncodeComparableVarint(b []byte, v int64) []byte {
if v < 0 {
// All negative value has a tag byte prefix (negativeTagEnd - length).
// Smaller negative value encodes to more bytes, has smaller tag.
if v >= -0xff {
return append(b, negativeTagEnd-1, byte(v))
} else if v >= -0xffff {
return append(b, negativeTagEnd-2, byte(v>>8), byte(v))
} else if v >= -0xffffff {
return append(b, negativeTagEnd-3, byte(v>>16), byte(v>>8), byte(v))
} else if v >= -0xffffffff {
return append(b, negativeTagEnd-4, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
} else if v >= -0xffffffffff {
return append(b, negativeTagEnd-5, byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
} else if v >= -0xffffffffffff {
return append(b, negativeTagEnd-6, byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8),
byte(v))
} else if v >= -0xffffffffffffff {
return append(b, negativeTagEnd-7, byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16),
byte(v>>8), byte(v))
}
return append(b, negativeTagEnd-8, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24),
byte(v>>16), byte(v>>8), byte(v))
}
return EncodeComparableUvarint(b, uint64(v))
}
// EncodeComparableUvarint encodes uint64 into mem-comparable bytes.
func EncodeComparableUvarint(b []byte, v uint64) []byte {
// The first byte has 256 values, [0, 7] is reserved for negative tags,
// [248, 255] is reserved for larger positive tags,
// So we can store value [0, 239] in a single byte.
// Values cannot be stored in single byte has a tag byte prefix (positiveTagStart+length).
// Larger value encodes to more bytes, has larger tag.
if v <= positiveTagStart-negativeTagEnd {
return append(b, byte(v)+negativeTagEnd)
} else if v <= 0xff {
return append(b, positiveTagStart+1, byte(v))
} else if v <= 0xffff {
return append(b, positiveTagStart+2, byte(v>>8), byte(v))
} else if v <= 0xffffff {
return append(b, positiveTagStart+3, byte(v>>16), byte(v>>8), byte(v))
} else if v <= 0xffffffff {
return append(b, positiveTagStart+4, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
} else if v <= 0xffffffffff {
return append(b, positiveTagStart+5, byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
} else if v <= 0xffffffffffff {
return append(b, positiveTagStart+6, byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8),
byte(v))
} else if v <= 0xffffffffffffff {
return append(b, positiveTagStart+7, byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16),
byte(v>>8), byte(v))
}
return append(b, positiveTagStart+8, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24),
byte(v>>16), byte(v>>8), byte(v))
}
var (
errDecodeInsufficient = errors.New("insufficient bytes to decode value")
errDecodeInvalid = errors.New("invalid bytes to decode value")
)
// DecodeComparableUvarint decodes mem-comparable uvarint.
func DecodeComparableUvarint(b []byte) ([]byte, uint64, error) {
if len(b) == 0 {
return nil, 0, errDecodeInsufficient
}
first := b[0]
b = b[1:]
if first < negativeTagEnd {
return nil, 0, errors.Trace(errDecodeInvalid)
}
if first <= positiveTagStart {
return b, uint64(first) - negativeTagEnd, nil
}
length := int(first) - positiveTagStart
if len(b) < length {
return nil, 0, errors.Trace(errDecodeInsufficient)
}
var v uint64
for _, c := range b[:length] {
v = (v << 8) | uint64(c)
}
return b[length:], v, nil
}
// DecodeComparableVarint decodes mem-comparable varint.
func DecodeComparableVarint(b []byte) ([]byte, int64, error) {
if len(b) == 0 {
return nil, 0, errors.Trace(errDecodeInsufficient)
}
first := b[0]
if first >= negativeTagEnd && first <= positiveTagStart {
return b, int64(first) - negativeTagEnd, nil
}
b = b[1:]
var length int
var v uint64
if first < negativeTagEnd {
length = negativeTagEnd - int(first)
v = math.MaxUint64 // negative value has all bits on by default.
} else {
length = int(first) - positiveTagStart
}
if len(b) < length {
return nil, 0, errors.Trace(errDecodeInsufficient)
}
for _, c := range b[:length] {
v = (v << 8) | uint64(c)
}
if first > positiveTagStart && v > math.MaxInt64 {
return nil, 0, errors.Trace(errDecodeInvalid)
} else if first < negativeTagEnd && v <= math.MaxInt64 {
return nil, 0, errors.Trace(errDecodeInvalid)
}
return b[length:], int64(v), nil
}