Example usage for org.apache.cassandra.io.compress CompressionMetadata chunkFor

List of usage examples for org.apache.cassandra.io.compress CompressionMetadata chunkFor

Introduction

In this page you can find the example usage for org.apache.cassandra.io.compress CompressionMetadata chunkFor.

Prototype

public Chunk chunkFor(long position) 

Source Link

Document

Get a chunk of compressed data (offset, length) corresponding to given position

Usage

From source file:com.fullcontact.cassandra.io.compress.CompressedRandomAccessReaderTest.java

License:Apache License

@Test
public void testDataCorruptionDetection() throws IOException {
    String CONTENT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae.";

    File file = new File("testDataCorruptionDetection");
    file.deleteOnExit();// w  w w.j  a v a 2  s .  c  om

    File metadata = new File(file.getPath() + ".meta");
    metadata.deleteOnExit();

    SSTableMetadata.Collector sstableMetadataCollector = SSTableMetadata.createCollector().replayPosition(null);
    SequentialWriter writer = new CompressedSequentialWriter(file, metadata.getPath(), false,
            new CompressionParameters(SnappyCompressor.instance), sstableMetadataCollector);

    writer.write(CONTENT.getBytes());
    writer.close();

    // open compression metadata and get chunk information
    CompressionMetadata meta = new CompressionMetadata(metadata.getPath(), file.length(), fs);
    CompressionMetadata.Chunk chunk = meta.chunkFor(0);

    RandomAccessReader reader = CompressedRandomAccessReader.open(new Path(file.getPath()), meta, false, fs);
    // read and verify compressed data
    assertEquals(CONTENT, reader.readLine());
    // close reader
    reader.close();

    Random random = new Random();
    RandomAccessFile checksumModifier = null;

    try {
        checksumModifier = new RandomAccessFile(file, "rw");
        byte[] checksum = new byte[4];

        // seek to the end of the compressed chunk
        checksumModifier.seek(chunk.length);
        // read checksum bytes
        checksumModifier.read(checksum);
        // seek back to the chunk end
        checksumModifier.seek(chunk.length);

        // lets modify one byte of the checksum on each iteration
        for (int i = 0; i < checksum.length; i++) {
            checksumModifier.write(random.nextInt());
            checksumModifier.getFD().sync(); // making sure that change was synced with disk

            final RandomAccessReader r = CompressedRandomAccessReader.open(new Path(file.getPath()), meta,
                    false, fs);

            Throwable exception = null;
            try {
                r.readLine();
            } catch (Throwable t) {
                exception = t;
            }
            assertNotNull(exception);
            assertEquals(exception.getClass(), CorruptSSTableException.class);
            assertEquals(exception.getCause().getClass(), CorruptBlockException.class);

            r.close();
        }

        // lets write original checksum and check if we can read data
        updateChecksum(checksumModifier, chunk.length, checksum);

        reader = CompressedRandomAccessReader.open(new Path(file.getPath()), meta, false, fs);
        // read and verify compressed data
        assertEquals(CONTENT, reader.readLine());
        // close reader
        reader.close();
    } finally {
        if (checksumModifier != null)
            checksumModifier.close();
    }
}