Search in sources :

Example 41 with Field

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

the class Bug6425939TestCase method canDeserializeAnnotationsOnZeroLengthStrings.

@Test
public void canDeserializeAnnotationsOnZeroLengthStrings() {
    StringFieldValue emptyString = new StringFieldValue("");
    emptyString.setSpanTree(createSpanTree());
    GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
    DocumentSerializer serializer = DocumentSerializerFactory.create42(buffer);
    Field strField = new Field("flarn", DataType.STRING);
    serializer.write(strField, emptyString);
    buffer.flip();
    // Should not throw exception if bug 6425939 is fixed:
    DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(man, buffer);
    StringFieldValue deserializedString = new StringFieldValue();
    deserializer.read(strField, deserializedString);
    assertEquals("", deserializedString.getString());
    SpanTree readTree = deserializedString.getSpanTree("SpanTree1");
    assertNotNull(readTree);
}
Also used : Field(com.yahoo.document.Field) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Test(org.junit.Test)

Example 42 with Field

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

the class SpanTreeTestCase method testCleanup.

@Test
public void testCleanup() {
    SpanList a = new SpanList();
    SpanTree tree = new SpanTree("ballooO", a);
    Span b = new Span(0, 1);
    Span c = new Span(1, 1);
    a.add(b).add(c);
    AnnotationReferenceDataType refTypeToDummy = new AnnotationReferenceDataType(dummy);
    AnnotationType annotationTypeWithRefToDummy = new AnnotationType("reftodummy", refTypeToDummy);
    AnnotationReferenceDataType refTypeToRefTypeToDummy = new AnnotationReferenceDataType(annotationTypeWithRefToDummy);
    AnnotationType annotationTypeWithRefToRefToDummy = new AnnotationType("reftoreftodummy", refTypeToRefTypeToDummy);
    StructDataType structType = new StructDataType("str");
    Field refToDummyField = new Field("refToDummy", refTypeToDummy);
    structType.addField(refToDummyField);
    AnnotationType annotationTypeWithStruct = new AnnotationType("structwithref", structType);
    Annotation dummyAnnotationForB = new Annotation(dummy);
    tree.annotate(b, dummyAnnotationForB);
    AnnotationReference referenceToDummyB = new AnnotationReference(refTypeToDummy, dummyAnnotationForB);
    Annotation annotationWithRefToDummyB = new Annotation(annotationTypeWithRefToDummy, referenceToDummyB);
    tree.annotate(annotationWithRefToDummyB);
    AnnotationReference referenceToRefToDummyB = new AnnotationReference(refTypeToRefTypeToDummy, annotationWithRefToDummyB);
    Annotation annotationWithRefToRefToDummyB = new Annotation(annotationTypeWithRefToRefToDummy, referenceToRefToDummyB);
    tree.annotate(annotationWithRefToRefToDummyB);
    Struct struct = new Struct(structType);
    struct.setFieldValue(refToDummyField, new AnnotationReference(refTypeToDummy, dummyAnnotationForB));
    Annotation annotationWithStruct = new Annotation(annotationTypeWithStruct, struct);
    tree.annotate(annotationWithStruct);
    Iterator<Annotation> annotationIt;
    assertEquals(4, tree.numAnnotations());
    assertTrue(a.isValid());
    assertTrue(b.isValid());
    assertTrue(c.isValid());
    assertEquals(2, a.numChildren());
    {
        List<Annotation> allAnnotationsList = new ArrayList<>();
        for (Annotation an : tree) {
            allAnnotationsList.add(an);
        }
        Collections.sort(allAnnotationsList);
        annotationIt = allAnnotationsList.iterator();
    }
    // NOTE: ordering of annotations is derived from their name
    assertSame(annotationWithRefToDummyB, annotationIt.next());
    assertFalse(annotationWithRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToDummyB.isSpanNodeValid());
    assertTrue(annotationWithRefToDummyB.hasFieldValue());
    assertSame(dummyAnnotationForB, ((AnnotationReference) annotationWithRefToDummyB.getFieldValue()).getReference());
    assertSame(annotationWithStruct, annotationIt.next());
    assertFalse(annotationWithStruct.hasSpanNode());
    assertFalse(annotationWithStruct.isSpanNodeValid());
    assertTrue(annotationWithStruct.hasFieldValue());
    assertSame(struct, annotationWithStruct.getFieldValue());
    assertSame(dummyAnnotationForB, ((AnnotationReference) struct.getFieldValue(refToDummyField)).getReference());
    assertSame(annotationWithRefToRefToDummyB, annotationIt.next());
    assertFalse(annotationWithRefToRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToRefToDummyB.isSpanNodeValid());
    assertTrue(annotationWithRefToRefToDummyB.hasFieldValue());
    assertSame(annotationWithRefToDummyB, ((AnnotationReference) annotationWithRefToRefToDummyB.getFieldValue()).getReference());
    assertSame(dummyAnnotationForB, annotationIt.next());
    assertTrue(dummyAnnotationForB.hasSpanNode());
    assertTrue(dummyAnnotationForB.isSpanNodeValid());
    assertFalse(dummyAnnotationForB.hasFieldValue());
    assertFalse(annotationIt.hasNext());
    // removing b!!
    a.remove(b);
    assertEquals(4, tree.numAnnotations());
    assertTrue(a.isValid());
    assertFalse(b.isValid());
    assertTrue(c.isValid());
    assertEquals(1, a.numChildren());
    {
        List<Annotation> allAnnotationsList = new ArrayList<>();
        for (Annotation an : tree) {
            allAnnotationsList.add(an);
        }
        Collections.sort(allAnnotationsList);
        annotationIt = allAnnotationsList.iterator();
    }
    assertSame(annotationWithRefToDummyB, annotationIt.next());
    assertFalse(annotationWithRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToDummyB.isSpanNodeValid());
    assertTrue(annotationWithRefToDummyB.hasFieldValue());
    assertSame(dummyAnnotationForB, ((AnnotationReference) annotationWithRefToDummyB.getFieldValue()).getReference());
    assertSame(annotationWithStruct, annotationIt.next());
    assertFalse(annotationWithStruct.hasSpanNode());
    assertFalse(annotationWithStruct.isSpanNodeValid());
    assertTrue(annotationWithStruct.hasFieldValue());
    assertSame(struct, annotationWithStruct.getFieldValue());
    assertSame(dummyAnnotationForB, ((AnnotationReference) struct.getFieldValue(refToDummyField)).getReference());
    assertSame(annotationWithRefToRefToDummyB, annotationIt.next());
    assertFalse(annotationWithRefToRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToRefToDummyB.isSpanNodeValid());
    assertTrue(annotationWithRefToRefToDummyB.hasFieldValue());
    assertSame(annotationWithRefToDummyB, ((AnnotationReference) annotationWithRefToRefToDummyB.getFieldValue()).getReference());
    assertSame(dummyAnnotationForB, annotationIt.next());
    assertTrue(dummyAnnotationForB.hasSpanNode());
    assertFalse(dummyAnnotationForB.isSpanNodeValid());
    assertFalse(dummyAnnotationForB.hasFieldValue());
    assertFalse(annotationIt.hasNext());
    // cleaning up tree!!
    tree.cleanup();
    assertEquals(1, tree.numAnnotations());
    assertTrue(a.isValid());
    assertFalse(b.isValid());
    assertTrue(c.isValid());
    assertEquals(1, a.numChildren());
    assertFalse(dummyAnnotationForB.hasSpanNode());
    assertFalse(dummyAnnotationForB.isSpanNodeValid());
    assertFalse(dummyAnnotationForB.hasFieldValue());
    assertFalse(annotationWithRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToDummyB.isSpanNodeValid());
    assertFalse(annotationWithRefToDummyB.hasFieldValue());
    assertFalse(annotationWithRefToRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToRefToDummyB.isSpanNodeValid());
    assertFalse(annotationWithRefToRefToDummyB.hasFieldValue());
    annotationIt = tree.iterator();
    assertSame(annotationWithStruct, annotationIt.next());
    assertFalse(annotationWithStruct.hasSpanNode());
    assertFalse(annotationWithStruct.isSpanNodeValid());
    assertTrue(annotationWithStruct.hasFieldValue());
    assertSame(struct, annotationWithStruct.getFieldValue());
    assertNull(struct.getFieldValue(refToDummyField));
    assertFalse(annotationIt.hasNext());
}
Also used : Struct(com.yahoo.document.datatypes.Struct) Field(com.yahoo.document.Field) StructDataType(com.yahoo.document.StructDataType) Test(org.junit.Test)

