use of com.yahoo.document.MapDataType in project vespa by vespa-engine.
the class JsonWriterTestCase method registerMapDocumentType.
private void registerMapDocumentType() {
DocumentType x = new DocumentType("testmap");
DataType d = new MapDataType(DataType.STRING, DataType.STRING);
x.addField(new Field("actualmap", d));
types.registerDocumentType(x);
}
use of com.yahoo.document.MapDataType 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.MapDataType 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.MapDataType in project vespa by vespa-engine.
the class DocumentModelBuilder method resolveTemporariesRecurse.
@SuppressWarnings("deprecation")
private static DataType resolveTemporariesRecurse(DataType type, DataTypeCollection repo, Collection<NewDocumentType> docs) {
if (type instanceof TemporaryStructuredDataType) {
NewDocumentType docType = getDocumentType(docs, type.getId());
if (docType != null) {
type = docType;
return type;
}
DataType real = repo.getDataType(type.getId());
if (real == null) {
throw new NullPointerException("Can not find type '" + type.toString() + "', impossible.");
}
type = real;
} else if (type instanceof StructDataType) {
StructDataType dt = (StructDataType) type;
for (com.yahoo.document.Field field : dt.getFields()) {
if (field.getDataType() != type) {
// XXX deprecated:
field.setDataType(resolveTemporariesRecurse(field.getDataType(), repo, docs));
}
}
} else if (type instanceof MapDataType) {
MapDataType t = (MapDataType) type;
t.setKeyType(resolveTemporariesRecurse(t.getKeyType(), repo, docs));
t.setValueType(resolveTemporariesRecurse(t.getValueType(), repo, docs));
} else if (type instanceof CollectionDataType) {
CollectionDataType t = (CollectionDataType) type;
t.setNestedType(resolveTemporariesRecurse(t.getNestedType(), repo, docs));
} else if (type instanceof ReferenceDataType) {
ReferenceDataType t = (ReferenceDataType) type;
if (t.getTargetType() instanceof TemporaryStructuredDataType) {
DataType targetType = resolveTemporariesRecurse(t.getTargetType(), repo, docs);
t.setTargetType((StructuredDataType) targetType);
}
}
return type;
}
use of com.yahoo.document.MapDataType in project vespa by vespa-engine.
the class DocumentModelBuilder method extractNestedTypes.
private static void extractNestedTypes(NewDocumentType dt, DataType type) {
if (type instanceof StructDataType) {
StructDataType tmp = (StructDataType) type;
extractDataTypesFromFields(dt, tmp.getFieldsThisTypeOnly());
} else if (type instanceof DocumentType) {
throw new IllegalArgumentException("Can not handle nested document definitions. In document type '" + dt.getName().toString() + "', we can not define document type '" + type.toString());
} else if (type instanceof CollectionDataType) {
CollectionDataType tmp = (CollectionDataType) type;
extractNestedTypes(dt, tmp.getNestedType());
addType(dt, tmp.getNestedType());
} else if (type instanceof MapDataType) {
MapDataType tmp = (MapDataType) type;
extractNestedTypes(dt, tmp.getKeyType());
extractNestedTypes(dt, tmp.getValueType());
addType(dt, tmp.getKeyType());
addType(dt, tmp.getValueType());
} else if (type instanceof TemporaryAnnotationReferenceDataType) {
throw new IllegalArgumentException(type.toString());
}
}
Aggregations