From b4e3917d746fcf4be76d353be7c3b4ae9b229973 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Fri, 14 Dec 2018 11:28:18 +0100 Subject: Support for configuring and feeding float16 and bool field types --- .../src/main/java/com/yahoo/document/DataType.java | 7 +- .../java/com/yahoo/document/NumericDataType.java | 4 +- .../yahoo/document/datatypes/BoolFieldValue.java | 120 ++++++++++++++++++ .../yahoo/document/datatypes/ByteFieldValue.java | 12 +- .../document/datatypes/CollectionFieldValue.java | 5 +- .../document/datatypes/CompositeFieldValue.java | 2 + .../yahoo/document/datatypes/DoubleFieldValue.java | 6 +- .../datatypes/FieldPathIteratorHandler.java | 4 +- .../com/yahoo/document/datatypes/FieldValue.java | 1 + .../document/datatypes/Float16FieldValue.java | 137 +++++++++++++++++++++ .../yahoo/document/datatypes/FloatFieldValue.java | 11 +- .../document/datatypes/IntegerFieldValue.java | 4 +- .../yahoo/document/datatypes/LongFieldValue.java | 6 +- .../java/com/yahoo/document/datatypes/Raw.java | 2 +- .../document/datatypes/ReferenceFieldValue.java | 13 +- .../java/com/yahoo/document/datatypes/Struct.java | 4 +- .../yahoo/document/datatypes/UriFieldValue.java | 7 +- .../serialization/XmlSerializationHelper.java | 9 +- 18 files changed, 314 insertions(+), 40 deletions(-) create mode 100644 document/src/main/java/com/yahoo/document/datatypes/BoolFieldValue.java create mode 100644 document/src/main/java/com/yahoo/document/datatypes/Float16FieldValue.java (limited to 'document/src/main') diff --git a/document/src/main/java/com/yahoo/document/DataType.java b/document/src/main/java/com/yahoo/document/DataType.java index dfe55f5229c..fa5dffd042a 100644 --- a/document/src/main/java/com/yahoo/document/DataType.java +++ b/document/src/main/java/com/yahoo/document/DataType.java @@ -3,6 +3,7 @@ package com.yahoo.document; import com.yahoo.collections.Pair; import com.yahoo.concurrent.CopyOnWriteHashMap; +import com.yahoo.document.datatypes.BoolFieldValue; import com.yahoo.document.datatypes.ByteFieldValue; import com.yahoo.document.datatypes.DoubleFieldValue; import com.yahoo.document.datatypes.FieldValue; @@ -11,8 +12,8 @@ import com.yahoo.document.datatypes.IntegerFieldValue; import com.yahoo.document.datatypes.LongFieldValue; import com.yahoo.document.datatypes.PredicateFieldValue; import com.yahoo.document.datatypes.Raw; +import com.yahoo.document.datatypes.Float16FieldValue; import com.yahoo.document.datatypes.StringFieldValue; -import com.yahoo.document.datatypes.TensorFieldValue; import com.yahoo.document.datatypes.UriFieldValue; import com.yahoo.tensor.TensorType; import com.yahoo.vespa.objects.Identifiable; @@ -39,7 +40,6 @@ public abstract class DataType extends Identifiable implements Serializable, Com // NOTE: These types are also defined in // document/src/vespa/document/datatype/datatype.h // Changes here must also be done there - public final static NumericDataType NONE = new NumericDataType("none", -1, IntegerFieldValue.class, IntegerFieldValue.getFactory()); public final static NumericDataType INT = new NumericDataType("int", 0, IntegerFieldValue.class, IntegerFieldValue.getFactory()); public final static NumericDataType FLOAT = new NumericDataType("float", 1, FloatFieldValue.class, FloatFieldValue.getFactory()); @@ -47,6 +47,8 @@ public abstract class DataType extends Identifiable implements Serializable, Com public final static PrimitiveDataType RAW = new PrimitiveDataType("raw", 3, Raw.class, Raw.getFactory()); public final static NumericDataType LONG = new NumericDataType("long", 4, LongFieldValue.class, LongFieldValue.getFactory()); public final static NumericDataType DOUBLE = new NumericDataType("double", 5, DoubleFieldValue.class, DoubleFieldValue.getFactory()); + 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 NumericDataType BYTE = new NumericDataType("byte", 16, ByteFieldValue.class, ByteFieldValue.getFactory()); @@ -82,7 +84,6 @@ public abstract class DataType extends Identifiable implements Serializable, Com this.dataTypeId = dataTypeId; } - @SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException") public DataType clone() { return (DataType)super.clone(); } diff --git a/document/src/main/java/com/yahoo/document/NumericDataType.java b/document/src/main/java/com/yahoo/document/NumericDataType.java index f4a92eca5a1..26da90a709c 100644 --- a/document/src/main/java/com/yahoo/document/NumericDataType.java +++ b/document/src/main/java/com/yahoo/document/NumericDataType.java @@ -4,9 +4,10 @@ package com.yahoo.document; import com.yahoo.vespa.objects.Ids; /** - * @author Einar M R Rosenvinge + * @author Einar M R Rosenvinge */ public class NumericDataType extends PrimitiveDataType { + // The global class identifier shared with C++. public static int classId = registerClass(Ids.document + 52, NumericDataType.class); /** @@ -24,4 +25,5 @@ public class NumericDataType extends PrimitiveDataType { public NumericDataType clone() { return (NumericDataType) super.clone(); } + } diff --git a/document/src/main/java/com/yahoo/document/datatypes/BoolFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/BoolFieldValue.java new file mode 100644 index 00000000000..2a48b550658 --- /dev/null +++ b/document/src/main/java/com/yahoo/document/datatypes/BoolFieldValue.java @@ -0,0 +1,120 @@ +// Copyright 2019 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.document.datatypes; + +import com.yahoo.document.DataType; +import com.yahoo.document.Field; +import com.yahoo.document.PrimitiveDataType; +import com.yahoo.document.serialization.FieldReader; +import com.yahoo.document.serialization.FieldWriter; +import com.yahoo.document.serialization.XmlSerializationHelper; +import com.yahoo.document.serialization.XmlStream; +import com.yahoo.vespa.objects.Ids; + +/** + * A boolean field value + * + * @author bratseth + */ +public class BoolFieldValue extends FieldValue { + + private static class Factory extends PrimitiveDataType.Factory { + public FieldValue create() { + return new BoolFieldValue(); + } + } + + public static PrimitiveDataType.Factory getFactory() { return new Factory(); } + public static final int classId = registerClass(Ids.document + 17, BoolFieldValue.class); + private boolean value; + + public BoolFieldValue() { + this(false); + } + + public BoolFieldValue(boolean value) { + this.value = value; + } + + public BoolFieldValue(String s) { value = Boolean.parseBoolean(s); } + + @Override + public BoolFieldValue clone() { + return (BoolFieldValue)super.clone(); + } + + @Override + public void clear() { + value = false; + } + + @Override + public void assign(Object o) { + if ( ! checkAssign(o)) return; + if (o instanceof String || o instanceof StringFieldValue) { + value = Boolean.parseBoolean(o.toString()); + } else { + throw new IllegalArgumentException("Class " + o.getClass() + " not applicable to an " + this.getClass() + " instance."); + } + } + + public boolean getBoolean() { + return value; + } + + @Override + public Object getWrappedValue() { + return value; + } + + @Override + public DataType getDataType() { + return DataType.BOOL; + } + + @Override + public void printXml(XmlStream xml) { + XmlSerializationHelper.printBoolXml(this, xml); + } + + @Override + public String toString() { + return "" + value; + } + + @Override + public int hashCode() { + return super.hashCode() + ( value ? 3 : 0); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if ( ! (o instanceof BoolFieldValue)) return false; + if ( ! super.equals(o)) return false; + + BoolFieldValue that = (BoolFieldValue) o; + if (value != that.value) return false; + return true; + } + + @Override + public void serialize(Field field, FieldWriter writer) { + writer.write(field, this); + } + + /* (non-Javadoc) + * @see com.yahoo.document.datatypes.FieldValue#deserialize(com.yahoo.document.Field, com.yahoo.document.serialization.FieldReader) + */ + @Override + public void deserialize(Field field, FieldReader reader) { + reader.read(field, this); + } + + @Override + public int compareTo(FieldValue other) { + int comp = super.compareTo(other); + if (comp != 0) return comp; + return Boolean.compare(value, ((BoolFieldValue)other).value); + } + +} 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 4aa4c29db48..2f8cb96219c 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/ByteFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/ByteFieldValue.java @@ -11,16 +11,18 @@ import com.yahoo.document.serialization.XmlStream; import com.yahoo.vespa.objects.Ids; /** - * FieldValue which encapsulates a byte. + * A byte field value * - * @author Einar M R Rosenvinge + * @author Einar M R Rosenvinge */ public class ByteFieldValue extends NumericFieldValue { + private static class Factory extends PrimitiveDataType.Factory { public FieldValue create() { return new ByteFieldValue(); } } + public static PrimitiveDataType.Factory getFactory() { return new Factory(); } public static final int classId = registerClass(Ids.document + 10, ByteFieldValue.class); private byte value; @@ -125,9 +127,8 @@ public class ByteFieldValue extends NumericFieldValue { } /* (non-Javadoc) - * @see com.yahoo.document.datatypes.FieldValue#deserialize(com.yahoo.document.Field, com.yahoo.document.serialization.FieldReader) - */ - + * @see com.yahoo.document.datatypes.FieldValue#deserialize(com.yahoo.document.Field, com.yahoo.document.serialization.FieldReader) + */ @Override public void deserialize(Field field, FieldReader reader) { reader.read(field, this); @@ -151,4 +152,5 @@ public class ByteFieldValue extends NumericFieldValue { return 0; } } + } diff --git a/document/src/main/java/com/yahoo/document/datatypes/CollectionFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/CollectionFieldValue.java index fe135ccfd05..ad6d329cbae 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/CollectionFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/CollectionFieldValue.java @@ -7,9 +7,9 @@ import java.util.Collection; import java.util.Iterator; /** - * Date: Apr 16, 2008 + * Superclass of multivalue field values * - * @author Håkon Humberset + * @author Håkon Humberset */ public abstract class CollectionFieldValue extends CompositeFieldValue { @@ -79,4 +79,5 @@ public abstract class CollectionFieldValue extends Composi } public abstract int size(); + } diff --git a/document/src/main/java/com/yahoo/document/datatypes/CompositeFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/CompositeFieldValue.java index 3202c6b40af..6efd5a86f75 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/CompositeFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/CompositeFieldValue.java @@ -4,6 +4,7 @@ package com.yahoo.document.datatypes; import com.yahoo.document.DataType; public abstract class CompositeFieldValue extends FieldValue { + private DataType dataType; public CompositeFieldValue(DataType dataType) { @@ -36,4 +37,5 @@ public abstract class CompositeFieldValue extends FieldValue { result = 31 * result + (dataType != null ? dataType.hashCode() : 0); return result; } + } 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 0f1fe50818b..99bac017b78 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/DoubleFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/DoubleFieldValue.java @@ -11,16 +11,18 @@ import com.yahoo.document.serialization.XmlStream; import com.yahoo.vespa.objects.Ids; /** - * FieldValue which encapsulates a double. + * A 64-bit float field value * - * @author Einar M R Rosenvinge + * @author Einar M R Rosenvinge */ public final class DoubleFieldValue extends NumericFieldValue { + private static class Factory extends PrimitiveDataType.Factory { public FieldValue create() { return new DoubleFieldValue(); } } + public static PrimitiveDataType.Factory getFactory() { return new Factory(); } public static final int classId = registerClass(Ids.document + 14, DoubleFieldValue.class); private double value; diff --git a/document/src/main/java/com/yahoo/document/datatypes/FieldPathIteratorHandler.java b/document/src/main/java/com/yahoo/document/datatypes/FieldPathIteratorHandler.java index de645aff297..9189a97dbf6 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/FieldPathIteratorHandler.java +++ b/document/src/main/java/com/yahoo/document/datatypes/FieldPathIteratorHandler.java @@ -1,12 +1,11 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.document.datatypes; -import java.util.HashMap; import java.util.Map; import java.util.TreeMap; /** - * @author Thomas Gundersen + * @author Thomas Gundersen */ public abstract class FieldPathIteratorHandler { @@ -110,4 +109,5 @@ public abstract class FieldPathIteratorHandler { public VariableMap getVariables() { return variables; } + } 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 0460e303426..dc3fd36b367 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/FieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/FieldValue.java @@ -31,6 +31,7 @@ public abstract class FieldValue extends Identifiable implements ComparableEinar M R Rosenvinge + * @author Einar M R Rosenvinge */ public final class FloatFieldValue extends NumericFieldValue { + private static class Factory extends PrimitiveDataType.Factory { public FieldValue create() { return new FloatFieldValue(); } } + public static PrimitiveDataType.Factory getFactory() { return new Factory(); } public static final int classId = registerClass(Ids.document + 13, FloatFieldValue.class); private float value; @@ -120,9 +122,8 @@ public final class FloatFieldValue extends NumericFieldValue { } /* (non-Javadoc) - * @see com.yahoo.document.datatypes.FieldValue#deserialize(com.yahoo.document.Field, com.yahoo.document.serialization.FieldReader) - */ - + * @see com.yahoo.document.datatypes.FieldValue#deserialize(com.yahoo.document.Field, com.yahoo.document.serialization.FieldReader) + */ @Override public void deserialize(Field field, FieldReader reader) { reader.read(field, this); 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 19f34acde9a..62090bab8cb 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/IntegerFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/IntegerFieldValue.java @@ -11,9 +11,9 @@ import com.yahoo.document.serialization.XmlStream; import com.yahoo.vespa.objects.Ids; /** - * FieldValue which encapsulates an int. + * A 32-bit integer field value * - * @author Einar M R Rosenvinge + * @author Einar M R Rosenvinge */ public final class IntegerFieldValue extends NumericFieldValue { 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 2d3797f6735..45d044bcb9a 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/LongFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/LongFieldValue.java @@ -11,11 +11,12 @@ import com.yahoo.document.serialization.XmlStream; import com.yahoo.vespa.objects.Ids; /** - * FieldValue which encapsulates a long. + * A 64-bit integer field value * - * @author Einar M R Rosenvinge + * @author Einar M R Rosenvinge */ public final class LongFieldValue extends NumericFieldValue { + private static class Factory extends PrimitiveDataType.Factory { public FieldValue create() { return new LongFieldValue(); @@ -148,4 +149,5 @@ public final class LongFieldValue extends NumericFieldValue { return 0; } } + } 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 23ed0cee23e..7900615aa93 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/Raw.java +++ b/document/src/main/java/com/yahoo/document/datatypes/Raw.java @@ -15,7 +15,7 @@ import java.nio.ByteBuffer; import java.util.Arrays; /** - * FieldValue which encapsulates a Raw value + * A field value which is an array of byte data * * @author Einar M R Rosenvinge */ diff --git a/document/src/main/java/com/yahoo/document/datatypes/ReferenceFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/ReferenceFieldValue.java index 0097e5c93d0..034167b1121 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/ReferenceFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/ReferenceFieldValue.java @@ -14,21 +14,20 @@ import java.util.Objects; import java.util.Optional; /** - * A reference field value allows search queries to access fields in other document instances + *

