Search in sources :

Example 1 with ArrayDataType

use of com.yahoo.document.ArrayDataType in project vespa by vespa-engine.

the class JsonWriterTestCase method registerArrayDocumentType.

private void registerArrayDocumentType() {
    DocumentType x = new DocumentType("testarray");
    DataType d = new ArrayDataType(DataType.STRING);
    x.addField(new Field("actualarray", d));
    types.registerDocumentType(x);
}
Also used : Field(com.yahoo.document.Field) DocumentType(com.yahoo.document.DocumentType) DataType(com.yahoo.document.DataType) ArrayDataType(com.yahoo.document.ArrayDataType) StructDataType(com.yahoo.document.StructDataType) MapDataType(com.yahoo.document.MapDataType) PositionDataType(com.yahoo.document.PositionDataType) TensorDataType(com.yahoo.document.TensorDataType) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) ReferenceDataType(com.yahoo.document.ReferenceDataType) ArrayDataType(com.yahoo.document.ArrayDataType)

Example 2 with ArrayDataType

use of com.yahoo.document.ArrayDataType in project vespa by vespa-engine.

the class MapTestCase method testAdvancedMap.

public void testAdvancedMap() {
    MapDataType stringMapType1 = new MapDataType(DataType.STRING, DataType.STRING);
    MapDataType stringMapType2 = new MapDataType(DataType.STRING, DataType.STRING);
    MapFieldValue sm1 = stringMapType1.createFieldValue();
    MapFieldValue sm2 = stringMapType2.createFieldValue();
    StringFieldValue e = new StringFieldValue("e");
    StringFieldValue g = new StringFieldValue("g");
    sm1.put(new StringFieldValue("a"), new StringFieldValue("b"));
    sm1.put(new StringFieldValue("c"), new StringFieldValue("d"));
    sm2.put(e, new StringFieldValue("f"));
    sm2.put(g, new StringFieldValue("h"));
    StructDataType structType = new StructDataType("teststr");
    structType.addField(new Field("int", DataType.INT));
    structType.addField(new Field("flt", DataType.FLOAT));
    Struct s = structType.createFieldValue();
    s.setFieldValue("int", 99);
    s.setFieldValue("flt", -89.345);
    ArrayDataType twoDimArray = DataType.getArray(DataType.getArray(DataType.FLOAT));
    Array tda = twoDimArray.createFieldValue();
    MapDataType floatToTwoDimArray = new MapDataType(DataType.FLOAT, twoDimArray);
    MapDataType stringToStruct = new MapDataType(DataType.STRING, structType);
    MapDataType stringMapToStringMap = new MapDataType(stringMapType1, stringMapType2);
    MapFieldValue f2tda = floatToTwoDimArray.createFieldValue();
    f2tda.put(new FloatFieldValue(3.4f), tda);
    MapFieldValue s2sct = stringToStruct.createFieldValue();
    s2sct.put(new StringFieldValue("s1"), s);
    MapFieldValue sm2sm = stringMapToStringMap.createFieldValue();
    sm2sm.put(sm1, sm2);
    assertEquals(f2tda.get(new FloatFieldValue(3.4f)), tda);
    assertEquals(new IntegerFieldValue(99), ((Struct) (s2sct.get(new StringFieldValue("s1")))).getFieldValue("int"));
    assertEquals(new StringFieldValue("f"), ((MapFieldValue) (sm2sm.get(sm1))).get(e));
    assertEquals(new StringFieldValue("h"), ((MapFieldValue) (sm2sm.get(sm1))).get(g));
    // Look up using different map w same contents
    // TODO it works even if sm1_2 is empty, something with class id?
    MapFieldValue sm1_2 = stringMapType1.createFieldValue();
    sm1_2.put(new StringFieldValue("a"), new StringFieldValue("b"));
    sm1_2.put(new StringFieldValue("c"), new StringFieldValue("d"));
    assertEquals(new StringFieldValue("f"), ((MapFieldValue) (sm2sm.get(sm1_2))).get(e));
    assertEquals(new StringFieldValue("h"), ((MapFieldValue) (sm2sm.get(sm1_2))).get(g));
}
Also used : Field(com.yahoo.document.Field) StructDataType(com.yahoo.document.StructDataType) MapDataType(com.yahoo.document.MapDataType) ArrayDataType(com.yahoo.document.ArrayDataType)

Example 3 with ArrayDataType

use of com.yahoo.document.ArrayDataType in project vespa by vespa-engine.

the class MapTestCase method testSerializationComplex.

