use of org.janusgraph.graphdb.query.condition.PredicateCondition in project janusgraph by JanusGraph.
the class IndexSelectionUtil method getKeyIndexesForCondition.
private static Iterable<IndexType> getKeyIndexesForCondition(Condition<JanusGraphElement> condition) {
if (condition instanceof PredicateCondition) {
final RelationType type = ((PredicateCondition<RelationType, JanusGraphElement>) condition).getKey();
Preconditions.checkArgument(type != null && type.isPropertyKey());
return ((InternalRelationType) type).getKeyIndexes();
}
return Collections.emptySet();
}
use of org.janusgraph.graphdb.query.condition.PredicateCondition in project janusgraph by JanusGraph.
the class IndexSerializer method getQuery.
public IndexQuery getQuery(final MixedIndexType index, final Condition condition, final OrderList orders) {
final Condition newCondition = ConditionUtil.literalTransformation(condition, new Function<Condition<JanusGraphElement>, Condition<JanusGraphElement>>() {
@Nullable
@Override
public Condition<JanusGraphElement> apply(final Condition<JanusGraphElement> condition) {
Preconditions.checkArgument(condition instanceof PredicateCondition);
final PredicateCondition pc = (PredicateCondition) condition;
final PropertyKey key = (PropertyKey) pc.getKey();
return new PredicateCondition<>(key2Field(index, key), pc.getPredicate(), pc.getValue());
}
});
ImmutableList<IndexQuery.OrderEntry> newOrders = IndexQuery.NO_ORDER;
if (!orders.isEmpty() && IndexSelectionUtil.indexCoversOrder(index, orders)) {
final ImmutableList.Builder<IndexQuery.OrderEntry> lb = ImmutableList.builder();
for (int i = 0; i < orders.size(); i++) {
lb.add(new IndexQuery.OrderEntry(key2Field(index, orders.getKey(i)), orders.getOrder(i), orders.getKey(i).dataType()));
}
newOrders = lb.build();
}
return new IndexQuery(index.getStoreName(), newCondition, newOrders);
}
use of org.janusgraph.graphdb.query.condition.PredicateCondition in project janusgraph by JanusGraph.
the class AbstractIndexSelectionStrategy method coversAll.
private boolean coversAll(final MixedIndexType index, Condition<JanusGraphElement> condition, IndexSerializer indexInfo) {
if (condition.getType() != Condition.Type.LITERAL) {
return StreamSupport.stream(condition.getChildren().spliterator(), false).allMatch(child -> coversAll(index, child, indexInfo));
}
if (!(condition instanceof PredicateCondition)) {
return false;
}
final PredicateCondition<RelationType, JanusGraphElement> atom = (PredicateCondition) condition;
if (atom.getValue() == null && atom.getPredicate() != Cmp.NOT_EQUAL) {
return false;
}
Preconditions.checkArgument(atom.getKey().isPropertyKey());
final PropertyKey key = (PropertyKey) atom.getKey();
final ParameterIndexField[] fields = index.getFieldKeys();
final ParameterIndexField match = Arrays.stream(fields).filter(field -> field.getStatus() == SchemaStatus.ENABLED).filter(field -> field.getFieldKey().equals(key)).findAny().orElse(null);
if (match == null) {
return false;
}
boolean existsQuery = atom.getValue() == null && atom.getPredicate() == Cmp.NOT_EQUAL && indexInfo.supportsExistsQuery(index, match);
return existsQuery || indexInfo.supports(index, match, atom.getPredicate());
}
use of org.janusgraph.graphdb.query.condition.PredicateCondition 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.PredicateCondition 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));
}
Aggregations