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);
}
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));
}
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);
}
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());
}
}
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])));
}
Aggregations