Search in sources :

Example 1 with JointIndexQuery

use of org.janusgraph.graphdb.query.graph.JointIndexQuery in project janusgraph by JanusGraph.

the class ApproximateIndexSelectionStrategy method selectIndices.

/**
 * Iterate over all potential indexes and compute a score based on how many clauses
 * this index covers. The index with the highest score (as long as it covers at least one
 * additional clause) is picked and added to the joint query for as long as such exist.
 */
@Override
public SelectedIndexQuery selectIndices(final Set<IndexType> rawCandidates, final MultiCondition<JanusGraphElement> conditions, final Set<Condition> coveredClauses, OrderList orders, IndexSerializer serializer) {
    final JointIndexQuery jointQuery = new JointIndexQuery();
    boolean isSorted = orders.isEmpty();
    while (true) {
        IndexCandidate bestCandidate = null;
        boolean candidateSupportsSort = false;
        for (final IndexType index : rawCandidates) {
            final IndexCandidate indexCandidate = createIndexCandidate(index, conditions, serializer);
            if (indexCandidate == null) {
                continue;
            }
            boolean supportsSort = orders.isEmpty() || coveredClauses.isEmpty() && index.isMixedIndex() && IndexSelectionUtil.indexCoversOrder((MixedIndexType) index, orders);
            indexCandidate.setScore(calculateIndexCandidateScore(indexCandidate, coveredClauses, supportsSort));
            if (!coveredClauses.containsAll(indexCandidate.getSubCover()) && (bestCandidate == null || indexCandidate.getScore() > bestCandidate.getScore())) {
                bestCandidate = indexCandidate;
                candidateSupportsSort = supportsSort;
            }
        }
        if (bestCandidate != null) {
            if (coveredClauses.isEmpty()) {
                isSorted = candidateSupportsSort;
            }
            coveredClauses.addAll(bestCandidate.getSubCover());
            addToJointQuery(bestCandidate, jointQuery, serializer, orders);
        } else {
            break;
        }
    }
    return new SelectedIndexQuery(jointQuery, isSorted);
}
Also used : JointIndexQuery(org.janusgraph.graphdb.query.graph.JointIndexQuery) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) IndexType(org.janusgraph.graphdb.types.IndexType)

Example 2 with JointIndexQuery

use of org.janusgraph.graphdb.query.graph.JointIndexQuery in project janusgraph by JanusGraph.

the class BruteForceIndexSelectionStrategy method selectIndices.

/**
 * Determine the best jointIndexQuery by enumerating all possibilities with exponential time
 * complexity. Similar to Weighted Set Cover problem, to find the best choice is NP-Complete, so
 * we should be careful that the problem size MUST be small, otherwise it is more recommended to
 * use an approximation algorithm.
 */
@Override
public SelectedIndexQuery selectIndices(final Set<IndexType> rawCandidates, final MultiCondition<JanusGraphElement> conditions, final Set<Condition> coveredClauses, OrderList orders, IndexSerializer serializer) {
    final JointIndexQuery jointQuery = new JointIndexQuery();
    final Set<IndexCandidate> indexCandidates = new HashSet<>();
    boolean isSorted = orders.isEmpty();
    // validate, enrich index candidates and calculate scores
    for (final IndexType index : rawCandidates) {
        IndexCandidate ic = createIndexCandidate(index, conditions, serializer);
        if (ic == null) {
            continue;
        }
        ic.setScore(calculateIndexCandidateScore(ic));
        indexCandidates.add(ic);
    }
    IndexCandidateGroup bestGroup = null;
    for (Set<IndexCandidate> subset : new PowerSet<>(indexCandidates)) {
        if (subset.isEmpty())
            continue;
        final IndexCandidateGroup group = new IndexCandidateGroup(subset);
        if (group.compareTo(bestGroup) > 0) {
            bestGroup = group;
        }
    }
    if (bestGroup != null) {
        coveredClauses.addAll(bestGroup.getCoveredClauses());
        List<IndexCandidate> bestIndexes = new ArrayList<>(bestGroup.getIndexCandidates());
        // sort indexes by score descending order
        bestIndexes.sort((a, b) -> Double.compare(b.getScore(), a.getScore()));
        // isSorted depends on the first index subquery
        isSorted = orders.isEmpty() || bestIndexes.get(0).getIndex().isMixedIndex() && IndexSelectionUtil.indexCoversOrder((MixedIndexType) bestIndexes.get(0).getIndex(), orders);
        for (IndexCandidate c : bestIndexes) {
            addToJointQuery(c, jointQuery, serializer, orders);
        }
    }
    return new SelectedIndexQuery(jointQuery, isSorted);
}
Also used : JointIndexQuery(org.janusgraph.graphdb.query.graph.JointIndexQuery) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) ArrayList(java.util.ArrayList) PowerSet(org.janusgraph.util.datastructures.PowerSet) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) IndexType(org.janusgraph.graphdb.types.IndexType) HashSet(java.util.HashSet)

Aggregations

JointIndexQuery (org.janusgraph.graphdb.query.graph.JointIndexQuery)2 IndexType (org.janusgraph.graphdb.types.IndexType)2 MixedIndexType (org.janusgraph.graphdb.types.MixedIndexType)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 PowerSet (org.janusgraph.util.datastructures.PowerSet)1