Search in sources :

Example 1 with PowerSet

use of org.janusgraph.util.datastructures.PowerSet 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

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