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);
}
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) {
}
}
}
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);
}
}
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);
}
}
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);
}
Aggregations