Search in sources :

Example 61 with GrowableByteBuffer

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

the class ProgressToken method serialize.

public byte[] serialize() {
    DocumentSerializer out = DocumentSerializerFactory.create42(new GrowableByteBuffer());
    out.putInt(null, distributionBits);
    out.putLong(null, bucketCursor);
    out.putLong(null, finishedBucketCount);
    out.putLong(null, totalBucketCount);
    out.putInt(null, buckets.size());
    // Append individual bucket progress
    for (Map.Entry<BucketKeyWrapper, ProgressToken.BucketEntry> entry : buckets.entrySet()) {
        out.putLong(null, keyToBucketId(entry.getKey().getKey()));
        out.putLong(null, entry.getValue().getProgress().getRawId());
    }
    byte[] ret = new byte[out.getBuf().position()];
    out.getBuf().rewind();
    out.getBuf().get(ret);
    return ret;
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 62 with GrowableByteBuffer

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

the class DocumentGenPluginTest method roundtripSerialize.

private static Document roundtripSerialize(Document docToSerialize, DocumentTypeManager mgr) {
    final GrowableByteBuffer outputBuffer = new GrowableByteBuffer();
    final DocumentSerializer serializer = DocumentSerializerFactory.createHead(outputBuffer);
    serializer.write(docToSerialize);
    outputBuffer.flip();
    return new Document(DocumentDeserializerFactory.createHead(mgr, outputBuffer));
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) ProxyDocument(com.yahoo.docproc.proxy.ProxyDocument)

Example 63 with GrowableByteBuffer

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

the class VespaDocumentDeserializer42 method read.

public void read(FieldBase fieldDef, Struct s) {
    s.setVersion(version);
    int startPos = position();
    if (version < 6) {
        throw new DeserializationException("Illegal document serialization version " + version);
    }
    int dataSize;
    if (version < 7) {
        long rSize = getInt2_4_8Bytes(null);
        // TODO: Look into how to support data segments larger than INT_MAX bytes
        if (rSize > Integer.MAX_VALUE) {
            throw new DeserializationException("Raw size of data block is too large.");
        }
        dataSize = (int) rSize;
    } else {
        dataSize = getInt(null);
    }
    byte comprCode = getByte(null);
    CompressionType compression = CompressionType.valueOf(comprCode);
    int uncompressedSize = 0;
    if (compression != CompressionType.NONE && compression != CompressionType.INCOMPRESSIBLE) {
        // uncompressedsize (full size of FIELDS only, after decompression)
        long pSize = getInt2_4_8Bytes(null);
        // TODO: Look into how to support data segments larger than INT_MAX bytes
        if (pSize > Integer.MAX_VALUE) {
            throw new DeserializationException("Uncompressed size of data block is too large.");
        }
        uncompressedSize = (int) pSize;
    }
    int numberOfFields = getInt1_4Bytes(null);
    List<Tuple2<Integer, Long>> fieldIdsAndLengths = new ArrayList<>(numberOfFields);
    for (int i = 0; i < numberOfFields; ++i) {
        // id, length (length only used for unknown fields
        fieldIdsAndLengths.add(new Tuple2<>(getInt1_4Bytes(null), getInt2_4_8Bytes(null)));
    }
    // save a reference to the big buffer we're reading from:
    GrowableByteBuffer bigBuf = buf;
    if (version < 7) {
        // In V6 and earlier, the length included the header.
        int headerSize = position() - startPos;
        dataSize -= headerSize;
    }
    byte[] destination = compressor.decompress(compression, getBuf().array(), position(), uncompressedSize, Optional.of(dataSize));
    // set position in original buffer to after data
    position(position() + dataSize);
    // for a while: deserialize from this buffer instead:
    buf = GrowableByteBuffer.wrap(destination);
    s.clear();
    StructDataType type = s.getDataType();
    for (int i = 0; i < numberOfFields; ++i) {
        Field structField = type.getField(fieldIdsAndLengths.get(i).first, version);
        if (structField == null) {
            // ignoring unknown field:
            position(position() + fieldIdsAndLengths.get(i).second.intValue());
        } else {
            int posBefore = position();
            FieldValue value = structField.getDataType().createFieldValue();
            value.deserialize(structField, this);
            s.setFieldValue(structField, value);
            // jump to beginning of next field:
            position(posBefore + fieldIdsAndLengths.get(i).second.intValue());
        }
    }
    // restore the original buffer
    buf = bigBuf;
}
Also used : ArrayList(java.util.ArrayList) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Field(com.yahoo.document.Field) Tuple2(com.yahoo.collections.Tuple2) StructDataType(com.yahoo.document.StructDataType) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) ReferenceFieldValue(com.yahoo.document.datatypes.ReferenceFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) TensorFieldValue(com.yahoo.document.datatypes.TensorFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StructuredFieldValue(com.yahoo.document.datatypes.StructuredFieldValue) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) CompressionType(com.yahoo.compress.CompressionType)

Example 64 with GrowableByteBuffer

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

the class DocumentTestCase method testCppDoc.

@Test
public void testCppDoc() throws IOException {
    docMan = setUpCppDocType();
    byte[] data = readFile("src/test/document/serializecpp.dat");
    ByteBuffer buf = ByteBuffer.wrap(data);
    Document doc = docMan.createDocument(new GrowableByteBuffer(buf));
    validateCppDoc(doc);
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) ByteBuffer(java.nio.ByteBuffer) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Test(org.junit.Test)

Example 65 with GrowableByteBuffer

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

the class DocumentTestCase method testInheritance.

@Test
public void testInheritance() {
    // Create types that inherit each other.. And test that it works..
    DocumentType parentType = new DocumentType("parent");
    parentType.addField(new Field("parentbodyint", DataType.INT, false));
    parentType.addField(new Field("parentheaderint", DataType.INT, true));
    parentType.addField(new Field("overwritten", DataType.INT, true));
    DocumentType childType = new DocumentType("child");
    childType.addField(new Field("childbodyint", DataType.INT, false));
    childType.addField(new Field("childheaderint", DataType.INT, true));
    childType.addField(new Field("overwritten", DataType.INT, true));
    childType.inherit(parentType);
    DocumentTypeManager manager = new DocumentTypeManager();
    manager.register(childType);
    Document child = new Document(childType, new DocumentId("doc:what:test"));
    child.setFieldValue(childType.getField("parentbodyint"), new IntegerFieldValue(4));
    child.setFieldValue("parentheaderint", 6);
    child.setFieldValue("overwritten", 7);
    child.setFieldValue("childbodyint", 14);
    GrowableByteBuffer buffer = new GrowableByteBuffer(1024, 2f);
    child.serialize(buffer);
    buffer.flip();
    Document childCopy = manager.createDocument(buffer);
    // Test various ways of retrieving values
    assertEquals(new IntegerFieldValue(4), childCopy.getFieldValue(childType.getField("parentbodyint")));
    assertEquals(new IntegerFieldValue(6), childCopy.getFieldValue("parentheaderint"));
    assertEquals(new IntegerFieldValue(7), childCopy.getFieldValue("overwritten"));
    assertEquals(child, childCopy);
}
Also used : IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) 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