Search in sources :

Example 26 with Struct

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

the class SerializeAnnotationsTestCase method testSerializeAdvancedTree.

public void testSerializeAdvancedTree() throws IOException {
    SpanList root = new SpanList();
    SpanTree tree = new SpanTree("html", root);
    DataType positionType = docMan.getDataType("myposition");
    StructDataType cityDataType = (StructDataType) docMan.getDataType("annotation.city");
    AnnotationTypeRegistry registry = docMan.getAnnotationTypeRegistry();
    AnnotationType textType = registry.getType("text");
    AnnotationType beginTag = registry.getType("begintag");
    AnnotationType endTag = registry.getType("endtag");
    AnnotationType bodyType = registry.getType("body");
    AnnotationType paragraphType = registry.getType("paragraph");
    AnnotationType cityType = registry.getType("city");
    AnnotationReferenceDataType annRefType = (AnnotationReferenceDataType) docMan.getDataType("annotationreference<text>");
    Struct position = new Struct(positionType);
    position.setFieldValue("latitude", new DoubleFieldValue(37.774929));
    position.setFieldValue("longitude", new DoubleFieldValue(-122.419415));
    Annotation sanAnnotation = new Annotation(textType);
    Annotation franciscoAnnotation = new Annotation(textType);
    Struct positionWithRef = cityDataType.createFieldValue();
    positionWithRef.setFieldValue("position", position);
    Field referencesField = cityDataType.getField("references");
    Array<FieldValue> refList = new Array<FieldValue>(referencesField.getDataType());
    refList.add(new AnnotationReference(annRefType, sanAnnotation));
    refList.add(new AnnotationReference(annRefType, franciscoAnnotation));
    positionWithRef.setFieldValue(referencesField, refList);
    Annotation city = new Annotation(cityType, positionWithRef);
    AlternateSpanList paragraph = new AlternateSpanList();
    paragraph.addChildren(new ArrayList<SpanNode>(), 0);
    paragraph.setProbability(0, 0.9);
    paragraph.setProbability(1, 0.1);
    {
        Span span1 = new Span(6, 3);
        Span span2 = new Span(9, 10);
        Span span3 = new Span(19, 4);
        Span span4 = new Span(23, 4);
        paragraph.add(0, span1).add(0, span2).add(0, span3).add(0, span4);
        Span alt_span1 = new Span(6, 13);
        Span alt_span2 = new Span(19, 8);
        paragraph.add(1, alt_span1).add(1, alt_span2);
        tree.annotate(span1, beginTag).annotate(span2, textType).annotate(span3, sanAnnotation).annotate(span4, endTag).annotate(alt_span1, textType).annotate(alt_span2, bodyType).annotate(paragraph, paragraphType);
    }
    {
        Span span1 = new Span(0, 6);
        Span span2 = new Span(27, 9);
        Span span3 = new Span(36, 8);
        root.add(span1).add(paragraph).add(span2).add(span3);
        tree.annotate(span1, beginTag).annotate(span2, franciscoAnnotation).annotate(span3, endTag).annotate(root, bodyType).annotate(city);
    }
    StringFieldValue value = new StringFieldValue("lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj " + "lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj l jlkj lkj lkj " + "lkjoijoij oij oij oij oij oij oijoijoij oij oij oij oij oij " + "oijoijoijoijoijoijoijoijoijoijoijoijoij oij oij oij oij " + "oijaosdifjoai fdoais jdoasi jai os oafoai ai dfojsfoa dfoi dsf" + "aosifjasofija sodfij oasdifj aosdiosifjsi ooai oais osi");
    value.setSpanTree(tree);
    // important! call readFile() before writeFile()!
    ByteBuffer serializedFromFile = readFile("test_data_serialized_advanced");
    ByteBuffer serialized = writeFile(value, "test_data_serialized_advanced");
    assertEquals(serialized.limit(), serializedFromFile.limit());
    StringFieldValue valueFromFile = new StringFieldValue();
    DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(docMan, new GrowableByteBuffer(serializedFromFile));
    deserializer.read(null, valueFromFile);
    assertEquals(value, valueFromFile);
}
Also used : DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) ByteBuffer(java.nio.ByteBuffer) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) Field(com.yahoo.document.Field) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType) DataType(com.yahoo.document.DataType) StructDataType(com.yahoo.document.StructDataType) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue)

