use of org.apache.cassandra.io.util.DataOutputBufferFixed in project cassandra by apache.
the class DurationSerializer method serialize.
public ByteBuffer serialize(Duration duration) {
if (duration == null)
return ByteBufferUtil.EMPTY_BYTE_BUFFER;
long months = duration.getMonths();
long days = duration.getDays();
long nanoseconds = duration.getNanoseconds();
int size = VIntCoding.computeVIntSize(months) + VIntCoding.computeVIntSize(days) + VIntCoding.computeVIntSize(nanoseconds);
try (DataOutputBufferFixed output = new DataOutputBufferFixed(size)) {
output.writeVInt(months);
output.writeVInt(days);
output.writeVInt(nanoseconds);
return output.buffer();
} catch (IOException e) {
// this should never happen with a DataOutputBufferFixed
throw new AssertionError("Unexpected error", e);
}
}
use of org.apache.cassandra.io.util.DataOutputBufferFixed in project cassandra by apache.
the class RepairMessageSerializationsTest method serializeRoundTrip.
private <T extends RepairMessage> T serializeRoundTrip(T msg, IVersionedSerializer<T> serializer) throws IOException {
long size = serializer.serializedSize(msg, PROTOCOL_VERSION);
ByteBuffer buf = ByteBuffer.allocate((int) size);
DataOutputPlus out = new DataOutputBufferFixed(buf);
serializer.serialize(msg, out, PROTOCOL_VERSION);
Assert.assertEquals(size, buf.position());
buf.flip();
DataInputPlus in = new DataInputBuffer(buf, false);
T deserialized = serializer.deserialize(in, PROTOCOL_VERSION);
Assert.assertEquals(msg, deserialized);
Assert.assertEquals(msg.hashCode(), deserialized.hashCode());
return deserialized;
}
use of org.apache.cassandra.io.util.DataOutputBufferFixed in project cassandra by apache.
the class CommitLog method add.
/**
* Add a Mutation to the commit log. If CDC is enabled, this can fail.
*
* @param mutation the Mutation to add to the log
* @throws WriteTimeoutException
*/
public CommitLogPosition add(Mutation mutation) throws WriteTimeoutException {
assert mutation != null;
try (DataOutputBuffer dob = DataOutputBuffer.scratchBuffer.get()) {
Mutation.serializer.serialize(mutation, dob, MessagingService.current_version);
int size = dob.getLength();
int totalSize = size + ENTRY_OVERHEAD_SIZE;
if (totalSize > MAX_MUTATION_SIZE) {
throw new IllegalArgumentException(String.format("Mutation of %s is too large for the maximum size of %s", FBUtilities.prettyPrintMemory(totalSize), FBUtilities.prettyPrintMemory(MAX_MUTATION_SIZE)));
}
Allocation alloc = segmentManager.allocate(mutation, totalSize);
CRC32 checksum = new CRC32();
final ByteBuffer buffer = alloc.getBuffer();
try (BufferedDataOutputStreamPlus dos = new DataOutputBufferFixed(buffer)) {
// checksummed length
dos.writeInt(size);
updateChecksumInt(checksum, size);
buffer.putInt((int) checksum.getValue());
// checksummed mutation
dos.write(dob.getData(), 0, size);
updateChecksum(checksum, buffer, buffer.position() - size, size);
buffer.putInt((int) checksum.getValue());
} catch (IOException e) {
throw new FSWriteError(e, alloc.getSegment().getPath());
} finally {
alloc.markWritten();
}
executor.finishWriteFor(alloc);
return alloc.getCommitLogPosition();
} catch (IOException e) {
throw new FSWriteError(e, segmentManager.allocatingFrom().getPath());
}
}
use of org.apache.cassandra.io.util.DataOutputBufferFixed in project cassandra by apache.
the class PagingState method serialize.
public ByteBuffer serialize(ProtocolVersion protocolVersion) {
assert rowMark == null || protocolVersion == rowMark.protocolVersion;
try (DataOutputBuffer out = new DataOutputBufferFixed(serializedSize(protocolVersion))) {
ByteBuffer pk = partitionKey == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : partitionKey;
ByteBuffer mark = rowMark == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : rowMark.mark;
if (protocolVersion.isSmallerOrEqualTo(ProtocolVersion.V3)) {
ByteBufferUtil.writeWithShortLength(pk, out);
ByteBufferUtil.writeWithShortLength(mark, out);
out.writeInt(remaining);
out.writeInt(remainingInPartition);
} else {
ByteBufferUtil.writeWithVIntLength(pk, out);
ByteBufferUtil.writeWithVIntLength(mark, out);
out.writeUnsignedVInt(remaining);
out.writeUnsignedVInt(remainingInPartition);
}
return out.buffer();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.apache.cassandra.io.util.DataOutputBufferFixed in project cassandra by apache.
the class StreamInitMessage method createMessage.
/**
* Create serialized message.
*
* @param compress true if message is compressed
* @param version Streaming protocol version
* @return serialized message in ByteBuffer format
*/
public ByteBuffer createMessage(boolean compress, int version) {
int header = 0;
// set compression bit.
if (compress)
header |= 4;
// set streaming bit
header |= 8;
// Setting up the version bit
header |= (version << 8);
byte[] bytes;
try {
int size = (int) StreamInitMessage.serializer.serializedSize(this, version);
try (DataOutputBuffer buffer = new DataOutputBufferFixed(size)) {
StreamInitMessage.serializer.serialize(this, buffer, version);
bytes = buffer.getData();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
assert bytes.length > 0;
ByteBuffer buffer = ByteBuffer.allocate(4 + 4 + bytes.length);
buffer.putInt(MessagingService.PROTOCOL_MAGIC);
buffer.putInt(header);
buffer.put(bytes);
buffer.flip();
return buffer;
}
Aggregations