/
packed_integer.go
507 lines (461 loc) · 14.1 KB
/
packed_integer.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
//
// Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved.
//
// Licensed under the Universal Permissive License v 1.0 as shown at
// https://oss.oracle.com/licenses/upl/
//
// This file implements reading and writing packed integer values.
// This is ported from JE's PackedInteger class.
package binary
const (
// The maximum number of bytes needed to store a signed 32-bit integer
maxPackedInt32Length = 5
// The maximum number of bytes needed to store a signed 64-bit integer
maxPackedInt64Length = 9
)
// readSortedInt32 reads a sorted packed int32 value from buf starting at
// offset off and returns it
func readSortedInt32(buf []byte, off uint) int32 {
var byteLen int
var negative bool
// The first byte stores the length of the value part.
b1 := int(buf[off])
off++
// Adjust the byteLen to the real length of the value part.
switch {
case b1 < 0x08:
byteLen = 0x08 - b1
negative = true
case b1 > 0xF7:
byteLen = b1 - 0xF7
negative = false
default:
return int32(b1 - 127)
}
// The following bytes on the buf store the value as a big endian integer.
// We extract the significant bytes from the buf and put them into the
// value in big endian order.
value := int32(0)
if negative {
value = ^int32(0) // 0xFFFFFFFF
}
if byteLen > 3 {
value = (value << 8) | int32(buf[off])
off++
}
if byteLen > 2 {
value = (value << 8) | int32(buf[off])
off++
}
if byteLen > 1 {
value = (value << 8) | int32(buf[off])
off++
}
value = (value << 8) | int32(buf[off])
off++
// After get the adjusted value, we have to adjust it back to the
// original value.
if negative {
value -= 119
} else {
value += 121
}
return value
}
// readSortedInt64 reads a sorted packed int64 value from buf starting at
// offset off and returns it
func readSortedInt64(buf []byte, off uint) int64 {
var byteLen int
var negative bool
// The first byte stores the length of the value part.
b1 := int(buf[off])
off++
// Adjust the byteLen to the real length of the value part.
switch {
case b1 < 0x08:
byteLen = 0x08 - b1
negative = true
case b1 > 0xF7:
byteLen = b1 - 0xF7
negative = false
default:
return int64(b1 - 127)
}
// The following bytes on the buf store the value as a big endian integer.
// We extract the significant bytes from the buf and put them into the
// value in big endian order.
value := int64(0)
if negative {
value = ^int64(0) // 0xFFFFFFFFFFFFFFFF
}
if byteLen > 7 {
value = (value << 8) | int64(buf[off])
off++
}
if byteLen > 6 {
value = (value << 8) | int64(buf[off])
off++
}
if byteLen > 5 {
value = (value << 8) | int64(buf[off])
off++
}
if byteLen > 4 {
value = (value << 8) | int64(buf[off])
off++
}
if byteLen > 3 {
value = (value << 8) | int64(buf[off])
off++
}
if byteLen > 2 {
value = (value << 8) | int64(buf[off])
off++
}
if byteLen > 1 {
value = (value << 8) | int64(buf[off])
off++
}
value = (value << 8) | int64(buf[off])
off++
// After obtaining the adjusted value, we have to adjust it back to the
// original value.
if negative {
value -= 119
} else {
value += 121
}
return value
}
// getReadSortedInt32Length returns the number of bytes that would be read by
// readSortedInt32.
func getReadSortedInt32Length(buf []byte, off uint) int {
// The first byte stores the length of the value part.
b1 := int(buf[off])
switch {
case b1 < 0x08:
return 1 + 0x08 - b1
case b1 > 0xF7:
return 1 + b1 - 0xF7
default:
return 1
}
}
// getReadSortedInt64Length returns the number of bytes that would be read by
// readSortedInt64
func getReadSortedInt64Length(buf []byte, off uint) int {
// The length is stored in the same way for int32 and int64
return getReadSortedInt32Length(buf, off)
}
// writeSortedInt32 writes an int32 value as a packed sorted integer into buf
// starting at offset off, returns the next offset to be written.
func writeSortedInt32(buf []byte, off uint, value int32) uint {
// Values in the inclusive range [-119,120] are stored in a single byte.
// For values outside that range, the first byte stores the number of
// additional bytes. The additional bytes store
// (value + 119 for negative and value - 121 for positive) as an unsigned
// big endian integer.
byte1Off := off
off++
if value < -119 {
// If the value < -119, then first adjust the value by adding 119.
// Then the adjusted value is stored as an unsigned big endian integer.
value += 119
// Store the adjusted value as an unsigned big endian integer.
// For a negative integer, from left to right, the first significant
// byte is the byte which is not equal to 0xFF. Also please note that,
// because the adjusted value is stored in big endian integer, we
// extract the significant byte from left to right.
//
// In the left to right order, if the first byte of the adjusted value
// is a significant byte, it will be stored in the 2nd byte of the buf.
// Then we will look at the 2nd byte of the adjusted value to see if
// this byte is the significant byte, if yes, this byte will be stored
// in the 3rd byte of the buf, and the like.
if (value | 0x00FFFFFF) != ^int32(0) {
buf[off] = byte(value >> 24)
off++
}
if (value | 0x0000FFFF) != ^int32(0) {
buf[off] = byte(value >> 16)
off++
}
if (value | 0x000000FF) != ^int32(0) {
buf[off] = byte(value >> 8)
off++
}
buf[off] = byte(value)
off++
// valueLen is the length of the value part stored in buf. Because the
// first byte of buf is used to stored the length, we need to subtract
// one.
valueLen := off - byte1Off - 1
// The first byte stores the number of additional bytes. Here we store
// the result of 0x08 - valueLen, rather than directly store valueLen.
// The reason is to implement natural sort order for byte-by-byte
// comparison.
buf[byte1Off] = byte(0x08 - valueLen)
return off
}
if value > 120 {
// If the value > 120, then first adjust the value by subtracting 121.
// Then the adjusted value is stored as an unsigned big endian integer.
value -= 121
// Store the adjusted value as an unsigned big endian integer.
// For a positive integer, from left to right, the first significant
// byte is the byte which is not equal to 0x00.
//
// In the left to right order, if the first byte of the adjusted value
// is a significant byte, it will be stored in the 2nd byte of the buf.
// Then we will look at the 2nd byte of the adjusted value to see if
// this byte is the significant byte, if yes, this byte will be stored
// in the 3rd byte of the buf, and the like.
if ((value >> 24) & 0x000000FF) != 0 {
buf[off] = byte(value >> 24)
off++
}
if ((value >> 16) & 0x0000FFFF) != 0 {
buf[off] = byte(value >> 16)
off++
}
if ((value >> 8) & 0x00FFFFFF) != 0 {
buf[off] = byte(value >> 8)
off++
}
buf[off] = byte(value)
off++
// valueLen is the length of the value part stored in buf. Because the
// first byte of buf is used to stored the length, we need to subtract
// one.
valueLen := off - byte1Off - 1
// The first byte stores the number of additional bytes. Here we store
// the result of 0xF7 + valueLen, rather than directly store valueLen.
// The reason is to implement natural sort order for byte-by-byte
// comparison.
buf[byte1Off] = byte(0xF7 + valueLen)
return off
}
// If -119 <= value <= 120, only one byte is needed to store the value.
// The stored value is the original value plus 127.
buf[byte1Off] = byte(value + 127)
return off
}
// writeSortedInt64 writes an int64 value as a packed sorted integer into buf
// starting at offset off, returns the next offset to be written.
func writeSortedInt64(buf []byte, off uint, value int64) uint {
// Values in the inclusive range [-119,120] are stored in a single byte.
// For values outside that range, the first byte stores the number of
// additional bytes. The additional bytes store
// (value + 119 for negative and value - 121 for positive) as an unsigned
// big endian integer.
byte1Off := off
off++
if value < -119 {
// If the value < -119, then first adjust the value by adding 119.
// Then the adjusted value is stored as an unsigned big endian integer.
value += 119
// Store the adjusted value as an unsigned big endian integer.
// For an negative integer, from left to right, the first significant
// byte is the byte which is not equal to 0xFF. Also please note that,
// because the adjusted value is stored in big endian integer, we
// extract the significant byte from left to right.
//
// In the left to right order, if the first byte of the adjusted value
// is a significant byte, it will be stored in the 2nd byte of the buf.
// Then we will look at the 2nd byte of the adjusted value to see if
// this byte is the significant byte, if yes, this byte will be stored
// in the 3rd byte of the buf, and the like.
if (value | 0x00FFFFFFFFFFFFFF) != ^int64(0) {
buf[off] = byte(value >> 56)
off++
}
if (value | 0x0000FFFFFFFFFFFF) != ^int64(0) {
buf[off] = byte(value >> 48)
off++
}
if (value | 0x000000FFFFFFFFFF) != ^int64(0) {
buf[off] = byte(value >> 40)
off++
}
if (value | 0x00000000FFFFFFFF) != ^int64(0) {
buf[off] = byte(value >> 32)
off++
}
if (value | 0x0000000000FFFFFF) != ^int64(0) {
buf[off] = byte(value >> 24)
off++
}
if (value | 0x000000000000FFFF) != ^int64(0) {
buf[off] = byte(value >> 16)
off++
}
if (value | 0x00000000000000FF) != ^int64(0) {
buf[off] = byte(value >> 8)
off++
}
buf[off] = byte(value)
off++
// valueLen is the length of the value part stored in buf. Because
// the first byte of buf is used to stored the length, so we need to
// subtract one.
valueLen := off - byte1Off - 1
// The first byte stores the number of additional bytes. Here we store
// the result of 0x08 - valueLen, rather than directly store valueLen.
// The reason is to implement nature sort order for byte-by-byte
// comparison.
buf[byte1Off] = byte(0x08 - valueLen)
return off
}
if value > 120 {
// If the value > 120, then first adjust the value by subtracting 121.
// Then the adjusted value is stored as an unsigned big endian integer.
value -= 121
// Store the adjusted value as an unsigned big endian integer.
// For a positive integer, from left to right, the first significant
// byte is the byte which is not equal to 0x00.
//
// In the left to right order, if the first byte of the adjusted value
// is a significant byte, it will be stored in the 2nd byte of the buf.
// Then we will look at the 2nd byte of the adjusted value to see if
// this byte is the significant byte, if yes, this byte will be stored
// in the 3rd byte of the buf, and the like.
if ((value >> 56) & 0x00000000000000FF) != 0 {
buf[off] = byte(value >> 56)
off++
}
if ((value >> 48) & 0x000000000000FFFF) != 0 {
buf[off] = byte(value >> 48)
off++
}
if ((value >> 40) & 0x0000000000FFFFFF) != 0 {
buf[off] = byte(value >> 40)
off++
}
if ((value >> 32) & 0x00000000FFFFFFFF) != 0 {
buf[off] = byte(value >> 32)
off++
}
if ((value >> 24) & 0x000000FFFFFFFFFF) != 0 {
buf[off] = byte(value >> 24)
off++
}
if ((value >> 16) & 0x0000FFFFFFFFFFFF) != 0 {
buf[off] = byte(value >> 16)
off++
}
if ((value >> 8) & 0x00FFFFFFFFFFFFFF) != 0 {
buf[off] = byte(value >> 8)
off++
}
buf[off] = byte(value)
off++
// valueLen is the length of the value part stored in buf. Because the
// first byte of buf is used to stored the length, so we need to
// subtract one.
valueLen := off - byte1Off - 1
// The first byte stores the number of additional bytes. Here we store
// the result of 0xF7 + valueLen, rather than directly store valueLen.
// The reason is to implement nature sort order for byte-by-byte
// comparison.
buf[byte1Off] = byte(0xF7 + valueLen)
return off
}
// If -119 <= value <= 120, only one byte is needed to store the value.
// The stored value is the original value adds 127.
buf[byte1Off] = byte(value + 127)
return off
}
// getWriteSortedInt32Length returns the number of bytes that the value would
// be written by writeSortedInt32
func getWriteSortedInt32Length(value int32) int {
if value < -119 {
// Adjust the value.
value += 119
// Find the left most significant byte of the adjusted value, and return
// the length accordingly.
if (value | 0x000000FF) == ^int32(0) {
return 2
}
if (value | 0x0000FFFF) == ^int32(0) {
return 3
}
if (value | 0x00FFFFFF) == ^int32(0) {
return 4
}
return 5
}
if value > 120 {
value -= 121
if ((value >> 8) & 0x00FFFFFF) == 0 {
return 2
}
if ((value >> 16) & 0x0000FFFF) == 0 {
return 3
}
if ((value >> 24) & 0x000000FF) == 0 {
return 4
}
return 5
}
// If -119 <= value <= 120, only one byte is needed to store the value.
return 1
}
// getWriteSortedInt64Length returns the number of bytes that the value would
// be written by writeSortedInt64
func getWriteSortedInt64Length(value int64) int {
if value < -119 {
// Adjust the value.
value += 119
// Find the left most significant byte of the adjusted value, and return
// the length accordingly.
if (value | 0x00000000000000FF) == ^int64(0) {
return 2
}
if (value | 0x000000000000FFFF) == ^int64(0) {
return 3
}
if (value | 0x0000000000FFFFFF) == ^int64(0) {
return 4
}
if (value | 0x00000000FFFFFFFF) == ^int64(0) {
return 5
}
if (value | 0x000000FFFFFFFFFF) == ^int64(0) {
return 6
}
if (value | 0x0000FFFFFFFFFFFF) == ^int64(0) {
return 7
}
if (value | 0x00FFFFFFFFFFFFFF) == ^int64(0) {
return 8
}
return 9
}
if value > 120 {
value -= 121
if ((value >> 8) & 0x00FFFFFFFFFFFFFF) == 0 {
return 2
}
if ((value >> 16) & 0x0000FFFFFFFFFFFF) == 0 {
return 3
}
if ((value >> 24) & 0x000000FFFFFFFFFF) == 0 {
return 4
}
if ((value >> 32) & 0x00000000FFFFFFFF) == 0 {
return 5
}
if ((value >> 40) & 0x0000000000FFFFFF) == 0 {
return 6
}
if ((value >> 48) & 0x000000000000FFFF) == 0 {
return 7
}
if ((value >> 56) & 0x00000000000000FF) == 0 {
return 8
}
return 9
}
// If -119 <= value <= 120, only one byte is needed to store the value
return 1
}