Example 27 with Struct

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

the class ForEachExpression method doExecute.

@Override
protected void doExecute(final ExecutionContext ctx) {
    FieldValue input = ctx.getValue();
    if (input instanceof Array || input instanceof WeightedSet) {
        FieldValue next = new MyConverter(ctx, exp).convert(input);
        if (next == null) {
            VerificationContext vctx = new VerificationContext(ctx);
            vctx.setValue(input.getDataType()).execute(this);
            next = vctx.getValue().createFieldValue();
        }
        ctx.setValue(next);
    } else if (input instanceof Struct) {
        ctx.setValue(new MyConverter(ctx, exp).convert(input));
    } else {
        throw new IllegalArgumentException("Expected Array, Struct or WeightedSet input, got " + input.getDataType().getName() + ".");
    }
}
Also used : Array(com.yahoo.document.datatypes.Array) FieldValue(com.yahoo.document.datatypes.FieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet) Struct(com.yahoo.document.datatypes.Struct)

Example 28 with Struct

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

the class GetFieldTestCase method requireThatStructFieldsCanBeRead.

@Test
public void requireThatStructFieldsCanBeRead() {
    DataType barType = DataType.STRING;
    FieldValue bar = barType.createFieldValue("bar");
    StructDataType fooType = new StructDataType("my_struct");
    fooType.addField(new Field("bar", barType));
    Struct foo = new Struct(fooType);
    foo.setFieldValue("bar", bar);
    DocumentType docType = new DocumentType("my_doc");
    docType.addField("foo", fooType);
    Document doc = new Document(docType, "doc:scheme:");
    doc.setFieldValue("foo", foo);
    ExecutionContext ctx = new ExecutionContext(new SimpleDocumentAdapter(doc));
    assertEquals(bar, new StatementExpression(new InputExpression("foo"), new GetFieldExpression("bar")).execute(ctx));
}
Also used : SimpleDocumentAdapter(com.yahoo.vespa.indexinglanguage.SimpleDocumentAdapter) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 29 with Struct

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

the class InputTestCase method requireThatStructFieldsCanBeRead.

@Test
public void requireThatStructFieldsCanBeRead() {
    DataType barType = DataType.STRING;
    FieldValue bar = barType.createFieldValue("bar");
    StructDataType fooType = new StructDataType("my_struct");
    fooType.addField(new Field("bar", barType));
    Struct foo = new Struct(fooType);
    foo.setFieldValue("bar", bar);
    DocumentType docType = new DocumentType("my_doc");
    docType.addField("foo", fooType);
    Document doc = new Document(docType, "doc:scheme:");
    doc.setFieldValue("foo", foo);
    ExecutionContext ctx = new ExecutionContext(new SimpleDocumentAdapter(doc));
    assertEquals(foo, new InputExpression("foo").execute(ctx));
    assertEquals(bar, new InputExpression("foo.bar").execute(ctx));
}
Also used : SimpleDocumentAdapter(com.yahoo.vespa.indexinglanguage.SimpleDocumentAdapter) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 30 with Struct

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

the class DocumentTestCase method testModifyDocument.

