Skip to content

Commit 6d76e4e

Browse files
committed
HTTP: Improved performance by using a larger buffer
1 parent e6a40ec commit 6d76e4e

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

core/src/main/java/com/orientechnologies/orient/core/serialization/serializer/record/string/ORecordSerializerJSON.java

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@
1515
*/
1616
package com.orientechnologies.orient.core.serialization.serializer.record.string;
1717

18-
import java.io.IOException;
19-
import java.io.StringWriter;
20-
import java.text.ParseException;
21-
import java.util.Collection;
22-
import java.util.HashMap;
23-
import java.util.LinkedHashMap;
24-
import java.util.List;
25-
import java.util.Map;
26-
import java.util.Set;
27-
2818
import com.orientechnologies.common.parser.OStringParser;
2919
import com.orientechnologies.orient.core.Orient;
3020
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
@@ -59,18 +49,33 @@
5949
import com.orientechnologies.orient.core.util.ODateHelper;
6050
import com.orientechnologies.orient.core.version.ODistributedVersion;
6151

52+
import java.io.IOException;
53+
import java.io.StringWriter;
54+
import java.text.ParseException;
55+
import java.util.Collection;
56+
import java.util.HashMap;
57+
import java.util.LinkedHashMap;
58+
import java.util.List;
59+
import java.util.Map;
60+
import java.util.Set;
61+
6262
@SuppressWarnings("serial")
6363
public class ORecordSerializerJSON extends ORecordSerializerStringAbstract {
6464

6565
public static final String NAME = "json";
6666
public static final ORecordSerializerJSON INSTANCE = new ORecordSerializerJSON();
6767
public static final String ATTRIBUTE_FIELD_TYPES = "@fieldTypes";
6868
public static final char[] PARAMETER_SEPARATOR = new char[] { ':', ',' };
69+
public static final int INITIAL_SIZE = 5000;
6970
private static final Long MAX_INT = (long) Integer.MAX_VALUE;
7071
private static final Long MIN_INT = (long) Integer.MIN_VALUE;
7172
private static final Double MAX_FLOAT = (double) Float.MAX_VALUE;
7273
private static final Double MIN_FLOAT = (double) Float.MIN_VALUE;
7374

75+
private interface CollectionItemVisitor {
76+
void visitItem(Object item);
77+
}
78+
7479
public class FormatSettings {
7580
public boolean includeVer;
7681
public boolean includeType;
@@ -327,7 +332,7 @@ public StringBuilder toString(final ORecordInternal<?> iRecord, final StringBuil
327332
final OUserObject2RecordHandler iObjHandler, final Set<ODocument> iMarshalledRecords, boolean iOnlyDelta,
328333
boolean autoDetectCollectionType) {
329334
try {
330-
final StringWriter buffer = new StringWriter();
335+
final StringWriter buffer = new StringWriter(INITIAL_SIZE);
331336
final OJSONWriter json = new OJSONWriter(buffer, iFormat);
332337
final FormatSettings settings = new FormatSettings(iFormat);
333338

@@ -411,7 +416,7 @@ private Object getValue(final ODocument iRecord, String iFieldName, String iFiel
411416
else if (OStringSerializerHelper.contains(iFieldValue, '.')) {
412417
// DECIMAL FORMAT: DETERMINE IF DOUBLE OR FLOAT
413418
final Double v = new Double(OStringSerializerHelper.getStringContent(iFieldValue));
414-
419+
415420
if (canBeTrunkedToFloat(v))
416421
return v.floatValue();
417422
else
@@ -530,19 +535,20 @@ else if (OStringSerializerHelper.contains(iFieldValue, '.')) {
530535
}
531536

532537
private boolean canBeTrunkedToInt(Long v) {
533-
return (v > 0)?v.compareTo(MAX_INT) <= 0:v.compareTo(MIN_INT) >= 0;
538+
return (v > 0) ? v.compareTo(MAX_INT) <= 0 : v.compareTo(MIN_INT) >= 0;
534539
}
535540

536541
private boolean canBeTrunkedToFloat(Double v) {
537-
//TODO not really correct check. Small numbers with high precision will be trunked while they shouldn't be
542+
// TODO not really correct check. Small numbers with high precision will be trunked while they shouldn't be
538543

539-
return (v > 0)? v.compareTo(MAX_FLOAT) <= 0: v.compareTo(MIN_FLOAT) >= 0;
544+
return (v > 0) ? v.compareTo(MAX_FLOAT) <= 0 : v.compareTo(MIN_FLOAT) >= 0;
540545
}
541546

542547
/**
543548
* OBJECT OR MAP. CHECK THE TYPE ATTRIBUTE TO KNOW IT.
544549
*/
545-
private Object getValueAsObjectOrMap(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
550+
private Object getValueAsObjectOrMap(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType,
551+
Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
546552
final String[] fields = OStringParser.getWords(iFieldValue.substring(1, iFieldValue.length() - 1), ":,", true);
547553

548554
if (fields == null || fields.length == 0)
@@ -558,10 +564,10 @@ private Object getValueAsObjectOrMap(ODocument iRecord, String iFieldValue, OTyp
558564
}
559565
}
560566

561-
private Object getValueAsMap(ODocument iRecord, String iFieldValue, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions, String[] fields) {
567+
private Object getValueAsMap(ODocument iRecord, String iFieldValue, OType iLinkedType, Map<String, Character> iFieldTypes,
568+
boolean iNoMap, String iOptions, String[] fields) {
562569
if (fields.length % 2 == 1)
563-
throw new OSerializationException("Bad JSON format on map. Expected pairs of field:value but received '"
564-
+ iFieldValue + "'");
570+
throw new OSerializationException("Bad JSON format on map. Expected pairs of field:value but received '" + iFieldValue + "'");
565571

566572
final Map<String, Object> embeddedMap = new LinkedHashMap<String, Object>();
567573

@@ -586,7 +592,8 @@ private Object getValueAsRecord(ODocument iRecord, String iFieldValue, OType iTy
586592
return recordInternal;
587593
}
588594

589-
private Object getValueAsCollection(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
595+
private Object getValueAsCollection(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType,
596+
Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
590597
// remove square brackets
591598
iFieldValue = iFieldValue.substring(1, iFieldValue.length() - 1);
592599

@@ -602,17 +609,22 @@ public void visitItem(Object item) {
602609

603610
return bag;
604611
} else if (iType == OType.LINKSET) {
605-
return getValueAsLinkedCollection(new OMVRBTreeRIDSet(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
612+
return getValueAsLinkedCollection(new OMVRBTreeRIDSet(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes,
613+
iNoMap, iOptions);
606614
} else if (iType == OType.LINKLIST) {
607-
return getValueAsLinkedCollection(new ORecordLazyList(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
615+
return getValueAsLinkedCollection(new ORecordLazyList(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes,
616+
iNoMap, iOptions);
608617
} else if (iType == OType.EMBEDDEDSET) {
609-
return getValueAsEmbeddedCollection(new OTrackedSet<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
618+
return getValueAsEmbeddedCollection(new OTrackedSet<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes,
619+
iNoMap, iOptions);
610620
} else {
611-
return getValueAsEmbeddedCollection(new OTrackedList<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
621+
return getValueAsEmbeddedCollection(new OTrackedList<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes,
622+
iNoMap, iOptions);
612623
}
613624
}
614625

615-
private Object getValueAsLinkedCollection(final Collection<OIdentifiable> collection, ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
626+
private Object getValueAsLinkedCollection(final Collection<OIdentifiable> collection, ODocument iRecord, String iFieldValue,
627+
OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
616628

617629
parseCollection(iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions, new CollectionItemVisitor() {
618630
@Override
@@ -624,7 +636,8 @@ public void visitItem(Object item) {
624636
return collection;
625637
}
626638

627-
private Object getValueAsEmbeddedCollection(final Collection<Object> collection, ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
639+
private Object getValueAsEmbeddedCollection(final Collection<Object> collection, ODocument iRecord, String iFieldValue,
640+
OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
628641

629642
parseCollection(iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions, new CollectionItemVisitor() {
630643
@Override
@@ -637,16 +650,15 @@ public void visitItem(Object item) {
637650
}
638651

639652
private void parseCollection(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType,
640-
Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions,
641-
CollectionItemVisitor visitor) {
653+
Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions, CollectionItemVisitor visitor) {
642654
if (!iFieldValue.isEmpty()) {
643655
for (String item : OStringSerializerHelper.smartSplit(iFieldValue, ',')) {
644656
final String itemValue = item.trim();
645657

646-
final Object collectionItem = getValue(iRecord, null, itemValue, OStringSerializerHelper.getStringContent(itemValue), iLinkedType, null, iFieldTypes, iNoMap,
647-
iOptions);
658+
final Object collectionItem = getValue(iRecord, null, itemValue, OStringSerializerHelper.getStringContent(itemValue),
659+
iLinkedType, null, iFieldTypes, iNoMap, iOptions);
648660

649-
//TODO redundant in some cases, owner is already added by getValue in some cases
661+
// TODO redundant in some cases, owner is already added by getValue in some cases
650662
if (shouldBeDeserializedAsEmbedded(collectionItem, iType))
651663
((ODocument) collectionItem).addOwner(iRecord);
652664

@@ -657,10 +669,6 @@ private void parseCollection(ODocument iRecord, String iFieldValue, OType iType,
657669
}
658670
}
659671
}
660-
661-
private interface CollectionItemVisitor {
662-
void visitItem(Object item);
663-
}
664672

665673
private boolean shouldBeDeserializedAsEmbedded(Object record, OType iType) {
666674
return record instanceof ODocument && !((ODocument) record).getIdentity().isTemporary()

0 commit comments

Comments
 (0)