Example 43 with Field

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

the class SpanTreeTestCase method testCyclicReferences.

@Test
public void testCyclicReferences() {
    DocumentTypeManager docMan = new DocumentTypeManager();
    AnnotationTypeRegistry reg = docMan.getAnnotationTypeRegistry();
    StringFieldValue strfval = new StringFieldValue("hohohoho");
    SpanList root = new SpanList();
    SpanTree tree = new SpanTree("habahaba", root);
    strfval.setSpanTree(tree);
    // set up types:
    AnnotationType endTagType = new AnnotationType("endtag");
    AnnotationType beginTagType = new AnnotationType("begintag");
    AnnotationReferenceDataType refToBeginTagDataType = new AnnotationReferenceDataType(beginTagType);
    AnnotationReferenceDataType refToEndTagDataType = new AnnotationReferenceDataType(endTagType);
    endTagType.setDataType(refToBeginTagDataType);
    beginTagType.setDataType(refToEndTagDataType);
    // register types:
    reg.register(endTagType);
    reg.register(beginTagType);
    docMan.register(refToBeginTagDataType);
    docMan.register(refToEndTagDataType);
    // set up annotations and their references to each other:
    AnnotationReference refToBeginTag = new AnnotationReference(refToBeginTagDataType);
    AnnotationReference refToEndTag = new AnnotationReference(refToEndTagDataType);
    Annotation beginTag = new Annotation(beginTagType, refToEndTag);
    Annotation endTag = new Annotation(endTagType, refToBeginTag);
    refToBeginTag.setReference(beginTag);
    refToEndTag.setReference(endTag);
    // create spans:
    Span beginTagSpan = new Span(0, 1);
    Span endTagSpan = new Span(1, 1);
    root.add(beginTagSpan);
    root.add(endTagSpan);
    // annotate spans:
    tree.annotate(beginTagSpan, beginTag);
    tree.annotate(endTagSpan, endTag);
    // none of the below statements should lead to a StackOverflowError:
    assertFalse(endTag.toString().equals(beginTag.toString()));
    assertTrue(endTag.hashCode() != beginTag.hashCode());
    assertFalse(endTag.equals(beginTag));
    assertFalse(beginTag.equals(endTag));
    StringFieldValue copyString = strfval.clone();
    assertEquals(strfval, copyString);
    GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
    DocumentSerializer serializer = DocumentSerializerFactory.create42(buffer);
    serializer.write(new Field("stringfield", DataType.STRING), strfval);
    buffer.flip();
    DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(docMan, buffer);
    StringFieldValue stringFieldValue2 = new StringFieldValue();
    deserializer.read(new Field("stringfield", DataType.STRING), stringFieldValue2);
    assertEquals(strfval, stringFieldValue2);
    assertNotSame(strfval, stringFieldValue2);
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Field(com.yahoo.document.Field) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DocumentTypeManager(com.yahoo.document.DocumentTypeManager) Test(org.junit.Test)

Example 44 with Field

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

the class StructTestCase method testSetGetPrimitiveTypes.

@Test
public void testSetGetPrimitiveTypes() throws Exception {
    StructDataType type = new StructDataType("teststr");
    type.addField(new Field("int", DataType.INT));
    type.addField(new Field("flt", DataType.FLOAT));
    type.addField(new Field("str", DataType.STRING));
    type.addField(new Field("raw", DataType.RAW));
    type.addField(new Field("lng", DataType.LONG));
    type.addField(new Field("dbl", DataType.DOUBLE));
    type.addField(new Field("uri", DataType.URI));
    type.addField(new Field("byt", DataType.BYTE));
    Struct struct = new Struct(type);
    {
        IntegerFieldValue nt = new IntegerFieldValue(544);
        Object o = struct.setFieldValue("int", nt);
        assertNull(o);
        assertEquals(new IntegerFieldValue(544), struct.getFieldValue("int"));
        o = struct.setFieldValue("int", 500);
        assertEquals(nt, o);
        assertFalse(nt.equals(struct.getFieldValue("int")));
    }
    {
        FloatFieldValue flt = new FloatFieldValue(5.44f);
        Object o = struct.setFieldValue("flt", flt);
        assertNull(o);
        assertEquals(flt, struct.getFieldValue("flt"));
        o = struct.setFieldValue("flt", new FloatFieldValue(5.00f));
        assertEquals(flt, o);
        assertFalse(flt.equals(struct.getFieldValue("flt")));
    }
    {
        StringFieldValue string = new StringFieldValue("this is a string");
        Object o = struct.setFieldValue("str", string);
        assertNull(o);
        assertEquals(string, struct.getFieldValue("str"));
        o = struct.setFieldValue("str", "another string");
        assertEquals(string, o);
        assertSame(string, o);
        assertFalse(string.equals(struct.getFieldValue("str")));
    }
    {
        Raw buf = new Raw(ByteBuffer.wrap(new byte[100]));
        Object o = struct.setFieldValue("raw", buf);
        assertNull(o);
        assertEquals(buf, struct.getFieldValue("raw"));
        o = struct.setFieldValue("raw", new Raw(ByteBuffer.wrap(new byte[50])));
        assertEquals(buf, o);
        assertSame(buf, o);
        assertFalse(buf.equals(struct.getFieldValue("raw")));
    }
    {
        LongFieldValue lng = new LongFieldValue(59879879879079L);
        Object o = struct.setFieldValue("lng", lng);
        assertEquals(lng, struct.getFieldValue("lng"));
        o = struct.setFieldValue("lng", new LongFieldValue(23418798734243L));
        assertEquals(lng, o);
        assertFalse(lng.equals(struct.getFieldValue("lng")));
    }
    {
        DoubleFieldValue dbl = new DoubleFieldValue(5.44d);
        Object o = struct.setFieldValue("dbl", dbl);
        assertNull(o);
        assertEquals(dbl, struct.getFieldValue("dbl"));
        o = struct.setFieldValue("dbl", new DoubleFieldValue(5.00d));
        assertEquals(dbl, o);
        assertFalse(dbl.equals(struct.getFieldValue("dbl")));
    }
    {
        UriFieldValue uri = new UriFieldValue("this is a uri");
        Object o = struct.setFieldValue("uri", uri);
        assertNull(o);
        assertEquals(uri, struct.getFieldValue("uri"));
        o = struct.setFieldValue("uri", "another uri");
        assertEquals(uri, o);
        assertSame(uri, o);
        assertFalse(uri.equals(struct.getFieldValue("uri")));
    }
    {
        ByteFieldValue byt = new ByteFieldValue((byte) 123);
        Object o = struct.setFieldValue("byt", byt);
        assertNull(o);
        assertEquals(byt, struct.getFieldValue("byt"));
        o = struct.setFieldValue("byt", (byte) 100);
        assertEquals(byt, o);
        assertFalse(byt.equals(struct.getFieldValue("byt")));
    }
}
Also used : Field(com.yahoo.document.Field) StructDataType(com.yahoo.document.StructDataType) Test(org.junit.Test)

Example 45 with Field

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

the class StructTestCase method testBasicStuff.

@Test
public void testBasicStuff() throws Exception {
    StructDataType type = new StructDataType("teststr");
    type.addField(new Field("int", 0, DataType.INT, true));
    type.addField(new Field("flt", 1, DataType.FLOAT, true));
    type.addField(new Field("str", 2, DataType.STRING, true));
    type.addField(new Field("raw", 3, DataType.RAW, true));
    type.addField(new Field("lng", 4, DataType.LONG, true));
    type.addField(new Field("dbl", 5, DataType.DOUBLE, true));
    type.addField(new Field("uri", 6, DataType.URI, true));
    type.addField(new Field("byt", 8, DataType.BYTE, true));
    Struct struct = new Struct(type);
    {
        // add and remove again:
        assertEquals(0, struct.getFields().size());
        IntegerFieldValue ifv = new IntegerFieldValue(5);
        struct.setFieldValue("int", ifv);
        assertEquals(1, struct.getFields().size());
        struct.removeFieldValue("int");
        assertEquals(0, struct.getFields().size());
    }
    {
        // add three elements and remove one of them, and replace the last one:
        assertEquals(0, struct.getFields().size());
        IntegerFieldValue ifv = new IntegerFieldValue(5);
        struct.setFieldValue("int", ifv);
        assertEquals(1, struct.getFields().size());
        FloatFieldValue ffv = new FloatFieldValue(5.0f);
        struct.setFieldValue("flt", ffv);
        assertEquals(2, struct.getFields().size());
        DoubleFieldValue dfv = new DoubleFieldValue(6.0d);
        struct.setFieldValue("dbl", dfv);
        assertEquals(3, struct.getFields().size());
        Iterator<Map.Entry<Field, FieldValue>> it = struct.iterator();
        assertSame(ifv, it.next().getValue());
        assertSame(ffv, it.next().getValue());
        assertSame(dfv, it.next().getValue());
        assertFalse(it.hasNext());
        struct.removeFieldValue("flt");
        assertEquals(2, struct.getFields().size());
        it = struct.iterator();
        assertSame(ifv, it.next().getValue());
        assertSame(dfv, it.next().getValue());
        assertFalse(it.hasNext());
        DoubleFieldValue dfv2 = new DoubleFieldValue(9.0d);
        struct.setFieldValue("dbl", dfv2);
        assertEquals(2, struct.getFields().size());
        it = struct.iterator();
        assertSame(ifv, it.next().getValue());
        assertSame(dfv2, it.next().getValue());
        assertFalse(it.hasNext());
    }
}
Also used : Field(com.yahoo.document.Field) StructDataType(com.yahoo.document.StructDataType) Iterator(java.util.Iterator) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Field (com.yahoo.document.Field)115 Test (org.junit.Test)50 StructDataType (com.yahoo.document.StructDataType)46 DocumentType (com.yahoo.document.DocumentType)24 DataType (com.yahoo.document.DataType)17 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)14 ReferenceDataType (com.yahoo.document.ReferenceDataType)13 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)13 ArrayDataType (com.yahoo.document.ArrayDataType)12 MapDataType (com.yahoo.document.MapDataType)12 TensorDataType (com.yahoo.document.TensorDataType)11 WeightedSetDataType (com.yahoo.document.WeightedSetDataType)11 SDField (com.yahoo.searchdefinition.document.SDField)10 PositionDataType (com.yahoo.document.PositionDataType)9 FieldValue (com.yahoo.document.datatypes.FieldValue)9 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)9 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)8 Struct (com.yahoo.document.datatypes.Struct)7 DocumentTypeManager (com.yahoo.document.DocumentTypeManager)6 Document (com.yahoo.document.Document)5