use of org.apache.cassandra.io.util.DataOutputBuffer 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;
}
use of org.apache.cassandra.io.util.DataOutputBuffer in project cassandra by apache.
the class MerkleTreeTest method testSerialization.
@Test
public void testSerialization() throws Exception {
Range<Token> full = new Range<>(tok(-1), tok(-1));
// populate and validate the tree
mt.maxsize(256);
mt.init();
for (TreeRange range : mt.invalids()) range.addAll(new HIterator(range.right));
byte[] initialhash = mt.hash(full);
DataOutputBuffer out = new DataOutputBuffer();
MerkleTree.serializer.serialize(mt, out, MessagingService.current_version);
byte[] serialized = out.toByteArray();
DataInputPlus in = new DataInputBuffer(serialized);
MerkleTree restored = MerkleTree.serializer.deserialize(in, MessagingService.current_version);
assertHashEquals(initialhash, restored.hash(full));
}
use of org.apache.cassandra.io.util.DataOutputBuffer in project cassandra by apache.
the class StreamingHistogramTest method testNumericTypes.
@Test
public void testNumericTypes() throws Exception {
StreamingHistogram hist = new StreamingHistogram(5, 0, 1);
hist.update(2);
hist.update(2.0);
hist.update(2L);
Map<Number, long[]> asMap = hist.getAsMap();
assertEquals(1, asMap.size());
assertEquals(3L, asMap.get(2)[0]);
//Make sure it's working with Serde
DataOutputBuffer out = new DataOutputBuffer();
StreamingHistogram.serializer.serialize(hist, out);
byte[] bytes = out.toByteArray();
StreamingHistogram deserialized = StreamingHistogram.serializer.deserialize(new DataInputBuffer(bytes));
deserialized.update(2L);
asMap = deserialized.getAsMap();
assertEquals(1, asMap.size());
assertEquals(4L, asMap.get(2)[0]);
}
use of org.apache.cassandra.io.util.DataOutputBuffer in project cassandra by apache.
the class BatchlogManager method store.
public static void store(Batch batch, boolean durableWrites) {
List<ByteBuffer> mutations = new ArrayList<>(batch.encodedMutations.size() + batch.decodedMutations.size());
mutations.addAll(batch.encodedMutations);
for (Mutation mutation : batch.decodedMutations) {
try (DataOutputBuffer buffer = new DataOutputBuffer()) {
Mutation.serializer.serialize(mutation, buffer, MessagingService.current_version);
mutations.add(buffer.buffer());
} catch (IOException e) {
// shouldn't happen
throw new AssertionError(e);
}
}
PartitionUpdate.SimpleBuilder builder = PartitionUpdate.simpleBuilder(SystemKeyspace.Batches, batch.id);
builder.row().timestamp(batch.creationTime).add("version", MessagingService.current_version).appendAll("mutations", mutations);
builder.buildAsMutation().apply(durableWrites);
}
use of org.apache.cassandra.io.util.DataOutputBuffer 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());
}
}
Aggregations