Search in sources :

Example 21 with DataInputPlus

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

the class BatchlogTest 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 22 with DataInputPlus

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

the class HintMessageTest method testSerializer.

@Test
public void testSerializer() throws IOException {
    UUID hostId = UUID.randomUUID();
    long now = FBUtilities.timestampMicros();
    TableMetadata table = Schema.instance.getTableMetadata(KEYSPACE, TABLE);
    Mutation mutation = new RowUpdateBuilder(table, now, bytes("key")).clustering("column").add("val", "val" + 1234).build();
    Hint hint = Hint.create(mutation, now / 1000);
    HintMessage message = new HintMessage(hostId, hint);
    // serialize
    int serializedSize = (int) HintMessage.serializer.serializedSize(message, MessagingService.current_version);
    HintMessage deserializedMessage;
    try (DataOutputBuffer dob = new DataOutputBuffer()) {
        HintMessage.serializer.serialize(message, dob, MessagingService.current_version);
        assertEquals(serializedSize, dob.getLength());
        // deserialize
        DataInputPlus di = new DataInputBuffer(dob.buffer(), true);
        deserializedMessage = HintMessage.serializer.deserialize(di, MessagingService.current_version);
    }
    // compare before/after
    assertEquals(hostId, deserializedMessage.hostId);
    assertNotNull(deserializedMessage.hint);
    assertHintsEqual(hint, deserializedMessage.hint);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) 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 23 with DataInputPlus

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

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

the class BigTableZeroCopyWriterTest method getSSTableComponentData.

private Pair<DataInputPlus, Long> getSSTableComponentData(SSTableReader sstable, Component component, Function<ByteBuffer, DataInputPlus> bufferMapper) {
    FileHandle componentFile = new FileHandle.Builder(sstable.descriptor.filenameFor(component)).bufferSize(1024).complete();
    ByteBuffer buffer = ByteBuffer.allocate((int) componentFile.channel.size());
    componentFile.channel.read(buffer, 0);
    buffer.flip();
    DataInputPlus inputPlus = bufferMapper.apply(buffer);
    return Pair.create(inputPlus, componentFile.channel.size());
}
Also used : FileHandle(org.apache.cassandra.io.util.FileHandle) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) ByteBuffer(java.nio.ByteBuffer)

Example 25 with DataInputPlus

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

the class BytesReadTrackerTest method internalTestReadLine.

public void internalTestReadLine(boolean inputStream) throws Exception {
    DataInputStream in = new DataInputStream(new ByteArrayInputStream("1".getBytes()));
    BytesReadTracker tracker = inputStream ? new TrackedInputStream(in) : new TrackedDataInputPlus(in);
    DataInputPlus reader = inputStream ? new DataInputPlus.DataInputStreamPlus((TrackedInputStream) tracker) : (DataInputPlus) tracker;
    try {
        String line = reader.readLine();
        if (inputStream)
            assertEquals(line, "1");
        else
            fail("Should have thrown UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
        if (inputStream)
            fail("Should have not thrown UnsupportedOperationException");
    } finally {
        in.close();
    }
}
Also used : BytesReadTracker(org.apache.cassandra.io.util.BytesReadTracker) ByteArrayInputStream(java.io.ByteArrayInputStream) TrackedInputStream(org.apache.cassandra.io.util.TrackedInputStream) TrackedDataInputPlus(org.apache.cassandra.io.util.TrackedDataInputPlus) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) DataInputStream(java.io.DataInputStream) TrackedDataInputPlus(org.apache.cassandra.io.util.TrackedDataInputPlus)

Aggregations

DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)28 Test (org.junit.Test)15 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)13 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)11 IOException (java.io.IOException)9 DataOutputPlus (org.apache.cassandra.io.util.DataOutputPlus)8 ByteBuffer (java.nio.ByteBuffer)6 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 UUID (java.util.UUID)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Mutation (org.apache.cassandra.db.Mutation)4 BytesReadTracker (org.apache.cassandra.io.util.BytesReadTracker)4 TrackedDataInputPlus (org.apache.cassandra.io.util.TrackedDataInputPlus)4 TrackedInputStream (org.apache.cassandra.io.util.TrackedInputStream)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 TableMetadata (org.apache.cassandra.schema.TableMetadata)3 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2