A reference field value allows search queries to access fields in other document instances * as if they were fields natively stored within the searched document. This allows modelling * one-to-many relations such as a parent document with many children containing references - * back to the parent. + * back to the parent.

* - * Each ReferenceFieldValue may contain a single document ID which specifies the + *

Each ReferenceFieldValue may contain a single document ID which specifies the * instance the field should refer to. This document ID must have a type matching that of the - * reference data type of the field itself. + * reference data type of the field itself.

* - * Note that references are not polymorphic. This means that if you have a document type + *

Note that references are not polymorphic. This means that if you have a document type * "foo" inheriting "bar", you cannot have a reference<bar> field containing - * a document ID for a "foo" document. + * a document ID for a "foo" document.

* * @author vekterli - * @since 6.65 */ public class ReferenceFieldValue extends FieldValue { diff --git a/document/src/main/java/com/yahoo/document/datatypes/Struct.java b/document/src/main/java/com/yahoo/document/datatypes/Struct.java index 7d4e615b27b..fc75870bb94 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/Struct.java +++ b/document/src/main/java/com/yahoo/document/datatypes/Struct.java @@ -12,9 +12,7 @@ import com.yahoo.vespa.objects.Ids; import java.util.*; /** - * Date: Apr 15, 2008 - * - * @author humbe + * @author Håkon Humberset */ public class Struct extends StructuredFieldValue { 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 13688c8e311..1b51d6625e2 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/UriFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/UriFieldValue.java @@ -10,12 +10,10 @@ import com.yahoo.net.Url; import java.net.URI; /** - * Created with IntelliJ IDEA. - * User: magnarn - * Date: 11/2/12 - * Time: 2:37 PM + * @author Magnar Nedland */ public class UriFieldValue extends StringFieldValue { + public static class Factory extends PrimitiveDataType.Factory { public FieldValue create() { return new UriFieldValue(); @@ -46,4 +44,5 @@ public class UriFieldValue extends StringFieldValue { super.deserialize(field, reader); Url.fromString(toString()); // Throws if value is invalid. } + } diff --git a/document/src/main/java/com/yahoo/document/serialization/XmlSerializationHelper.java b/document/src/main/java/com/yahoo/document/serialization/XmlSerializationHelper.java index 941dbc8d406..a0e7f81ea73 100644 --- a/document/src/main/java/com/yahoo/document/serialization/XmlSerializationHelper.java +++ b/document/src/main/java/com/yahoo/document/serialization/XmlSerializationHelper.java @@ -7,7 +7,6 @@ import com.yahoo.document.datatypes.*; import com.yahoo.text.Utf8; import org.apache.commons.codec.binary.Base64; -import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -67,6 +66,14 @@ public class XmlSerializationHelper { xml.addContent(f.toString()); } + public static void printShortfloatXml(Float16FieldValue f, XmlStream xml) { + xml.addContent(f.toString()); + } + + public static void printBoolXml(BoolFieldValue f, XmlStream xml) { + xml.addContent(f.toString()); + } + public static void printIntegerXml(IntegerFieldValue f, XmlStream xml) { xml.addContent(f.toString()); } -- cgit v1.2.3