Search in sources :

Example 46 with DataOutputBuffer

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

the class RepairMessageSerializerTest method serdes.

static RepairMessage serdes(RepairMessage message) {
    int expectedSize = (int) RepairMessage.serializer.serializedSize(message, MS_VERSION);
    try (DataOutputBuffer out = new DataOutputBuffer(expectedSize)) {
        RepairMessage.serializer.serialize(message, out, MS_VERSION);
        Assert.assertEquals(expectedSize, out.buffer().limit());
        try (DataInputBuffer in = new DataInputBuffer(out.buffer(), false)) {
            return RepairMessage.serializer.deserialize(in, MS_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)

Example 47 with DataOutputBuffer

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

the class BitSetTest method populateAndReserialize.

private static void populateAndReserialize(IBitSet bs) throws IOException {
    for (long i = 0; i < bs.capacity(); i++) if (random.nextBoolean())
        bs.set(i);
    DataOutputBuffer out = new DataOutputBuffer();
    bs.serialize(out);
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(out.getData()));
    try (OffHeapBitSet newbs = OffHeapBitSet.deserialize(in)) {
        compare(bs, newbs);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) OffHeapBitSet(org.apache.cassandra.utils.obs.OffHeapBitSet) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputStream(java.io.DataInputStream)

Example 48 with DataOutputBuffer

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

the class LocalSessions method serializeRange.

private static ByteBuffer serializeRange(Range<Token> range) {
    int size = (int) Token.serializer.serializedSize(range.left, 0);
    size += (int) Token.serializer.serializedSize(range.right, 0);
    try (DataOutputBuffer buffer = new DataOutputBuffer(size)) {
        Token.serializer.serialize(range.left, buffer, 0);
        Token.serializer.serialize(range.right, buffer, 0);
        return buffer.buffer();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IOException(java.io.IOException)

Example 49 with DataOutputBuffer

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

the class BatchTest method testSerialization.

@Test
public void testSerialization() throws IOException {
    TableMetadata cfm = Keyspace.open(KEYSPACE).getColumnFamilyStore(CF_STANDARD).metadata();
    long now = FBUtilities.timestampMicros();
    int version = MessagingService.current_version;
    UUID uuid = UUIDGen.getTimeUUID();
    List<Mutation> mutations = new ArrayList<>(10);
    for (int i = 0; i < 10; i++) {
        mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), bytes(i)).clustering("name" + i).add("val", "val" + i).build());
    }
    Batch batch1 = Batch.createLocal(uuid, now, mutations);
    assertEquals(uuid, batch1.id);
    assertEquals(now, batch1.creationTime);
    assertEquals(mutations, batch1.decodedMutations);
    DataOutputBuffer out = new DataOutputBuffer();
    Batch.serializer.serialize(batch1, out, version);
    assertEquals(out.getLength(), Batch.serializer.serializedSize(batch1, version));
    DataInputPlus dis = new DataInputBuffer(out.getData());
    Batch batch2 = Batch.serializer.deserialize(dis, version);
    assertEquals(batch1.id, batch2.id);
    assertEquals(batch1.creationTime, batch2.creationTime);
    assertEquals(batch1.decodedMutations.size(), batch2.encodedMutations.size());
    Iterator<Mutation> it1 = batch1.decodedMutations.iterator();
    Iterator<ByteBuffer> it2 = batch2.encodedMutations.iterator();
    while (it1.hasNext()) {
        try (DataInputBuffer in = new DataInputBuffer(it2.next().array())) {
            assertEquals(it1.next().toString(), Mutation.serializer.deserialize(in, version).toString());
        }
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Mutation(org.apache.cassandra.db.Mutation) UUID(java.util.UUID) Test(org.junit.Test)

Example 50 with DataOutputBuffer

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

the class ColumnsTest method testSerializeSubset.

private void testSerializeSubset(Columns superset, Columns subset, List<ColumnMetadata> subsetDefinitions) throws IOException {
    try (DataOutputBuffer out = new DataOutputBuffer()) {
        Columns.serializer.serializeSubset(subset, superset, out);
        Assert.assertEquals(Columns.serializer.serializedSubsetSize(subset, superset), out.buffer().remaining());
        Columns deserialized = Columns.serializer.deserializeSubset(superset, new DataInputBuffer(out.buffer(), false));
        Assert.assertEquals(subset, deserialized);
        Assert.assertEquals(subset.hashCode(), deserialized.hashCode());
        assertContents(deserialized, subsetDefinitions);
    }
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer)

Aggregations

DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)68 Test (org.junit.Test)25 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)17 IOException (java.io.IOException)16 ByteBuffer (java.nio.ByteBuffer)15 ByteArrayInputStream (java.io.ByteArrayInputStream)10 DataInputStream (java.io.DataInputStream)10 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)8 File (java.io.File)6 CRC32 (java.util.zip.CRC32)5 InetAddress (java.net.InetAddress)3 ArrayList (java.util.ArrayList)3 DataOutputBufferFixed (org.apache.cassandra.io.util.DataOutputBufferFixed)3 SequentialWriter (org.apache.cassandra.io.util.SequentialWriter)3 Message (org.apache.cassandra.net.Message)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 Map (java.util.Map)2 UUID (java.util.UUID)2 Mutation (org.apache.cassandra.db.Mutation)2