forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacker.go
434 lines (383 loc) · 9.48 KB
/
packer.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
// Copyright 2013-2016 Aerospike, 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aerospike
import (
"bytes"
"fmt"
"math"
"reflect"
"time"
ParticleType "github.com/aerospike/aerospike-client-go/types/particle_type"
Buffer "github.com/aerospike/aerospike-client-go/utils/buffer"
)
type packer struct {
buffer *bytes.Buffer
offset int
}
func packValueArray(val []Value) ([]byte, error) {
packer := newPacker()
if err := packer.packValueArray(val); err != nil {
return nil, nil
}
return packer.buffer.Bytes(), nil
}
func packAnyArray(val []interface{}) ([]byte, error) {
packer := newPacker()
if err := packer.PackList(val); err != nil {
return nil, nil
}
return packer.buffer.Bytes(), nil
}
func packAnyMap(val map[interface{}]interface{}) ([]byte, error) {
packer := newPacker()
if err := packer.PackMap(val); err != nil {
return nil, nil
}
return packer.buffer.Bytes(), nil
}
///////////////////////////////////////////////////////////////////////////////
func newPacker() *packer {
p := &packer{
buffer: bytes.NewBuffer(make([]byte, 0, 256)),
}
return p
}
func (pckr *packer) packValueArray(values []Value) error {
pckr.PackArrayBegin(len(values))
for i := range values {
if err := values[i].pack(pckr); err != nil {
return err
}
}
return nil
}
func (pckr *packer) PackList(list []interface{}) error {
pckr.PackArrayBegin(len(list))
for i := range list {
if err := pckr.PackObject(list[i]); err != nil {
return err
}
}
return nil
}
func (pckr *packer) PackArrayBegin(size int) {
if size < 16 {
pckr.PackAByte(0x90 | byte(size))
} else if size <= math.MaxUint16 {
pckr.PackShort(0xdc, int16(size))
} else {
pckr.PackInt(0xdd, int32(size))
}
}
func (pckr *packer) PackMap(theMap map[interface{}]interface{}) error {
pckr.PackMapBegin(len(theMap))
for k, v := range theMap {
if k != nil {
t := reflect.TypeOf(k)
if t.Kind() == reflect.Map || t.Kind() == reflect.Slice ||
(t.Kind() == reflect.Array && t.Elem().Kind() != reflect.Uint8) {
panic("Maps, Slices, and bounded arrays other than Bounded Byte Arrays are not supported as Map keys.")
}
}
if err := pckr.PackObject(k); err != nil {
return err
}
if err := pckr.PackObject(v); err != nil {
return err
}
}
return nil
}
func (pckr *packer) PackMapBegin(size int) {
if size < 16 {
pckr.PackAByte(0x80 | byte(size))
} else if size <= math.MaxUint16 {
pckr.PackShort(0xde, int16(size))
} else {
pckr.PackInt(0xdf, int32(size))
}
}
func (pckr *packer) PackBytes(b []byte) {
pckr.PackByteArrayBegin(len(b) + 1)
pckr.PackAByte(ParticleType.BLOB)
pckr.PackByteArray(b, 0, len(b))
}
func (pckr *packer) PackByteArrayBegin(length int) {
if length < 32 {
pckr.PackAByte(0xa0 | byte(length))
} else if length < 65536 {
pckr.PackShort(0xda, int16(length))
} else {
pckr.PackInt(0xdb, int32(length))
}
}
func (pckr *packer) PackObject(obj interface{}) error {
switch v := obj.(type) {
case Value:
return v.pack(pckr)
case string:
pckr.PackString(v)
return nil
case []byte:
pckr.PackBytes(obj.([]byte))
return nil
case int8:
pckr.PackAInt(int(v))
return nil
case uint8:
pckr.PackAInt(int(v))
return nil
case int16:
pckr.PackAInt(int(v))
return nil
case uint16:
pckr.PackAInt(int(v))
return nil
case int32:
pckr.PackAInt(int(v))
return nil
case uint32:
pckr.PackAInt(int(v))
return nil
case int:
if Buffer.Arch32Bits {
pckr.PackAInt(v)
return nil
}
pckr.PackALong(int64(v))
return nil
case uint:
if Buffer.Arch32Bits {
pckr.PackAInt(int(v))
return nil
}
pckr.PackAULong(uint64(v))
case int64:
pckr.PackALong(v)
return nil
case uint64:
pckr.PackAULong(v)
return nil
case time.Time:
pckr.PackALong(v.UnixNano())
return nil
case nil:
pckr.PackNil()
return nil
case bool:
pckr.PackBool(v)
return nil
case float32:
pckr.PackFloat32(v)
return nil
case float64:
pckr.PackFloat64(v)
return nil
case struct{}:
pckr.PackMap(map[interface{}]interface{}{})
return nil
case []interface{}:
return pckr.PackList(obj.([]interface{}))
case map[interface{}]interface{}:
return pckr.PackMap(obj.(map[interface{}]interface{}))
}
// check for array and map
rv := reflect.ValueOf(obj)
switch reflect.TypeOf(obj).Kind() {
case reflect.Array, reflect.Slice:
// pack bounded array of bytes differently
if reflect.TypeOf(obj).Kind() == reflect.Array && reflect.TypeOf(obj).Elem().Kind() == reflect.Uint8 {
l := rv.Len()
arr := make([]byte, l)
for i := 0; i < l; i++ {
arr[i] = rv.Index(i).Interface().(uint8)
}
pckr.PackBytes(arr)
return nil
}
l := rv.Len()
arr := make([]interface{}, l)
for i := 0; i < l; i++ {
arr[i] = rv.Index(i).Interface()
}
return pckr.PackList(arr)
case reflect.Map:
l := rv.Len()
amap := make(map[interface{}]interface{}, l)
for _, i := range rv.MapKeys() {
amap[i.Interface()] = rv.MapIndex(i).Interface()
}
return pckr.PackMap(amap)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return pckr.PackObject(rv.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return pckr.PackObject(rv.Uint())
case reflect.Bool:
return pckr.PackObject(rv.Bool())
case reflect.String:
return pckr.PackObject(rv.String())
case reflect.Float32, reflect.Float64:
return pckr.PackObject(rv.Float())
}
panic(fmt.Sprintf("Type `%v` not supported to pack.", reflect.TypeOf(obj)))
}
func (pckr *packer) PackAULong(val uint64) {
pckr.PackULong(val)
}
func (pckr *packer) PackALong(val int64) {
if val >= 0 {
if val < 128 {
pckr.PackAByte(byte(val))
return
}
if val <= math.MaxUint8 {
pckr.PackByte(0xcc, byte(val))
return
}
if val <= math.MaxUint16 {
pckr.PackShort(0xcd, int16(val))
return
}
if val <= math.MaxUint32 {
pckr.PackInt(0xce, int32(val))
return
}
pckr.PackLong(0xd3, val)
} else {
if val >= -32 {
pckr.PackAByte(0xe0 | (byte(val) + 32))
return
}
if val >= math.MinInt8 {
pckr.PackByte(0xd0, byte(val))
return
}
if val >= math.MinInt16 {
pckr.PackShort(0xd1, int16(val))
return
}
if val >= math.MinInt32 {
pckr.PackInt(0xd2, int32(val))
return
}
pckr.PackLong(0xd3, val)
}
}
func (pckr *packer) PackAInt(val int) {
if val >= 0 {
if val < 128 {
pckr.PackAByte(byte(val))
return
}
if val < 256 {
pckr.PackByte(0xcc, byte(val))
return
}
if val < 65536 {
pckr.PackShort(0xcd, int16(val))
return
}
pckr.PackInt(0xce, int32(val))
} else {
if val >= -32 {
pckr.PackAByte(0xe0 | (byte(val) + 32))
return
}
if val >= math.MinInt8 {
pckr.PackByte(0xd0, byte(val))
return
}
if val >= math.MinInt16 {
pckr.PackShort(0xd1, int16(val))
return
}
pckr.PackInt(0xd2, int32(val))
}
}
var _b8 = []byte{0, 0, 0, 0, 0, 0, 0, 0}
var _b4 = []byte{0, 0, 0, 0}
var _b2 = []byte{0, 0}
func (pckr *packer) grow(b []byte) int {
pos := pckr.buffer.Len()
pckr.buffer.Write(b)
return pos
}
func (pckr *packer) PackString(val string) {
size := len(val) + 1
pckr.PackByteArrayBegin(size)
pckr.buffer.WriteByte(byte(ParticleType.STRING))
pckr.buffer.WriteString(val)
}
func (pckr *packer) PackGeoJson(val string) {
size := len(val) + 1
pckr.PackByteArrayBegin(size)
pckr.buffer.WriteByte(byte(ParticleType.GEOJSON))
pckr.buffer.WriteString(val)
}
func (pckr *packer) PackByteArray(src []byte, srcOffset int, srcLength int) {
pckr.buffer.Write(src[srcOffset : srcOffset+srcLength])
}
func (pckr *packer) PackLong(valType int, val int64) {
pckr.buffer.WriteByte(byte(valType))
pos := pckr.grow(_b8)
Buffer.Int64ToBytes(val, pckr.buffer.Bytes(), pos)
}
func (pckr *packer) PackULong(val uint64) {
pckr.buffer.WriteByte(byte(0xcf))
pos := pckr.grow(_b8)
Buffer.Int64ToBytes(int64(val), pckr.buffer.Bytes(), pos)
}
func (pckr *packer) PackInt(valType int, val int32) {
pckr.buffer.WriteByte(byte(valType))
pos := pckr.grow(_b4)
Buffer.Int32ToBytes(val, pckr.buffer.Bytes(), pos)
}
func (pckr *packer) PackShort(valType int, val int16) {
pckr.buffer.WriteByte(byte(valType))
pos := pckr.grow(_b2)
Buffer.Int16ToBytes(val, pckr.buffer.Bytes(), pos)
}
// This method is not compatible with MsgPack specs and is only used by aerospike client<->server
// for wire transfer only
func (pckr *packer) PackShortRaw(val int16) {
pos := pckr.grow(_b2)
Buffer.Int16ToBytes(val, pckr.buffer.Bytes(), pos)
}
func (pckr *packer) PackByte(valType int, val byte) {
pckr.buffer.WriteByte(byte(valType))
pckr.buffer.WriteByte(val)
}
func (pckr *packer) PackNil() {
pckr.buffer.WriteByte(0xc0)
}
func (pckr *packer) PackBool(val bool) {
if val {
pckr.buffer.WriteByte(0xc3)
} else {
pckr.buffer.WriteByte(0xc2)
}
}
func (pckr *packer) PackFloat32(val float32) {
pckr.buffer.WriteByte(0xca)
pos := pckr.grow(_b4)
Buffer.Float32ToBytes(val, pckr.buffer.Bytes(), pos)
}
func (pckr *packer) PackFloat64(val float64) {
pckr.buffer.WriteByte(0xcb)
pos := pckr.grow(_b8)
Buffer.Float64ToBytes(val, pckr.buffer.Bytes(), pos)
}
func (pckr *packer) PackAByte(val byte) {
pckr.buffer.WriteByte(val)
}