use of com.thinkaurelius.titan.core.TitanGraphQuery in project titan by thinkaurelius.
the class TitanGraphTest method evaluateQuery.
public static void evaluateQuery(TitanGraphQuery query, ElementCategory resultType, int expectedResults, boolean[] subQuerySpecs, Map<PropertyKey, Order> orderMap, String... intersectingIndexes) {
if (intersectingIndexes == null)
intersectingIndexes = new String[0];
SimpleQueryProfiler profiler = new SimpleQueryProfiler();
((GraphCentricQueryBuilder) query).profiler(profiler);
Iterable<? extends TitanElement> result;
switch(resultType) {
case PROPERTY:
result = query.properties();
break;
case EDGE:
result = query.edges();
break;
case VERTEX:
result = query.vertices();
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;
TitanElement previous = null;
for (TitanElement 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
SimpleQueryProfiler subp = Iterables.getOnlyElement(Iterables.filter(profiler, p -> !p.getGroupName().equals(QueryProfiler.OPTIMIZATION)));
if (subQuerySpecs.length == 2) {
//0=>fitted, 1=>ordered
assertEquals(subQuerySpecs[0], (Boolean) subp.getAnnotation(QueryProfiler.FITTED_ANNOTATION));
assertEquals(subQuerySpecs[1], (Boolean) subp.getAnnotation(QueryProfiler.ORDERED_ANNOTATION));
}
Set<String> indexNames = new HashSet<>();
int indexQueries = 0;
boolean fullscan = false;
for (SimpleQueryProfiler indexp : subp) {
if (indexp.getAnnotation(QueryProfiler.FULLSCAN_ANNOTATION) != null) {
fullscan = true;
} else {
indexNames.add(indexp.getAnnotation(QueryProfiler.INDEX_ANNOTATION));
indexQueries++;
}
}
if (indexQueries > 0)
assertFalse(fullscan);
if (fullscan)
assertTrue(intersectingIndexes.length == 0);
assertEquals(intersectingIndexes.length, indexQueries);
assertEquals(Sets.newHashSet(intersectingIndexes), indexNames);
}
use of com.thinkaurelius.titan.core.TitanGraphQuery in project titan by thinkaurelius.
the class TitanIndexTest method testIndexing.
@Test
public void testIndexing() {
PropertyKey text = makeKey("text", String.class);
createExternalVertexIndex(text, INDEX);
createExternalEdgeIndex(text, INDEX);
PropertyKey location = makeKey("location", Geoshape.class);
createExternalVertexIndex(location, INDEX);
createExternalEdgeIndex(location, INDEX);
PropertyKey time = makeKey("time", Long.class);
createExternalVertexIndex(time, INDEX);
createExternalEdgeIndex(time, INDEX);
PropertyKey category = makeKey("category", Integer.class);
mgmt.buildIndex("vcategory", Vertex.class).addKey(category).buildCompositeIndex();
mgmt.buildIndex("ecategory", Edge.class).addKey(category).buildCompositeIndex();
PropertyKey group = makeKey("group", Byte.class);
createExternalVertexIndex(group, INDEX);
createExternalEdgeIndex(group, INDEX);
PropertyKey id = makeVertexIndexedKey("uid", Integer.class);
EdgeLabel knows = ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("knows")).sortKey(time).signature(location).make();
finishSchema();
clopen();
String[] words = { "world", "aurelius", "titan", "graph" };
int numCategories = 5;
int numGroups = 10;
double distance, offset;
int numV = 100;
final int originalNumV = numV;
for (int i = 0; i < numV; i++) {
TitanVertex v = tx.addVertex();
v.property(VertexProperty.Cardinality.single, "uid", i);
v.property(VertexProperty.Cardinality.single, "category", i % numCategories);
v.property(VertexProperty.Cardinality.single, "group", i % numGroups);
v.property(VertexProperty.Cardinality.single, "text", "Vertex " + words[i % words.length]);
v.property(VertexProperty.Cardinality.single, "time", i);
offset = (i % 2 == 0 ? 1 : -1) * (i * 50.0 / numV);
v.property(VertexProperty.Cardinality.single, "location", Geoshape.point(0.0 + offset, 0.0 + offset));
Edge e = v.addEdge("knows", getVertex("uid", Math.max(0, i - 1)));
e.property("text", "Vertex " + words[i % words.length]);
e.property("time", i);
e.property("category", i % numCategories);
e.property("group", i % numGroups);
e.property("location", Geoshape.point(0.0 + offset, 0.0 + offset));
}
for (int i = 0; i < words.length; i++) {
int expectedSize = numV / words.length;
assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).vertices());
assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).edges());
//Test ordering
for (String orderKey : new String[] { "time", "category" }) {
for (Order order : Order.values()) {
for (TitanGraphQuery traversal : ImmutableList.of(tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()), tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()))) {
verifyElementOrder(traversal.vertices(), orderKey, order, expectedSize);
}
}
}
}
assertCount(3, tx.query().has("group", 3).orderBy("time", incr).limit(3).vertices());
assertCount(3, tx.query().has("group", 3).orderBy("time", decr).limit(3).edges());
for (int i = 0; i < numV / 2; i += numV / 10) {
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices());
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges());
}
for (int i = 0; i < numV; i += 10) {
offset = (i * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices());
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges());
}
//Queries combining mixed and composite indexes
assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).vertices());
assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).edges());
assertCount(5, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices());
offset = (19 * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());
assertCount(numV, tx.query().vertices());
assertCount(numV, tx.query().edges());
//--------------
clopen();
for (int i = 0; i < words.length; i++) {
int expectedSize = numV / words.length;
assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).vertices());
assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).edges());
//Test ordering
for (String orderKey : new String[] { "time", "category" }) {
for (Order order : Order.values()) {
for (TitanGraphQuery traversal : ImmutableList.of(tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()), tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()))) {
verifyElementOrder(traversal.vertices(), orderKey, order, expectedSize);
}
}
}
}
assertCount(3, tx.query().has("group", 3).orderBy("time", incr).limit(3).vertices());
assertCount(3, tx.query().has("group", 3).orderBy("time", decr).limit(3).edges());
for (int i = 0; i < numV / 2; i += numV / 10) {
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices());
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges());
}
for (int i = 0; i < numV; i += 10) {
offset = (i * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices());
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges());
}
//Queries combining mixed and composite indexes
assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).vertices());
assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).edges());
assertCount(5, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices());
offset = (19 * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());
assertCount(numV, tx.query().vertices());
assertCount(numV, tx.query().edges());
newTx();
int numDelete = 12;
for (int i = numV - numDelete; i < numV; i++) {
getVertex("uid", i).remove();
}
numV = numV - numDelete;
//Copied from above
for (int i = 0; i < words.length; i++) {
assertCount(numV / words.length, tx.query().has("text", Text.CONTAINS, words[i]).vertices());
assertCount(numV / words.length, tx.query().has("text", Text.CONTAINS, words[i]).edges());
}
for (int i = 0; i < numV / 2; i += numV / 10) {
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices());
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges());
}
for (int i = 0; i < numV; i += 10) {
offset = (i * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices());
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges());
}
assertCount(5, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices());
offset = (19 * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());
assertCount(numV, tx.query().vertices());
assertCount(numV, tx.query().edges());
}
Aggregations