Search in sources :

Example 16 with GrowableByteBuffer

use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.

the class DocumentTestCase method testDocumentDataType.

@Test
public void testDocumentDataType() {
    // use documenttypemanagerconfigurer to read config
    docMan = new DocumentTypeManager();
    DocumentTypeManagerConfigurer.configure(docMan, "file:src/test/document/docindoc.cfg");
    // get a document type
    docMan.getDocumentType("outerdoc");
    // create document and necessary structures
    Document outerdoc = new Document(docMan.getDocumentType("outerdoc"), new DocumentId("doc:recursion:outerdoc"));
    Document innerdoc = new Document(docMan.getDocumentType("innerdoc"), new DocumentId("doc:recursion:innerdoc"));
    innerdoc.setFieldValue("intfield", 55);
    outerdoc.setFieldValue("stringfield", "boooo");
    outerdoc.setFieldValue("docfield", innerdoc);
    // serialize document
    int size = outerdoc.getSerializedSize();
    GrowableByteBuffer buf = new GrowableByteBuffer(size, 2.0f);
    outerdoc.serialize(buf);
    assertEquals(size, buf.position());
    // deserialize document
    buf.position(0);
    Document outerdoc2 = docMan.createDocument(buf);
    // compare values
    assertEquals(outerdoc, outerdoc2);
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Test(org.junit.Test)

Example 17 with GrowableByteBuffer

use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.

the class DocumentTestCase method testGenerateSerializedFile.

@Test
public void testGenerateSerializedFile() throws IOException {
    docMan = setUpCppDocType();
    Document doc = new Document(docMan.getDocumentType("serializetest"), new DocumentId("doc:serializetest:http://test.doc.id/"));
    Document docindoc = new Document(docMan.getDocumentType("docindoc"), new DocumentId("doc:serializetest:http://doc.in.doc/"));
    docindoc.setFieldValue("stringindocfield", "Elvis is dead");
    doc.setFieldValue("docfield", docindoc);
    Array<FloatFieldValue> l = new Array<>(doc.getField("arrayoffloatfield").getDataType());
    l.add(new FloatFieldValue((float) 1.0));
    l.add(new FloatFieldValue((float) 2.0));
    doc.setFieldValue("arrayoffloatfield", l);
    WeightedSet<StringFieldValue> wset = new WeightedSet<>(doc.getDataType().getField("wsfield").getDataType());
    wset.put(new StringFieldValue("Weighted 0"), 50);
    wset.put(new StringFieldValue("Weighted 1"), 199);
    doc.setFieldValue("wsfield", wset);
    MapFieldValue<StringFieldValue, StringFieldValue> map = new MapFieldValue<>((MapDataType) doc.getDataType().getField("mapfield").getDataType());
    map.put(new StringFieldValue("foo1"), new StringFieldValue("bar1"));
    map.put(new StringFieldValue("foo2"), new StringFieldValue("bar2"));
    doc.setFieldValue("mapfield", map);
    doc.setFieldValue("bytefield", new ByteFieldValue((byte) 254));
    doc.setFieldValue("rawfield", new Raw(ByteBuffer.wrap("RAW DATA".getBytes())));
    doc.setFieldValue("intfield", new IntegerFieldValue(5));
    doc.setFieldValue("floatfield", new FloatFieldValue(-9.23f));
    doc.setFieldValue("stringfield", new StringFieldValue("This is a string."));
    doc.setFieldValue("longfield", new LongFieldValue(398420092938472983L));
    doc.setFieldValue("doublefield", new DoubleFieldValue(98374532.398820d));
    doc.setFieldValue("urifield", new StringFieldValue("http://this.is.a.test/"));
    int size = doc.getSerializedSize();
    GrowableByteBuffer buf = new GrowableByteBuffer(size, 2.0f);
    doc.serialize(buf);
    assertEquals(size, buf.position());
    buf.position(0);
    FileOutputStream fos = new FileOutputStream("src/tests/data/serializejava.dat");
    fos.write(buf.array(), 0, size);
    fos.close();
    CompressionConfig noncomp = new CompressionConfig();
    CompressionConfig lz4comp = new CompressionConfig(CompressionType.LZ4);
    doc.getDataType().getHeaderType().setCompressionConfig(lz4comp);
    doc.getDataType().getBodyType().setCompressionConfig(lz4comp);
    buf = new GrowableByteBuffer(size, 2.0f);
    doc.serialize(buf);
    doc.getDataType().getHeaderType().setCompressionConfig(noncomp);
    doc.getDataType().getBodyType().setCompressionConfig(noncomp);
    fos = new FileOutputStream("src/tests/data/serializejava-compressed.dat");
    fos.write(buf.array(), 0, buf.position());
    fos.close();
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) Raw(com.yahoo.document.datatypes.Raw) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FileOutputStream(java.io.FileOutputStream) WeightedSet(com.yahoo.document.datatypes.WeightedSet) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) Test(org.junit.Test)

