Search in sources :

Example 1 with SyncSegment

use of org.apache.cassandra.db.commitlog.CommitLogSegmentReader.SyncSegment in project cassandra by apache.

the class SegmentReaderTest method compressedSegmenter.

private void compressedSegmenter(ICompressor compressor) throws IOException {
    int rawSize = (1 << 15) - 137;
    ByteBuffer plainTextBuffer = compressor.preferredBufferType().allocate(rawSize);
    byte[] b = new byte[rawSize];
    random.nextBytes(b);
    plainTextBuffer.put(b);
    plainTextBuffer.flip();
    // need to add in the plain text size to the block we write out
    int uncompressedHeaderSize = 4;
    int length = compressor.initialCompressedBufferLength(rawSize);
    ByteBuffer compBuffer = ByteBufferUtil.ensureCapacity(null, length + uncompressedHeaderSize, true, compressor.preferredBufferType());
    compBuffer.putInt(rawSize);
    compressor.compress(plainTextBuffer, compBuffer);
    compBuffer.flip();
    File compressedFile = File.createTempFile("compressed-segment-", ".log");
    compressedFile.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(compressedFile);
    fos.getChannel().write(compBuffer);
    fos.close();
    try (RandomAccessReader reader = RandomAccessReader.open(compressedFile)) {
        CompressedSegmenter segmenter = new CompressedSegmenter(compressor, reader);
        int fileLength = (int) compressedFile.length();
        SyncSegment syncSegment = segmenter.nextSegment(0, fileLength);
        FileDataInput fileDataInput = syncSegment.input;
        ByteBuffer fileBuffer = readBytes(fileDataInput, rawSize);
        plainTextBuffer.flip();
        Assert.assertEquals(plainTextBuffer, fileBuffer);
        // CompressedSegmenter includes the Sync header length in the syncSegment.endPosition (value)
        Assert.assertEquals(rawSize, syncSegment.endPosition - CommitLogSegment.SYNC_MARKER_SIZE);
    }
}
Also used : FileDataInput(org.apache.cassandra.io.util.FileDataInput) RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) FileOutputStream(java.io.FileOutputStream) ByteBuffer(java.nio.ByteBuffer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) CompressedSegmenter(org.apache.cassandra.db.commitlog.CommitLogSegmentReader.CompressedSegmenter) SyncSegment(org.apache.cassandra.db.commitlog.CommitLogSegmentReader.SyncSegment)

Example 2 with SyncSegment

use of org.apache.cassandra.db.commitlog.CommitLogSegmentReader.SyncSegment in project cassandra by apache.

the class SegmentReaderTest method underlyingEncryptedSegmenterTest.

public void underlyingEncryptedSegmenterTest(BiFunction<FileDataInput, Integer, ByteBuffer> readFun) throws IOException {
    EncryptionContext context = EncryptionContextGenerator.createContext(true);
    CipherFactory cipherFactory = new CipherFactory(context.getTransparentDataEncryptionOptions());
    int plainTextLength = (1 << 13) - 137;
    ByteBuffer plainTextBuffer = ByteBuffer.allocate(plainTextLength);
    random.nextBytes(plainTextBuffer.array());
    ByteBuffer compressedBuffer = EncryptionUtils.compress(plainTextBuffer, null, true, context.getCompressor());
    Cipher cipher = cipherFactory.getEncryptor(context.getTransparentDataEncryptionOptions().cipher, context.getTransparentDataEncryptionOptions().key_alias);
    File encryptedFile = File.createTempFile("encrypted-segment-", ".log");
    encryptedFile.deleteOnExit();
    FileChannel channel = new RandomAccessFile(encryptedFile, "rw").getChannel();
    channel.write(ByteBufferUtil.bytes(plainTextLength));
    EncryptionUtils.encryptAndWrite(compressedBuffer, channel, true, cipher);
    channel.close();
    try (RandomAccessReader reader = RandomAccessReader.open(encryptedFile)) {
        context = EncryptionContextGenerator.createContext(cipher.getIV(), true);
        EncryptedSegmenter segmenter = new EncryptedSegmenter(reader, context);
        SyncSegment syncSegment = segmenter.nextSegment(0, (int) reader.length());
        // EncryptedSegmenter includes the Sync header length in the syncSegment.endPosition (value)
        Assert.assertEquals(plainTextLength, syncSegment.endPosition - CommitLogSegment.SYNC_MARKER_SIZE);
        ByteBuffer fileBuffer = readFun.apply(syncSegment.input, plainTextLength);
        plainTextBuffer.position(0);
        Assert.assertEquals(plainTextBuffer, fileBuffer);
    }
}
Also used : EncryptionContext(org.apache.cassandra.security.EncryptionContext) RandomAccessFile(java.io.RandomAccessFile) RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) FileChannel(java.nio.channels.FileChannel) EncryptedSegmenter(org.apache.cassandra.db.commitlog.CommitLogSegmentReader.EncryptedSegmenter) CipherFactory(org.apache.cassandra.security.CipherFactory) Cipher(javax.crypto.Cipher) ByteBuffer(java.nio.ByteBuffer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) SyncSegment(org.apache.cassandra.db.commitlog.CommitLogSegmentReader.SyncSegment)

Aggregations

File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 ByteBuffer (java.nio.ByteBuffer)2 SyncSegment (org.apache.cassandra.db.commitlog.CommitLogSegmentReader.SyncSegment)2 RandomAccessReader (org.apache.cassandra.io.util.RandomAccessReader)2 FileOutputStream (java.io.FileOutputStream)1 FileChannel (java.nio.channels.FileChannel)1 Cipher (javax.crypto.Cipher)1 CompressedSegmenter (org.apache.cassandra.db.commitlog.CommitLogSegmentReader.CompressedSegmenter)1 EncryptedSegmenter (org.apache.cassandra.db.commitlog.CommitLogSegmentReader.EncryptedSegmenter)1 FileDataInput (org.apache.cassandra.io.util.FileDataInput)1 CipherFactory (org.apache.cassandra.security.CipherFactory)1 EncryptionContext (org.apache.cassandra.security.EncryptionContext)1