Java Code Examples for com.googlecode.cqengine.resultset.ResultSet#getRetrievalCost()

The following examples show how to use com.googlecode.cqengine.resultset.ResultSet#getRetrievalCost() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryCostComparators.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(ResultSet o1, ResultSet o2) {
    final int o1RetrievalCost = o1.getRetrievalCost();
    final int o2RetrievalCost = o2.getRetrievalCost();
    if (o1RetrievalCost < o2RetrievalCost) {
        return -1;
    }
    else if (o1RetrievalCost > o2RetrievalCost) {
        return +1;
    }
    else {
        return 0;
    }
}
 
Example 2
Source File: ResultSetUnionAll.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the sum of the retrieval costs of the the underlying {@code ResultSet}s.
 * @return the sum of the retrieval costs of the the underlying {@code ResultSet}s
 */
@Override
public int getRetrievalCost() {
    long retrievalCost = 0;
    for (ResultSet<O> resultSet : this.resultSets) {
        retrievalCost = retrievalCost + resultSet.getRetrievalCost();
    }
    return (int)Math.min(retrievalCost, Integer.MAX_VALUE);
}
 
Example 3
Source File: ResultSetIntersection.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the retrieval cost from the underlying {@code ResultSet} which has the lowest merge cost.
 * @return the retrieval cost from the underlying {@code ResultSet} which has the lowest merge cost
 */
@Override
public int getRetrievalCost() {
    if (resultSets.isEmpty()) {
        return 0;
    }
    else {
        ResultSet<O> lowestMergeCostResultSet = resultSets.get(0);
        return lowestMergeCostResultSet.getRetrievalCost();
    }
}
 
Example 4
Source File: ResultSetUnion.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the sum of the retrieval costs of the the underlying {@code ResultSet}s.
 * @return the sum of the retrieval costs of the the underlying {@code ResultSet}s
 */
@Override
public int getRetrievalCost() {
    long retrievalCost = 0;
    for (ResultSet<O> resultSet : this.resultSets) {
        retrievalCost = retrievalCost + resultSet.getRetrievalCost();
    }
    return (int)Math.min(retrievalCost, Integer.MAX_VALUE);
}
 
Example 5
Source File: CollectionQueryEngine.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link ResultSet} from the index with the lowest retrieval cost which supports the given query.
 * <p/>
 * For a definition of retrieval cost see {@link ResultSet#getRetrievalCost()}.
 *
 * @param query The query which refers to an attribute
 * @param queryOptions Optional parameters for the query
 * @return A {@link ResultSet} from the index with the lowest retrieval cost which supports the given query
 */
<A> ResultSet<O> retrieveComparativeQuery(ComparativeQuery<O, A> query, QueryOptions queryOptions) {
    // Determine which of the indexes on the query's attribute have the lowest retrieval cost...
    int lowestRetrievalCost = 0;
    ResultSet<O> lowestCostResultSet = null;

    Iterable<Index<O>> indexesOnAttribute = getIndexesOnAttribute(query.getAttribute());

    // Choose the index with the lowest retrieval cost for this query...
    for (Index<O> index : indexesOnAttribute) {
        if (index.supportsQuery(query, queryOptions)) {
            ResultSet<O> thisIndexResultSet = index.retrieve(query, queryOptions);
            int thisIndexRetrievalCost = thisIndexResultSet.getRetrievalCost();
            if (lowestCostResultSet == null || thisIndexRetrievalCost < lowestRetrievalCost) {
                lowestCostResultSet = thisIndexResultSet;
                lowestRetrievalCost = thisIndexRetrievalCost;
            }
        }
    }

    if (lowestCostResultSet == null) {
        // This should never happen (would indicate a bug);
        // the fallback index should have been selected in worst case...
        throw new IllegalStateException("Failed to locate an index supporting query: " + query);
    }
    return new CostCachingResultSet<O>(lowestCostResultSet);
}
 
Example 6
Source File: CollectionQueryEngine.java    From cqengine with Apache License 2.0 5 votes vote down vote up
static <O> boolean indexesAvailableForAllResultSets(Iterable<ResultSet<O>> resultSetsToMerge) {
    for (ResultSet<O> resultSet : resultSetsToMerge) {
        if (resultSet.getRetrievalCost() == Integer.MAX_VALUE) {
            return false;
        }
    }
    return true;
}
 
Example 7
Source File: CollectionQueryEngine.java    From cqengine with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a {@link ResultSet} from the index with the lowest retrieval cost which supports the given query.
 * <p/>
 * For a definition of retrieval cost see {@link ResultSet#getRetrievalCost()}.
 *
 * @param query The query which refers to an attribute
 * @param queryOptions Optional parameters for the query
 * @return A {@link ResultSet} from the index with the lowest retrieval cost which supports the given query
 */
<A> ResultSet<O> retrieveSimpleQuery(SimpleQuery<O, A> query, QueryOptions queryOptions) {
    // First, check if a standing query index is available for the query...
    ResultSet<O> lowestCostResultSet = retrieveFromStandingQueryIndexIfAvailable(query, queryOptions);
    if (lowestCostResultSet != null) {
        // A standing query index is available for this query.
        // Results from standing query indexes are considered to have the lowest cost, so we simply return these results...
        return lowestCostResultSet;
    }
    // At this point, no standing query indexes were available,
    // so we proceed to check for other indexes on the attribute...

    // Check if a UniqueIndex is available, as this will have the lowest cost of attribute-based indexes...
    Index<O> uniqueIndex = uniqueIndexes.get(query.getAttribute());
    if (uniqueIndex!= null && uniqueIndex.supportsQuery(query, queryOptions)){
        return uniqueIndex.retrieve(query, queryOptions);
    }

    // At this point, we did not find any UniqueIndex, so we now check for other attribute-based indexes
    // and we determine which one has the lowest retrieval cost...

    int lowestRetrievalCost = 0;
    // Examine other (non-unique) indexes...
    Iterable<Index<O>> indexesOnAttribute = getIndexesOnAttribute(query.getAttribute());

    // Choose the index with the lowest retrieval cost for this query...
    for (Index<O> index : indexesOnAttribute) {
        if (index.supportsQuery(query, queryOptions)) {
            ResultSet<O> thisIndexResultSet = index.retrieve(query, queryOptions);
            int thisIndexRetrievalCost = thisIndexResultSet.getRetrievalCost();
            if (lowestCostResultSet == null || thisIndexRetrievalCost < lowestRetrievalCost) {
                lowestCostResultSet = thisIndexResultSet;
                lowestRetrievalCost = thisIndexRetrievalCost;
            }
        }
    }

    if (lowestCostResultSet == null) {
        // This should never happen (would indicate a bug);
        // the fallback index should have been selected in worst case...
        throw new IllegalStateException("Failed to locate an index supporting query: " + query);
    }
    return new CostCachingResultSet<O>(lowestCostResultSet);
}