Example 18 with GrowableByteBuffer

use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.

the class DocumentTestCase method testTimestamp.

@Test
public void testTimestamp() {
    Document doc = new Document(docMan.getDocumentType("testdoc"), new DocumentId("doc:testdoc:timetest"));
    assertNull(doc.getLastModified());
    doc.setLastModified(4350129845023985L);
    assertEquals(new Long(4350129845023985L), doc.getLastModified());
    doc.setLastModified(null);
    assertNull(doc.getLastModified());
    Long timestamp = System.currentTimeMillis();
    doc.setLastModified(timestamp);
    assertEquals(timestamp, doc.getLastModified());
    GrowableByteBuffer buf = new GrowableByteBuffer();
    doc.getSerializedSize();
    doc.serialize(buf);
    buf.position(0);
    Document doc2 = docMan.createDocument(buf);
    assertNull(doc2.getLastModified());
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Test(org.junit.Test)

Example 19 with GrowableByteBuffer

use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.

the class DocumentTestCase method testUnknownFieldsDeserialization.

@Test
public void testUnknownFieldsDeserialization() {
    DocumentTypeManager docTypeManasjer = new DocumentTypeManager();
    GrowableByteBuffer buf = new GrowableByteBuffer();
    {
        DocumentType typeWithDinner = new DocumentType("elvis");
        typeWithDinner.addField("breakfast", DataType.STRING);
        typeWithDinner.addField("lunch", DataType.INT);
        typeWithDinner.addField("dinner", DataType.DOUBLE);
        docTypeManasjer.registerDocumentType(typeWithDinner);
        Document docWithDinner = new Document(typeWithDinner, "doc:elvis:has:left:the:building");
        docWithDinner.setFieldValue("breakfast", "peanut butter");
        docWithDinner.setFieldValue("lunch", 14);
        docWithDinner.setFieldValue("dinner", 5.43d);
        docWithDinner.serialize(buf);
        buf.flip();
        docTypeManasjer.clear();
    }
    {
        DocumentType typeWithoutDinner = new DocumentType("elvis");
        typeWithoutDinner.addField("breakfast", DataType.STRING);
        typeWithoutDinner.addField("lunch", DataType.INT);
        // no dinner
        docTypeManasjer.registerDocumentType(typeWithoutDinner);
        Document docWithoutDinner = docTypeManasjer.createDocument(buf);
        assertEquals(new StringFieldValue("peanut butter"), docWithoutDinner.getFieldValue("breakfast"));
        assertEquals(new IntegerFieldValue(14), docWithoutDinner.getFieldValue("lunch"));
        assertNull(docWithoutDinner.getFieldValue("dinner"));
    }
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Test(org.junit.Test)

Example 20 with GrowableByteBuffer

use of com.yahoo.io.GrowableByteBuffer 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)

Aggregations

GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)74 Test (org.junit.Test)34 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)18 Document (com.yahoo.document.Document)9 BufferSerializer (com.yahoo.vespa.objects.BufferSerializer)9 Field (com.yahoo.document.Field)8 ByteBuffer (java.nio.ByteBuffer)8 FileOutputStream (java.io.FileOutputStream)6 DocumentType (com.yahoo.document.DocumentType)5 DocumentTypeManager (com.yahoo.document.DocumentTypeManager)5 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)5 AbstractTypesTest (com.yahoo.document.annotation.AbstractTypesTest)4 Raw (com.yahoo.document.datatypes.Raw)4 Struct (com.yahoo.document.datatypes.Struct)4 Grouping (com.yahoo.searchlib.aggregation.Grouping)4 Array (com.yahoo.document.datatypes.Array)3 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)3 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)3 FieldUpdate (com.yahoo.document.update.FieldUpdate)3 IOException (java.io.IOException)3