Search in sources :

Example 76 with StringFieldValue

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

the class VespaDocumentSerializerTestCase method compressed_map_of_compressed_structs_is_supported.

@Test
public void compressed_map_of_compressed_structs_is_supported() {
    CompressionFixture fixture = new CompressionFixture();
    Document doc = new Document(fixture.docType, "id:foo:map_of_structs::flarn");
    Struct nested = new Struct(fixture.nestedType);
    nested.setFieldValue("str", new StringFieldValue(CompressionFixture.COMPRESSABLE_STRING));
    MapFieldValue<StringFieldValue, Struct> map = new MapFieldValue<StringFieldValue, Struct>(fixture.mapType);
    map.put(new StringFieldValue("foo"), nested);
    map.put(new StringFieldValue("bar"), nested);
    doc.setFieldValue("map", map);
    // Should _not_ throw any deserialization exceptions
    Document result = fixture.roundtripSerialize(doc);
    assertEquals(doc, result);
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) Document(com.yahoo.document.Document) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 77 with StringFieldValue

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

the class VespaDocumentSerializerTestCase method incompressable_structs_are_serialized_without_buffer_size_overhead_bug.

@Test
public void incompressable_structs_are_serialized_without_buffer_size_overhead_bug() {
    CompressionFixture fixture = new CompressionFixture();
    Document doc = new Document(fixture.docType, "id:foo:map_of_structs::flarn");
    Struct nested = new Struct(fixture.nestedType);
    nested.setFieldValue("str", new StringFieldValue(CompressionFixture.COMPRESSABLE_STRING));
    MapFieldValue<StringFieldValue, Struct> map = new MapFieldValue<StringFieldValue, Struct>(fixture.mapType);
    // Only 1 struct added. Not enough redundant information that header struct containing map itself
    // can be compressed.
    map.put(new StringFieldValue("foo"), nested);
    doc.setFieldValue("map", map);
    GrowableByteBuffer buf = CompressionFixture.asSerialized(doc);
    // Explanation of arbitrary value: buffer copy bug meant that incompressable structs were all serialized
    // rounded up to 4096 bytes.
    assertTrue(buf.remaining() < 4096);
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Document(com.yahoo.document.Document) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 78 with StringFieldValue

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

the class DocInDocTestCase method testDocInDoc.

@Test
public void testDocInDoc() {
    DocumentTypeManager manager = new DocumentTypeManager();
    DocumentTypeManagerConfigurer.configure(manager, "file:src/test/java/com/yahoo/document/documentmanager.docindoc.cfg");
    Document inner1 = new Document(manager.getDocumentType("docindoc"), "doc:inner:number:one");
    inner1.setFieldValue("name", new StringFieldValue("Donald Duck"));
    inner1.setFieldValue("content", new StringFieldValue("Lives in Duckburg"));
    Document inner2 = new Document(manager.getDocumentType("docindoc"), "doc:inner:number:two");
    inner2.setFieldValue("name", new StringFieldValue("Uncle Scrooge"));
    inner2.setFieldValue("content", new StringFieldValue("Lives in Duckburg, too."));
    Array<Document> innerArray = (Array<Document>) manager.getDocumentType("outerdoc").getField("innerdocuments").getDataType().createFieldValue();
    innerArray.add(inner1);
    innerArray.add(inner2);
    Document outer = new Document(manager.getDocumentType("outerdoc"), "doc:outer:the:only:one");
    outer.setFieldValue("innerdocuments", innerArray);
    DocumentSerializer serializer = DocumentSerializerFactory.create42();
    serializer.write(outer);
    GrowableByteBuffer buf = serializer.getBuf();
    buf.flip();
    DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(manager, buf);
    Document outerDeserialized = new Document(deserializer);
    assertEquals(outer, outerDeserialized);
    assertNotSame(outer, outerDeserialized);
}
Also used : Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Test(org.junit.Test)

Example 79 with StringFieldValue

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

the class DocumentTestCase method testSerializeDeserialize.

@Test
public void testSerializeDeserialize() {
    setUpSertestDocType();
    Document doc = getSertestDocument();
    GrowableByteBuffer data = new GrowableByteBuffer();
    doc.serialize(data);
    int size = doc.getSerializedSize();
    assertEquals(size, data.position());
    data.flip();
    try {
        FileOutputStream fos = new FileOutputStream("src/test/files/testser.dat");
        fos.write(data.array(), 0, data.remaining());
        fos.close();
    } catch (Exception e) {
    }
    Document doc2 = docMan.createDocument(data);
    assertEquals(doc.getFieldValue("mailid"), doc2.getFieldValue("mailid"));
    assertEquals(doc.getFieldValue("date"), doc2.getFieldValue("date"));
    assertEquals(doc.getFieldValue("from"), doc2.getFieldValue("from"));
    assertEquals(doc.getFieldValue("to"), doc2.getFieldValue("to"));
    assertEquals(doc.getFieldValue("subject"), doc2.getFieldValue("subject"));
    assertEquals(doc.getFieldValue("body"), doc2.getFieldValue("body"));
    assertEquals(doc.getFieldValue("attachmentcount"), doc2.getFieldValue("attachmentcount"));
    assertEquals(doc.getFieldValue("attachments"), doc2.getFieldValue("attachments"));
    byte[] docRawBytes = ((Raw) doc.getFieldValue("rawfield")).getByteBuffer().array();
    byte[] doc2RawBytes = ((Raw) doc2.getFieldValue("rawfield")).getByteBuffer().array();
    assertEquals(docRawBytes.length, doc2RawBytes.length);
    for (int i = 0; i < docRawBytes.length; i++) {
        assertEquals(docRawBytes[i], doc2RawBytes[i]);
    }
    assertEquals(doc.getFieldValue("weightedfield"), doc2.getFieldValue("weightedfield"));
    assertEquals(doc.getFieldValue("mapfield"), doc2.getFieldValue("mapfield"));
    // Do the same thing, splitting document in two
    DocumentSerializer header = DocumentSerializerFactory.create42(new GrowableByteBuffer(), true);
    DocumentSerializer body = DocumentSerializerFactory.create42(new GrowableByteBuffer());
    doc.serializeHeader(header);
    doc.serializeBody(body);
    header.getBuf().flip();
    body.getBuf().flip();
    try {
        FileOutputStream fos = new FileOutputStream("src/test/files/testser-split.header.dat");
        fos.write(header.getBuf().array(), 0, header.getBuf().remaining());
        fos.close();
        fos = new FileOutputStream("src/test/files/testser-split.body.dat");
        fos.write(body.getBuf().array(), 0, body.getBuf().remaining());
        fos.close();
    } catch (Exception e) {
    }
    DocumentDeserializer deser = DocumentDeserializerFactory.create42(docMan, header.getBuf(), body.getBuf());
    doc2 = new Document(deser);
    assertEquals(doc.getFieldValue("mailid"), doc2.getFieldValue("mailid"));
    assertEquals(doc.getFieldValue("date"), doc2.getFieldValue("date"));
    assertEquals(doc.getFieldValue("from"), doc2.getFieldValue("from"));
    assertEquals(doc.getFieldValue("to"), doc2.getFieldValue("to"));
    assertEquals(doc.getFieldValue("subject"), doc2.getFieldValue("subject"));
    assertEquals(doc.getFieldValue("body"), doc2.getFieldValue("body"));
    assertEquals(doc.getFieldValue("attachmentcount"), doc2.getFieldValue("attachmentcount"));
    assertEquals(doc.getFieldValue("attachments"), doc2.getFieldValue("attachments"));
    docRawBytes = ((Raw) doc.getFieldValue("rawfield")).getByteBuffer().array();
    doc2RawBytes = ((Raw) doc2.getFieldValue("rawfield")).getByteBuffer().array();
    assertEquals(docRawBytes.length, doc2RawBytes.length);
    for (int i = 0; i < docRawBytes.length; i++) {
        assertEquals(docRawBytes[i], doc2RawBytes[i]);
    }
    assertEquals(doc.getFieldValue("weightedfield"), doc2.getFieldValue("weightedfield"));
    assertEquals(doc.getFieldValue("mapfield"), doc2.getFieldValue("mapfield"));
    Document docInDoc = (Document) doc.getFieldValue("docindoc");
    assert (docInDoc != null);
    assertEquals(new StringFieldValue("ball"), docInDoc.getFieldValue("tull"));
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FileOutputStream(java.io.FileOutputStream) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Raw(com.yahoo.document.datatypes.Raw) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 80 with StringFieldValue

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

the class DocumentTestCase method testEmptyStringsSerialization.

@Test
public void testEmptyStringsSerialization() {
    docMan = new DocumentTypeManager();
    DocumentType docType = new DocumentType("emptystrings");
    docType.addField(new Field("emptystring", DataType.STRING));
    docType.addField(new Field("nullstring", DataType.STRING));
    docType.addField(new Field("spacestring", DataType.STRING));
    docType.addField(new Field("astring", DataType.STRING));
    docMan.registerDocumentType(docType);
    GrowableByteBuffer grbuf = new GrowableByteBuffer();
    {
        Document doc = new Document(docType, new DocumentId("doc:a:b:emptystrings"));
        doc.setFieldValue("emptystring", "");
        doc.removeFieldValue("nullstring");
        doc.setFieldValue("spacestring", " ");
        doc.setFieldValue("astring", "a");
        assertEquals(new StringFieldValue(""), doc.getFieldValue("emptystring"));
        assertNull(doc.getFieldValue("nullstring"));
        assertEquals(new StringFieldValue(" "), doc.getFieldValue("spacestring"));
        assertEquals(new StringFieldValue("a"), doc.getFieldValue("astring"));
        doc.getSerializedSize();
        doc.serialize(grbuf);
        grbuf.flip();
    }
    {
        Document doc2 = docMan.createDocument(grbuf);
        assertEquals(new StringFieldValue(""), doc2.getFieldValue("emptystring"));
        assertNull(doc2.getFieldValue("nullstring"));
        assertEquals(new StringFieldValue(" "), doc2.getFieldValue("spacestring"));
        assertEquals(new StringFieldValue("a"), doc2.getFieldValue("astring"));
    }
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Test(org.junit.Test)

Aggregations

StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)210 Test (org.junit.Test)136 FieldValue (com.yahoo.document.datatypes.FieldValue)49 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)40 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)37 Document (com.yahoo.document.Document)30 Array (com.yahoo.document.datatypes.Array)25 DocumentType (com.yahoo.document.DocumentType)21 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)21 Struct (com.yahoo.document.datatypes.Struct)21 DocumentPut (com.yahoo.document.DocumentPut)20 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)20 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)18 SpanTree (com.yahoo.document.annotation.SpanTree)15 FieldUpdate (com.yahoo.document.update.FieldUpdate)15 DocumentUpdate (com.yahoo.document.DocumentUpdate)12 Field (com.yahoo.document.Field)12 Annotation (com.yahoo.document.annotation.Annotation)12 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)12 HashMap (java.util.HashMap)12