Search in sources :

Example 31 with ColumnMetadata

use of org.apache.cassandra.schema.ColumnMetadata in project cassandra by apache.

the class PartitionImplementationTest method makeRow.

Row makeRow(Clustering clustering, String colValue) {
    ColumnMetadata defCol = metadata.getColumn(new ColumnIdentifier("col", true));
    Row.Builder row = BTreeRow.unsortedBuilder(TIMESTAMP);
    row.newRow(clustering);
    row.addCell(BufferCell.live(defCol, TIMESTAMP, ByteBufferUtil.bytes(colValue)));
    return row.build();
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier)

Example 32 with ColumnMetadata

use of org.apache.cassandra.schema.ColumnMetadata in project cassandra by apache.

the class PartitionImplementationTest method makeStaticRow.

Row makeStaticRow() {
    ColumnMetadata defCol = metadata.getColumn(new ColumnIdentifier("static_col", true));
    Row.Builder row = BTreeRow.unsortedBuilder(TIMESTAMP);
    row.newRow(Clustering.STATIC_CLUSTERING);
    row.addCell(BufferCell.live(defCol, TIMESTAMP, ByteBufferUtil.bytes("static value")));
    return row.build();
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier)

Example 33 with ColumnMetadata

use of org.apache.cassandra.schema.ColumnMetadata in project cassandra by apache.

the class PartitionImplementationTest method testIter.

