/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.cassandra.db.index;

import java.nio.ByteBuffer;
import java.util.*;

import org.apache.cassandra.db.*;
import org.apache.cassandra.db.filter.ExtendedFilter;
import org.apache.cassandra.thrift.IndexExpression;
import org.apache.cassandra.thrift.IndexOperator;
import org.apache.cassandra.tracing.Tracing;
import org.apache.cassandra.utils.FBUtilities;

public abstract class SecondaryIndexSearcher
{
    protected final SecondaryIndexManager indexManager;
    protected final Set<ByteBuffer> columns;
    protected final ColumnFamilyStore baseCfs;

    public SecondaryIndexSearcher(SecondaryIndexManager indexManager, Set<ByteBuffer> columns)
    {
        this.indexManager = indexManager;
        this.columns = columns;
        this.baseCfs = indexManager.baseCfs;
    }

    public abstract List<Row> search(ExtendedFilter filter);

    /**
     * @return true this index is able to handle given clauses.
     */
    public boolean isIndexing(List<IndexExpression> clause)
    {
        return highestSelectivityPredicate(clause) != null;
    }

    protected IndexExpression highestSelectivityPredicate(List<IndexExpression> clause)
    {
        IndexExpression best = null;
        int bestMeanCount = Integer.MAX_VALUE;
        Map<SecondaryIndex, Integer> candidates = new HashMap<>();

        for (IndexExpression expression : clause)
        {
            // skip columns belonging to a different index type
            if (!columns.contains(expression.column_name))
                continue;

            SecondaryIndex index = indexManager.getIndexForColumn(expression.column_name);
            if (index == null || index.getIndexCfs() == null || expression.op != IndexOperator.EQ)
                continue;
            int columns = index.getIndexCfs().getMeanColumns();
            candidates.put(index, columns);
            if (columns < bestMeanCount)
            {
                best = expression;
                bestMeanCount = columns;
            }
        }

        if (best == null)
            Tracing.trace("No applicable indexes found");
        else
            Tracing.trace("Candidate index mean cardinalities are {}. Scanning with {}.",
                          FBUtilities.toString(candidates), indexManager.getIndexForColumn(best.column_name).getIndexName());

        return best;
    }
    
    public void validate(List<IndexExpression> clause) {
	}

	public boolean requiresFullScan(AbstractRangeCommand command) {
		return false;
	}

	public List<Row> combine(AbstractRangeCommand command, List<Row> rows) {
		if (command.countCQL3Rows())
			return rows;
		else
			return rows.size() > command.limit() ? rows.subList(0, command.limit()) : rows;
	}
}

@@ -3100,16 +3100,51 @@
 mn_name)
+%0A                                  
 .getInde
@@ -3185,160 +3185,1340 @@
   %7D%0A
+%0A
     
-%0A    public void validate(List%3CIndexExpression%3E clause) %7B%0A%09%7D%0A%0A%09public boolean requiresFullScan(AbstractRangeCommand command) %7B%0A%09%09return false;%0A%09%7D%0A%0A%09
+/**%0A     * Validates the specified %7B@link IndexExpression%7D clause. It will throw a %7B@link RuntimeException%7D if the provided%0A     * clause is not valid for the index implementation.%0A     * %0A     * @param clause%0A     *            An %7B@link IndexExpression%7D to be validated%0A     */%0A    public void validate(List%3CIndexExpression%3E clause)%0A    %7B%0A    %7D%0A%0A    /**%0A     * Returns %7B@code true%7D if the specified %7B@link AbstractRangeCommand%7D requires a full scan of all the nodes,%0A     * %7B@code false%7D otherwise.%0A     * %0A     * @param command%0A     *            A %7B@link AbstractRangeCommand%7D.%0A     * @return %7B@code true%7D if the %7B@code command%7D requires a full scan, %7B@code false%7D otherwise.%0A     */%0A    public boolean requiresFullScan(AbstractRangeCommand command)%0A    %7B%0A        return false;%0A    %7D%0A%0A    /**%0A     * Combines a list of cluster-wide collected %7B@link Row%7Ds to satisfy the specified %7B@link AbstractRangeCommand%7D.%0A     * This default implementation just truncates the provided list of rows according to the number of requested rows.%0A     * %0A     * @param command%0A     *            A %7B@link AbstractRangeCommand%7D.%0A     * @param rows%0A     *            A list of %7B@link Row%7Ds collected by local instances of this index.%0A     * @return The combination of the specified partial results according to the specified command.%0A     */%0A    
 publ
@@ -4587,13 +4587,23 @@
 ows)
- %7B%0A%09%09
+%0A    %7B%0A        
 if (
@@ -4627,19 +4627,28 @@
 Rows())%0A
-%09%09%09
+            
 return r
@@ -4656,18 +4656,33 @@
 ws;%0A
-%09%09else%0A%09%09%09
+        else%0A            
 retu
@@ -4761,9 +4761,12 @@
 ws;%0A
-%09
+    
 %7D%0A%7D%0A

improve JavaDoc.