use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class SchemaMappingAndAccessesTest method testMappedDocStruct.
public void testMappedDocStruct() {
StructDataType materialsStructType = new StructDataType("materialstype");
materialsStructType.addField(new com.yahoo.document.Field("ceiling", DataType.STRING));
materialsStructType.addField(new com.yahoo.document.Field("walls", DataType.STRING));
DocumentType docType = new DocumentType("album");
docType.addField("title", DataType.STRING);
docType.addField("artist", DataType.STRING);
StructDataType storeStructType = new StructDataType("storetype");
storeStructType.addField(new com.yahoo.document.Field("name", DataType.STRING));
storeStructType.addField(new com.yahoo.document.Field("city", DataType.STRING));
storeStructType.addField(new com.yahoo.document.Field("materials", materialsStructType));
docType.addField("store", storeStructType);
Document doc = new Document(docType, new DocumentId("doc:map:test:1"));
doc.setFieldValue("title", new StringFieldValue("Black Rock"));
doc.setFieldValue("artist", new StringFieldValue("Joe Bonamassa"));
Struct material = new Struct(materialsStructType);
material.setFieldValue("ceiling", new StringFieldValue("wood"));
material.setFieldValue("walls", new StringFieldValue("brick"));
Struct store = new Struct(storeStructType);
store.setFieldValue("name", new StringFieldValue("Platekompaniet"));
store.setFieldValue("city", new StringFieldValue("Trondheim"));
store.setFieldValue(storeStructType.getField("materials"), material);
doc.setFieldValue(docType.getField("store"), store);
Map<String, String> fieldMap = new HashMap<>();
fieldMap.put("t", "title");
fieldMap.put("c", "store.city");
fieldMap.put("w", "store.materials.walls");
ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
assertEquals(new StringFieldValue("Trondheim"), mapped.getFieldValue("c"));
assertEquals(new StringFieldValue("Black Rock"), mapped.getFieldValue("t"));
assertEquals(new StringFieldValue("brick"), mapped.getFieldValue("w"));
assertEquals(new StringFieldValue("brick"), material.getFieldValue("walls"));
mapped.setFieldValue("c", new StringFieldValue("Steinkjer"));
mapped.setFieldValue("w", new StringFieldValue("plaster"));
assertEquals(new StringFieldValue("plaster"), mapped.getFieldValue("w"));
assertEquals(new StringFieldValue("plaster"), material.getFieldValue("walls"));
assertEquals(new StringFieldValue("Steinkjer"), store.getFieldValue("city"));
assertEquals(new StringFieldValue("Steinkjer"), mapped.getFieldValue("c"));
assertEquals(new StringFieldValue("Steinkjer"), mapped.getFieldValue("c"));
mapped.setFieldValue("c", new StringFieldValue("Levanger"));
assertEquals(new StringFieldValue("Levanger"), store.getFieldValue("city"));
assertEquals(new StringFieldValue("Levanger"), mapped.getFieldValue("c"));
mapped.setFieldValue("c", mapped.getFieldValue("c") + "Kommune");
assertEquals(new StringFieldValue("LevangerKommune"), mapped.getFieldValue("c"));
// mapped.set(mapped.getField("c"), mapped.get("c")+"Styre");
// assertEquals(new StringFieldValue("LevangerKommuneStyre"), mapped.getFieldValue("c"));
}
use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class SchemaMappingAndAccessesTest method testMappingStructsInArrays.
public void testMappingStructsInArrays() {
Document doc = getDoc();
DocumentProcessor proc = new TestMappingStructInArrayProcessor();
Map<String, String> fieldMap = new HashMap<>();
fieldMap.put("name", "listeners[0].firstname");
ProxyDocument mapped = new ProxyDocument(proc, doc, fieldMap);
Processing p = Processing.of(new DocumentPut(mapped));
proc.process(p);
assertEquals(2, ((Array<Struct>) doc.getFieldValue("listeners")).size());
assertEquals("peter", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("firstname")).getString()));
assertEquals("olsen", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("lastname")).getString()));
assertEquals("anders", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("firstname")).getString()));
assertEquals("and", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("lastname")).getString()));
fieldMap.clear();
fieldMap.put("name", "listeners[2].firstname");
mapped = new ProxyDocument(proc, doc, fieldMap);
p = Processing.of(new DocumentPut(mapped));
try {
proc.process(p);
fail("Should not have worked");
} catch (IllegalArgumentException iae) {
// ok!
}
assertEquals(2, ((Array<Struct>) doc.getFieldValue("listeners")).size());
assertEquals("peter", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("firstname")).getString()));
assertEquals("olsen", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("lastname")).getString()));
assertEquals("anders", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("firstname")).getString()));
assertEquals("and", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("lastname")).getString()));
// test remove:
proc = new TestRemovingMappingStructInArrayProcessor();
fieldMap.clear();
fieldMap.put("name", "listeners[1].lastname");
mapped = new ProxyDocument(proc, doc, fieldMap);
p = Processing.of(new DocumentPut(mapped));
proc.process(p);
assertEquals(2, ((Array<Struct>) doc.getFieldValue("listeners")).size());
assertEquals("peter", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("firstname")).getString()));
assertEquals("olsen", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("lastname")).getString()));
assertEquals("anders", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("firstname")).getString()));
assertNull(((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("lastname"));
fieldMap.clear();
fieldMap.put("name", "listeners[2].lastname");
mapped = new ProxyDocument(proc, doc, fieldMap);
p = Processing.of(new DocumentPut(mapped));
try {
proc.process(p);
fail("Should not have worked");
} catch (IllegalArgumentException iae) {
// ok!
}
assertEquals(2, ((Array<Struct>) doc.getFieldValue("listeners")).size());
assertEquals("peter", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("firstname")).getString()));
assertEquals("olsen", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("lastname")).getString()));
assertEquals("anders", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("firstname")).getString()));
assertNull(((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("lastname"));
}
use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class PositionDataType method valueOf.
public static Struct valueOf(Integer x, Integer y) {
Struct ret = new Struct(INSTANCE);
ret.setFieldValue(FIELD_X, x != null ? new IntegerFieldValue(x) : null);
ret.setFieldValue(FIELD_Y, y != null ? new IntegerFieldValue(y) : null);
return ret;
}
use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class StructDataType method createFieldValue.
@Override
public FieldValue createFieldValue(Object o) {
Struct struct;
if (o.getClass().equals(Struct.class)) {
struct = new Struct(this);
} else {
// This indicates for example that o is a generated struct subtype, try the empty constructor
try {
struct = (Struct) o.getClass().getConstructor().newInstance();
} catch (Exception e) {
// Fallback, let assign handle the error if o is completely bogus
struct = new Struct(this);
}
}
struct.assign(o);
return struct;
}
use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class ZCurveExpression method doExecute.
@Override
protected void doExecute(ExecutionContext ctx) {
Struct input = ((Struct) ctx.getValue());
Integer x = getFieldValue(input, PositionDataType.FIELD_X);
Integer y = getFieldValue(input, PositionDataType.FIELD_Y);
if (x != null && y != null) {
ctx.setValue(new LongFieldValue(ZCurve.encode(x, y)));
} else {
ctx.setValue(null);
}
}
Aggregations