public void testSerializationComplex() {
    ArrayDataType twoDimArray = DataType.getArray(DataType.getArray(DataType.FLOAT));
    MapDataType floatToTwoDimArray = new MapDataType(DataType.FLOAT, twoDimArray);
    MapFieldValue<FloatFieldValue, Array<Array<FloatFieldValue>>> map = floatToTwoDimArray.createFieldValue();
    Array<FloatFieldValue> af1 = new Array(DataType.getArray(DataType.FLOAT));
    af1.add(new FloatFieldValue(11f));
    af1.add(new FloatFieldValue(111f));
    Array<FloatFieldValue> af2 = new Array(DataType.getArray(DataType.FLOAT));
    af2.add(new FloatFieldValue(22f));
    af2.add(new FloatFieldValue(222f));
    Array<Array<FloatFieldValue>> aaf1 = new Array(twoDimArray);
    aaf1.add(af1);
    aaf1.add(af2);
    Array<FloatFieldValue> af3 = new Array(DataType.getArray(DataType.FLOAT));
    af3.add(new FloatFieldValue(33f));
    af3.add(new FloatFieldValue(333f));
    Array<FloatFieldValue> af4 = new Array(DataType.getArray(DataType.FLOAT));
    af4.add(new FloatFieldValue(44f));
    af4.add(new FloatFieldValue(444f));
    Array<Array<FloatFieldValue>> aaf2 = new Array(twoDimArray);
    aaf2.add(af3);
    aaf2.add(af4);
    map.put(new FloatFieldValue(1.1f), aaf1);
    map.put(new FloatFieldValue(2.2f), aaf2);
    assertCorrectSerialization(floatToTwoDimArray, map);
}
Also used : MapDataType(com.yahoo.document.MapDataType) ArrayDataType(com.yahoo.document.ArrayDataType)

Example 4 with ArrayDataType

use of com.yahoo.document.ArrayDataType in project vespa by vespa-engine.

the class ArrayTestCase method testCreateIllegalArray.

public void testCreateIllegalArray() {
    ArrayList<FieldValue> arrayList = new ArrayList<>();
    arrayList.add(new StringFieldValue("foo"));
    arrayList.add(new IntegerFieldValue(1000));
    DataType stringType = new ArrayDataType(DataType.STRING);
    try {
        Array<FieldValue> illegalArray = new Array<>(stringType, arrayList);
        fail("Expected an exception");
    } catch (IllegalArgumentException e) {
        assertEquals("FieldValue 1000 is not compatible with datatype " + "Array<string> (code: -1486737430).", e.getMessage());
    }
    DataType intType = new ArrayDataType(DataType.INT);
    Array<IntegerFieldValue> intArray = new Array<>(intType);
    intArray.add(new IntegerFieldValue(42));
    Array<StringFieldValue> stringArray = new Array<>(stringType);
    try {
        stringArray.assign(intArray);
        fail("Expected an exception");
    } catch (IllegalArgumentException e) {
        assertEquals("Incompatible data types. Got datatype int (code: 0)," + " expected datatype string (code: 2)", e.getMessage());
    }
}
Also used : DataType(com.yahoo.document.DataType) ArrayDataType(com.yahoo.document.ArrayDataType) ArrayDataType(com.yahoo.document.ArrayDataType)

Example 5 with ArrayDataType

use of com.yahoo.document.ArrayDataType in project vespa by vespa-engine.

the class ArrayTestCase method testListWrapperToArray.

public void testListWrapperToArray() {
    Array<StringFieldValue> array = new Array<>(new ArrayDataType(DataType.STRING));
    List<StringFieldValue> assignFrom = new ArrayList<>(3);
    assignFrom.add(new StringFieldValue("a"));
    assignFrom.add(new StringFieldValue("b"));
    assignFrom.add(new StringFieldValue("c"));
    array.assign(assignFrom);
    final StringFieldValue[] expected = new StringFieldValue[] { new StringFieldValue("a"), new StringFieldValue("b"), new StringFieldValue("c") };
    assertTrue(Arrays.equals(expected, array.toArray(new StringFieldValue[0])));
}
Also used : ArrayDataType(com.yahoo.document.ArrayDataType)

Aggregations

ArrayDataType (com.yahoo.document.ArrayDataType)20 DataType (com.yahoo.document.DataType)8 WeightedSetDataType (com.yahoo.document.WeightedSetDataType)8 Field (com.yahoo.document.Field)7 MapDataType (com.yahoo.document.MapDataType)6 DocumentType (com.yahoo.document.DocumentType)5 StructDataType (com.yahoo.document.StructDataType)5 PositionDataType (com.yahoo.document.PositionDataType)4 TensorDataType (com.yahoo.document.TensorDataType)4 FieldValue (com.yahoo.document.datatypes.FieldValue)4 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)4 ReferenceDataType (com.yahoo.document.ReferenceDataType)3 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)3 SDField (com.yahoo.searchdefinition.document.SDField)3 Test (org.junit.Test)3 StructuredDataType (com.yahoo.document.StructuredDataType)2 Array (com.yahoo.document.datatypes.Array)2 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)2 CollectionFieldValue (com.yahoo.document.datatypes.CollectionFieldValue)2 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)2