private void testIter(Supplier<Collection<? extends Unfiltered>> contentSupplier, Row staticRow) {
    NavigableSet<Clusterable> sortedContent = new TreeSet<Clusterable>(metadata.comparator);
    sortedContent.addAll(contentSupplier.get());
    AbstractBTreePartition partition;
    try (UnfilteredRowIterator iter = new Util.UnfilteredSource(metadata, Util.dk("pk"), staticRow, sortedContent.stream().map(x -> (Unfiltered) x).iterator())) {
        partition = ImmutableBTreePartition.create(iter);
    }
    ColumnMetadata defCol = metadata.getColumn(new ColumnIdentifier("col", true));
    ColumnFilter cf = ColumnFilter.selectionBuilder().add(defCol).build();
    Function<? super Clusterable, ? extends Clusterable> colFilter = x -> x instanceof Row ? ((Row) x).filter(cf, metadata) : x;
    Slices slices = Slices.with(metadata.comparator, Slice.make(clustering(KEY_RANGE / 4), clustering(KEY_RANGE * 3 / 4)));
    Slices multiSlices = makeSlices();
    // lastRow
    assertRowsEqual((Row) get(sortedContent.descendingSet(), x -> x instanceof Row), partition.lastRow());
    // get(static)
    assertRowsEqual(staticRow, partition.getRow(Clustering.STATIC_CLUSTERING));
    // get
    for (int i = 0; i < KEY_RANGE; ++i) {
        Clustering cl = clustering(i);
        assertRowsEqual(getRow(sortedContent, cl), partition.getRow(cl));
    }
    // isEmpty
    assertEquals(sortedContent.isEmpty() && staticRow == null, partition.isEmpty());
    // hasRows
    assertEquals(sortedContent.stream().anyMatch(x -> x instanceof Row), partition.hasRows());
    // iterator
    assertIteratorsEqual(sortedContent.stream().filter(x -> x instanceof Row).iterator(), partition.iterator());
    // unfiltered iterator
    assertIteratorsEqual(sortedContent.iterator(), partition.unfilteredIterator());
    // unfiltered iterator
    assertIteratorsEqual(sortedContent.iterator(), partition.unfilteredIterator(ColumnFilter.all(metadata), Slices.ALL, false));
    // column-filtered
    assertIteratorsEqual(sortedContent.stream().map(colFilter).iterator(), partition.unfilteredIterator(cf, Slices.ALL, false));
    // sliced
    assertIteratorsEqual(slice(sortedContent, slices.get(0)), partition.unfilteredIterator(ColumnFilter.all(metadata), slices, false));
    assertIteratorsEqual(streamOf(slice(sortedContent, slices.get(0))).map(colFilter).iterator(), partition.unfilteredIterator(cf, slices, false));
    // randomly multi-sliced
    assertIteratorsEqual(slice(sortedContent, multiSlices), partition.unfilteredIterator(ColumnFilter.all(metadata), multiSlices, false));
    assertIteratorsEqual(streamOf(slice(sortedContent, multiSlices)).map(colFilter).iterator(), partition.unfilteredIterator(cf, multiSlices, false));
    // reversed
    assertIteratorsEqual(sortedContent.descendingIterator(), partition.unfilteredIterator(ColumnFilter.all(metadata), Slices.ALL, true));
    assertIteratorsEqual(sortedContent.descendingSet().stream().map(colFilter).iterator(), partition.unfilteredIterator(cf, Slices.ALL, true));
    assertIteratorsEqual(invert(slice(sortedContent, slices.get(0))), partition.unfilteredIterator(ColumnFilter.all(metadata), slices, true));
    assertIteratorsEqual(streamOf(invert(slice(sortedContent, slices.get(0)))).map(colFilter).iterator(), partition.unfilteredIterator(cf, slices, true));
    assertIteratorsEqual(invert(slice(sortedContent, multiSlices)), partition.unfilteredIterator(ColumnFilter.all(metadata), multiSlices, true));
    assertIteratorsEqual(streamOf(invert(slice(sortedContent, multiSlices))).map(colFilter).iterator(), partition.unfilteredIterator(cf, multiSlices, true));
    // search iterator
    testSearchIterator(sortedContent, partition, ColumnFilter.all(metadata), false);
    testSearchIterator(sortedContent, partition, cf, false);
    testSearchIterator(sortedContent, partition, ColumnFilter.all(metadata), true);
    testSearchIterator(sortedContent, partition, cf, true);
    // sliceable iter
    testSlicingOfIterators(sortedContent, partition, ColumnFilter.all(metadata), false);
    testSlicingOfIterators(sortedContent, partition, cf, false);
    testSlicingOfIterators(sortedContent, partition, ColumnFilter.all(metadata), true);
    testSlicingOfIterators(sortedContent, partition, cf, true);
}
Also used : AbstractBTreePartition(org.apache.cassandra.db.partitions.AbstractBTreePartition) java.util(java.util) Iterables(com.google.common.collect.Iterables) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) BeforeClass(org.junit.BeforeClass) SearchIterator(org.apache.cassandra.utils.SearchIterator) org.apache.cassandra.db(org.apache.cassandra.db) Deletion(org.apache.cassandra.db.rows.Row.Deletion) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) org.apache.cassandra.db.rows(org.apache.cassandra.db.rows) Partition(org.apache.cassandra.db.partitions.Partition) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) ImmutableBTreePartition(org.apache.cassandra.db.partitions.ImmutableBTreePartition) StreamSupport(java.util.stream.StreamSupport) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) Predicate(java.util.function.Predicate) Util(org.apache.cassandra.Util) ByteBufferUtil(org.apache.cassandra.utils.ByteBufferUtil) KeyspaceParams(org.apache.cassandra.schema.KeyspaceParams) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SchemaLoader(org.apache.cassandra.SchemaLoader) Stream(java.util.stream.Stream) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) TableMetadata(org.apache.cassandra.schema.TableMetadata) Assert(org.junit.Assert) AsciiType(org.apache.cassandra.db.marshal.AsciiType) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) AbstractBTreePartition(org.apache.cassandra.db.partitions.AbstractBTreePartition) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier)

Example 34 with ColumnMetadata

use of org.apache.cassandra.schema.ColumnMetadata in project cassandra by apache.

the class HintTest method readPartition.

private static FilteredPartition readPartition(String key, String table, RegularAndStaticColumns columns) {
    String[] columnNames = new String[columns.size()];
    int i = 0;
    for (ColumnMetadata column : columns) columnNames[i++] = column.name.toString();
    return Util.getOnlyPartition(Util.cmd(cfs(table), key).columns(columnNames).build());
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata)

Example 35 with ColumnMetadata

use of org.apache.cassandra.schema.ColumnMetadata in project cassandra by apache.

the class PerSSTableIndexWriterTest method testSparse.

