-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuilder.go
1186 lines (1084 loc) · 28.9 KB
/
builder.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// DISCLAIMER
//
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//
package velocypack
import (
"encoding/binary"
"fmt"
"io"
"math"
"reflect"
)
// BuilderOptions contains options that influence how Builder builds slices.
type BuilderOptions struct {
BuildUnindexedArrays bool
BuildUnindexedObjects bool
CheckAttributeUniqueness bool
}
// Builder is used to build VPack structures.
type Builder struct {
BuilderOptions
buf builderBuffer
stack builderStack
index []indexVector
keyWritten bool
}
func NewBuilder(capacity uint) *Builder {
b := &Builder{
buf: make(builderBuffer, 0, capacity),
}
return b
}
// Clear and start from scratch:
func (b *Builder) Clear() {
b.buf = nil
b.stack.Clear()
b.keyWritten = false
}
// Bytes return the generated bytes.
// The returned slice is shared with the builder itself, so you must not modify it.
// When the builder is not closed, an error is returned.
func (b *Builder) Bytes() ([]byte, error) {
if !b.IsClosed() {
return nil, WithStack(BuilderNotClosedError)
}
return b.buf, nil
}
// Slice returns a slice of the result.
func (b *Builder) Slice() (Slice, error) {
if b.buf.IsEmpty() {
return Slice{}, nil
}
bytes, err := b.Bytes()
return bytes, WithStack(err)
}
// WriteTo writes the generated bytes to the given writer.
// When the builder is not closed, an error is returned.
func (b *Builder) WriteTo(w io.Writer) (int64, error) {
if !b.IsClosed() {
return 0, WithStack(BuilderNotClosedError)
}
if n, err := w.Write(b.buf); err != nil {
return 0, WithStack(err)
} else {
return int64(n), nil
}
}
// Size returns the actual size of the generated slice.
// Returns an error when builder is not closed.
func (b *Builder) Size() (ValueLength, error) {
if !b.IsClosed() {
return 0, WithStack(BuilderNotClosedError)
}
return b.buf.Len(), nil
}
// IsEmpty returns true when no bytes have been generated yet.
func (b *Builder) IsEmpty() bool {
return b.buf.IsEmpty()
}
// IsOpenObject returns true when the builder has an open object at the top of the stack.
func (b *Builder) IsOpenObject() bool {
if b.stack.IsEmpty() {
return false
}
tos, _ := b.stack.Tos()
h := b.buf[tos]
return h == 0x0b || h == 0x014
}
// IsOpenArray returns true when the builder has an open array at the top of the stack.
func (b *Builder) IsOpenArray() bool {
if b.stack.IsEmpty() {
return false
}
tos, _ := b.stack.Tos()
h := b.buf[tos]
return h == 0x06 || h == 0x013
}
// OpenObject starts a new object.
// This must be closed using Close.
func (b *Builder) OpenObject(unindexed ...bool) error {
var vType byte
if optionalBool(unindexed, false) {
vType = 0x14
} else {
vType = 0x0b
}
return WithStack(b.openCompoundValue(vType))
}
// OpenArray starts a new array.
// This must be closed using Close.
func (b *Builder) OpenArray(unindexed ...bool) error {
var vType byte
if optionalBool(unindexed, false) {
vType = 0x13
} else {
vType = 0x06
}
return WithStack(b.openCompoundValue(vType))
}
// Close ends an open object or array.
func (b *Builder) Close() error {
if b.IsClosed() {
return WithStack(BuilderNeedOpenCompoundError)
}
tos, _ := b.stack.Tos()
head := b.buf[tos]
vpackAssert(head == 0x06 || head == 0x0b || head == 0x13 || head == 0x14)
isArray := (head == 0x06 || head == 0x13)
index := b.index[b.stack.Len()-1]
if index.IsEmpty() {
b.closeEmptyArrayOrObject(tos, isArray)
return nil
}
// From now on index.size() > 0
vpackAssert(len(index) > 0)
// check if we can use the compact Array / Object format
if head == 0x13 || head == 0x14 ||
(head == 0x06 && b.BuilderOptions.BuildUnindexedArrays) ||
(head == 0x0b && (b.BuilderOptions.BuildUnindexedObjects || len(index) == 1)) {
if b.closeCompactArrayOrObject(tos, isArray, index) {
return nil
}
// This might fall through, if closeCompactArrayOrObject gave up!
}
if isArray {
b.closeArray(tos, index)
return nil
}
// From now on we're closing an object
// fix head byte in case a compact Array / Object was originally requested
b.buf[tos] = 0x0b
// First determine byte length and its format:
offsetSize := uint(8)
// can be 1, 2, 4 or 8 for the byte width of the offsets,
// the byte length and the number of subvalues:
if b.buf.Len()-tos+ValueLength(len(index))-6 <= 0xff {
// We have so far used _pos - tos bytes, including the reserved 8
// bytes for byte length and number of subvalues. In the 1-byte number
// case we would win back 6 bytes but would need one byte per subvalue
// for the index table
offsetSize = 1
// Maybe we need to move down data:
targetPos := ValueLength(3)
if b.buf.Len() > (tos + 9) {
_len := ValueLength(b.buf.Len() - (tos + 9))
checkOverflow(_len)
src := b.buf[tos+9:]
copy(b.buf[tos+targetPos:], src[:_len])
}
diff := ValueLength(9 - targetPos)
b.buf.Shrink(uint(diff))
n := len(index)
for i := 0; i < n; i++ {
index[i] -= diff
}
// One could move down things in the offsetSize == 2 case as well,
// since we only need 4 bytes in the beginning. However, saving these
// 4 bytes has been sacrificed on the Altar of Performance.
} else if b.buf.Len()-tos+2*ValueLength(len(index)) <= 0xffff {
offsetSize = 2
} else if b.buf.Len()-tos+4*ValueLength(len(index)) <= 0xffffffff {
offsetSize = 4
}
// Now build the table:
extraSpace := offsetSize * uint(len(index))
if offsetSize == 8 {
extraSpace += 8
}
b.buf.ReserveSpace(extraSpace)
tableBase := b.buf.Len()
b.buf.Grow(offsetSize * uint(len(index)))
// Object
if len(index) >= 2 {
if err := b.sortObjectIndex(b.buf[tos:], index); err != nil {
return WithStack(err)
}
}
for i := uint(0); i < uint(len(index)); i++ {
indexBase := tableBase + ValueLength(offsetSize*i)
x := uint64(index[i])
for j := uint(0); j < offsetSize; j++ {
b.buf[indexBase+ValueLength(j)] = byte(x & 0xff)
x >>= 8
}
}
// Finally fix the byte width in the type byte:
if offsetSize > 1 {
if offsetSize == 2 {
b.buf[tos] += 1
} else if offsetSize == 4 {
b.buf[tos] += 2
} else { // offsetSize == 8
b.buf[tos] += 3
b.appendLength(ValueLength(len(index)), 8)
}
}
// Fix the byte length in the beginning:
x := ValueLength(b.buf.Len() - tos)
for i := uint(1); i <= offsetSize; i++ {
b.buf[tos+ValueLength(i)] = byte(x & 0xff)
x >>= 8
}
if offsetSize < 8 {
x := len(index)
for i := uint(offsetSize + 1); i <= 2*offsetSize; i++ {
b.buf[tos+ValueLength(i)] = byte(x & 0xff)
x >>= 8
}
}
// And, if desired, check attribute uniqueness:
if b.BuilderOptions.CheckAttributeUniqueness && len(index) > 1 {
// check uniqueness of attribute names
if err := b.checkAttributeUniqueness(Slice(b.buf[tos:])); err != nil {
return WithStack(err)
}
}
// Now the array or object is complete, we pop a ValueLength off the _stack:
b.stack.Pop()
// Intentionally leave _index[depth] intact to avoid future allocs!
return nil
}
// IsClosed returns true if there are no more open objects or arrays.
func (b *Builder) IsClosed() bool {
return b.stack.IsEmpty()
}
// HasKey checks whether an Object value has a specific key attribute.
func (b *Builder) HasKey(key string) (bool, error) {
if b.stack.IsEmpty() {
return false, WithStack(BuilderNeedOpenObjectError)
}
tos, _ := b.stack.Tos()
h := b.buf[tos]
if h != 0x0b && h != 0x14 {
return false, WithStack(BuilderNeedOpenObjectError)
}
index := b.index[b.stack.Len()-1]
if index.IsEmpty() {
return false, nil
}
for _, idx := range index {
s := Slice(b.buf[tos+idx:])
k, err := s.makeKey()
if err != nil {
return false, WithStack(err)
}
if eq, err := k.IsEqualString(key); err != nil {
return false, WithStack(err)
} else if eq {
return true, nil
}
}
return false, nil
}
// GetKey returns the value for a specific key of an Object value.
// Returns Slice of type None when key is not found.
func (b *Builder) GetKey(key string) (Slice, error) {
if b.stack.IsEmpty() {
return nil, WithStack(BuilderNeedOpenObjectError)
}
tos, _ := b.stack.Tos()
h := b.buf[tos]
if h != 0x0b && h != 0x14 {
return nil, WithStack(BuilderNeedOpenObjectError)
}
index := b.index[b.stack.Len()-1]
if index.IsEmpty() {
return nil, nil
}
for _, idx := range index {
s := Slice(b.buf[tos+idx:])
k, err := s.makeKey()
if err != nil {
return nil, WithStack(err)
}
if eq, err := k.IsEqualString(key); err != nil {
return nil, WithStack(err)
} else if eq {
value, err := s.Next()
if err != nil {
return nil, WithStack(err)
}
return value, nil
}
}
return nil, nil
}
// RemoveLast removes last subvalue written to an (unclosed) object or array.
func (b *Builder) RemoveLast() error {
if b.stack.IsEmpty() {
return WithStack(BuilderNeedOpenCompoundError)
}
tos, _ := b.stack.Tos()
index := &b.index[b.stack.Len()-1]
if index.IsEmpty() {
return WithStack(BuilderNeedSubValueError)
}
newLength := tos + (*index)[len(*index)-1]
lastSize := b.buf.Len() - newLength
b.buf.Shrink(uint(lastSize))
index.RemoveLast()
return nil
}
// addNull adds a null value to the buffer.
func (b *Builder) addNull() {
b.buf.WriteByte(0x18)
}
// addFalse adds a bool false value to the buffer.
func (b *Builder) addFalse() {
b.buf.WriteByte(0x19)
}
// addTrue adds a bool true value to the buffer.
func (b *Builder) addTrue() {
b.buf.WriteByte(0x1a)
}
// addBool adds a bool value to the buffer.
func (b *Builder) addBool(v bool) {
if v {
b.addTrue()
} else {
b.addFalse()
}
}
// addDouble adds a double value to the buffer.
func (b *Builder) addDouble(v float64) {
bits := math.Float64bits(v)
b.buf.ReserveSpace(9)
b.buf.WriteByte(0x1b)
binary.LittleEndian.PutUint64(b.buf.Grow(8), bits)
}
// addInt adds an int value to the buffer.
func (b *Builder) addInt(v int64) {
if v >= 0 && v <= 9 {
b.buf.WriteByte(0x30 + byte(v))
} else if v < 0 && v >= -6 {
b.buf.WriteByte(byte(0x40 + int(v)))
} else {
b.appendInt(v, 0x1f)
}
}
// addUInt adds an uint value to the buffer.
func (b *Builder) addUInt(v uint64) {
if v <= 9 {
b.buf.WriteByte(0x30 + byte(v))
} else {
b.appendUInt(v, 0x27)
}
}
// addUTCDate adds an UTC date value to the buffer.
func (b *Builder) addUTCDate(v int64) {
x := toUInt64(v)
dst := b.buf.Grow(9)
dst[0] = 0x1c
setLength(dst[1:], ValueLength(x), 8)
}
// addString adds a string value to the buffer.
func (b *Builder) addString(v string) {
strLen := uint(len(v))
if strLen > 126 {
// long string
dst := b.buf.Grow(1 + 8 + strLen)
dst[0] = 0xbf
setLength(dst[1:], ValueLength(strLen), 8) // string length
copy(dst[9:], v) // string data
} else {
dst := b.buf.Grow(1 + strLen)
dst[0] = byte(0x40 + strLen) // short string (with length)
copy(dst[1:], v) // string data
}
}
// addBinary adds a binary value to the buffer.
func (b *Builder) addBinary(v []byte) {
l := uint(len(v))
b.buf.ReserveSpace(1 + 8 + l)
b.appendUInt(uint64(l), 0xbf) // data length
b.buf.Write(v) // data
}
// addIllegal adds an Illegal value to the buffer.
func (b *Builder) addIllegal() {
b.buf.WriteByte(0x17)
}
// addMinKey adds a MinKey value to the buffer.
func (b *Builder) addMinKey() {
b.buf.WriteByte(0x1e)
}
// addMaxKey adds a MaxKey value to the buffer.
func (b *Builder) addMaxKey() {
b.buf.WriteByte(0x1f)
}
// Add adds a raw go value value to an array/raw value/object.
func (b *Builder) Add(v interface{}) error {
if it, ok := v.(*ObjectIterator); ok {
return WithStack(b.AddKeyValuesFromIterator(it))
}
if it, ok := v.(*ArrayIterator); ok {
return WithStack(b.AddValuesFromIterator(it))
}
value := NewValue(v)
if value.IsIllegal() {
return WithStack(BuilderUnexpectedTypeError{fmt.Sprintf("Cannot convert value of type %s", reflect.TypeOf(v).Name())})
}
if err := b.addInternal(value); err != nil {
return WithStack(err)
}
return nil
}
// AddValue adds a value to an array/raw value/object.
func (b *Builder) AddValue(v Value) error {
if err := b.addInternal(v); err != nil {
return WithStack(err)
}
return nil
}
// AddKeyValue adds a key+value to an open object.
func (b *Builder) AddKeyValue(key string, v Value) error {
if err := b.addInternalKeyValue(key, v); err != nil {
return WithStack(err)
}
return nil
}
// AddValuesFromIterator adds values to an array from the given iterator.
// The array must be opened before a call to this function and the array is left open Intentionally.
func (b *Builder) AddValuesFromIterator(it *ArrayIterator) error {
if b.stack.IsEmpty() {
return WithStack(BuilderNeedOpenArrayError)
}
tos, _ := b.stack.Tos()
h := b.buf[tos]
if h != 0x06 && h != 0x13 {
return WithStack(BuilderNeedOpenArrayError)
}
for it.IsValid() {
v, err := it.Value()
if err != nil {
return WithStack(err)
}
if err := b.addInternal(NewSliceValue(v)); err != nil {
return WithStack(err)
}
if err := it.Next(); err != nil {
return WithStack(err)
}
}
return nil
}
// AddKeyValuesFromIterator adds values to an object from the given iterator.
// The object must be opened before a call to this function and the object is left open Intentionally.
func (b *Builder) AddKeyValuesFromIterator(it *ObjectIterator) error {
if b.stack.IsEmpty() {
return WithStack(BuilderNeedOpenObjectError)
}
tos, _ := b.stack.Tos()
h := b.buf[tos]
if h != 0x0b && h != 0x14 {
return WithStack(BuilderNeedOpenObjectError)
}
if b.keyWritten {
return WithStack(BuilderKeyAlreadyWrittenError)
}
for it.IsValid() {
k, err := it.Key(true)
if err != nil {
return WithStack(err)
}
key, err := k.GetString()
if err != nil {
return WithStack(err)
}
v, err := it.Value()
if err != nil {
return WithStack(err)
}
if err := b.addInternalKeyValue(key, NewSliceValue(v)); err != nil {
return WithStack(err)
}
if err := it.Next(); err != nil {
return WithStack(err)
}
}
return nil
}
// returns number of bytes required to store the value in 2s-complement
func intLength(value int64) uint {
if value >= -0x80 && value <= 0x7f {
// shortcut for the common case
return 1
}
var x uint64
if value >= 0 {
x = uint64(value)
} else {
x = uint64(-(value + 1))
}
xSize := uint(0)
for {
xSize++
x >>= 8
if x < 0x80 {
return xSize + 1
}
}
}
func (b *Builder) appendInt(v int64, base uint) {
vSize := intLength(v)
var x uint64
if vSize == 8 {
x = toUInt64(v)
} else {
shift := int64(1) << (vSize*8 - 1) // will never overflow!
if v >= 0 {
x = uint64(v)
} else {
x = uint64(v+shift) + uint64(shift)
}
// x = v >= 0 ? static_cast<uint64_t>(v)
// : static_cast<uint64_t>(v + shift) + shift;
}
dst := b.buf.Grow(1 + vSize)
dst[0] = byte(base + vSize)
off := 1
for ; vSize > 0; vSize-- {
dst[off] = byte(x & 0xff)
x >>= 8
off++
}
}
func (b *Builder) appendUInt(v uint64, base uint) {
b.buf.ReserveSpace(9)
save := b.buf.Len()
b.buf.WriteByte(0) // Will be overwritten at end of function.
vSize := uint(0)
for {
vSize++
b.buf.WriteByte(byte(v & 0xff))
v >>= 8
if v == 0 {
break
}
}
b.buf[save] = byte(base + vSize)
}
func (b *Builder) appendLength(v ValueLength, n uint) {
dst := b.buf.Grow(n)
setLength(dst, v, n)
}
func setLength(dst []byte, v ValueLength, n uint) {
for i := uint(0); i < n; i++ {
dst[i] = byte(v & 0xff)
v >>= 8
}
}
// openCompoundValue opens an array/object, checking the context.
func (b *Builder) openCompoundValue(vType byte) error {
//haveReported := false
tos, stackLen := b.stack.Tos()
if stackLen > 0 {
h := b.buf[tos]
if !b.keyWritten {
if h != 0x06 && h != 0x13 {
return WithStack(BuilderNeedOpenArrayError)
}
b.reportAdd()
//haveReported = true
} else {
b.keyWritten = false
}
}
b.addCompoundValue(vType)
// if err && haveReported { b.cleanupAdd() }
return nil
}
// addCompoundValue adds the start of a component value to the stream & stack.
func (b *Builder) addCompoundValue(vType byte) {
pos := b.buf.Len()
b.stack.Push(pos)
stackLen := b.stack.Len()
toAdd := stackLen - len(b.index)
for toAdd > 0 {
newIndex := make(indexVector, 0, 16) // Pre-allocate 16 entries so we don't have to allocate memory for the first 16 entries
b.index = append(b.index, newIndex)
toAdd--
}
b.index[stackLen-1].Clear()
b.buf.Write([]byte{vType, 0, 0, 0, 0, 0, 0, 0, 0})
}
// closeEmptyArrayOrObject closes an empty array/object, removing the pre-allocated length space.
func (b *Builder) closeEmptyArrayOrObject(tos ValueLength, isArray bool) {
// empty Array or Object
if isArray {
b.buf[tos] = 0x01
} else {
b.buf[tos] = 0x0a
}
vpackAssert(b.buf.Len() == tos+9)
b.buf.Shrink(8)
b.stack.Pop()
}
// closeCompactArrayOrObject tries to close an array/object using compact notation.
// Returns true when a compact notation was possible, false otherwise.
func (b *Builder) closeCompactArrayOrObject(tos ValueLength, isArray bool, index indexVector) bool {
// use compact notation
nrItems := len(index)
nrItemsLen := getVariableValueLength(ValueLength(nrItems))
vpackAssert(nrItemsLen > 0)
byteSize := b.buf.Len() - (tos + 8) + nrItemsLen
vpackAssert(byteSize > 0)
byteSizeLen := getVariableValueLength(byteSize)
byteSize += byteSizeLen
if getVariableValueLength(byteSize) != byteSizeLen {
byteSize++
byteSizeLen++
}
if byteSizeLen < 9 {
// can only use compact notation if total byte length is at most 8 bytes long
if isArray {
b.buf[tos] = 0x13
} else {
b.buf[tos] = 0x14
}
valuesLen := b.buf.Len() - (tos + 9) // Amount of bytes taken up by array/object values.
if valuesLen > 0 && byteSizeLen < 8 {
// We have array/object values and our byteSize needs less than the pre-allocated 8 bytes.
// So we move the array/object values back.
checkOverflow(valuesLen)
src := b.buf[tos+9:]
copy(b.buf[tos+1+byteSizeLen:], src[:valuesLen])
}
// Shrink buffer, removing unused space allocated for byteSize.
b.buf.Shrink(uint(8 - byteSizeLen))
// store byte length
vpackAssert(byteSize > 0)
storeVariableValueLength(b.buf, tos+1, byteSize, false)
// store nrItems
b.buf.Grow(uint(nrItemsLen))
storeVariableValueLength(b.buf, tos+byteSize-1, ValueLength(len(index)), true)
b.stack.Pop()
return true
}
return false
}
// checkAttributeUniqueness checks the given slice for duplicate keys.
// It returns an error when duplicate keys are found, nil otherwise.
func (b *Builder) checkAttributeUniqueness(obj Slice) error {
vpackAssert(b.BuilderOptions.CheckAttributeUniqueness)
n, err := obj.Length()
if err != nil {
return WithStack(err)
}
if obj.IsSorted() {
// object attributes are sorted
previous, err := obj.KeyAt(0)
if err != nil {
return WithStack(err)
}
p, err := previous.GetString()
if err != nil {
return WithStack(err)
}
// compare each two adjacent attribute names
for i := ValueLength(1); i < n; i++ {
current, err := obj.KeyAt(i)
if err != nil {
return WithStack(err)
}
// keyAt() guarantees a string as returned type
vpackAssert(current.IsString())
q, err := current.GetString()
if err != nil {
return WithStack(err)
}
if p == q {
// identical key
return WithStack(DuplicateAttributeNameError)
}
// re-use already calculated values for next round
p = q
}
} else {
keys := make(map[string]struct{})
for i := ValueLength(0); i < n; i++ {
// note: keyAt() already translates integer attributes
key, err := obj.KeyAt(i)
if err != nil {
return WithStack(err)
}
// keyAt() guarantees a string as returned type
vpackAssert(key.IsString())
k, err := key.GetString()
if err != nil {
return WithStack(err)
}
if _, found := keys[k]; found {
return WithStack(DuplicateAttributeNameError)
}
keys[k] = struct{}{}
}
}
return nil
}
func findAttrName(base []byte) ([]byte, error) {
b := base[0]
if b >= 0x40 && b <= 0xbe {
// short UTF-8 string
l := b - 0x40
return base[1 : 1+l], nil
}
if b == 0xbf {
// long UTF-8 string
l := uint(0)
// read string length
for i := 8; i >= 1; i-- {
l = (l << 8) + uint(base[i])
}
return base[1+8 : 1+8+l], nil
}
// translate attribute name
key, err := Slice(base).makeKey()
if err != nil {
return nil, WithStack(err)
}
return findAttrName(key)
}
func (b *Builder) sortObjectIndex(objBase []byte, offsets []ValueLength) error {
list := make(sortEntries, len(offsets))
for i, off := range offsets {
name, err := findAttrName(objBase[off:])
if err != nil {
return WithStack(err)
}
list[i] = sortEntry{
Offset: off,
Name: name,
}
}
list.Sort()
//sort.Sort(list)
for i, entry := range list {
offsets[i] = entry.Offset
}
return nil
}
func (b *Builder) closeArray(tos ValueLength, index []ValueLength) {
// fix head byte in case a compact Array was originally requested:
b.buf[tos] = 0x06
needIndexTable := true
needNrSubs := true
if len(index) == 1 {
needIndexTable = false
needNrSubs = false
} else if (b.buf.Len()-tos)-index[0] == ValueLength(len(index))*(index[1]-index[0]) {
// In this case it could be that all entries have the same length
// and we do not need an offset table at all:
noTable := true
subLen := index[1] - index[0]
if (b.buf.Len()-tos)-index[len(index)-1] != subLen {
noTable = false
} else {
for i := 1; i < len(index)-1; i++ {
if index[i+1]-index[i] != subLen {
noTable = false
break
}
}
}
if noTable {
needIndexTable = false
needNrSubs = false
}
}
// First determine byte length and its format:
var offsetSize uint
// can be 1, 2, 4 or 8 for the byte width of the offsets,
// the byte length and the number of subvalues:
var indexLenIfNeeded ValueLength
if needIndexTable {
indexLenIfNeeded = ValueLength(len(index))
}
nrSubsLenIfNeeded := ValueLength(7)
if needNrSubs {
nrSubsLenIfNeeded = 6
}
if b.buf.Len()-tos+(indexLenIfNeeded)-(nrSubsLenIfNeeded) <= 0xff {
// We have so far used _pos - tos bytes, including the reserved 8
// bytes for byte length and number of subvalues. In the 1-byte number
// case we would win back 6 bytes but would need one byte per subvalue
// for the index table
offsetSize = 1
} else if b.buf.Len()-tos+(indexLenIfNeeded*2) <= 0xffff {
offsetSize = 2
} else if b.buf.Len()-tos+(indexLenIfNeeded*4) <= 0xffffffff {
offsetSize = 4
} else {
offsetSize = 8
}
// Maybe we need to move down data:
if offsetSize == 1 {
targetPos := ValueLength(3)
if !needIndexTable {
targetPos = 2
}
if b.buf.Len() > (tos + 9) {
_len := ValueLength(b.buf.Len() - (tos + 9))
checkOverflow(_len)
src := b.buf[tos+9:]
copy(b.buf[tos+targetPos:], src[:_len])
}
diff := ValueLength(9 - targetPos)
b.buf.Shrink(uint(diff))
if needIndexTable {
n := len(index)
for i := 0; i < n; i++ {
index[i] -= diff
}
} // Note: if !needIndexTable the index array is now wrong!
}
// One could move down things in the offsetSize == 2 case as well,
// since we only need 4 bytes in the beginning. However, saving these
// 4 bytes has been sacrificed on the Altar of Performance.
// Now build the table:
if needIndexTable {
extraSpaceNeeded := offsetSize * uint(len(index))
if offsetSize == 8 {
extraSpaceNeeded += 8
}
b.buf.ReserveSpace(extraSpaceNeeded)
tableBase := b.buf.Grow(offsetSize * uint(len(index)))
for i := uint(0); i < uint(len(index)); i++ {
x := uint64(index[i])
for j := uint(0); j < offsetSize; j++ {
tableBase[offsetSize*i+j] = byte(x & 0xff)
x >>= 8
}
}
} else { // no index table
b.buf[tos] = 0x02
}
// Finally fix the byte width in the type byte:
if offsetSize > 1 {
if offsetSize == 2 {
b.buf[tos] += 1
} else if offsetSize == 4 {
b.buf[tos] += 2
} else { // offsetSize == 8
b.buf[tos] += 3
if needNrSubs {
b.appendLength(ValueLength(len(index)), 8)
}
}
}
// Fix the byte length in the beginning:
x := ValueLength(b.buf.Len() - tos)
for i := uint(1); i <= offsetSize; i++ {
b.buf[tos+ValueLength(i)] = byte(x & 0xff)
x >>= 8
}
if offsetSize < 8 && needNrSubs {
x = ValueLength(len(index))
for i := offsetSize + 1; i <= 2*offsetSize; i++ {
b.buf[tos+ValueLength(i)] = byte(x & 0xff)
x >>= 8
}
}
// Now the array or object is complete, we pop a ValueLength
// off the _stack:
b.stack.Pop()
// Intentionally leave _index[depth] intact to avoid future allocs!
}
func (b *Builder) cleanupAdd() {
depth := b.stack.Len() - 1
b.index[depth].RemoveLast()
}
func (b *Builder) reportAdd() {
tos, stackLen := b.stack.Tos()
depth := stackLen - 1