Search in sources :

Example 46 with DataInputBuffer

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

the class HintTest method testSerializer.

@Test
public void testSerializer() throws IOException {
    long now = FBUtilities.timestampMicros();
    Mutation mutation = createMutation("testSerializer", now);
    Hint hint = Hint.create(mutation, now / 1000);
    // serialize
    int serializedSize = (int) Hint.serializer.serializedSize(hint, MessagingService.current_version);
    DataOutputBuffer dob = new DataOutputBuffer();
    Hint.serializer.serialize(hint, dob, MessagingService.current_version);
    assertEquals(serializedSize, dob.getLength());
    // deserialize
    DataInputPlus di = new DataInputBuffer(dob.buffer(), true);
    Hint deserializedHint = Hint.serializer.deserialize(di, MessagingService.current_version);
    // compare before/after
    assertHintsEqual(hint, deserializedHint);
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Test(org.junit.Test)

Example 47 with DataInputBuffer

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

the class StreamingTombstoneHistogramBuilderTest method testNumericTypes.

@Test
public void testNumericTypes() throws Exception {
    StreamingTombstoneHistogramBuilder builder = new StreamingTombstoneHistogramBuilder(5, 0, 1);
    builder.update(2);
    builder.update(2);
    builder.update(2);
    // To check that value overflow is handled correctly
    builder.update(2, Integer.MAX_VALUE);
    TombstoneHistogram hist = builder.build();
    Map<Integer, Integer> asMap = asMap(hist);
    assertEquals(1, asMap.size());
    assertEquals(Integer.MAX_VALUE, asMap.get(2).intValue());
    // Make sure it's working with Serde
    DataOutputBuffer out = new DataOutputBuffer();
    TombstoneHistogram.serializer.serialize(hist, out);
    byte[] bytes = out.toByteArray();
    TombstoneHistogram deserialized = TombstoneHistogram.serializer.deserialize(new DataInputBuffer(bytes));
    asMap = asMap(deserialized);
    assertEquals(1, deserialized.size());
    assertEquals(Integer.MAX_VALUE, asMap.get(2).intValue());
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) Test(org.junit.Test)

Example 48 with DataInputBuffer

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

the class MerkleTreeTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Range<Token> full = new Range<>(tok(-1), tok(-1));
    // populate and validate the tree
    mt.maxsize(256);
    mt.init();
    for (TreeRange range : mt.rangeIterator()) range.addAll(new HIterator(range.right));
    byte[] initialhash = mt.hash(full);
    DataOutputBuffer out = new DataOutputBuffer();
    mt.serialize(out, MessagingService.current_version);
    byte[] serialized = out.toByteArray();
    MerkleTree restoredOnHeap = MerkleTree.deserialize(new DataInputBuffer(serialized), false, MessagingService.current_version);
    MerkleTree restoredOffHeap = MerkleTree.deserialize(new DataInputBuffer(serialized), true, MessagingService.current_version);
    MerkleTree movedOffHeap = mt.moveOffHeap();
    assertHashEquals(initialhash, restoredOnHeap.hash(full));
    assertHashEquals(initialhash, restoredOffHeap.hash(full));
    assertHashEquals(initialhash, movedOffHeap.hash(full));
    assertEquals(mt, restoredOnHeap);
    assertEquals(mt, restoredOffHeap);
    assertEquals(mt, movedOffHeap);
    assertEquals(restoredOnHeap, restoredOffHeap);
    assertEquals(restoredOnHeap, movedOffHeap);
    assertEquals(restoredOffHeap, movedOffHeap);
}
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) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) Test(org.junit.Test)

Example 49 with DataInputBuffer

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

the class JMXToolTest method serde.

private void serde(JMXTool.Dump.Format serializer, JMXTool.Diff.Format deserializer, DataOutputBuffer buffer, Map<String, JMXTool.Info> map) throws IOException {
    buffer.clear();
    serializer.dump(buffer, map);
    Map<String, JMXTool.Info> read = deserializer.load(new DataInputBuffer(buffer.buffer(), false));
    Assertions.assertThat(read).as("property deserialize(serialize(value)) == value failed").isEqualTo(map);
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer)

Example 50 with DataInputBuffer

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

the class SerializationUtils method cycleSerialization.

public static <T> T cycleSerialization(T src, IVersionedSerializer<T> serializer, int version) {
    int expectedSize = (int) serializer.serializedSize(src, version);
    try (DataOutputBuffer out = new DataOutputBuffer(expectedSize)) {
        serializer.serialize(src, out, version);
        Assert.assertEquals(expectedSize, out.buffer().limit());
        try (DataInputBuffer in = new DataInputBuffer(out.buffer(), false)) {
            return serializer.deserialize(in, version);
        }
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IOException(java.io.IOException)

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