23
23
import java .io .UnsupportedEncodingException ;
24
24
import java .math .BigDecimal ;
25
25
import java .math .BigInteger ;
26
+ import java .nio .charset .StandardCharsets ;
26
27
import java .sql .Timestamp ;
27
28
import java .util .ArrayList ;
28
29
import java .util .Arrays ;
46
47
import com .arangodb .velocypack .internal .DefaultVPackBuilderOptions ;
47
48
import com .arangodb .velocypack .internal .Value ;
48
49
import com .arangodb .velocypack .internal .util .NumberUtil ;
50
+ import com .fasterxml .jackson .core .io .CharTypes ;
49
51
50
52
/**
51
53
* @author Mark Vollmary
@@ -216,7 +218,7 @@ public void append(final VPackBuilder builder, final VPackSlice value) throws VP
216
218
private int size ;
217
219
private final List <Integer > stack ; // Start positions of open
218
220
// objects/arrays
219
- private final Map < Integer , List <Integer >> index ; // Indices for starts
221
+ private List <Integer >[] index ; // Indices for starts
220
222
// of
221
223
// subindex
222
224
private boolean keyWritten ; // indicates that in the current object the key
@@ -233,7 +235,7 @@ public VPackBuilder(final BuilderOptions options) {
233
235
size = 0 ;
234
236
buffer = new byte [10 ];
235
237
stack = new ArrayList <Integer >();
236
- index = new HashMap < Integer , List < Integer >>() ;
238
+ index = new List [ 4 ] ;
237
239
}
238
240
239
241
public BuilderOptions getOptions () {
@@ -546,7 +548,7 @@ private void set(final Value item) throws VPackBuilderException {
546
548
throw new VPackBuilderUnexpectedValueException (ValueType .UINT , Long .class , Integer .class ,
547
549
BigInteger .class );
548
550
}
549
- if (- 1 == vUInt .compareTo (BigInteger .ZERO )) {
551
+ if (vUInt .compareTo (BigInteger .ZERO ) < 0 ) {
550
552
throw new VPackBuilderUnexpectedValueException (ValueType .UINT , "non-negative" , Long .class ,
551
553
Integer .class , BigInteger .class );
552
554
}
@@ -621,21 +623,17 @@ private void appendSQLTimestamp(final Timestamp value) {
621
623
}
622
624
623
625
private void appendString (final String value ) throws VPackBuilderException {
624
- try {
625
- final byte [] bytes = value .getBytes ("UTF-8" );
626
- final int length = bytes .length ;
627
- if (length <= 126 ) {
628
- // short string
629
- add ((byte ) (0x40 + length ));
630
- } else {
631
- // long string
632
- add ((byte ) 0xbf );
633
- appendLength (length );
634
- }
635
- appendString (bytes );
636
- } catch (final UnsupportedEncodingException e ) {
637
- throw new VPackBuilderException (e );
626
+ final byte [] bytes = value .getBytes (StandardCharsets .UTF_8 );
627
+ final int length = bytes .length ;
628
+ if (length <= 126 ) {
629
+ // short string
630
+ add ((byte ) (0x40 + length ));
631
+ } else {
632
+ // long string
633
+ add ((byte ) 0xbf );
634
+ appendLength (length );
638
635
}
636
+ appendString (bytes );
639
637
}
640
638
641
639
private void appendString (final byte [] bytes ) {
@@ -670,7 +668,11 @@ private void addObject(final boolean unindexed) {
670
668
private void addCompoundValue (final byte head ) {
671
669
// an Array or Object is started:
672
670
stack .add (size );
673
- index .put (stack .size () - 1 , new ArrayList <Integer >());
671
+ if (index .length < stack .size ()) {
672
+ // ensureCapacity
673
+ index = Arrays .copyOf (index , index .length * 2 );
674
+ }
675
+ index [stack .size () - 1 ] = new ArrayList <Integer >();
674
676
add (head );
675
677
// Will be filled later with bytelength and nr subs
676
678
size += 8 ;
@@ -682,12 +684,12 @@ private void appendLength(final long length) {
682
684
}
683
685
684
686
private void reportAdd () {
685
- final Collection <Integer > depth = index . get ( stack .size () - 1 ) ;
687
+ final Collection <Integer > depth = index [ stack .size () - 1 ] ;
686
688
depth .add (size - stack .get (stack .size () - 1 ));
687
689
}
688
690
689
691
private void cleanupAdd () {
690
- final List <Integer > depth = index . get ( stack .size () - 1 ) ;
692
+ final List <Integer > depth = index [ stack .size () - 1 ] ;
691
693
depth .remove (depth .size () - 1 );
692
694
}
693
695
@@ -708,7 +710,7 @@ protected VPackBuilder close(final boolean sort)
708
710
}
709
711
final byte head = head ();
710
712
final boolean isArray = head == 0x06 || head == 0x13 ;
711
- final List <Integer > in = index . get ( stack .size () - 1 ) ;
713
+ final List <Integer > in = index [ stack .size () - 1 ] ;
712
714
final int tos = stack .get (stack .size () - 1 );
713
715
if (in .isEmpty ()) {
714
716
return closeEmptyArrayOrObject (tos , isArray );
@@ -990,10 +992,10 @@ private VPackBuilder closeArray(final int tos, final List<Integer> in) {
990
992
}
991
993
992
994
private static class SortEntry {
993
- private final VPackSlice slice ;
995
+ private final VPackStringSlice slice ;
994
996
private final int offset ;
995
997
996
- public SortEntry (final VPackSlice slice , final int offset ) {
998
+ public SortEntry (final VPackStringSlice slice , final int offset ) {
997
999
super ();
998
1000
this .slice = slice ;
999
1001
this .offset = offset ;
@@ -1002,17 +1004,18 @@ public SortEntry(final VPackSlice slice, final int offset) {
1002
1004
1003
1005
private void sortObjectIndex (final int start , final List <Integer > offsets )
1004
1006
throws VPackKeyTypeException , VPackNeedAttributeTranslatorException {
1005
- final List <VPackBuilder .SortEntry > attributes = new ArrayList <VPackBuilder .SortEntry >();
1006
- for (final Integer offset : offsets ) {
1007
- attributes .add (new SortEntry (new VPackSlice (buffer , start + offset ).makeKey (), offset ));
1007
+ VPackBuilder .SortEntry [] attributes = new VPackBuilder .SortEntry [offsets .size ()];
1008
+ for (int i = 0 ; i < offsets .size (); i ++) {
1009
+ Integer offset = offsets .get (i );
1010
+ attributes [i ] = new SortEntry (new VPackSlice (buffer , start + offset ).makeKey ().getAsStringSlice (), offset );
1008
1011
}
1009
1012
final Comparator <SortEntry > comparator = new Comparator <SortEntry >() {
1010
1013
@ Override
1011
1014
public int compare (final SortEntry o1 , final SortEntry o2 ) {
1012
- return o1 .slice .getAsString (). compareTo (o2 .slice . getAsString () );
1015
+ return o1 .slice .compareTo (o2 .slice );
1013
1016
}
1014
1017
};
1015
- Collections .sort (attributes , comparator );
1018
+ Arrays .sort (attributes , comparator );
1016
1019
offsets .clear ();
1017
1020
for (final SortEntry sortEntry : attributes ) {
1018
1021
offsets .add (sortEntry .offset );
0 commit comments