Search in sources :

Example 46 with GrowableByteBuffer

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

the class TypedBinaryFormat method encode.

public static byte[] encode(Tensor tensor) {
    GrowableByteBuffer buffer = new GrowableByteBuffer();
    if (tensor instanceof MixedTensor) {
        buffer.putInt1_4Bytes(MIXED_BINARY_FORMAT_TYPE);
        new MixedBinaryFormat().encode(buffer, tensor);
    } else if (tensor instanceof IndexedTensor) {
        buffer.putInt1_4Bytes(DENSE_BINARY_FORMAT_TYPE);
        new DenseBinaryFormat().encode(buffer, tensor);
    } else {
        buffer.putInt1_4Bytes(SPARSE_BINARY_FORMAT_TYPE);
        new SparseBinaryFormat().encode(buffer, tensor);
    }
    buffer.flip();
    byte[] result = new byte[buffer.remaining()];
    buffer.get(result);
    return result;
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) MixedTensor(com.yahoo.tensor.MixedTensor) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 47 with GrowableByteBuffer

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

the class SerializeTestCase method testOne.

@Test
public void testOne() {
    SomeIdClass s = new SomeIdClass();
    BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
    s.serializeWithId(buf);
    buf.flip();
    Identifiable s2 = Identifiable.create(buf);
    assertThat((s2 instanceof SomeIdClass), is(true));
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Test(org.junit.Test)

Example 48 with GrowableByteBuffer

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

the class SerializeTestCase method testBig.

@Test
public void testBig() {
    BigIdClass dv = new BigIdClass();
    BigIdClass ov = new BigIdClass(6667666);
    BigIdClass bv = new BigIdClass(123456789);
    assertThat(BigIdClass.classId, is(42));
    assertThat(dv.getClassId(), is(42));
    assertThat(ov.getClassId(), is(42));
    assertThat(bv.getClassId(), is(42));
    assertThat(ov.equals(dv), is(false));
    assertThat(dv.equals(bv), is(false));
    assertThat(bv.equals(ov), is(false));
    BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
    ov.serialize(buf);
    buf.flip();
    dv.deserialize(buf);
    assertThat(ov, equalTo(dv));
    assertThat(dv, equalTo(ov));
    buf = new BufferSerializer(new GrowableByteBuffer());
    bv.serializeWithId(buf);
    buf.flip();
    dv.deserializeWithId(buf);
    assertThat(bv, equalTo(dv));
    assertThat(dv, equalTo(bv));
    buf = new BufferSerializer(new GrowableByteBuffer());
    SomeIdClass s = new SomeIdClass();
    assertThat(dv.equals(s), is(false));
    assertThat(ov.equals(s), is(false));
    assertThat(bv.equals(s), is(false));
    assertThat(dv.equals(new Object()), is(false));
    s.serializeWithId(buf);
    buf.flip();
    boolean caught = false;
    try {
        dv.deserializeWithId(buf);
    } catch (IllegalArgumentException ex) {
        caught = true;
    // System.out.println(ex);
    }
    assertThat(caught, is(true));
    buf = new BufferSerializer(new GrowableByteBuffer());
    buf.putLong(null, 0x7777777777777777L);
    buf.flip();
    caught = false;
    try {
        dv.deserializeWithId(buf);
    } catch (IllegalArgumentException ex) {
        caught = true;
    // System.out.println(ex);
    }
    assertThat(caught, is(true));
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Test(org.junit.Test)

Example 49 with GrowableByteBuffer

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

the class SerializeTestCase method testOrdering.

@Test
public void testOrdering() {
    BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
    assertThat(buf.order(), is(ByteOrder.BIG_ENDIAN));
    buf.putInt(null, 0x11223344);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.putInt(null, 0x55667788);
    assertThat(buf.order(), is(ByteOrder.LITTLE_ENDIAN));
    buf.flip();
    assertThat(buf.getByte(null), is((byte) 0x11));
    assertThat(buf.getByte(null), is((byte) 0x22));
    assertThat(buf.getShort(null), is((short) 0x4433));
    buf.order(ByteOrder.BIG_ENDIAN);
    assertThat(buf.getByte(null), is((byte) 0x88));
    assertThat(buf.getByte(null), is((byte) 0x77));
    assertThat(buf.getShort(null), is((short) 0x6655));
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Test(org.junit.Test)

Example 50 with GrowableByteBuffer

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

the class Document method getSerializedSize.

public int getSerializedSize() throws SerializationException {
    DocumentSerializer data = DocumentSerializerFactory.create42(new GrowableByteBuffer(64 * 1024, 2.0f));
    data.write(this);
    return data.getBuf().position();
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer)

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