Search in sources :

Example 41 with DataInputBuffer

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

the class PagingState method modernDeserialize.

@SuppressWarnings({ "resource", "RedundantSuppression" })
private static PagingState modernDeserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws IOException {
    if (protocolVersion.isSmallerThan(ProtocolVersion.V4))
        throw new IllegalArgumentException();
    DataInputBuffer in = new DataInputBuffer(bytes, false);
    ByteBuffer partitionKey = readWithVIntLength(in);
    ByteBuffer rawMark = readWithVIntLength(in);
    int remaining = Ints.checkedCast(in.readUnsignedVInt());
    int remainingInPartition = Ints.checkedCast(in.readUnsignedVInt());
    return new PagingState(partitionKey.hasRemaining() ? partitionKey : null, rawMark.hasRemaining() ? new RowMark(rawMark, protocolVersion) : null, remaining, remainingInPartition);
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 42 with DataInputBuffer

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

the class ColumnsTest method testDeserializeCorruption.

@Test
public void testDeserializeCorruption() throws IOException {
    ColumnsCheck check = randomSmall(1, 0, 3, 0);
    Columns superset = check.columns;
    List<ColumnMetadata> minus1 = new ArrayList<>(check.definitions);
    minus1.remove(3);
    Columns minus2 = check.columns.without(check.columns.getSimple(3)).without(check.columns.getSimple(2));
    try (DataOutputBuffer out = new DataOutputBuffer()) {
        // serialize a subset
        Columns.serializer.serializeSubset(minus1, superset, out);
        try (DataInputBuffer in = new DataInputBuffer(out.toByteArray())) {
            Columns.serializer.deserializeSubset(minus2, in);
            Assert.assertFalse(true);
        } catch (IOException e) {
        }
    }
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IOException(java.io.IOException) Test(org.junit.Test)

Example 43 with DataInputBuffer

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

Example 44 with DataInputBuffer

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

the class DescribeStatement method getOffset.

private long getOffset(PagingState pagingState, UUID schemaVersion) {
    if (pagingState == null)
        return 0L;
    try (DataInputBuffer in = new DataInputBuffer(pagingState.partitionKey, false)) {
        checkTrue(in.readShort() == PAGING_STATE_VERSION, "Incompatible paging state");
        final String pagingStateServerVersion = in.readUTF();
        final String releaseVersion = FBUtilities.getReleaseVersionString();
        checkTrue(pagingStateServerVersion.equals(releaseVersion), "The server version of the paging state %s is different from the one of the server %s", pagingStateServerVersion, releaseVersion);
        byte[] bytes = new byte[UUIDGen.UUID_LEN];
        in.read(bytes);
        UUID version = UUIDGen.getUUID(ByteBuffer.wrap(bytes));
        checkTrue(schemaVersion.equals(version), SCHEMA_CHANGED_WHILE_PAGING_MESSAGE);
        return in.readLong();
    } catch (IOException e) {
        throw new InvalidRequestException("Invalid paging state.", e);
    }
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) IOException(java.io.IOException)

Example 45 with DataInputBuffer

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

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