Search in sources :

Example 6 with ClusteringComparator

use of org.apache.cassandra.db.ClusteringComparator in project cassandra by apache.

the class CompressedSequentialWriterTest method resetAndTruncateTest.

@Test
@Override
public void resetAndTruncateTest() {
    File tempFile = new File(Files.createTempDir(), "reset.txt");
    File offsetsFile = FileUtils.createTempFile("compressedsequentialwriter.offset", "test");
    final int bufferSize = 48;
    final int writeSize = 64;
    byte[] toWrite = new byte[writeSize];
    try (SequentialWriter writer = new CompressedSequentialWriter(tempFile, offsetsFile.getPath(), null, SequentialWriterOption.DEFAULT, CompressionParams.lz4(bufferSize), new MetadataCollector(new ClusteringComparator(UTF8Type.instance)))) {
        // write bytes greather than buffer
        writer.write(toWrite);
        long flushedOffset = writer.getLastFlushOffset();
        assertEquals(writeSize, writer.position());
        // mark thi position
        DataPosition pos = writer.mark();
        // write another
        writer.write(toWrite);
        // another buffer should be flushed
        assertEquals(flushedOffset * 2, writer.getLastFlushOffset());
        assertEquals(writeSize * 2, writer.position());
        // reset writer
        writer.resetAndTruncate(pos);
        // current position and flushed size should be changed
        assertEquals(writeSize, writer.position());
        assertEquals(flushedOffset, writer.getLastFlushOffset());
        // write another byte less than buffer
        writer.write(new byte[] { 0 });
        assertEquals(writeSize + 1, writer.position());
        // flush off set should not be increase
        assertEquals(flushedOffset, writer.getLastFlushOffset());
        writer.finish();
    } catch (IOException e) {
        Assert.fail();
    }
}
Also used : ClusteringComparator(org.apache.cassandra.db.ClusteringComparator) IOException(java.io.IOException) MetadataCollector(org.apache.cassandra.io.sstable.metadata.MetadataCollector) File(java.io.File) Test(org.junit.Test)

Example 7 with ClusteringComparator

use of org.apache.cassandra.db.ClusteringComparator in project cassandra by apache.

the class GroupMakerTest method testIsNewGroupWithOneClusteringColumnsPrefix.

@Test
public void testIsNewGroupWithOneClusteringColumnsPrefix() {
    ClusteringComparator comparator = newComparator(false, false, false);
    GroupMaker groupMaker = GroupMaker.newInstance(comparator, 1);
    assertTrue(groupMaker.isNewGroup(partitionKey(1), clustering(1, 1, 1)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(1, 1, 2)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(1, 1, 3)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(1, 2, 1)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(1, 3, 1)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(1, 3, 2)));
    assertTrue(groupMaker.isNewGroup(partitionKey(1), clustering(2, 1, 1)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(2, 1, 2)));
    assertTrue(groupMaker.isNewGroup(partitionKey(2), clustering(2, 2, 1)));
}
Also used : ClusteringComparator(org.apache.cassandra.db.ClusteringComparator) Test(org.junit.Test)

Example 8 with ClusteringComparator

use of org.apache.cassandra.db.ClusteringComparator in project cassandra by apache.

the class GroupMakerTest method testIsNewGroupWithClusteringColumns.

@Test
public void testIsNewGroupWithClusteringColumns() {
    ClusteringComparator comparator = newComparator(false, false, false);
    GroupMaker groupMaker = GroupMaker.newInstance(comparator, 2);
    assertTrue(groupMaker.isNewGroup(partitionKey(1), clustering(1, 1, 1)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(1, 1, 2)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(1, 1, 3)));
    assertTrue(groupMaker.isNewGroup(partitionKey(1), clustering(1, 2, 1)));
    assertTrue(groupMaker.isNewGroup(partitionKey(1), clustering(1, 3, 1)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(1, 3, 2)));
    assertTrue(groupMaker.isNewGroup(partitionKey(1), clustering(2, 1, 1)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(2, 1, 2)));
    assertTrue(groupMaker.isNewGroup(partitionKey(2), clustering(2, 2, 1)));
}
Also used : ClusteringComparator(org.apache.cassandra.db.ClusteringComparator) Test(org.junit.Test)

Example 9 with ClusteringComparator

use of org.apache.cassandra.db.ClusteringComparator in project cassandra by apache.

