use of org.apache.cassandra.db.commitlog.CommitLogSegment.Allocation 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 CDCWriteException
*/
public CommitLogPosition add(Mutation mutation) throws CDCWriteException {
assert mutation != null;
mutation.validateSize(MessagingService.current_version, ENTRY_OVERHEAD_SIZE);
try (DataOutputBuffer dob = DataOutputBuffer.scratchBuffer.get()) {
Mutation.serializer.serialize(mutation, dob, MessagingService.current_version);
int size = dob.getLength();
int totalSize = size + ENTRY_OVERHEAD_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