Search in sources :

Example 16 with DataInputBuffer

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

the class InetAddressAndPortSerializerTest method testAddress.

private void testAddress(InetAddressAndPort address, int version) throws Exception {
    ByteBuffer out;
    try (DataOutputBuffer dob = new DataOutputBuffer()) {
        inetAddressAndPortSerializer.serialize(address, dob, version);
        out = dob.buffer();
    }
    assertEquals(out.remaining(), inetAddressAndPortSerializer.serializedSize(address, version));
    InetAddressAndPort roundtripped;
    try (DataInputBuffer dib = new DataInputBuffer(out, false)) {
        roundtripped = inetAddressAndPortSerializer.deserialize(dib, version);
    }
    if (version >= MessagingService.VERSION_40) {
        assertEquals(address, roundtripped);
    } else {
        assertEquals(address.getAddress(), roundtripped.getAddress());
        assertEquals(InetAddressAndPort.getDefaultPort(), roundtripped.getPort());
    }
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 17 with DataInputBuffer

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

the class MerkleTreesTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Range<Token> first = new Range<>(tok(3), tok(4));
    Collection<Range<Token>> ranges = new ArrayList<>();
    ranges.add(first);
    ranges.add(new Range<Token>(tok(5), tok(2)));
    mts = new MerkleTrees(partitioner);
    mts.addMerkleTrees(256, ranges);
    // populate and validate the tree
    mts.init();
    for (TreeRange range : mts.rangeIterator()) range.addAll(new HIterator(range.right));
    byte[] initialhash = mts.hash(first);
    long serializedSize = MerkleTrees.serializer.serializedSize(mts, MessagingService.current_version);
    DataOutputBuffer out = new DataOutputBuffer();
    MerkleTrees.serializer.serialize(mts, out, MessagingService.current_version);
    byte[] serialized = out.toByteArray();
    assertEquals(serializedSize, serialized.length);
    DataInputBuffer in = new DataInputBuffer(serialized);
    MerkleTrees restored = MerkleTrees.serializer.deserialize(in, MessagingService.current_version);
    assertHashEquals(initialhash, restored.hash(first));
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) Test(org.junit.Test)

Example 18 with DataInputBuffer

use of org.apache.cassandra.io.util.DataInputBuffer 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)

Example 19 with DataInputBuffer

use of org.apache.cassandra.io.util.DataInputBuffer 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 20 with DataInputBuffer

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

the class HintsBufferTest method validateEntry.

private static int validateEntry(UUID hostId, ByteBuffer buffer, long baseTimestamp, UUID[] load) throws IOException {
    CRC32 crc = new CRC32();
    DataInputPlus di = new DataInputBuffer(buffer, true);
    // read and validate size
    int hintSize = di.readInt();
    assertEquals(hintSize + HintsBuffer.ENTRY_OVERHEAD_SIZE, buffer.remaining());
    // read and validate size crc
    updateChecksum(crc, buffer, buffer.position(), 4);
    assertEquals((int) crc.getValue(), di.readInt());
    // read the hint and update/validate overall crc
    Hint hint = Hint.serializer.deserialize(di, MessagingService.current_version);
    updateChecksum(crc, buffer, buffer.position() + 8, hintSize);
    assertEquals((int) crc.getValue(), di.readInt());
    // further validate hint correctness
    int idx = (int) (hint.creationTime - baseTimestamp);
    assertEquals(hostId, load[idx]);
    Row row = hint.mutation.getPartitionUpdates().iterator().next().iterator().next();
    assertEquals(1, Iterables.size(row.cells()));
    ValueAccessors.assertDataEquals(bytes(idx), row.clustering().get(0));
    Cell<?> cell = row.cells().iterator().next();
    assertEquals(TimeUnit.MILLISECONDS.toMicros(baseTimestamp + idx), cell.timestamp());
    ValueAccessors.assertDataEquals(bytes(idx), cell.buffer());
    return idx;
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) CRC32(java.util.zip.CRC32) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Row(org.apache.cassandra.db.rows.Row)

Aggregations

DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)54 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)33 Test (org.junit.Test)23 IOException (java.io.IOException)14 ByteBuffer (java.nio.ByteBuffer)13 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)13 TableMetadata (org.apache.cassandra.schema.TableMetadata)6 Mutation (org.apache.cassandra.db.Mutation)5 UUID (java.util.UUID)4 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)4 DataOutputPlus (org.apache.cassandra.io.util.DataOutputPlus)4 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)4 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 ArrayList (java.util.ArrayList)3 DataOutputBufferFixed (org.apache.cassandra.io.util.DataOutputBufferFixed)3 ByteBuf (io.netty.buffer.ByteBuf)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ClusteringIndexSliceFilter (org.apache.cassandra.db.filter.ClusteringIndexSliceFilter)2