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);
}
}
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));
}
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);
}
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);
}
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();
}
}
Aggregations