Search in sources :

Example 6 with Index

use of org.apache.cassandra.index.Index in project cassandra by apache.

the class IndexMetadata method validate.

public void validate(TableMetadata table) {
    if (!isNameValid(name))
        throw new ConfigurationException("Illegal index name " + name);
    if (kind == null)
        throw new ConfigurationException("Index kind is null for index " + name);
    if (kind == Kind.CUSTOM) {
        if (options == null || !options.containsKey(IndexTarget.CUSTOM_INDEX_OPTION_NAME))
            throw new ConfigurationException(String.format("Required option missing for index %s : %s", name, IndexTarget.CUSTOM_INDEX_OPTION_NAME));
        String className = options.get(IndexTarget.CUSTOM_INDEX_OPTION_NAME);
        Class<Index> indexerClass = FBUtilities.classForName(className, "custom indexer");
        if (!Index.class.isAssignableFrom(indexerClass))
            throw new ConfigurationException(String.format("Specified Indexer class (%s) does not implement the Indexer interface", className));
        validateCustomIndexOptions(table, indexerClass, options);
    }
}
Also used : ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) Index(org.apache.cassandra.index.Index)

Example 7 with Index

use of org.apache.cassandra.index.Index in project cassandra by apache.

the class PartitionRangeQueryPager method nextPageReadCommand.

protected ReadCommand nextPageReadCommand(int pageSize) throws RequestExecutionException {
    DataLimits limits;
    DataRange fullRange = ((PartitionRangeReadCommand) command).dataRange();
    DataRange pageRange;
    if (lastReturnedKey == null) {
        pageRange = fullRange;
        limits = command.limits().forPaging(pageSize);
    } else {
        // We want to include the last returned key only if we haven't achieved our per-partition limit, otherwise, don't bother.
        boolean includeLastKey = remainingInPartition() > 0 && lastReturnedRow != null;
        AbstractBounds<PartitionPosition> bounds = makeKeyBounds(lastReturnedKey, includeLastKey);
        if (includeLastKey) {
            pageRange = fullRange.forPaging(bounds, command.metadata().comparator, lastReturnedRow.clustering(command.metadata()), false);
            limits = command.limits().forPaging(pageSize, lastReturnedKey.getKey(), remainingInPartition());
        } else {
            pageRange = fullRange.forSubRange(bounds);
            limits = command.limits().forPaging(pageSize);
        }
    }
    Index index = command.getIndex(Keyspace.openAndGetStore(command.metadata()));
    Optional<IndexMetadata> indexMetadata = index != null ? Optional.of(index.getIndexMetadata()) : Optional.empty();
    return new PartitionRangeReadCommand(command.metadata(), command.nowInSec(), command.columnFilter(), command.rowFilter(), limits, pageRange, indexMetadata);
}
Also used : Index(org.apache.cassandra.index.Index) IndexMetadata(org.apache.cassandra.schema.IndexMetadata) DataLimits(org.apache.cassandra.db.filter.DataLimits)

Example 8 with Index

use of org.apache.cassandra.index.Index in project cassandra by apache.

the class SecondaryIndexTest method testIndexCreate.

@Test
public void testIndexCreate() throws IOException, InterruptedException, ExecutionException {
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(COMPOSITE_INDEX_TO_BE_ADDED);
    // create a row and update the birthdate value, test that the index query fetches the new version
    new RowUpdateBuilder(cfs.metadata(), 0, "k1").clustering("c").add("birthdate", 1L).build().applyUnsafe();
    String indexName = "birthdate_index";
    ColumnMetadata old = cfs.metadata().getColumn(ByteBufferUtil.bytes("birthdate"));
    IndexMetadata indexDef = IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(old.name, IndexTarget.Type.VALUES)), indexName, IndexMetadata.Kind.COMPOSITES, Collections.EMPTY_MAP);
    TableMetadata current = cfs.metadata();
    TableMetadata updated = current.unbuild().indexes(current.indexes.with(indexDef)).build();
    MigrationManager.announceTableUpdate(updated, true);
    // fait for the index to be built
    Index index = cfs.indexManager.getIndex(indexDef);
    do {
        TimeUnit.MILLISECONDS.sleep(100);
    } while (!cfs.indexManager.isIndexQueryable(index));
    // we had a bug (CASSANDRA-2244) where index would get created but not flushed -- check for that
    // the way we find the index cfs is a bit convoluted at the moment
    ColumnFamilyStore indexCfs = cfs.indexManager.getIndex(indexDef).getBackingTable().orElseThrow(throwAssert("Index not found"));
    assertFalse(indexCfs.getLiveSSTables().isEmpty());
    assertIndexedOne(cfs, ByteBufferUtil.bytes("birthdate"), 1L);
    // validate that drop clears it out & rebuild works (CASSANDRA-2320)
    assertTrue(cfs.getBuiltIndexes().contains(indexName));
    cfs.indexManager.removeIndex(indexDef.name);
    assertFalse(cfs.getBuiltIndexes().contains(indexName));
    // rebuild & re-query
    Future future = cfs.indexManager.addIndex(indexDef);
    future.get();
    assertIndexedOne(cfs, ByteBufferUtil.bytes("birthdate"), 1L);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) IndexTarget(org.apache.cassandra.cql3.statements.IndexTarget) Future(java.util.concurrent.Future) Index(org.apache.cassandra.index.Index) IndexMetadata(org.apache.cassandra.schema.IndexMetadata) Test(org.junit.Test)