@Test
public void testModifyDocument() {
    Document doc = new Document(testDocType, new DocumentId("doc:ns:testdoc"));
    doc.setFieldValue("primitive1", 1);
    Struct l1s1 = new Struct(doc.getField("l1s1").getDataType());
    l1s1.setFieldValue("primitive1", 2);
    Struct l2s1 = new Struct(doc.getField("struct2").getDataType());
    l2s1.setFieldValue("primitive1", 3);
    l2s1.setFieldValue("primitive2", 4);
    Array<IntegerFieldValue> iarr1 = new Array<>(l2s1.getField("iarray").getDataType());
    iarr1.add(new IntegerFieldValue(11));
    iarr1.add(new IntegerFieldValue(12));
    iarr1.add(new IntegerFieldValue(13));
    l2s1.setFieldValue("iarray", iarr1);
    ArrayDataType dt = (ArrayDataType) l2s1.getField("sarray").getDataType();
    Array<Struct> sarr1 = new Array<>(dt);
    {
        Struct l3s1 = new Struct(dt.getNestedType());
        l3s1.setFieldValue("primitive1", 1);
        l3s1.setFieldValue("primitive2", 2);
        sarr1.add(l3s1);
    }
    {
        Struct l3s1 = new Struct(dt.getNestedType());
        l3s1.setFieldValue("primitive1", 1);
        l3s1.setFieldValue("primitive2", 2);
        sarr1.add(l3s1);
    }
    l2s1.setFieldValue("sarray", sarr1);
    MapFieldValue<StringFieldValue, StringFieldValue> smap1 = new MapFieldValue<>((MapDataType) l2s1.getField("smap").getDataType());
    smap1.put(new StringFieldValue("leonardo"), new StringFieldValue("dicaprio"));
    smap1.put(new StringFieldValue("ellen"), new StringFieldValue("page"));
    smap1.put(new StringFieldValue("joseph"), new StringFieldValue("gordon-levitt"));
    l2s1.setFieldValue("smap", smap1);
    l1s1.setFieldValue("ss", l2s1.clone());
    MapFieldValue<StringFieldValue, Struct> structmap1 = new MapFieldValue<>((MapDataType) l1s1.getField("structmap").getDataType());
    structmap1.put(new StringFieldValue("test"), l2s1.clone());
    l1s1.setFieldValue("structmap", structmap1);
    WeightedSet<StringFieldValue> wset1 = new WeightedSet<>(l1s1.getField("wset").getDataType());
    wset1.add(new StringFieldValue("foo"));
    wset1.add(new StringFieldValue("bar"));
    wset1.add(new StringFieldValue("zoo"));
    l1s1.setFieldValue("wset", wset1);
    Struct l2s2 = new Struct(doc.getField("struct2").getDataType());
    l2s2.setFieldValue("primitive1", 5);
    l2s2.setFieldValue("primitive2", 6);
    WeightedSet<Struct> wset2 = new WeightedSet<>(l1s1.getField("structwset").getDataType());
    wset2.add(l2s1.clone());
    wset2.add(l2s2.clone());
    l1s1.setFieldValue("structwset", wset2);
    doc.setFieldValue("l1s1", l1s1.clone());
    {
        ModifyIteratorHandler handler = new ModifyIteratorHandler();
        FieldPath path = doc.getDataType().buildFieldPath("l1s1.structmap.value.smap{leonardo}");
        doc.iterateNested(path, 0, handler);
        FieldValue fv = doc.getRecursiveValue("l1s1.structmap.value.smap{leonardo}");
        assertEquals(new StringFieldValue("newvalue"), fv);
    }
    {
        AddIteratorHandler handler = new AddIteratorHandler();
        FieldPath path = doc.getDataType().buildFieldPath("l1s1.ss.iarray");
        doc.iterateNested(path, 0, handler);
        FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray");
        assertTrue(((Array) fv).contains(new IntegerFieldValue(32)));
        assertEquals(4, ((Array) fv).size());
    }
    {
        RemoveIteratorHandler handler = new RemoveIteratorHandler();
        FieldPath path = doc.getDataType().buildFieldPath("l1s1.ss.iarray[1]");
        doc.iterateNested(path, 0, handler);
        FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray");
        assertFalse(((Array) fv).contains(new Integer(12)));
        assertEquals(3, ((Array) fv).size());
    }
    {
        RemoveIteratorHandler handler = new RemoveIteratorHandler();
        FieldPath path = doc.getDataType().buildFieldPath("l1s1.ss.iarray[$x]");
        doc.iterateNested(path, 0, handler);
        FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray");
        assertEquals(0, ((Array) fv).size());
    }
    {
        RemoveIteratorHandler handler = new RemoveIteratorHandler();
        FieldPath path = doc.getDataType().buildFieldPath("l1s1.structmap.value.smap{leonardo}");
        doc.iterateNested(path, 0, handler);
        FieldValue fv = doc.getRecursiveValue("l1s1.structmap.value.smap");
        assertFalse(((MapFieldValue) fv).contains(new StringFieldValue("leonardo")));
    }
    {
        RemoveIteratorHandler handler = new RemoveIteratorHandler();
        FieldPath path = doc.getDataType().buildFieldPath("l1s1.wset{foo}");
        doc.iterateNested(path, 0, handler);
        FieldValue fv = doc.getRecursiveValue("l1s1.wset");
        assertFalse(((WeightedSet) fv).contains(new StringFieldValue("foo")));
    }
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet) Test(org.junit.Test)

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