@Test
public void testSparse() throws Exception {
    final String columnName = "timestamp";
    ColumnFamilyStore cfs = Keyspace.open(KS_NAME).getColumnFamilyStore(CF_NAME);
    ColumnMetadata column = cfs.metadata().getColumn(UTF8Type.instance.decompose(columnName));
    SASIIndex sasi = (SASIIndex) cfs.indexManager.getIndexByName(cfs.name + "_" + columnName);
    File directory = cfs.getDirectories().getDirectoryForNewSSTables();
    Descriptor descriptor = cfs.newSSTableDescriptor(directory);
    PerSSTableIndexWriter indexWriter = (PerSSTableIndexWriter) sasi.getFlushObserver(descriptor, OperationType.FLUSH);
    final long now = System.currentTimeMillis();
    indexWriter.begin();
    indexWriter.indexes.put(column, indexWriter.newIndex(sasi.getIndex()));
    populateSegment(cfs.metadata(), indexWriter.getIndex(column), new HashMap<Long, Set<Integer>>() {

        {
            put(now, new HashSet<>(Arrays.asList(0, 1)));
            put(now + 1, new HashSet<>(Arrays.asList(2, 3)));
            put(now + 2, new HashSet<>(Arrays.asList(4, 5, 6, 7, 8, 9)));
        }
    });
    Callable<OnDiskIndex> segmentBuilder = indexWriter.getIndex(column).scheduleSegmentFlush(false);
    Assert.assertNull(segmentBuilder.call());
    PerSSTableIndexWriter.Index index = indexWriter.getIndex(column);
    Random random = ThreadLocalRandom.current();
    Set<String> segments = new HashSet<>();
    // now let's test multiple correct segments with yield incorrect final segment
    for (int i = 0; i < 3; i++) {
        populateSegment(cfs.metadata(), index, new HashMap<Long, Set<Integer>>() {

            {
                put(now, new HashSet<>(Arrays.asList(random.nextInt(), random.nextInt(), random.nextInt())));
                put(now + 1, new HashSet<>(Arrays.asList(random.nextInt(), random.nextInt(), random.nextInt())));
                put(now + 2, new HashSet<>(Arrays.asList(random.nextInt(), random.nextInt(), random.nextInt())));
            }
        });
        try {
            // flush each of the new segments, they should all succeed
            OnDiskIndex segment = index.scheduleSegmentFlush(false).call();
            index.segments.add(Futures.immediateFuture(segment));
            segments.add(segment.getIndexPath());
        } catch (Exception | FSError e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
    // make sure that all of the segments are present of the filesystem
    for (String segment : segments) Assert.assertTrue(new File(segment).exists());
    indexWriter.complete();
    // make sure that individual segments have been cleaned up
    for (String segment : segments) Assert.assertFalse(new File(segment).exists());
    // and combined index doesn't exist either
    Assert.assertFalse(new File(index.outputFile).exists());
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) FSError(org.apache.cassandra.io.FSError) SASIIndex(org.apache.cassandra.index.sasi.SASIIndex) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Descriptor(org.apache.cassandra.io.sstable.Descriptor) File(java.io.File) Test(org.junit.Test)

Aggregations

ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)123 Test (org.junit.Test)35 ByteBuffer (java.nio.ByteBuffer)23 Row (org.apache.cassandra.db.rows.Row)17 TableMetadata (org.apache.cassandra.schema.TableMetadata)16 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)15 AbstractType (org.apache.cassandra.db.marshal.AbstractType)8 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)5 java.util (java.util)4 IndexTarget (org.apache.cassandra.cql3.statements.IndexTarget)4 CollectionType (org.apache.cassandra.db.marshal.CollectionType)4 IndexMetadata (org.apache.cassandra.schema.IndexMetadata)4 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)3 ColumnFilter (org.apache.cassandra.db.filter.ColumnFilter)3 ImmutableBTreePartition (org.apache.cassandra.db.partitions.ImmutableBTreePartition)3 Cell (org.apache.cassandra.db.rows.Cell)3 RowIterator (org.apache.cassandra.db.rows.RowIterator)3 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)3