Example 9 with Index

use of org.apache.cassandra.index.Index in project cassandra by apache.

the class PartitionRangeReadCommand method postReconciliationProcessing.

/**
     * Allow to post-process the result of the query after it has been reconciled on the coordinator
     * but before it is passed to the CQL layer to return the ResultSet.
     *
     * See CASSANDRA-8717 for why this exists.
     */
public PartitionIterator postReconciliationProcessing(PartitionIterator result) {
    ColumnFamilyStore cfs = Keyspace.open(metadata().keyspace).getColumnFamilyStore(metadata().name);
    Index index = getIndex(cfs);
    return index == null ? result : index.postProcessorFor(this).apply(result, this);
}
Also used : Index(org.apache.cassandra.index.Index)

Example 10 with Index

use of org.apache.cassandra.index.Index in project cassandra by apache.

the class Keyspace method getValidColumnFamilies.

public Iterable<ColumnFamilyStore> getValidColumnFamilies(boolean allowIndexes, boolean autoAddIndexes, String... cfNames) throws IOException {
    Set<ColumnFamilyStore> valid = new HashSet<>();
    if (cfNames.length == 0) {
        // all stores are interesting
        for (ColumnFamilyStore cfStore : getColumnFamilyStores()) {
            valid.add(cfStore);
            if (autoAddIndexes)
                valid.addAll(getIndexColumnFamilyStores(cfStore));
        }
        return valid;
    }
    // include the specified stores and possibly the stores of any of their indexes
    for (String cfName : cfNames) {
        if (SecondaryIndexManager.isIndexColumnFamily(cfName)) {
            if (!allowIndexes) {
                logger.warn("Operation not allowed on secondary Index table ({})", cfName);
                continue;
            }
            String baseName = SecondaryIndexManager.getParentCfsName(cfName);
            String indexName = SecondaryIndexManager.getIndexName(cfName);
            ColumnFamilyStore baseCfs = getColumnFamilyStore(baseName);
            Index index = baseCfs.indexManager.getIndexByName(indexName);
            if (index == null)
                throw new IllegalArgumentException(String.format("Invalid index specified: %s/%s.", baseCfs.metadata.name, indexName));
            if (index.getBackingTable().isPresent())
                valid.add(index.getBackingTable().get());
        } else {
            ColumnFamilyStore cfStore = getColumnFamilyStore(cfName);
            valid.add(cfStore);
            if (autoAddIndexes)
                valid.addAll(getIndexColumnFamilyStores(cfStore));
        }
    }
    return valid;
}
Also used : Index(org.apache.cassandra.index.Index)

Aggregations

Index (org.apache.cassandra.index.Index)10 IndexMetadata (org.apache.cassandra.schema.IndexMetadata)2 Future (java.util.concurrent.Future)1 IndexTarget (org.apache.cassandra.cql3.statements.IndexTarget)1 DataLimits (org.apache.cassandra.db.filter.DataLimits)1 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)1 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)1 IndexNotAvailableException (org.apache.cassandra.index.IndexNotAvailableException)1 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)1 TableMetadata (org.apache.cassandra.schema.TableMetadata)1 Test (org.junit.Test)1