Search in sources :

Example 1 with DataStatePair

use of com.datatorrent.stram.codec.StatefulStreamCodec.DataStatePair in project apex-core by apache.

the class BufferServerPublisher method put.

/**
   *
   * @param payload
   */
@Override
@SuppressWarnings("SleepWhileInLoop")
public void put(Object payload) {
    count++;
    byte[] array;
    if (payload instanceof Tuple) {
        final Tuple t = (Tuple) payload;
        switch(t.getType()) {
            case CHECKPOINT:
                if (statefulSerde != null) {
                    statefulSerde.resetState();
                }
                array = WindowIdTuple.getSerializedTuple((int) t.getWindowId());
                array[0] = MessageType.CHECKPOINT_VALUE;
                break;
            case BEGIN_WINDOW:
                array = BeginWindowTuple.getSerializedTuple((int) t.getWindowId());
                break;
            case END_WINDOW:
                array = EndWindowTuple.getSerializedTuple((int) t.getWindowId());
                break;
            case CUSTOM_CONTROL:
                if (statefulSerde == null) {
                    array = com.datatorrent.bufferserver.packet.CustomControlTuple.getSerializedTuple(MessageType.CUSTOM_CONTROL_VALUE, serde.toByteArray(payload));
                } else {
                    DataStatePair dsp = statefulSerde.toDataStatePair(payload);
                    if (dsp.state != null) {
                        array = com.datatorrent.bufferserver.packet.CustomControlTuple.getSerializedTuple(MessageType.CODEC_STATE_VALUE, dsp.state);
                        try {
                            while (!write(array)) {
                                sleep(5);
                            }
                        } catch (InterruptedException ie) {
                            throw new RuntimeException(ie);
                        }
                    }
                    array = com.datatorrent.bufferserver.packet.CustomControlTuple.getSerializedTuple(MessageType.CUSTOM_CONTROL_VALUE, dsp.data);
                }
                break;
            case END_STREAM:
                array = EndStreamTuple.getSerializedTuple((int) t.getWindowId());
                break;
            case RESET_WINDOW:
                com.datatorrent.stram.tuple.ResetWindowTuple rwt = (com.datatorrent.stram.tuple.ResetWindowTuple) t;
                array = ResetWindowTuple.getSerializedTuple(rwt.getBaseSeconds(), rwt.getIntervalMillis());
                break;
            default:
                throw new UnsupportedOperationException("this data type is not handled in the stream");
        }
    } else {
        if (statefulSerde == null) {
            array = PayloadTuple.getSerializedTuple(serde.getPartition(payload), serde.toByteArray(payload));
        } else {
            DataStatePair dsp = statefulSerde.toDataStatePair(payload);
            /*
         * if there is any state write that for the subscriber before we write the data.
         */
            if (dsp.state != null) {
                array = DataTuple.getSerializedTuple(MessageType.CODEC_STATE_VALUE, dsp.state);
                try {
                    while (!write(array)) {
                        sleep(5);
                    }
                } catch (InterruptedException ie) {
                    throw new RuntimeException(ie);
                }
            }
            /*
         * Now that the state if any has been sent, we can proceed with the actual data we want to send.
         */
            array = PayloadTuple.getSerializedTuple(statefulSerde.getPartition(payload), dsp.data);
        }
    }
    try {
        while (!write(array)) {
            sleep(5);
        }
        publishedByteCount.addAndGet(array.length);
    } catch (InterruptedException ie) {
        throw new RuntimeException(ie);
    }
}
Also used : ResetWindowTuple(com.datatorrent.bufferserver.packet.ResetWindowTuple) ResetWindowTuple(com.datatorrent.bufferserver.packet.ResetWindowTuple) EndWindowTuple(com.datatorrent.bufferserver.packet.EndWindowTuple) BeginWindowTuple(com.datatorrent.bufferserver.packet.BeginWindowTuple) DataTuple(com.datatorrent.bufferserver.packet.DataTuple) CustomControlTuple(com.datatorrent.stram.tuple.CustomControlTuple) ControlTuple(org.apache.apex.api.operator.ControlTuple) EndStreamTuple(com.datatorrent.bufferserver.packet.EndStreamTuple) PayloadTuple(com.datatorrent.bufferserver.packet.PayloadTuple) WindowIdTuple(com.datatorrent.bufferserver.packet.WindowIdTuple) Tuple(com.datatorrent.stram.tuple.Tuple) DataStatePair(com.datatorrent.stram.codec.StatefulStreamCodec.DataStatePair)

Example 2 with DataStatePair

use of com.datatorrent.stram.codec.StatefulStreamCodec.DataStatePair in project apex-core by apache.

the class DefaultStatefulStreamCodecTest method testString.

@Test
public void testString() {
    StatefulStreamCodec<Object> coder = new DefaultStatefulStreamCodec<>();
    StatefulStreamCodec<Object> decoder = new DefaultStatefulStreamCodec<>();
    String hello = "hello";
    DataStatePair dsp = coder.toDataStatePair(hello);
    Assert.assertEquals("both are hello", hello, decoder.fromDataStatePair(dsp));
}
Also used : DataStatePair(com.datatorrent.stram.codec.StatefulStreamCodec.DataStatePair) Test(org.junit.Test)