the class GroupMakerTest method testIsNewGroupWithReversedClusteringColumns.

@Test
public void testIsNewGroupWithReversedClusteringColumns() {
    ClusteringComparator comparator = newComparator(true, true, true);
    GroupMaker groupMaker = GroupMaker.newInstance(comparator, 2);
    assertTrue(groupMaker.isNewGroup(partitionKey(1), clustering(1, 3, 2)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(1, 3, 1)));
    assertTrue(groupMaker.isNewGroup(partitionKey(1), clustering(1, 2, 1)));
    assertTrue(groupMaker.isNewGroup(partitionKey(1), clustering(1, 1, 3)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(1, 1, 2)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(1, 1, 1)));
    assertTrue(groupMaker.isNewGroup(partitionKey(1), clustering(2, 1, 2)));
    assertFalse(groupMaker.isNewGroup(partitionKey(1), clustering(2, 1, 1)));
    assertTrue(groupMaker.isNewGroup(partitionKey(2), clustering(2, 2, 1)));
}
Also used : ClusteringComparator(org.apache.cassandra.db.ClusteringComparator) Test(org.junit.Test)

Example 10 with ClusteringComparator

use of org.apache.cassandra.db.ClusteringComparator in project cassandra by apache.

the class CompressedRandomAccessReaderTest method testResetAndTruncate.

private static void testResetAndTruncate(File f, boolean compressed, boolean usemmap, int junkSize, double minCompressRatio) throws IOException {
    final String filename = f.getAbsolutePath();
    MetadataCollector sstableMetadataCollector = new MetadataCollector(new ClusteringComparator(BytesType.instance));
    try (SequentialWriter writer = compressed ? new CompressedSequentialWriter(f, filename + ".metadata", null, SequentialWriterOption.DEFAULT, CompressionParams.snappy(), sstableMetadataCollector) : new SequentialWriter(f)) {
        writer.write("The quick ".getBytes());
        DataPosition mark = writer.mark();
        writer.write("blue fox jumps over the lazy dog".getBytes());
        // write enough to be sure to change chunk
        for (int i = 0; i < junkSize; ++i) {
            writer.write((byte) 1);
        }
        writer.resetAndTruncate(mark);
        writer.write("brown fox jumps over the lazy dog".getBytes());
        writer.finish();
    }
    assert f.exists();
    CompressionMetadata compressionMetadata = compressed ? new CompressionMetadata(filename + ".metadata", f.length(), true) : null;
    try (FileHandle.Builder builder = new FileHandle.Builder(filename).mmapped(usemmap).withCompressionMetadata(compressionMetadata);
        FileHandle fh = builder.complete();
        RandomAccessReader reader = fh.createReader()) {
        String expected = "The quick brown fox jumps over the lazy dog";
        assertEquals(expected.length(), reader.length());
        byte[] b = new byte[expected.length()];
        reader.readFully(b);
        assert new String(b).equals(expected) : "Expecting '" + expected + "', got '" + new String(b) + '\'';
    } finally {
        if (f.exists())
            assertTrue(f.delete());
        File metadata = new File(filename + ".metadata");
        if (compressed && metadata.exists())
            metadata.delete();
    }
}
Also used : ClusteringComparator(org.apache.cassandra.db.ClusteringComparator) MetadataCollector(org.apache.cassandra.io.sstable.metadata.MetadataCollector) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

ClusteringComparator (org.apache.cassandra.db.ClusteringComparator)13 Test (org.junit.Test)10 MetadataCollector (org.apache.cassandra.io.sstable.metadata.MetadataCollector)7 File (java.io.File)6 RandomAccessFile (java.io.RandomAccessFile)3 ByteBuffer (java.nio.ByteBuffer)2 CompressedSequentialWriter (org.apache.cassandra.io.compress.CompressedSequentialWriter)2 CompressionMetadata (org.apache.cassandra.io.compress.CompressionMetadata)2 IOException (java.io.IOException)1 Random (java.util.Random)1 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)1 Descriptor (org.apache.cassandra.io.sstable.Descriptor)1 CompressionParams (org.apache.cassandra.schema.CompressionParams)1 CompressedInputStream (org.apache.cassandra.streaming.compress.CompressedInputStream)1 CompressionInfo (org.apache.cassandra.streaming.compress.CompressionInfo)1 Pair (org.apache.cassandra.utils.Pair)1