Search in sources :

Example 21 with Array

use of com.yahoo.document.datatypes.Array 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 22 with Array

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

the class UriParserTestCase method requireThatUriFieldsCanBeParsed.

@Test
public void requireThatUriFieldsCanBeParsed() throws Exception {
    DocumentTypeManager mgr = new DocumentTypeManager();
    DocumentType docType = new DocumentType("my_doc");
    docType.addField("my_uri", DataType.URI);
    docType.addField("my_arr", DataType.getArray(DataType.URI));
    mgr.registerDocumentType(docType);
    VespaXMLFeedReader parser = new VespaXMLFeedReader("src/test/vespaxmlparser/test_uri.xml", mgr);
    Iterator<VespaXMLFeedReader.Operation> it = parser.readAll().iterator();
    Document doc = nextDocument(it);
    assertNotNull(doc);
    assertEquals(new StringFieldValue("scheme://host"), doc.getFieldValue("my_uri"));
    assertNull(doc.getFieldValue("my_arr"));
    assertNotNull(doc = nextDocument(it));
    assertNull(doc.getFieldValue("my_uri"));
    FieldValue val = doc.getFieldValue("my_arr");
    assertNotNull(val);
    assertTrue(val instanceof Array);
    Array arr = (Array) val;
    assertEquals(1, arr.size());
    assertEquals(new StringFieldValue("scheme://host"), arr.get(0));
    DocumentUpdate upd = nextUpdate(it);
    assertNotNull(upd);
    assertEquals(1, upd.getFieldUpdates().size());
    FieldUpdate fieldUpd = upd.getFieldUpdate(0);
    assertNotNull(fieldUpd);
    assertEquals(docType.getField("my_arr"), fieldUpd.getField());
    assertEquals(1, fieldUpd.getValueUpdates().size());
    ValueUpdate valueUpd = fieldUpd.getValueUpdate(0);
    assertNotNull(valueUpd);
    assertTrue(valueUpd instanceof AddValueUpdate);
    assertEquals(new StringFieldValue("scheme://host"), valueUpd.getValue());
    assertFalse(it.hasNext());
}
Also used : Array(com.yahoo.document.datatypes.Array) AddValueUpdate(com.yahoo.document.update.AddValueUpdate) ValueUpdate(com.yahoo.document.update.ValueUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) AddValueUpdate(com.yahoo.document.update.AddValueUpdate) FieldUpdate(com.yahoo.document.update.FieldUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) Test(org.junit.Test)

Example 23 with Array

use of com.yahoo.document.datatypes.Array 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 24 with Array

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

the class SplitTestCase method requireThatEmptyInputProducesEmptyArray.

@Test
public void requireThatEmptyInputProducesEmptyArray() {
    ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
    ctx.setValue(new StringFieldValue(""));
    new SplitExpression(";").execute(ctx);
    FieldValue val = ctx.getValue();
    assertTrue(val.getDataType().equals(DataType.getArray(DataType.STRING)));
    assertTrue(val instanceof Array);
    assertEquals(0, ((Array) val).size());
}
Also used : Array(com.yahoo.document.datatypes.Array) SimpleTestAdapter(com.yahoo.vespa.indexinglanguage.SimpleTestAdapter) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) Test(org.junit.Test)

Example 25 with Array

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

the class ToArrayTestCase method requireThatValueIsConverted.

@Test
public void requireThatValueIsConverted() {
    ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
    ctx.setValue(new StringFieldValue("69")).execute(new ToArrayExpression());
    FieldValue val = ctx.getValue();
    assertEquals(Array.class, val.getClass());
    Array arr = (Array) val;
    ArrayDataType type = arr.getDataType();
    assertEquals(DataType.STRING, type.getNestedType());
    assertEquals(1, arr.size());
    assertEquals(new StringFieldValue("69"), arr.get(0));
}
Also used : Array(com.yahoo.document.datatypes.Array) SimpleTestAdapter(com.yahoo.vespa.indexinglanguage.SimpleTestAdapter) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) ArrayDataType(com.yahoo.document.ArrayDataType) Test(org.junit.Test)

Aggregations

Array (com.yahoo.document.datatypes.Array)34 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)27 FieldValue (com.yahoo.document.datatypes.FieldValue)20 Test (org.junit.Test)17 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)16 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)9 Struct (com.yahoo.document.datatypes.Struct)9 WeightedSet (com.yahoo.document.datatypes.WeightedSet)9 Document (com.yahoo.document.Document)8 DocumentPut (com.yahoo.document.DocumentPut)7 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)7 StructDataType (com.yahoo.document.StructDataType)6 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)6 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)6 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)6 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)5 DocumentType (com.yahoo.document.DocumentType)4 Field (com.yahoo.document.Field)4 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)4 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)4