Search in sources :

Example 16 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.

the class ASMDatumCodecTest method testRecordContainer.

@Test
public void testRecordContainer() throws IOException, UnsupportedTypeException {
    TypeToken<List<Record>> type = new TypeToken<List<Record>>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<List<Record>> writer = getWriter(type);
    List<Record> writeValue = ImmutableList.of(new Record(10, "testing", ImmutableList.of("a", "b", "c"), TestEnum.VALUE2));
    writer.encode(writeValue, new BinaryEncoder(os));
    ReflectionDatumReader<List<Record>> reader = new ReflectionDatumReader<>(getSchema(type), type);
    List<Record> value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(writeValue, value);
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 17 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.

the class ASMDatumCodecTest method testSpeed.

@Ignore
@Test
public void testSpeed() throws UnsupportedTypeException, IOException {
    TypeToken<Node> type = new TypeToken<Node>() {
    };
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    long startTime;
    long endTime;
    Node writeValue = 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));
    DatumWriter<Node> writer = getWriter(type);
    startTime = System.nanoTime();
    for (int i = 0; i < 100000; i++) {
        os.reset();
        writer.encode(writeValue, new BinaryEncoder(os));
    }
    endTime = System.nanoTime();
    System.out.println("Time spent: " + TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS));
    ReflectionDatumWriter<Node> datumWriter = new ReflectionDatumWriter<>(getSchema(type));
    startTime = System.nanoTime();
    for (int i = 0; i < 100000; i++) {
        os.reset();
        datumWriter.encode(writeValue, new BinaryEncoder(os));
    }
    endTime = System.nanoTime();
    System.out.println("Time spent: " + TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS));
    writer = getWriter(type);
    startTime = System.nanoTime();
    for (int i = 0; i < 100000; i++) {
        os.reset();
        writer.encode(writeValue, new BinaryEncoder(os));
    }
    endTime = System.nanoTime();
    System.out.println("Time spent: " + TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS));
    datumWriter = new ReflectionDatumWriter<>(getSchema(type));
    startTime = System.nanoTime();
    for (int i = 0; i < 100000; i++) {
        os.reset();
        datumWriter.encode(writeValue, new BinaryEncoder(os));
    }
    endTime = System.nanoTime();
    System.out.println("Time spent: " + TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS));
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) ReflectionDatumWriter(io.cdap.cdap.internal.io.ReflectionDatumWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.

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(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 19 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.

the class ASMDatumCodecTest method testString.

@Test
public void testString() throws UnsupportedTypeException, IOException {
    TypeToken<String> type = new TypeToken<String>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<String> writer = getWriter(type);
    writer.encode("Testing message", new BinaryEncoder(os));
    ReflectionDatumReader<String> reader = new ReflectionDatumReader<>(getSchema(type), type);
    String value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals("Testing message", value);
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 20 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.

the class ASMDatumCodecTest method testPrimitiveArray.

@Test
public void testPrimitiveArray() throws IOException, UnsupportedTypeException {
    TypeToken<int[]> type = new TypeToken<int[]>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    int[] writeValue = { 1, 2, 3, 4, -5, -6, -7, -8 };
    DatumWriter<int[]> writer = getWriter(type);
    writer.encode(writeValue, new BinaryEncoder(os));
    ReflectionDatumReader<int[]> reader = new ReflectionDatumReader<>(getSchema(type), type);
    int[] value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertArrayEquals(writeValue, value);
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Aggregations

BinaryEncoder (io.cdap.cdap.common.io.BinaryEncoder)54 Test (org.junit.Test)46 BinaryDecoder (io.cdap.cdap.common.io.BinaryDecoder)42 PipedInputStream (java.io.PipedInputStream)40 PipedOutputStream (java.io.PipedOutputStream)40 TypeToken (com.google.common.reflect.TypeToken)34 ReflectionDatumReader (io.cdap.cdap.internal.io.ReflectionDatumReader)34 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 Schema (io.cdap.cdap.api.data.schema.Schema)12 ReflectionSchemaGenerator (io.cdap.cdap.internal.io.ReflectionSchemaGenerator)12 ImmutableList (com.google.common.collect.ImmutableList)10 List (java.util.List)10 Encoder (io.cdap.cdap.common.io.Encoder)8 ReflectionDatumWriter (io.cdap.cdap.internal.io.ReflectionDatumWriter)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 Map (java.util.Map)4 Predicate (com.google.common.base.Predicate)2 DataSetException (io.cdap.cdap.api.dataset.DataSetException)2 Decoder (io.cdap.cdap.common.io.Decoder)2 TypeRepresentation (io.cdap.cdap.internal.io.TypeRepresentation)2