use of org.janusgraph.core.JanusGraphElement in project janusgraph by JanusGraph.
the class JanusGraphTest method evaluateQuery.
public static void evaluateQuery(JanusGraphVertexQuery query, RelationCategory resultType, int expectedResults, int numSubQueries, boolean[] subQuerySpecs, Map<PropertyKey, Order> orderMap) {
SimpleQueryProfiler profiler = new SimpleQueryProfiler();
((BasicVertexCentricQueryBuilder) query).profiler(profiler);
Iterable<? extends JanusGraphElement> result;
switch(resultType) {
case PROPERTY:
result = query.properties();
break;
case EDGE:
result = query.edges();
break;
case RELATION:
result = query.relations();
break;
default:
throw new AssertionError();
}
OrderList orders = profiler.getAnnotation(QueryProfiler.ORDERS_ANNOTATION);
// Check elements and that they are returned in the correct order
int no = 0;
JanusGraphElement previous = null;
for (JanusGraphElement e : result) {
assertNotNull(e);
no++;
if (previous != null && !orders.isEmpty()) {
assertTrue(orders.compare(previous, e) <= 0);
}
previous = e;
}
assertEquals(expectedResults, no);
// Check OrderList of query
assertNotNull(orders);
assertEquals(orderMap.size(), orders.size());
for (int i = 0; i < orders.size(); i++) {
assertEquals(orderMap.get(orders.getKey(i)), orders.getOrder(i));
}
for (PropertyKey key : orderMap.keySet()) assertTrue(orders.containsKey(key));
// Check subqueries
assertEquals(Integer.valueOf(1), profiler.getAnnotation(QueryProfiler.NUMVERTICES_ANNOTATION));
int subQueryCounter = 0;
for (SimpleQueryProfiler subProfiler : profiler) {
assertNotNull(subProfiler);
if (subProfiler.getGroupName().equals(QueryProfiler.OPTIMIZATION))
continue;
if (subQuerySpecs.length == 2) {
// 0=>fitted, 1=>ordered
assertEquals(subQuerySpecs[0], subProfiler.getAnnotation(QueryProfiler.FITTED_ANNOTATION));
assertEquals(subQuerySpecs[1], subProfiler.getAnnotation(QueryProfiler.ORDERED_ANNOTATION));
}
// assertEquals(1,Iterables.size(subProfiler)); This only applies if a disk call is necessary
subQueryCounter++;
}
assertEquals(numSubQueries, subQueryCounter);
}
use of org.janusgraph.core.JanusGraphElement in project janusgraph by JanusGraph.
the class IndexSerializerTest method mockIndexAppliesTo.
private JanusGraphElement mockIndexAppliesTo(MixedIndexType mit, boolean indexable) {
String key = "foo";
String value = "bar";
JanusGraphElement indexableElement = mockIndexableElement(key, value, indexable);
ElementCategory ec = ElementCategory.VERTEX;
doReturn(ec).when(mit).getElement();
doReturn(false).when(mit).hasSchemaTypeConstraint();
PropertyKey pk = mock(PropertyKey.class);
doReturn(1L).when(pk).longId();
doReturn(key).when(pk).name();
ParameterIndexField pif = mock(ParameterIndexField.class);
Parameter[] parameter = { new Parameter(key, value) };
doReturn(parameter).when(pif).getParameters();
doReturn(SchemaStatus.REGISTERED).when(pif).getStatus();
doReturn(pk).when(pif).getFieldKey();
ParameterIndexField[] ifField = { pif };
doReturn(ifField).when(mit).getFieldKeys();
return indexableElement;
}
use of org.janusgraph.core.JanusGraphElement 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.core.JanusGraphElement 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.core.JanusGraphElement in project janusgraph by JanusGraph.
the class JanusGraphVertexStep method flatMap.
@Override
protected Iterator<E> flatMap(final Traverser.Admin<Vertex> traverser) {
Iterable<? extends JanusGraphElement> result;
if (useMultiQuery) {
if (multiQueryResults == null || !multiQueryResults.containsKey(traverser.get())) {
// current batch is exhausted, fetch new batch
prefetchNextBatch(traverser);
}
result = multiQueryResults.get(traverser.get());
} else {
final JanusGraphVertexQuery query = makeQuery((JanusGraphTraversalUtil.getJanusGraphVertex(traverser)).query());
result = (Vertex.class.isAssignableFrom(getReturnClass())) ? query.vertices() : query.edges();
}
if (batchPropertyPrefetching && txVertexCacheSize > 1) {
Set<Vertex> vertices = new HashSet<>();
for (JanusGraphElement v : result) {
vertices.add((Vertex) v);
if (vertices.size() >= txVertexCacheSize) {
break;
}
}
// populate the vertex cache so subsequent queries of properties don't have to go to the storage back end
if (vertices.size() > 1) {
JanusGraphMultiVertexQuery propertyMultiQuery = JanusGraphTraversalUtil.getTx(traversal).multiQuery();
((BasicVertexCentricQueryBuilder) propertyMultiQuery).profiler(queryProfiler);
propertyMultiQuery.addAllVertices(vertices).preFetch();
}
}
return (Iterator<E>) result.iterator();
}
Aggregations