Search in sources :

Example 11 with BinaryEncoder

use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.

the class ASMDatumCodecTest method testTree.

@Test
public void testTree() throws IOException, UnsupportedTypeException {
    TypeToken<Node> type = new TypeToken<Node>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Node> writer = getWriter(type);
    Node root = new Node((short) 1, new Node((short) 2, null, new Node((short) 3, null, null)), new Node((short) 4, new Node((short) 5, null, null), null));
    writer.encode(root, new BinaryEncoder(os));
    ReflectionDatumReader<Node> reader = new ReflectionDatumReader<>(getSchema(type), type);
    Node value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(root, value);
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 12 with BinaryEncoder

use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.

the class QueueEntry method serializeEmptyHashKeys.

// many entries will have no hash keys. Serialize that once and for good
private static byte[] serializeEmptyHashKeys() {
    try {
        // we don't synchronize here: the worst thing that go wrong here is repeated assignment to the same value
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Encoder encoder = new BinaryEncoder(bos);
        encoder.writeInt(0);
        return bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException("encoding empty hash keys went wrong - bailing out: " + e.getMessage(), e);
    }
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Encoder(co.cask.cdap.common.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 13 with BinaryEncoder

use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.

the class StreamEventCodec method encodePayload.

/**
   * Encodes the given {@link StreamEvent} into {@code byte[]} that could become
   * payload of a QueueEntry.
   *
   * @param event The {@link StreamEvent} to encode.
   * @return Encoded {@code byte[]}.
   */
public byte[] encodePayload(StreamEvent event) {
    // TODO: This is a bit hacky to do it directly for now, for performance reason.
    ByteBuffer body = event.getBody();
    Map<String, String> headers = event.getHeaders();
    long timestamp = event.getTimestamp();
    // Some assumption on the header size to minimize array copying
    // 16 bytes Schema hash + body size + (header size) * (50 bytes key/value pair) + 9 bytes timestamp (vlong encoding)
    ByteArrayOutputStream os = new ByteArrayOutputStream(16 + body.remaining() + headers.size() * 50 + 9);
    Encoder encoder = new BinaryEncoder(os);
    try {
        // Write the schema hash
        os.write(STREAM_EVENT_SCHEMA.getSchemaHash().toByteArray());
        StreamEventDataCodec.encode(event, encoder);
        encoder.writeLong(timestamp);
        return os.toByteArray();
    } catch (IOException e) {
        // It should never happens, otherwise something very wrong.
        throw Throwables.propagate(e);
    }
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Encoder(co.cask.cdap.common.io.Encoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 14 with BinaryEncoder

use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.

the class AccessTokenIdentifierCodec method encode.

@Override
public byte[] encode(AccessTokenIdentifier identifier) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(AccessTokenIdentifier.Schemas.getVersion());
    DatumWriter<AccessTokenIdentifier> writer = writerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, AccessTokenIdentifier.Schemas.getCurrentSchema());
    writer.encode(identifier, encoder);
    return bos.toByteArray();
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Encoder(co.cask.cdap.common.io.Encoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 15 with BinaryEncoder

use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.

the class KeyIdentifierCodec method encode.

@Override
public byte[] encode(KeyIdentifier keyIdentifier) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(KeyIdentifier.Schemas.getVersion());
    DatumWriter<KeyIdentifier> writer = writerFactory.create(KEY_IDENTIFIER_TYPE, KeyIdentifier.Schemas.getCurrentSchema());
    writer.encode(keyIdentifier, encoder);
    return bos.toByteArray();
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Encoder(co.cask.cdap.common.io.Encoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)32 Test (org.junit.Test)24 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)22 PipedInputStream (java.io.PipedInputStream)21 PipedOutputStream (java.io.PipedOutputStream)21 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)18 TypeToken (com.google.common.reflect.TypeToken)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Encoder (co.cask.cdap.common.io.Encoder)7 Schema (co.cask.cdap.api.data.schema.Schema)6 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)6 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 IOException (java.io.IOException)4 ReflectionDatumWriter (co.cask.cdap.internal.io.ReflectionDatumWriter)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3 ByteBuffer (java.nio.ByteBuffer)2 DataSetException (co.cask.cdap.api.dataset.DataSetException)1 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)1