Example 3 with DataStatePair

use of com.datatorrent.stram.codec.StatefulStreamCodec.DataStatePair in project apex-core by apache.

the class DefaultStatefulStreamCodecTest method testCustomObject.

@Test
public void testCustomObject() {
    DefaultStatefulStreamCodec<TestClass> coder = new DefaultStatefulStreamCodec<>();
    DefaultStatefulStreamCodec<TestClass> decoder = coder.newInstance();
    TestClass tc = new TestClass("hello!", 42);
    //String tc = "hello";
    DataStatePair dsp = coder.toDataStatePair(tc);
    Assert.assertNotNull(dsp.state);
    byte[] state1 = DataTuple.getSerializedTuple(MessageType.CODEC_STATE_VALUE, dsp.state);
    byte[] data1 = PayloadTuple.getSerializedTuple(0, dsp.data);
    dsp = coder.toDataStatePair(tc);
    Assert.assertNull(dsp.state);
    byte[] data2 = PayloadTuple.getSerializedTuple(0, dsp.data);
    Assert.assertNotSame(data1, data2);
    Assert.assertArrayEquals(data1, data2);
    dsp.state = Tuple.getTuple(state1, 0, state1.length).getData();
    dsp.data = Tuple.getTuple(data1, 0, data1.length).getData();
    Assert.assertEquals(tc, decoder.fromDataStatePair(dsp));
    dsp.state = null;
    dsp.data = Tuple.getTuple(data2, 0, data2.length).getData();
    Assert.assertEquals(tc, decoder.fromDataStatePair(dsp));
    coder.resetState();
    dsp = coder.toDataStatePair(tc);
    Assert.assertArrayEquals(state1, DataTuple.getSerializedTuple(MessageType.CODEC_STATE_VALUE, dsp.state));
    Assert.assertNull(coder.toDataStatePair(tc).state);
    data1 = PayloadTuple.getSerializedTuple(Integer.MAX_VALUE, coder.toDataStatePair(tc).data);
    data2 = PayloadTuple.getSerializedTuple(Integer.MAX_VALUE, coder.toDataStatePair(tc).data);
    Assert.assertArrayEquals(data1, data2);
}
Also used : DataStatePair(com.datatorrent.stram.codec.StatefulStreamCodec.DataStatePair) Test(org.junit.Test)

Example 4 with DataStatePair

use of com.datatorrent.stram.codec.StatefulStreamCodec.DataStatePair in project apex-core by apache.

the class DefaultStatefulStreamCodecTest method testFinalFieldSerialization.

@Test
public void testFinalFieldSerialization() throws Exception {
    TestTuple t1 = new TestTuple(5);
    DefaultStatefulStreamCodec<Object> c = new DefaultStatefulStreamCodec<>();
    DataStatePair dsp = c.toDataStatePair(t1);
    TestTuple t2 = (TestTuple) c.fromDataStatePair(dsp);
    Assert.assertEquals("", t1.finalField, t2.finalField);
}
Also used : DataStatePair(com.datatorrent.stram.codec.StatefulStreamCodec.DataStatePair) Test(org.junit.Test)

Example 5 with DataStatePair

use of com.datatorrent.stram.codec.StatefulStreamCodec.DataStatePair in project apex-core by apache.

the class DefaultStatefulStreamCodecTest method testInnerClassSerialization.

@Test
public void testInnerClassSerialization() throws Exception {
    OuterClass outer = new OuterClass();
    Object inner = outer.new InnerClass();
    for (Object o : new Object[] { outer, inner }) {
        DefaultStatefulStreamCodec<Object> c = new DefaultStatefulStreamCodec<>();
        DataStatePair dsp = c.toDataStatePair(o);
        c.fromDataStatePair(dsp);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(o);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
        ois.readObject();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) DataStatePair(com.datatorrent.stram.codec.StatefulStreamCodec.DataStatePair) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

DataStatePair (com.datatorrent.stram.codec.StatefulStreamCodec.DataStatePair)5 Test (org.junit.Test)4 BeginWindowTuple (com.datatorrent.bufferserver.packet.BeginWindowTuple)1 DataTuple (com.datatorrent.bufferserver.packet.DataTuple)1 EndStreamTuple (com.datatorrent.bufferserver.packet.EndStreamTuple)1 EndWindowTuple (com.datatorrent.bufferserver.packet.EndWindowTuple)1 PayloadTuple (com.datatorrent.bufferserver.packet.PayloadTuple)1 ResetWindowTuple (com.datatorrent.bufferserver.packet.ResetWindowTuple)1 WindowIdTuple (com.datatorrent.bufferserver.packet.WindowIdTuple)1 CustomControlTuple (com.datatorrent.stram.tuple.CustomControlTuple)1 Tuple (com.datatorrent.stram.tuple.Tuple)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ControlTuple (org.apache.apex.api.operator.ControlTuple)1