Search in sources :

Example 1 with ICompressor

use of org.apache.cassandra.io.compress.ICompressor in project cassandra by apache.

the class EncryptedSegment method write.

void write(int startMarker, int nextMarker) {
    int contentStart = startMarker + SYNC_MARKER_SIZE;
    final int length = nextMarker - contentStart;
    // The length may be 0 when the segment is being closed.
    assert length > 0 || length == 0 && !isStillAllocating();
    final ICompressor compressor = encryptionContext.getCompressor();
    final int blockSize = encryptionContext.getChunkLength();
    try {
        ByteBuffer inputBuffer = buffer.duplicate();
        inputBuffer.limit(contentStart + length).position(contentStart);
        ByteBuffer buffer = manager.getBufferPool().getThreadLocalReusableBuffer(DatabaseDescriptor.getCommitLogSegmentSize());
        // save space for the sync marker at the beginning of this section
        final long syncMarkerPosition = lastWrittenPos;
        channel.position(syncMarkerPosition + ENCRYPTED_SECTION_HEADER_SIZE);
        // loop over the segment data in encryption buffer sized chunks
        while (contentStart < nextMarker) {
            int nextBlockSize = nextMarker - blockSize > contentStart ? blockSize : nextMarker - contentStart;
            ByteBuffer slice = inputBuffer.duplicate();
            slice.limit(contentStart + nextBlockSize).position(contentStart);
            buffer = EncryptionUtils.compress(slice, buffer, true, compressor);
            // reuse the same buffer for the input and output of the encryption operation
            buffer = EncryptionUtils.encryptAndWrite(buffer, channel, true, cipher);
            contentStart += nextBlockSize;
            manager.addSize(buffer.limit() + ENCRYPTED_BLOCK_HEADER_SIZE);
        }
        lastWrittenPos = channel.position();
        // rewind to the beginning of the section and write out the sync marker
        buffer.position(0).limit(ENCRYPTED_SECTION_HEADER_SIZE);
        writeSyncMarker(buffer, 0, (int) syncMarkerPosition, (int) lastWrittenPos);
        buffer.putInt(SYNC_MARKER_SIZE, length);
        buffer.rewind();
        manager.addSize(buffer.limit());
        channel.position(syncMarkerPosition);
        channel.write(buffer);
        SyncUtil.force(channel, true);
    } catch (Exception e) {
        throw new FSWriteError(e, getPath());
    }
}
Also used : FSWriteError(org.apache.cassandra.io.FSWriteError) ICompressor(org.apache.cassandra.io.compress.ICompressor) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 FSWriteError (org.apache.cassandra.io.FSWriteError)1 ICompressor (org.apache.cassandra.io.compress.ICompressor)1