Search in sources :

Example 1 with DataOutputPlus

use of org.apache.cassandra.io.util.DataOutputPlus in project cassandra by apache.

the class RepairMessageSerializationsTest method serializeRoundTrip.

private <T extends RepairMessage> T serializeRoundTrip(T msg, IVersionedSerializer<T> serializer) throws IOException {
    long size = serializer.serializedSize(msg, PROTOCOL_VERSION);
    ByteBuffer buf = ByteBuffer.allocate((int) size);
    DataOutputPlus out = new DataOutputBufferFixed(buf);
    serializer.serialize(msg, out, PROTOCOL_VERSION);
    Assert.assertEquals(size, buf.position());
    buf.flip();
    DataInputPlus in = new DataInputBuffer(buf, false);
    T deserialized = serializer.deserialize(in, PROTOCOL_VERSION);
    Assert.assertEquals(msg, deserialized);
    Assert.assertEquals(msg.hashCode(), deserialized.hashCode());
    return deserialized;
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) ByteBuffer(java.nio.ByteBuffer) DataOutputPlus(org.apache.cassandra.io.util.DataOutputPlus) DataOutputBufferFixed(org.apache.cassandra.io.util.DataOutputBufferFixed)

Example 2 with DataOutputPlus

use of org.apache.cassandra.io.util.DataOutputPlus in project cassandra by apache.

the class IntervalTreeTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    List<Interval<Integer, String>> intervals = new ArrayList<Interval<Integer, String>>();
    intervals.add(Interval.<Integer, String>create(-300, -200, "a"));
    intervals.add(Interval.<Integer, String>create(-3, -2, "b"));
    intervals.add(Interval.<Integer, String>create(1, 2, "c"));
    intervals.add(Interval.<Integer, String>create(1, 3, "d"));
    intervals.add(Interval.<Integer, String>create(2, 4, "e"));
    intervals.add(Interval.<Integer, String>create(3, 6, "f"));
    intervals.add(Interval.<Integer, String>create(4, 6, "g"));
    intervals.add(Interval.<Integer, String>create(5, 7, "h"));
    intervals.add(Interval.<Integer, String>create(8, 9, "i"));
    intervals.add(Interval.<Integer, String>create(15, 20, "j"));
    intervals.add(Interval.<Integer, String>create(40, 50, "k"));
    intervals.add(Interval.<Integer, String>create(49, 60, "l"));
    IntervalTree<Integer, String, Interval<Integer, String>> it = IntervalTree.build(intervals);
    IVersionedSerializer<IntervalTree<Integer, String, Interval<Integer, String>>> serializer = IntervalTree.serializer(new ISerializer<Integer>() {

        public void serialize(Integer i, DataOutputPlus out) throws IOException {
            out.writeInt(i);
        }

        public Integer deserialize(DataInputPlus in) throws IOException {
            return in.readInt();
        }

        public long serializedSize(Integer i) {
            return 4;
        }
    }, new ISerializer<String>() {

        public void serialize(String v, DataOutputPlus out) throws IOException {
            out.writeUTF(v);
        }

        public String deserialize(DataInputPlus in) throws IOException {
            return in.readUTF();
        }

        public long serializedSize(String v) {
            return v.length();
        }
    }, (Constructor<Interval<Integer, String>>) (Object) Interval.class.getConstructor(Object.class, Object.class, Object.class));
    DataOutputBuffer out = new DataOutputBuffer();
    serializer.serialize(it, out, 0);
    DataInputPlus in = new DataInputBuffer(out.toByteArray());
    IntervalTree<Integer, String, Interval<Integer, String>> it2 = serializer.deserialize(in, 0);
    List<Interval<Integer, String>> intervals2 = new ArrayList<Interval<Integer, String>>();
    for (Interval<Integer, String> i : it2) intervals2.add(i);
    assertEquals(intervals, intervals2);
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) DataOutputPlus(org.apache.cassandra.io.util.DataOutputPlus) Test(org.junit.Test)

Aggregations

DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)2 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)2 DataOutputPlus (org.apache.cassandra.io.util.DataOutputPlus)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)1 DataOutputBufferFixed (org.apache.cassandra.io.util.DataOutputBufferFixed)1 Test (org.junit.Test)1