Search in sources :

Example 1 with CompressionInfo

use of org.apache.cassandra.db.streaming.CompressionInfo in project cassandra by apache.

the class CompressedInputStreamTest method testCompressedReadWith.

/**
 * @param valuesToCheck array of longs of range(0-999)
 * @throws Exception
 */
private void testCompressedReadWith(long[] valuesToCheck, boolean testTruncate, boolean testException, double minCompressRatio) throws Exception {
    assert valuesToCheck != null && valuesToCheck.length > 0;
    // write compressed data file of longs
    File parentDir = new File(tempFolder.newFolder());
    Descriptor desc = new Descriptor(parentDir, "ks", "cf", 1);
    File tmp = new File(desc.filenameFor(Component.DATA));
    MetadataCollector collector = new MetadataCollector(new ClusteringComparator(BytesType.instance));
    CompressionParams param = CompressionParams.snappy(32, minCompressRatio);
    Map<Long, Long> index = new HashMap<Long, Long>();
    try (CompressedSequentialWriter writer = new CompressedSequentialWriter(tmp, desc.filenameFor(Component.COMPRESSION_INFO), null, SequentialWriterOption.DEFAULT, param, collector)) {
        for (long l = 0L; l < 1000; l++) {
            index.put(l, writer.position());
            writer.writeLong(l);
        }
        writer.finish();
    }
    CompressionMetadata comp = CompressionMetadata.create(tmp.absolutePath());
    List<SSTableReader.PartitionPositionBounds> sections = new ArrayList<>();
    for (long l : valuesToCheck) {
        long position = index.get(l);
        sections.add(new SSTableReader.PartitionPositionBounds(position, position + 8));
    }
    CompressionMetadata.Chunk[] chunks = comp.getChunksForSections(sections);
    long totalSize = comp.getTotalSizeForSections(sections);
    long expectedSize = 0;
    for (CompressionMetadata.Chunk c : chunks) expectedSize += c.length + 4;
    assertEquals(expectedSize, totalSize);
    // buffer up only relevant parts of file
    int size = 0;
    for (CompressionMetadata.Chunk c : chunks) // 4bytes CRC
    size += (c.length + 4);
    byte[] toRead = new byte[size];
    try (RandomAccessReader f = RandomAccessReader.open(tmp)) {
        int pos = 0;
        for (CompressionMetadata.Chunk c : chunks) {
            f.seek(c.offset);
            pos += f.read(toRead, pos, c.length + 4);
        }
    }
    if (testTruncate) {
        byte[] actuallyRead = new byte[50];
        System.arraycopy(toRead, 0, actuallyRead, 0, 50);
        toRead = actuallyRead;
    }
    // read buffer using CompressedInputStream
    CompressionInfo info = CompressionInfo.newInstance(chunks, param);
    if (testException) {
        testException(sections, info);
        return;
    }
    CompressedInputStream input = new CompressedInputStream(new DataInputStreamPlus(new ByteArrayInputStream(toRead)), info, ChecksumType.CRC32, () -> 1.0);
    try (DataInputStream in = new DataInputStream(input)) {
        for (int i = 0; i < sections.size(); i++) {
            input.position(sections.get(i).lowerPosition);
            long readValue = in.readLong();
            assertEquals("expected " + valuesToCheck[i] + " but was " + readValue, valuesToCheck[i], readValue);
        }
    }
}
Also used : ClusteringComparator(org.apache.cassandra.db.ClusteringComparator) CompressionInfo(org.apache.cassandra.db.streaming.CompressionInfo) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) DataInputStreamPlus(org.apache.cassandra.io.util.DataInputPlus.DataInputStreamPlus) CompressedSequentialWriter(org.apache.cassandra.io.compress.CompressedSequentialWriter) CompressionMetadata(org.apache.cassandra.io.compress.CompressionMetadata) DataInputStream(java.io.DataInputStream) CompressionParams(org.apache.cassandra.schema.CompressionParams) CompressedInputStream(org.apache.cassandra.db.streaming.CompressedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Descriptor(org.apache.cassandra.io.sstable.Descriptor) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) MetadataCollector(org.apache.cassandra.io.sstable.metadata.MetadataCollector) File(org.apache.cassandra.io.util.File)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)1 ClusteringComparator (org.apache.cassandra.db.ClusteringComparator)1 CompressedInputStream (org.apache.cassandra.db.streaming.CompressedInputStream)1 CompressionInfo (org.apache.cassandra.db.streaming.CompressionInfo)1 CompressedSequentialWriter (org.apache.cassandra.io.compress.CompressedSequentialWriter)1 CompressionMetadata (org.apache.cassandra.io.compress.CompressionMetadata)1 Descriptor (org.apache.cassandra.io.sstable.Descriptor)1 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)1 MetadataCollector (org.apache.cassandra.io.sstable.metadata.MetadataCollector)1 DataInputStreamPlus (org.apache.cassandra.io.util.DataInputPlus.DataInputStreamPlus)1 File (org.apache.cassandra.io.util.File)1 RandomAccessReader (org.apache.cassandra.io.util.RandomAccessReader)1 CompressionParams (org.apache.cassandra.schema.CompressionParams)1