Search in sources :

Example 6 with Struct

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"));
}
Also used : HashMap(java.util.HashMap) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) Struct(com.yahoo.document.datatypes.Struct) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType)

Example 7 with Struct

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"));
}
Also used : DocumentProcessor(com.yahoo.docproc.DocumentProcessor) HashMap(java.util.HashMap) DocumentPut(com.yahoo.document.DocumentPut) Document(com.yahoo.document.Document) Processing(com.yahoo.docproc.Processing) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue)

Example 8 with Struct

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;
}
Also used : IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Struct(com.yahoo.document.datatypes.Struct)

Example 9 with Struct

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;
}
Also used : Struct(com.yahoo.document.datatypes.Struct)

Example 10 with 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);
    }
}
Also used : LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) Struct(com.yahoo.document.datatypes.Struct)

Aggregations

Struct (com.yahoo.document.datatypes.Struct)38 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)25 Test (org.junit.Test)21 FieldValue (com.yahoo.document.datatypes.FieldValue)13 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)11 StructDataType (com.yahoo.document.StructDataType)9 Array (com.yahoo.document.datatypes.Array)9 Field (com.yahoo.document.Field)7 Document (com.yahoo.document.Document)6 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)5 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)4 DocumentType (com.yahoo.document.DocumentType)3 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)3 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)3 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)3 WeightedSet (com.yahoo.document.datatypes.WeightedSet)3 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)3 FieldUpdate (com.yahoo.document.update.FieldUpdate)3 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)3 ValueUpdate (com.yahoo.document.update.ValueUpdate)3