diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-03-31 09:07:54 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2022-03-31 09:15:27 +0200 |
commit | 8f2d1c2197f9a2b7a76b97e37ef26488d4253338 (patch) | |
tree | 2ef68d9d61a6f216bffa8bce7b882201cdfa86bf /document | |
parent | 0ce79641b9c8bb4cb12ecd1a61b052e4c4742efe (diff) |
Avoid using reflection for creating values from a String. That is done frequently when parsing json.
Diffstat (limited to 'document')
17 files changed, 48 insertions, 60 deletions
diff --git a/document/abi-spec.json b/document/abi-spec.json index 661bc35bb83..d7cc513619f 100644 --- a/document/abi-spec.json +++ b/document/abi-spec.json @@ -922,7 +922,8 @@ ], "methods": [ "public void <init>()", - "public abstract com.yahoo.document.datatypes.FieldValue create()" + "public abstract com.yahoo.document.datatypes.FieldValue create()", + "public abstract com.yahoo.document.datatypes.FieldValue create(java.lang.String)" ], "fields": [] }, @@ -936,6 +937,7 @@ "protected void <init>(java.lang.String, int, java.lang.Class, com.yahoo.document.PrimitiveDataType$Factory)", "public com.yahoo.document.PrimitiveDataType clone()", "public com.yahoo.document.datatypes.FieldValue createFieldValue()", + "public com.yahoo.document.datatypes.FieldValue createFieldValue(java.lang.Object)", "public java.lang.Class getValueClass()", "public boolean isValueCompatible(com.yahoo.document.datatypes.FieldValue)", "public com.yahoo.document.PrimitiveDataType getPrimitiveType()", @@ -2345,18 +2347,6 @@ ], "fields": [] }, - "com.yahoo.document.datatypes.UriFieldValue$Factory": { - "superClass": "com.yahoo.document.PrimitiveDataType$Factory", - "interfaces": [], - "attributes": [ - "public" - ], - "methods": [ - "public void <init>()", - "public com.yahoo.document.datatypes.FieldValue create()" - ], - "fields": [] - }, "com.yahoo.document.datatypes.UriFieldValue": { "superClass": "com.yahoo.document.datatypes.StringFieldValue", "interfaces": [], @@ -2364,6 +2354,7 @@ "public" ], "methods": [ + "public static com.yahoo.document.datatypes.UriFieldValue$Factory getFactory()", "public void <init>()", "public void <init>(java.lang.String)", "public void assign(java.lang.Object)", diff --git a/document/src/main/java/com/yahoo/document/DataType.java b/document/src/main/java/com/yahoo/document/DataType.java index 2fcbe7333b4..d5de78cd21d 100644 --- a/document/src/main/java/com/yahoo/document/DataType.java +++ b/document/src/main/java/com/yahoo/document/DataType.java @@ -50,7 +50,7 @@ public abstract class DataType extends Identifiable implements Serializable, Com public final static PrimitiveDataType BOOL = new PrimitiveDataType("bool", 6, BoolFieldValue.class, BoolFieldValue.getFactory()); public final static NumericDataType FLOAT16 = new NumericDataType("float16", 7, Float16FieldValue.class, Float16FieldValue.getFactory()); public final static DocumentType DOCUMENT = new DocumentType("document"); - public final static PrimitiveDataType URI = new PrimitiveDataType("uri", 10, UriFieldValue.class, new UriFieldValue.Factory()); + public final static PrimitiveDataType URI = new PrimitiveDataType("uri", 10, UriFieldValue.class, UriFieldValue.getFactory()); public final static NumericDataType BYTE = new NumericDataType("byte", 16, ByteFieldValue.class, ByteFieldValue.getFactory()); final static int TAG_ID = 18; public final static PrimitiveDataType PREDICATE = new PrimitiveDataType("predicate", 20, PredicateFieldValue.class, PredicateFieldValue.getFactory()); diff --git a/document/src/main/java/com/yahoo/document/NumericDataType.java b/document/src/main/java/com/yahoo/document/NumericDataType.java index 052c18866fa..e8fbdb8ad2a 100644 --- a/document/src/main/java/com/yahoo/document/NumericDataType.java +++ b/document/src/main/java/com/yahoo/document/NumericDataType.java @@ -1,6 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.document; +import com.yahoo.document.datatypes.FieldValue; import com.yahoo.vespa.objects.Ids; /** @@ -17,7 +18,7 @@ public class NumericDataType extends PrimitiveDataType { * @param code the code (id) of the type * @param type the field value used for this type */ - protected NumericDataType(java.lang.String name, int code, Class type, Factory factory) { + protected NumericDataType(java.lang.String name, int code, Class<? extends FieldValue> type, Factory factory) { super(name, code, type, factory); } diff --git a/document/src/main/java/com/yahoo/document/PrimitiveDataType.java b/document/src/main/java/com/yahoo/document/PrimitiveDataType.java index b653953dce9..5b59170797a 100644 --- a/document/src/main/java/com/yahoo/document/PrimitiveDataType.java +++ b/document/src/main/java/com/yahoo/document/PrimitiveDataType.java @@ -14,6 +14,7 @@ public class PrimitiveDataType extends DataType { public static abstract class Factory { public abstract FieldValue create(); + public abstract FieldValue create(String value); } // The global class identifier shared with C++. @@ -41,9 +42,16 @@ public class PrimitiveDataType extends DataType { return (PrimitiveDataType)super.clone(); } + @Override public FieldValue createFieldValue() { return factory.create(); } + @Override + public FieldValue createFieldValue(Object arg) { + if (arg == null) return factory.create(); + if (arg instanceof String) return factory.create((String)arg); + return super.createFieldValue(arg); + } @Override public Class<? extends FieldValue> getValueClass() { diff --git a/document/src/main/java/com/yahoo/document/datatypes/BoolFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/BoolFieldValue.java index 6d2b12405b4..962ed6b1817 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/BoolFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/BoolFieldValue.java @@ -18,9 +18,8 @@ import com.yahoo.vespa.objects.Ids; public class BoolFieldValue extends FieldValue { private static class Factory extends PrimitiveDataType.Factory { - public FieldValue create() { - return new BoolFieldValue(); - } + @Override public FieldValue create() { return new BoolFieldValue(); } + @Override public FieldValue create(String value) { return new BoolFieldValue(value); } } public static PrimitiveDataType.Factory getFactory() { return new Factory(); } diff --git a/document/src/main/java/com/yahoo/document/datatypes/ByteFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/ByteFieldValue.java index eefc33c9d85..bf010b4715d 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/ByteFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/ByteFieldValue.java @@ -18,9 +18,8 @@ import com.yahoo.vespa.objects.Ids; public class ByteFieldValue extends NumericFieldValue { private static class Factory extends PrimitiveDataType.Factory { - public FieldValue create() { - return new ByteFieldValue(); - } + @Override public FieldValue create() { return new ByteFieldValue(); } + @Override public FieldValue create(String value) { return new ByteFieldValue(value); } } public static PrimitiveDataType.Factory getFactory() { return new Factory(); } diff --git a/document/src/main/java/com/yahoo/document/datatypes/DoubleFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/DoubleFieldValue.java index 724671e4ff1..55dfff2a954 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/DoubleFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/DoubleFieldValue.java @@ -18,9 +18,8 @@ import com.yahoo.vespa.objects.Ids; public final class DoubleFieldValue extends NumericFieldValue { private static class Factory extends PrimitiveDataType.Factory { - public FieldValue create() { - return new DoubleFieldValue(); - } + @Override public FieldValue create() { return new DoubleFieldValue(); } + @Override public FieldValue create(String value) { return new DoubleFieldValue(value); } } public static PrimitiveDataType.Factory getFactory() { return new Factory(); } diff --git a/document/src/main/java/com/yahoo/document/datatypes/FieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/FieldValue.java index 268390543cf..493dd964b9e 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/FieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/FieldValue.java @@ -95,7 +95,7 @@ public abstract class FieldValue extends Identifiable implements Comparable<Fiel return this; } - class RecursiveIteratorHandler extends FieldPathIteratorHandler { + static class RecursiveIteratorHandler extends FieldPathIteratorHandler { FieldValue retVal = null; boolean multiValue = false; @@ -109,9 +109,9 @@ public abstract class FieldValue extends Identifiable implements Comparable<Fiel public void onPrimitive(FieldValue fv) { if (retVal != null) { if (multiValue) { - ((Array) retVal).add(fv); + ((Array<FieldValue>) retVal).add(fv); } else { - Array afv = new Array(new ArrayDataType(retVal.getDataType())); + Array<FieldValue> afv = new Array<>(new ArrayDataType(retVal.getDataType())); afv.add(retVal); afv.add(fv); retVal = afv; diff --git a/document/src/main/java/com/yahoo/document/datatypes/Float16FieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/Float16FieldValue.java index 12e2507587e..f9cf7419e01 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/Float16FieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/Float16FieldValue.java @@ -18,9 +18,8 @@ import com.yahoo.vespa.objects.Ids; public final class Float16FieldValue extends NumericFieldValue { private static class Factory extends PrimitiveDataType.Factory { - public FieldValue create() { - return new Float16FieldValue(); - } + @Override public FieldValue create() { return new Float16FieldValue(); } + @Override public FieldValue create(String value) { return new Float16FieldValue(value); } } public static PrimitiveDataType.Factory getFactory() { return new Factory(); } diff --git a/document/src/main/java/com/yahoo/document/datatypes/FloatFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/FloatFieldValue.java index 27a6d3570b5..5fa850f69a3 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/FloatFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/FloatFieldValue.java @@ -18,9 +18,8 @@ import com.yahoo.vespa.objects.Ids; public final class FloatFieldValue extends NumericFieldValue { private static class Factory extends PrimitiveDataType.Factory { - public FieldValue create() { - return new FloatFieldValue(); - } + @Override public FieldValue create() { return new FloatFieldValue(); } + @Override public FieldValue create(String value) { return new FloatFieldValue(value); } } public static PrimitiveDataType.Factory getFactory() { return new Factory(); } diff --git a/document/src/main/java/com/yahoo/document/datatypes/IntegerFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/IntegerFieldValue.java index df2d92bcfba..c02122525b1 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/IntegerFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/IntegerFieldValue.java @@ -18,9 +18,8 @@ import com.yahoo.vespa.objects.Ids; public final class IntegerFieldValue extends NumericFieldValue { private static class Factory extends PrimitiveDataType.Factory { - public FieldValue create() { - return new IntegerFieldValue(); - } + @Override public FieldValue create() { return new IntegerFieldValue(); } + @Override public FieldValue create(String value) { return new IntegerFieldValue(value); } } public static PrimitiveDataType.Factory getFactory() { return new Factory(); } diff --git a/document/src/main/java/com/yahoo/document/datatypes/LongFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/LongFieldValue.java index 44fbd1ba5e1..da6b91e4071 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/LongFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/LongFieldValue.java @@ -18,9 +18,8 @@ import com.yahoo.vespa.objects.Ids; public final class LongFieldValue extends NumericFieldValue { private static class Factory extends PrimitiveDataType.Factory { - public FieldValue create() { - return new LongFieldValue(); - } + @Override public FieldValue create() { return new LongFieldValue(); } + @Override public FieldValue create(String value) { return new LongFieldValue(value); } } public static PrimitiveDataType.Factory getFactory() { return new Factory(); } public static final int classId = registerClass(Ids.document + 12, LongFieldValue.class); diff --git a/document/src/main/java/com/yahoo/document/datatypes/PredicateFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/PredicateFieldValue.java index 393cb01f5c2..aba5e51f723 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/PredicateFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/PredicateFieldValue.java @@ -125,12 +125,10 @@ public class PredicateFieldValue extends FieldValue { } public static PrimitiveDataType.Factory getFactory() { - return new PrimitiveDataType.Factory() { - - @Override - public FieldValue create() { - return new PredicateFieldValue(); - } - }; + return new Factory(); + } + private static class Factory extends PrimitiveDataType.Factory { + @Override public FieldValue create() { return new PredicateFieldValue(); } + @Override public FieldValue create(String value) { return new PredicateFieldValue(value); } } } diff --git a/document/src/main/java/com/yahoo/document/datatypes/Raw.java b/document/src/main/java/com/yahoo/document/datatypes/Raw.java index ecd751327a3..f6efc626f06 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/Raw.java +++ b/document/src/main/java/com/yahoo/document/datatypes/Raw.java @@ -10,9 +10,9 @@ import com.yahoo.document.serialization.XmlSerializationHelper; import com.yahoo.document.serialization.XmlStream; import com.yahoo.vespa.objects.Ids; -import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import java.util.Arrays; +import java.util.Base64; /** * A field value which is an array of byte data @@ -22,9 +22,8 @@ import java.util.Arrays; public final class Raw extends FieldValue { private static class Factory extends PrimitiveDataType.Factory { - public FieldValue create() { - return new Raw(); - } + @Override public FieldValue create() { return new Raw(); } + @Override public FieldValue create(String value) { return new Raw(Base64.getMimeDecoder().decode(value)); } } public static final int classId = registerClass(Ids.document + 16, Raw.class); diff --git a/document/src/main/java/com/yahoo/document/datatypes/StringFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/StringFieldValue.java index 2d89d2c5db7..de96c548652 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/StringFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/StringFieldValue.java @@ -30,9 +30,8 @@ import java.util.OptionalInt; public class StringFieldValue extends FieldValue { private static class Factory extends PrimitiveDataType.Factory { - public FieldValue create() { - return new StringFieldValue(); - } + @Override public FieldValue create() { return new StringFieldValue(); } + @Override public FieldValue create(String value) { return new StringFieldValue(value); } } public static PrimitiveDataType.Factory getFactory() { return new Factory(); } diff --git a/document/src/main/java/com/yahoo/document/datatypes/UriFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/UriFieldValue.java index bf525de38ee..96ac73d0e0f 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/UriFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/UriFieldValue.java @@ -14,11 +14,12 @@ import java.net.URI; */ public class UriFieldValue extends StringFieldValue { - public static class Factory extends PrimitiveDataType.Factory { - public FieldValue create() { - return new UriFieldValue(); - } + private static class Factory extends PrimitiveDataType.Factory { + @Override public FieldValue create() { return new UriFieldValue(); } + @Override public FieldValue create(String value) { return new UriFieldValue(value); } } + public static Factory getFactory() { return new Factory(); } + public UriFieldValue() { super(); } public UriFieldValue(String value) { diff --git a/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java b/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java index 929beca1e10..0ccdbdaa9d5 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java +++ b/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java @@ -85,9 +85,7 @@ public class SingleValueReader { } public static FieldValue readAtomic(String field, DataType expectedType) { - if (expectedType.equals(DataType.RAW)) { - return expectedType.createFieldValue(Base64.getMimeDecoder().decode(field)); - } else if (expectedType.equals(PositionDataType.INSTANCE)) { + if (expectedType.equals(PositionDataType.INSTANCE)) { return PositionDataType.fromString(field); } else if (expectedType instanceof ReferenceDataType) { return readReferenceFieldValue(field, expectedType); |