Search in sources :

Example 1 with Index

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

the class StorageProxy method estimateResultsPerRange.

/**
     * Estimate the number of result rows per range in the ring based on our local data.
     * <p>
     * This assumes that ranges are uniformly distributed across the cluster and
     * that the queried data is also uniformly distributed.
     */
private static float estimateResultsPerRange(PartitionRangeReadCommand command, Keyspace keyspace) {
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(command.metadata().id);
    Index index = command.getIndex(cfs);
    float maxExpectedResults = index == null ? command.limits().estimateTotalResults(cfs) : index.getEstimatedResultRows();
    // adjust maxExpectedResults by the number of tokens this node has and the replication factor for this ks
    return (maxExpectedResults / DatabaseDescriptor.getNumTokens()) / keyspace.getReplicationStrategy().getReplicationFactor();
}
Also used : Index(org.apache.cassandra.index.Index)

Example 2 with Index

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

the class StatementRestrictions method processCustomIndexExpressions.

private void processCustomIndexExpressions(List<CustomIndexExpression> expressions, VariableSpecifications boundNames, SecondaryIndexManager indexManager) {
    if (expressions.size() > 1)
        throw new InvalidRequestException(IndexRestrictions.MULTIPLE_EXPRESSIONS);
    CustomIndexExpression expression = expressions.get(0);
    CFName cfName = expression.targetIndex.getCfName();
    if (cfName.hasKeyspace() && !expression.targetIndex.getKeyspace().equals(table.keyspace))
        throw IndexRestrictions.invalidIndex(expression.targetIndex, table);
    if (cfName.getColumnFamily() != null && !cfName.getColumnFamily().equals(table.name))
        throw IndexRestrictions.invalidIndex(expression.targetIndex, table);
    if (!table.indexes.has(expression.targetIndex.getIdx()))
        throw IndexRestrictions.indexNotFound(expression.targetIndex, table);
    Index index = indexManager.getIndex(table.indexes.get(expression.targetIndex.getIdx()).get());
    if (!index.getIndexMetadata().isCustom())
        throw IndexRestrictions.nonCustomIndexInExpression(expression.targetIndex);
    AbstractType<?> expressionType = index.customExpressionValueType();
    if (expressionType == null)
        throw IndexRestrictions.customExpressionNotSupported(expression.targetIndex);
    expression.prepareValue(table, expressionType, boundNames);
    filterRestrictions.add(expression);
}
Also used : InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) Index(org.apache.cassandra.index.Index)

Example 3 with Index

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

the class ReadCommand method getIndex.

public Index getIndex(ColumnFamilyStore cfs) {
    // the result should be cached here.
    if (index.isPresent())
        return cfs.indexManager.getIndex(index.get());
    // then no registered index is suitable for this command, so just return null.
    if (indexManagerQueried)
        return null;
    // do the lookup, set the flag to indicate so and cache the result if not null
    Index selected = cfs.indexManager.getBestIndexFor(this);
    indexManagerQueried = true;
    if (selected == null)
        return null;
    index = Optional.of(selected.getIndexMetadata());
    return selected;
}
Also used : Index(org.apache.cassandra.index.Index)

Example 4 with Index

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

the class ReadCommand method executeLocally.

/**
     * Executes this command on the local host.
     *
     * @param executionController the execution controller spanning this command
     *
     * @return an iterator over the result of executing this command locally.
     */
// The result iterator is closed upon exceptions (we know it's fine to potentially not close the intermediary
@SuppressWarnings("resource")
public // iterators created inside the try as long as we do close the original resultIterator), or by closing the result.
UnfilteredPartitionIterator executeLocally(ReadExecutionController executionController) {
    long startTimeNanos = System.nanoTime();
    ColumnFamilyStore cfs = Keyspace.openAndGetStore(metadata());
    Index index = getIndex(cfs);
    Index.Searcher searcher = null;
    if (index != null) {
        if (!cfs.indexManager.isIndexQueryable(index))
            throw new IndexNotAvailableException(index);
        searcher = index.searcherFor(this);
        Tracing.trace("Executing read on {}.{} using index {}", cfs.metadata.keyspace, cfs.metadata.name, index.getIndexMetadata().name);
    }
    UnfilteredPartitionIterator resultIterator = searcher == null ? queryStorage(cfs, executionController) : searcher.search(executionController);
    try {
        resultIterator = withStateTracking(resultIterator);
        resultIterator = withMetricsRecording(withoutPurgeableTombstones(resultIterator, cfs), cfs.metric, startTimeNanos);
        // If we've used a 2ndary index, we know the result already satisfy the primary expression used, so
        // no point in checking it again.
        RowFilter updatedFilter = searcher == null ? rowFilter() : index.getPostIndexQueryFilter(rowFilter());
        // processing we do on it).
        return limits().filter(updatedFilter.filter(resultIterator, nowInSec()), nowInSec());
    } catch (RuntimeException | Error e) {
        resultIterator.close();
        throw e;
    }
}
Also used : Index(org.apache.cassandra.index.Index) IndexNotAvailableException(org.apache.cassandra.index.IndexNotAvailableException)

Example 5 with Index

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

the class SSTableWriter method observers.

private static Collection<SSTableFlushObserver> observers(Descriptor descriptor, Collection<Index> indexes, OperationType operationType) {
    if (indexes == null)
        return Collections.emptyList();
    List<SSTableFlushObserver> observers = new ArrayList<>(indexes.size());
    for (Index index : indexes) {
        SSTableFlushObserver observer = index.getFlushObserver(descriptor, operationType);
        if (observer != null) {
            observer.begin();
            observers.add(observer);
        }
    }
    return ImmutableList.copyOf(observers);
}
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