use of org.janusgraph.graphdb.query.condition.MultiCondition in project janusgraph by JanusGraph.
the class JanusGraphTest method testGetMatchingIndexes.
@Test
public void testGetMatchingIndexes() {
final PropertyKey name = makeKey("name", String.class);
final PropertyKey age = makeKey("age", Integer.class);
mgmt.buildIndex("byName", Vertex.class).addKey(name).buildCompositeIndex();
mgmt.buildIndex("byAge", Vertex.class).addKey(age).buildCompositeIndex();
finishSchema();
String searchName = "someName";
Integer searchAge = 42;
// test with no valid constraints
assertEquals(Collections.emptySet(), IndexSelectionUtil.getMatchingIndexes(null));
assertEquals(Collections.emptySet(), IndexSelectionUtil.getMatchingIndexes(null, null));
assertEquals(Collections.emptySet(), IndexSelectionUtil.getMatchingIndexes(null, i -> true));
// test with two valid constraints
List<PredicateCondition<String, JanusGraphElement>> constraints = Arrays.asList(new PredicateCondition<>("name", JanusGraphPredicateUtils.convert(P.eq(searchName).getBiPredicate()), searchName), new PredicateCondition<>("age", JanusGraphPredicateUtils.convert(P.eq(searchAge).getBiPredicate()), searchAge));
MultiCondition<JanusGraphElement> conditions = QueryUtil.constraints2QNF((StandardJanusGraphTx) tx, constraints);
assertEquals(2, IndexSelectionUtil.getMatchingIndexes(conditions).size());
assertEquals(1, IndexSelectionUtil.getMatchingIndexes(conditions, i -> i.getName().equals("byAge")).size());
// test with invalid filter
assertEquals(0, IndexSelectionUtil.getMatchingIndexes(conditions, null).size());
}
use of org.janusgraph.graphdb.query.condition.MultiCondition in project janusgraph by JanusGraph.
the class JanusGraphTest method testExistsMatchingIndex.
@Test
public void testExistsMatchingIndex() {
final PropertyKey name = makeKey("name", String.class);
final PropertyKey age = makeKey("age", Integer.class);
mgmt.buildIndex("byName", Vertex.class).addKey(name).buildCompositeIndex();
mgmt.buildIndex("byAge", Vertex.class).addKey(age).buildCompositeIndex();
finishSchema();
String searchName = "someName";
Integer searchAge = 42;
// test with no valid constraints
assertEquals(false, IndexSelectionUtil.existsMatchingIndex(null));
assertEquals(false, IndexSelectionUtil.existsMatchingIndex(null, null));
assertEquals(false, IndexSelectionUtil.existsMatchingIndex(null, i -> true));
// test with two valid constraints
List<PredicateCondition<String, JanusGraphElement>> constraints = Arrays.asList(new PredicateCondition<>("name", JanusGraphPredicateUtils.convert(P.eq(searchName).getBiPredicate()), searchName), new PredicateCondition<>("age", JanusGraphPredicateUtils.convert(P.eq(searchAge).getBiPredicate()), searchAge));
MultiCondition<JanusGraphElement> conditions = QueryUtil.constraints2QNF((StandardJanusGraphTx) tx, constraints);
assertEquals(true, IndexSelectionUtil.existsMatchingIndex(conditions));
assertEquals(true, IndexSelectionUtil.existsMatchingIndex(conditions, i -> i.getName().equals("byAge")));
assertEquals(false, IndexSelectionUtil.existsMatchingIndex(conditions, i -> i.getName().equals("byNonExistentKey")));
// test with invalid filter
assertEquals(false, IndexSelectionUtil.existsMatchingIndex(conditions, null));
}
use of org.janusgraph.graphdb.query.condition.MultiCondition in project janusgraph by JanusGraph.
the class AdjacentVertexHasUniquePropertyOptimizerStrategy method isValidStep.
/**
* Determines whether this HasStep can be answered by a unique index and thus, returns either 0 or 1 match
*/
@Override
protected boolean isValidStep(HasStep<?> step) {
StandardJanusGraphTx tx = (StandardJanusGraphTx) JanusGraphTraversalUtil.getTx(step.getTraversal());
List<String> givenKeys = step.getHasContainers().stream().map(HasContainer::getKey).collect(Collectors.toList());
List<PredicateCondition<String, JanusGraphElement>> constraints = step.getHasContainers().stream().filter(hc -> hc.getBiPredicate() == Compare.eq).map(hc -> new PredicateCondition<>(hc.getKey(), JanusGraphPredicateUtils.convert(hc.getBiPredicate()), hc.getValue())).filter(pc -> tx.validDataType(pc.getValue().getClass())).collect(Collectors.toList());
final MultiCondition<JanusGraphElement> conditions = QueryUtil.constraints2QNF(tx, constraints);
// check all matching unique indexes
return IndexSelectionUtil.existsMatchingIndex(conditions, indexType -> indexType.isCompositeIndex() && ((CompositeIndexType) indexType).getCardinality() == Cardinality.SINGLE && IndexSelectionUtil.isIndexSatisfiedByGivenKeys(indexType, givenKeys));
}
use of org.janusgraph.graphdb.query.condition.MultiCondition in project janusgraph by JanusGraph.
the class AbstractIndexSelectionStrategy method createIndexCandidate.
/**
* Creates an <code>IndexCandidate</code> from a <code>MultiCondition</code> which it covers.
* @param index
* @param conditions For the condition to be valid, it needs to match these rules:
* <ul>
* <li>It must be an equality condition</li>
* <li>It must not cover multiple labels</li>
* <li>The label must match the given <code>index</code></li>
* </ul>
* @return An instance of <code>IndexCandidate</code> if the parameter <code>conditions</code> is valid, <code>null</code> else.
*/
@Nullable
protected IndexCandidate createIndexCandidate(final IndexType index, final MultiCondition<JanusGraphElement> conditions, IndexSerializer serializer) {
final Set<Condition> subCover = new HashSet<>(1);
// Check that this index actually applies in case of a schema constraint
if (index.hasSchemaTypeConstraint()) {
final JanusGraphSchemaType type = index.getSchemaTypeConstraint();
final Map.Entry<Condition, Collection<Object>> equalCon = getEqualityConditionValues(conditions, ImplicitKey.LABEL);
if (equalCon == null) {
// Only equality conditions are supported
return null;
}
final Collection<Object> labels = equalCon.getValue();
assert labels.size() >= 1;
if (labels.size() > 1) {
// The query optimizer currently does not support multiple label constraints
return null;
}
if (!type.name().equals(labels.iterator().next())) {
// Given IndexType does not match given condition label
return null;
}
subCover.add(equalCon.getKey());
}
Object subCondition;
if (index.isCompositeIndex()) {
subCondition = indexCover((CompositeIndexType) index, conditions, subCover);
} else {
subCondition = indexCover((MixedIndexType) index, conditions, serializer, subCover);
}
if (subCondition == null || subCover.isEmpty()) {
// Unable to initialize IndexCandidate from given parameters
return null;
}
return new IndexCandidate(index, subCover, subCondition);
}
Aggregations