use of org.janusgraph.core.VertexList in project janusgraph by JanusGraph.
the class JanusGraphTest method testRelationTypeIndexes.
@Test
public void testRelationTypeIndexes() {
PropertyKey weight = makeKey("weight", Float.class);
PropertyKey time = makeKey("time", Long.class);
PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.LIST).make();
EdgeLabel connect = mgmt.makeEdgeLabel("connect").signature(time).make();
EdgeLabel child = mgmt.makeEdgeLabel("child").multiplicity(Multiplicity.ONE2MANY).make();
EdgeLabel link = mgmt.makeEdgeLabel("link").unidirected().make();
RelationTypeIndex name1 = mgmt.buildPropertyIndex(name, "weightDesc", weight);
RelationTypeIndex connect1 = mgmt.buildEdgeIndex(connect, "weightAsc", Direction.BOTH, asc, weight);
RelationTypeIndex connect2 = mgmt.buildEdgeIndex(connect, "weightDesc", Direction.OUT, desc, weight);
RelationTypeIndex connect3 = mgmt.buildEdgeIndex(connect, "time+weight", Direction.OUT, desc, time, weight);
RelationTypeIndex child1 = mgmt.buildEdgeIndex(child, "time", Direction.OUT, time);
RelationTypeIndex link1 = mgmt.buildEdgeIndex(link, "time", Direction.OUT, time);
final String name1n = name1.name(), connect1n = connect1.name(), connect2n = connect2.name(), connect3n = connect3.name(), child1n = child1.name(), link1n = link1.name();
// ########### INSPECTION & FAILURE ##############
assertTrue(mgmt.containsRelationIndex(name, "weightDesc"));
assertTrue(mgmt.containsRelationIndex(connect, "weightDesc"));
assertFalse(mgmt.containsRelationIndex(child, "weightDesc"));
assertEquals("time+weight", mgmt.getRelationIndex(connect, "time+weight").name());
assertNotNull(mgmt.getRelationIndex(link, "time"));
assertNull(mgmt.getRelationIndex(name, "time"));
assertEquals(1, Iterables.size(mgmt.getRelationIndexes(child)));
assertEquals(3, Iterables.size(mgmt.getRelationIndexes(connect)));
assertEquals(0, Iterables.size(mgmt.getRelationIndexes(weight)));
try {
// Name already exists
mgmt.buildEdgeIndex(connect, "weightAsc", Direction.OUT, time);
fail();
} catch (SchemaViolationException ignored) {
}
// } catch (IllegalArgumentException e) {}
try {
// Not valid in this direction due to multiplicity constraint
mgmt.buildEdgeIndex(child, "blablub", Direction.IN, time);
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Not valid in this direction due to unidirectionality
mgmt.buildEdgeIndex(link, "blablub", Direction.BOTH, time);
fail();
} catch (IllegalArgumentException ignored) {
}
// ########## END INSPECTION ###########
finishSchema();
weight = mgmt.getPropertyKey("weight");
time = mgmt.getPropertyKey("time");
name = mgmt.getPropertyKey("name");
connect = mgmt.getEdgeLabel("connect");
child = mgmt.getEdgeLabel("child");
link = mgmt.getEdgeLabel("link");
// ########### INSPECTION & FAILURE (copied from above) ##############
assertTrue(mgmt.containsRelationIndex(name, "weightDesc"));
assertTrue(mgmt.containsRelationIndex(connect, "weightDesc"));
assertFalse(mgmt.containsRelationIndex(child, "weightDesc"));
assertEquals("time+weight", mgmt.getRelationIndex(connect, "time+weight").name());
assertNotNull(mgmt.getRelationIndex(link, "time"));
assertNull(mgmt.getRelationIndex(name, "time"));
assertEquals(1, Iterables.size(mgmt.getRelationIndexes(child)));
assertEquals(3, Iterables.size(mgmt.getRelationIndexes(connect)));
assertEquals(0, Iterables.size(mgmt.getRelationIndexes(weight)));
try {
// Name already exists
mgmt.buildEdgeIndex(connect, "weightAsc", Direction.OUT, time);
fail();
} catch (SchemaViolationException ignored) {
}
// } catch (IllegalArgumentException e) {}
try {
// Not valid in this direction due to multiplicity constraint
mgmt.buildEdgeIndex(child, "blablub", Direction.IN, time);
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Not valid in this direction due to unidirectionality
mgmt.buildEdgeIndex(link, "blablub", Direction.BOTH, time);
fail();
} catch (IllegalArgumentException ignored) {
}
// ########## END INSPECTION ###########
mgmt.rollback();
/*
########## TEST WITHIN TRANSACTION ##################
*/
weight = tx.getPropertyKey("weight");
time = tx.getPropertyKey("time");
final int numV = 100;
JanusGraphVertex v = tx.addVertex();
JanusGraphVertex[] ns = new JanusGraphVertex[numV];
for (int i = 0; i < numV; i++) {
double w = (i * 0.5) % 5;
long t = (i + 77) % numV;
VertexProperty p = v.property("name", "v" + i, "weight", w, "time", t);
ns[i] = tx.addVertex();
for (String label : new String[] { "connect", "child", "link" }) {
Edge e = v.addEdge(label, ns[i], "weight", w, "time", t);
}
}
JanusGraphVertex u = ns[0];
VertexList vl;
// ######### QUERIES ##########
v = getV(tx, v);
u = getV(tx, u);
evaluateQuery(v.query().keys("name").has("weight", Cmp.GREATER_THAN, 3.6), PROPERTY, 2 * numV / 10, 1, new boolean[] { false, true });
evaluateQuery(v.query().keys("name").has("weight", Cmp.LESS_THAN, 0.9).orderBy("weight", asc), PROPERTY, 2 * numV / 10, 1, new boolean[] { true, true }, weight, Order.ASC);
evaluateQuery(v.query().keys("name").interval("weight", 1.1, 2.2).orderBy("weight", desc).limit(numV / 10), PROPERTY, numV / 10, 1, new boolean[] { true, false }, weight, Order.DESC);
evaluateQuery(v.query().keys("name").has("time", Cmp.EQUAL, 5).orderBy("weight", desc), PROPERTY, 1, 1, new boolean[] { false, false }, weight, Order.DESC);
evaluateQuery(v.query().keys("name"), PROPERTY, numV, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, 5), EDGE, 1, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("child").direction(BOTH).has("time", Cmp.EQUAL, 5), EDGE, 1, 2, new boolean[0]);
evaluateQuery(v.query().labels("child").direction(OUT).interval("time", 10, 20).orderBy("weight", desc).limit(5), EDGE, 5, 1, new boolean[] { true, false }, weight, Order.DESC);
evaluateQuery(v.query().labels("child").direction(BOTH).interval("weight", 0.0, 1.0).orderBy("weight", desc), EDGE, 2 * numV / 10, 2, new boolean[] { false, false }, weight, Order.DESC);
evaluateQuery(v.query().labels("child").direction(OUT).interval("weight", 0.0, 1.0), EDGE, 2 * numV / 10, 1, new boolean[] { false, true });
evaluateQuery(v.query().labels("child").direction(BOTH), EDGE, numV, 1, new boolean[] { true, true });
vl = v.query().labels("child").direction(BOTH).vertexIds();
assertEquals(numV, vl.size());
assertTrue(vl.isSorted());
assertTrue(isSortedByID(vl));
evaluateQuery(v.query().labels("child").interval("weight", 0.0, 1.0).direction(OUT), EDGE, 2 * numV / 10, 1, new boolean[] { false, true });
vl = v.query().labels("child").interval("weight", 0.0, 1.0).direction(OUT).vertexIds();
assertEquals(2 * numV / 10, vl.size());
assertTrue(vl.isSorted());
assertTrue(isSortedByID(vl));
evaluateQuery(v.query().labels("child").interval("time", 70, 80).direction(OUT).orderBy("time", asc), EDGE, 10, 1, new boolean[] { true, true }, time, Order.ASC);
vl = v.query().labels("child").interval("time", 70, 80).direction(OUT).orderBy("time", asc).vertexIds();
assertEquals(10, vl.size());
assertFalse(vl.isSorted());
assertFalse(isSortedByID(vl));
vl.sort();
assertTrue(vl.isSorted());
assertTrue(isSortedByID(vl));
evaluateQuery(v.query().labels("connect").has("time", Cmp.EQUAL, 5).interval("weight", 0.0, 5.0).direction(OUT), EDGE, 1, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("connect").has("time", Cmp.EQUAL, 5).interval("weight", 0.0, 5.0).direction(BOTH), EDGE, 1, 2, new boolean[0]);
evaluateQuery(v.query().labels("connect").interval("time", 10, 20).interval("weight", 0.0, 5.0).direction(OUT), EDGE, 10, 1, new boolean[] { false, true });
evaluateQuery(v.query().labels("connect").direction(OUT).orderBy("weight", asc).limit(10), EDGE, 10, 1, new boolean[] { true, true }, weight, Order.ASC);
evaluateQuery(v.query().labels("connect").direction(OUT).orderBy("weight", desc).limit(10), EDGE, 10, 1, new boolean[] { true, true }, weight, Order.DESC);
evaluateQuery(v.query().labels("connect").direction(OUT).interval("weight", 1.4, 2.75).orderBy("weight", desc), EDGE, 3 * numV / 10, 1, new boolean[] { true, true }, weight, Order.DESC);
evaluateQuery(v.query().labels("connect").direction(OUT).has("time", Cmp.EQUAL, 22).orderBy("weight", desc), EDGE, 1, 1, new boolean[] { true, true }, weight, Order.DESC);
evaluateQuery(v.query().labels("connect").direction(OUT).has("time", Cmp.EQUAL, 22).orderBy("weight", asc), EDGE, 1, 1, new boolean[] { true, false }, weight, Order.ASC);
evaluateQuery(v.query().labels("connect").direction(OUT).adjacent(u), EDGE, 1, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("connect").direction(OUT).has("weight", Cmp.EQUAL, 0.0).adjacent(u), EDGE, 1, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("connect").direction(OUT).interval("weight", 0.0, 1.0).adjacent(u), EDGE, 1, 1, new boolean[] { false, true });
evaluateQuery(v.query().labels("connect").direction(OUT).interval("time", 50, 100).adjacent(u), EDGE, 1, 1, new boolean[] { false, true });
evaluateQuery(v.query(), RELATION, numV * 4, 1, new boolean[] { true, true });
evaluateQuery(v.query().direction(OUT), RELATION, numV * 4, 1, new boolean[] { false, true });
// --------------
clopen();
weight = tx.getPropertyKey("weight");
time = tx.getPropertyKey("time");
// ######### QUERIES (copied from above) ##########
v = getV(tx, v);
u = getV(tx, u);
evaluateQuery(v.query().keys("name").has("weight", Cmp.GREATER_THAN, 3.6), PROPERTY, 2 * numV / 10, 1, new boolean[] { false, true });
evaluateQuery(v.query().keys("name").has("weight", Cmp.LESS_THAN, 0.9).orderBy("weight", asc), PROPERTY, 2 * numV / 10, 1, new boolean[] { true, true }, weight, Order.ASC);
evaluateQuery(v.query().keys("name").interval("weight", 1.1, 2.2).orderBy("weight", desc).limit(numV / 10), PROPERTY, numV / 10, 1, new boolean[] { true, false }, weight, Order.DESC);
evaluateQuery(v.query().keys("name").has("time", Cmp.EQUAL, 5).orderBy("weight", desc), PROPERTY, 1, 1, new boolean[] { false, false }, weight, Order.DESC);
evaluateQuery(v.query().keys("name"), PROPERTY, numV, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, 5), EDGE, 1, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("child").direction(BOTH).has("time", Cmp.EQUAL, 5), EDGE, 1, 2, new boolean[0]);
evaluateQuery(v.query().labels("child").direction(OUT).interval("time", 10, 20).orderBy("weight", desc).limit(5), EDGE, 5, 1, new boolean[] { true, false }, weight, Order.DESC);
evaluateQuery(v.query().labels("child").direction(BOTH).interval("weight", 0.0, 1.0).orderBy("weight", desc), EDGE, 2 * numV / 10, 2, new boolean[] { false, false }, weight, Order.DESC);
evaluateQuery(v.query().labels("child").direction(OUT).interval("weight", 0.0, 1.0), EDGE, 2 * numV / 10, 1, new boolean[] { false, true });
evaluateQuery(v.query().labels("child").direction(BOTH), EDGE, numV, 1, new boolean[] { true, true });
vl = v.query().labels("child").direction(BOTH).vertexIds();
assertEquals(numV, vl.size());
assertTrue(vl.isSorted());
assertTrue(isSortedByID(vl));
evaluateQuery(v.query().labels("child").interval("weight", 0.0, 1.0).direction(OUT), EDGE, 2 * numV / 10, 1, new boolean[] { false, true });
vl = v.query().labels("child").interval("weight", 0.0, 1.0).direction(OUT).vertexIds();
assertEquals(2 * numV / 10, vl.size());
assertTrue(vl.isSorted());
assertTrue(isSortedByID(vl));
evaluateQuery(v.query().labels("child").interval("time", 70, 80).direction(OUT).orderBy("time", asc), EDGE, 10, 1, new boolean[] { true, true }, time, Order.ASC);
vl = v.query().labels("child").interval("time", 70, 80).direction(OUT).orderBy("time", asc).vertexIds();
assertEquals(10, vl.size());
assertFalse(vl.isSorted());
assertFalse(isSortedByID(vl));
vl.sort();
assertTrue(vl.isSorted());
assertTrue(isSortedByID(vl));
evaluateQuery(v.query().labels("connect").has("time", Cmp.EQUAL, 5).interval("weight", 0.0, 5.0).direction(OUT), EDGE, 1, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("connect").has("time", Cmp.EQUAL, 5).interval("weight", 0.0, 5.0).direction(BOTH), EDGE, 1, 2, new boolean[0]);
evaluateQuery(v.query().labels("connect").interval("time", 10, 20).interval("weight", 0.0, 5.0).direction(OUT), EDGE, 10, 1, new boolean[] { false, true });
evaluateQuery(v.query().labels("connect").direction(OUT).orderBy("weight", asc).limit(10), EDGE, 10, 1, new boolean[] { true, true }, weight, Order.ASC);
evaluateQuery(v.query().labels("connect").direction(OUT).orderBy("weight", desc).limit(10), EDGE, 10, 1, new boolean[] { true, true }, weight, Order.DESC);
evaluateQuery(v.query().labels("connect").direction(OUT).interval("weight", 1.4, 2.75).orderBy("weight", desc), EDGE, 3 * numV / 10, 1, new boolean[] { true, true }, weight, Order.DESC);
evaluateQuery(v.query().labels("connect").direction(OUT).has("time", Cmp.EQUAL, 22).orderBy("weight", desc), EDGE, 1, 1, new boolean[] { true, true }, weight, Order.DESC);
evaluateQuery(v.query().labels("connect").direction(OUT).has("time", Cmp.EQUAL, 22).orderBy("weight", asc), EDGE, 1, 1, new boolean[] { true, false }, weight, Order.ASC);
evaluateQuery(v.query().labels("connect").direction(OUT).adjacent(u), EDGE, 1, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("connect").direction(OUT).has("weight", Cmp.EQUAL, 0.0).adjacent(u), EDGE, 1, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("connect").direction(OUT).interval("weight", 0.0, 1.0).adjacent(u), EDGE, 1, 1, new boolean[] { false, true });
evaluateQuery(v.query().labels("connect").direction(OUT).interval("time", 50, 100).adjacent(u), EDGE, 1, 1, new boolean[] { false, true });
evaluateQuery(v.query(), RELATION, numV * 4, 1, new boolean[] { true, true });
evaluateQuery(v.query().direction(OUT), RELATION, numV * 4, 1, new boolean[] { false, true });
// Update in transaction
for (Object o : v.query().labels("name").properties()) {
JanusGraphVertexProperty<String> p = (JanusGraphVertexProperty<String>) o;
if (p.<Long>value("time") < (numV / 2))
p.remove();
}
for (JanusGraphEdge o : v.query().direction(BOTH).edges()) {
if (o.<Long>value("time") < (numV / 2))
o.remove();
}
ns = new JanusGraphVertex[numV * 3 / 2];
for (int i = numV; i < numV * 3 / 2; i++) {
double w = (i * 0.5) % 5;
v.property("name", "v" + i, "weight", w, "time", (long) i);
ns[i] = tx.addVertex();
for (String label : new String[] { "connect", "child", "link" }) {
JanusGraphEdge e = v.addEdge(label, ns[i], "weight", w, "time", (long) i);
}
}
// ######### UPDATED QUERIES ##########
evaluateQuery(v.query().keys("name").has("weight", Cmp.GREATER_THAN, 3.6), PROPERTY, 2 * numV / 10, 1, new boolean[] { false, true });
evaluateQuery(v.query().keys("name").interval("time", numV / 2 - 10, numV / 2 + 10), PROPERTY, 10, 1, new boolean[] { false, true });
evaluateQuery(v.query().keys("name").interval("time", numV / 2 - 10, numV / 2 + 10).orderBy("weight", desc), PROPERTY, 10, 1, new boolean[] { false, false }, weight, Order.DESC);
evaluateQuery(v.query().keys("name").interval("time", numV, numV + 10).limit(5), PROPERTY, 5, 1, new boolean[] { false, true });
evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, 5), EDGE, 0, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, numV + 5), EDGE, 1, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("child").direction(OUT).interval("time", 10, 20).orderBy("weight", desc).limit(5), EDGE, 0, 1, new boolean[] { true, false }, weight, Order.DESC);
evaluateQuery(v.query().labels("child").direction(OUT).interval("time", numV + 10, numV + 20).orderBy("weight", desc).limit(5), EDGE, 5, 1, new boolean[] { true, false }, weight, Order.DESC);
evaluateQuery(v.query(), RELATION, numV * 4, 1, new boolean[] { true, true });
evaluateQuery(v.query().direction(OUT), RELATION, numV * 4, 1, new boolean[] { false, true });
// ######### END UPDATED QUERIES ##########
newTx();
weight = tx.getPropertyKey("weight");
time = tx.getPropertyKey("time");
v = getV(tx, v);
u = getV(tx, u);
// ######### UPDATED QUERIES (copied from above) ##########
evaluateQuery(v.query().keys("name").has("weight", Cmp.GREATER_THAN, 3.6), PROPERTY, 2 * numV / 10, 1, new boolean[] { false, true });
evaluateQuery(v.query().keys("name").interval("time", numV / 2 - 10, numV / 2 + 10), PROPERTY, 10, 1, new boolean[] { false, true });
evaluateQuery(v.query().keys("name").interval("time", numV / 2 - 10, numV / 2 + 10).orderBy("weight", desc), PROPERTY, 10, 1, new boolean[] { false, false }, weight, Order.DESC);
evaluateQuery(v.query().keys("name").interval("time", numV, numV + 10).limit(5), PROPERTY, 5, 1, new boolean[] { false, true });
evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, 5), EDGE, 0, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, numV + 5), EDGE, 1, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("child").direction(OUT).interval("time", 10, 20).orderBy("weight", desc).limit(5), EDGE, 0, 1, new boolean[] { true, false }, weight, Order.DESC);
evaluateQuery(v.query().labels("child").direction(OUT).interval("time", numV + 10, numV + 20).orderBy("weight", desc).limit(5), EDGE, 5, 1, new boolean[] { true, false }, weight, Order.DESC);
evaluateQuery(v.query(), RELATION, numV * 4, 1, new boolean[] { true, true });
evaluateQuery(v.query().direction(OUT), RELATION, numV * 4, 1, new boolean[] { false, true });
// ######### END UPDATED QUERIES ##########
}
use of org.janusgraph.core.VertexList in project janusgraph by JanusGraph.
the class BasicVertexCentricQueryBuilder method executeVertexIds.
public VertexList executeVertexIds(InternalVertex vertex, BaseVertexCentricQuery baseQuery) {
if (isPartitionedVertex(vertex)) {
// If there is a sort order, we need to first merge the relations (and sort) and then compute vertices
if (!orders.isEmpty())
return edges2VertexIds((Iterable) executeRelations(vertex, baseQuery), vertex);
if (!hasAllCanonicalTypes()) {
InternalVertex[] representatives = tx.getAllRepresentatives(vertex, restrict2Partitions);
VertexListInternal merge = null;
for (InternalVertex rep : representatives) {
if (merge != null && merge.size() >= baseQuery.getLimit())
break;
VertexList vertexList = executeIndividualVertexIds(rep, baseQuery);
if (merge == null)
merge = (VertexListInternal) vertexList;
else
merge.addAll(vertexList);
}
if (merge != null && merge.size() > baseQuery.getLimit()) {
merge = (VertexListInternal) merge.subList(0, baseQuery.getLimit());
}
return merge;
} else
vertex = tx.getCanonicalVertex(vertex);
}
return executeIndividualVertexIds(vertex, baseQuery);
}
use of org.janusgraph.core.VertexList in project janusgraph by JanusGraph.
the class JanusGraphTest method testVertexCentricQuery.
public void testVertexCentricQuery(int noVertices) {
makeVertexIndexedUniqueKey("name", String.class);
PropertyKey time = makeKey("time", Integer.class);
PropertyKey weight = makeKey("weight", Double.class);
PropertyKey number = makeKey("number", Long.class);
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("connect")).sortKey(time).make();
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("connectDesc")).sortKey(time).sortOrder(Order.DESC).make();
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("friend")).sortKey(weight, time).sortOrder(Order.ASC).signature(number).make();
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("friendDesc")).sortKey(weight, time).sortOrder(Order.DESC).signature(number).make();
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("knows")).sortKey(number, weight).make();
mgmt.makeEdgeLabel("follows").make();
finishSchema();
JanusGraphVertex v = tx.addVertex("name", "v");
JanusGraphVertex u = tx.addVertex("name", "u");
assertEquals(0, (noVertices - 1) % 3);
JanusGraphVertex[] vs = new JanusGraphVertex[noVertices];
for (int i = 0; i < noVertices; i++) {
vs[i] = tx.addVertex("name", "v" + i);
}
EdgeLabel[] labelsV = { tx.getEdgeLabel("connect"), tx.getEdgeLabel("friend"), tx.getEdgeLabel("knows") };
EdgeLabel[] labelsU = { tx.getEdgeLabel("connectDesc"), tx.getEdgeLabel("friendDesc"), tx.getEdgeLabel("knows") };
for (int i = 1; i < noVertices; i++) {
for (final JanusGraphVertex vertex : new JanusGraphVertex[] { v, u }) {
for (final Direction d : new Direction[] { OUT, IN }) {
EdgeLabel label = vertex == v ? labelsV[i % 3] : labelsU[i % 3];
JanusGraphEdge e = d == OUT ? vertex.addEdge(n(label), vs[i]) : vs[i].addEdge(n(label), vertex);
e.property("time", i);
e.property("weight", i % 4 + 0.5);
e.property("name", "e" + i);
e.property("number", i % 5);
}
}
}
int edgesPerLabel = noVertices / 3;
VertexList vl;
Map<JanusGraphVertex, Iterable<JanusGraphEdge>> results;
Map<JanusGraphVertex, Iterable<JanusGraphVertexProperty>> results2;
JanusGraphVertex[] qvs;
int lastTime;
Iterator<? extends Edge> outer;
clopen();
long[] vertexIdSubset = new long[31 - 3];
for (int i = 0; i < vertexIdSubset.length; i++) vertexIdSubset[i] = vs[i + 3].longId();
Arrays.sort(vertexIdSubset);
// ##################################################
// Queries from Cache
// ##################################################
clopen();
for (int i = 1; i < noVertices; i++) vs[i] = getV(tx, vs[i].longId());
v = getV(tx, v.longId());
u = getV(tx, u.longId());
qvs = new JanusGraphVertex[] { vs[6], vs[9], vs[12], vs[15], vs[60] };
// To trigger queries from cache (don't copy!!!)
assertCount(2 * (noVertices - 1), v.query().direction(Direction.BOTH).edges());
assertEquals(1, v.query().propertyCount());
assertEquals(10, Iterables.size(v.query().labels("connect").limit(10).vertices()));
assertEquals(10, Iterables.size(u.query().labels("connectDesc").limit(10).vertices()));
assertEquals(10, Iterables.size(v.query().labels("connect").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
assertEquals(10, Iterables.size(u.query().labels("connectDesc").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
lastTime = 0;
for (final JanusGraphEdge e : v.query().labels("connect").direction(OUT).limit(20).edges()) {
int nowTime = e.value("time");
assertTrue(lastTime <= nowTime, lastTime + " vs. " + nowTime);
lastTime = nowTime;
}
lastTime = Integer.MAX_VALUE;
for (final Edge e : u.query().labels("connectDesc").direction(OUT).limit(20).edges()) {
int nowTime = e.value("time");
assertTrue(lastTime >= nowTime, lastTime + " vs. " + nowTime);
lastTime = nowTime;
}
assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
assertEquals(10, Iterables.size(u.query().labels("connectDesc").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
outer = v.query().labels("connect").direction(OUT).limit(20).edges().iterator();
for (final Edge e : v.query().labels("connect").direction(OUT).limit(10).edges()) {
assertEquals(e, outer.next());
}
evaluateQuery(v.query().labels("connect").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("connect").direction(OUT).has("time", 15).has("weight", 3.5), EDGE, 1, 1, new boolean[] { false, true });
evaluateQuery(u.query().labels("connectDesc").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
assertEquals(10, v.query().labels("connect").direction(IN).interval("time", 3, 31).edgeCount());
assertEquals(10, u.query().labels("connectDesc").direction(IN).interval("time", 3, 31).edgeCount());
assertEquals(0, v.query().labels("connect").direction(OUT).has("time", null).edgeCount());
assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertexIds().size());
assertEquals(edgesPerLabel - 10, v.query().labels("connect").direction(OUT).has("time", Cmp.GREATER_THAN, 31).count());
assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertices()));
assertEquals(3, v.query().labels("friend").direction(OUT).limit(3).count());
evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", 0.5).limit(3), EDGE, 3, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5), EDGE, 3, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", Contain.IN, ImmutableList.of(0.5)), EDGE, 3, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5, 2.5)).interval("time", 3, 33), EDGE, 7, 3, new boolean[] { true, true });
int friendsWhoHaveOutEdgesWithWeightBetweenPointFiveAndOnePointFive = (int) Math.round(Math.ceil(1667 * noVertices / 10000.0));
evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5)), EDGE, friendsWhoHaveOutEdgesWithWeightBetweenPointFiveAndOnePointFive, 2, new boolean[] { true, true });
assertEquals(3, u.query().labels("friendDesc").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
assertEquals(1, v.query().labels("friend").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
assertEquals(1, u.query().labels("friendDesc").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
assertEquals(4, v.query().labels("friend").direction(OUT).has("time", Cmp.LESS_THAN_EQUAL, 10).edgeCount());
assertEquals(2, v.query().labels("friend").direction(OUT).has("time", Cmp.LESS_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN_EQUAL, 5).edgeCount());
assertEquals(edgesPerLabel - 4, v.query().labels("friend").direction(OUT).has("time", Cmp.GREATER_THAN, 10).edgeCount());
assertEquals(20, v.query().labels("friend", "connect").direction(OUT).interval("time", 3, 33).edgeCount());
assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).edgeCount());
assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 4.0).edgeCount());
assertEquals((int) Math.ceil(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 2.0).edgeCount());
assertEquals((int) Math.floor(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 2.1, 4.0).edgeCount());
assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertices()));
assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertexIds()));
assertEquals(30, v.query().labels("friend", "connect", "knows").direction(OUT).interval("time", 3, 33).edgeCount());
assertEquals(noVertices - 2, v.query().labels("friend", "connect", "knows").direction(OUT).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
assertEquals(0, v.query().has("age", null).labels("undefined").direction(OUT).edgeCount());
assertEquals(1, v.query().labels("connect").direction(OUT).adjacent(vs[6]).has("time", 6).edgeCount());
assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).edgeCount());
assertEquals(1, v.query().labels("knows").direction(IN).adjacent(vs[11]).edgeCount());
assertEquals(2, v.query().labels("knows").direction(BOTH).adjacent(vs[11]).edgeCount());
assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).has("weight", 3.5).edgeCount());
assertEquals(2, v.query().labels("connect").adjacent(vs[6]).has("time", 6).edgeCount());
assertEquals(0, v.query().labels("connect").adjacent(vs[8]).has("time", 8).edgeCount());
assertEquals(2, v.query().labels().direction(BOTH).adjacent(vs[11]).edgeCount());
assertEquals(2, v.query().direction(BOTH).adjacent(vs[11]).edgeCount());
assertEquals(2, v.query().adjacent(vs[11]).edgeCount());
// v and vs[0] are not adjacent
assertEquals(0, v.query().adjacent(vs[0]).edgeCount());
assertEquals(0, v.query().labels().adjacent(vs[0]).edgeCount());
assertEquals(0, v.query().direction(BOTH).adjacent(vs[0]).edgeCount());
assertEquals(0, v.query().labels().direction(BOTH).adjacent(vs[0]).edgeCount());
assertEquals(edgesPerLabel, v.query().labels("connect").direction(OUT).edgeCount());
assertEquals(edgesPerLabel, v.query().labels("connect").direction(IN).edgeCount());
assertEquals(2 * edgesPerLabel, v.query().labels("connect").direction(BOTH).edgeCount());
assertEquals(edgesPerLabel, v.query().labels("connect").has("undefined", null).direction(OUT).edgeCount());
assertEquals(2 * (int) Math.ceil((noVertices - 1) / 4.0), Iterables.size(v.query().labels("connect", "friend", "knows").has("weight", 1.5).vertexIds()));
assertEquals(1, v.query().direction(IN).has("time", 1).edgeCount());
assertEquals(10, v.query().direction(OUT).interval("time", 4, 14).edgeCount());
assertEquals(9, v.query().direction(IN).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
assertEquals(9, v.query().direction(OUT).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
assertEquals(noVertices - 1, Iterables.size(v.query().direction(OUT).vertices()));
assertEquals(noVertices - 1, Iterables.size(v.query().direction(IN).vertices()));
for (final Direction dir : new Direction[] { IN, OUT }) {
vl = v.query().labels().direction(dir).interval("time", 3, 31).vertexIds();
vl.sort();
for (int i = 0; i < vl.size(); i++) assertEquals(vertexIdSubset[i], vl.getID(i));
}
assertCount(2 * (noVertices - 1), v.query().direction(Direction.BOTH).edges());
// Property queries
assertEquals(1, Iterables.size(v.query().properties()));
assertEquals(1, Iterables.size(v.query().keys("name").properties()));
// MultiQueries
results = tx.multiQuery(qvs).direction(IN).labels("connect").edges();
for (Iterable<JanusGraphEdge> result : results.values()) assertEquals(1, Iterables.size(result));
results = tx.multiQuery(Sets.newHashSet(qvs)).labels("connect").edges();
for (Iterable<JanusGraphEdge> result : results.values()) assertEquals(2, Iterables.size(result));
results = tx.multiQuery(qvs).labels("knows").edges();
for (Iterable<JanusGraphEdge> result : results.values()) assertEquals(0, Iterables.size(result));
results = tx.multiQuery(qvs).edges();
for (Iterable<JanusGraphEdge> result : results.values()) assertEquals(4, Iterables.size(result));
results2 = tx.multiQuery(qvs).properties();
for (Iterable<JanusGraphVertexProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
results2 = tx.multiQuery(qvs).keys("name").properties();
for (Iterable<JanusGraphVertexProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
// ##################################################
// Same queries as above but without memory loading (i.e. omitting the first query)
// ##################################################
clopen();
for (int i = 1; i < noVertices; i++) vs[i] = getV(tx, vs[i].longId());
v = getV(tx, v.longId());
u = getV(tx, u.longId());
qvs = new JanusGraphVertex[] { vs[6], vs[9], vs[12], vs[15], vs[60] };
assertEquals(10, Iterables.size(v.query().labels("connect").limit(10).vertices()));
assertEquals(10, Iterables.size(u.query().labels("connectDesc").limit(10).vertices()));
assertEquals(10, Iterables.size(v.query().labels("connect").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
assertEquals(10, Iterables.size(u.query().labels("connectDesc").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
lastTime = 0;
for (final Edge e : v.query().labels("connect").direction(OUT).limit(20).edges()) {
int nowTime = e.value("time");
assertTrue(lastTime <= nowTime, lastTime + " vs. " + nowTime);
lastTime = nowTime;
}
lastTime = Integer.MAX_VALUE;
for (final Edge e : u.query().labels("connectDesc").direction(OUT).limit(20).edges()) {
int nowTime = e.value("time");
assertTrue(lastTime >= nowTime, lastTime + " vs. " + nowTime);
lastTime = nowTime;
}
assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
assertEquals(10, Iterables.size(u.query().labels("connectDesc").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
outer = v.query().labels("connect").direction(OUT).limit(20).edges().iterator();
for (final Edge e : v.query().labels("connect").direction(OUT).limit(10).edges()) {
assertEquals(e, outer.next());
}
evaluateQuery(v.query().labels("connect").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("connect").direction(OUT).has("time", 15).has("weight", 3.5), EDGE, 1, 1, new boolean[] { false, true });
evaluateQuery(u.query().labels("connectDesc").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
assertEquals(10, v.query().labels("connect").direction(IN).interval("time", 3, 31).edgeCount());
assertEquals(10, u.query().labels("connectDesc").direction(IN).interval("time", 3, 31).edgeCount());
assertEquals(0, v.query().labels("connect").direction(OUT).has("time", null).edgeCount());
assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertexIds().size());
assertEquals(edgesPerLabel - 10, v.query().labels("connect").direction(OUT).has("time", Cmp.GREATER_THAN, 31).count());
assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertices()));
assertEquals(3, v.query().labels("friend").direction(OUT).limit(3).count());
evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", 0.5).limit(3), EDGE, 3, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5), EDGE, 3, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", Contain.IN, ImmutableList.of(0.5)), EDGE, 3, 1, new boolean[] { true, true });
evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5, 2.5)).interval("time", 3, 33), EDGE, 7, 3, new boolean[] { true, true });
evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5)), EDGE, friendsWhoHaveOutEdgesWithWeightBetweenPointFiveAndOnePointFive, 2, new boolean[] { true, true });
assertEquals(3, u.query().labels("friendDesc").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
assertEquals(1, v.query().labels("friend").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
assertEquals(1, u.query().labels("friendDesc").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
assertEquals(4, v.query().labels("friend").direction(OUT).has("time", Cmp.LESS_THAN_EQUAL, 10).edgeCount());
assertEquals(edgesPerLabel - 4, v.query().labels("friend").direction(OUT).has("time", Cmp.GREATER_THAN, 10).edgeCount());
assertEquals(20, v.query().labels("friend", "connect").direction(OUT).interval("time", 3, 33).edgeCount());
assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).edgeCount());
assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 4.0).edgeCount());
assertEquals((int) Math.ceil(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 2.0).edgeCount());
assertEquals((int) Math.floor(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 2.1, 4.0).edgeCount());
assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertices()));
assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertexIds()));
assertEquals(30, v.query().labels("friend", "connect", "knows").direction(OUT).interval("time", 3, 33).edgeCount());
assertEquals(noVertices - 2, v.query().labels("friend", "connect", "knows").direction(OUT).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
assertEquals(0, v.query().has("age", null).labels("undefined").direction(OUT).edgeCount());
assertEquals(1, v.query().labels("connect").direction(OUT).adjacent(vs[6]).has("time", 6).edgeCount());
assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).edgeCount());
assertEquals(1, v.query().labels("knows").direction(IN).adjacent(vs[11]).edgeCount());
assertEquals(2, v.query().labels("knows").direction(BOTH).adjacent(vs[11]).edgeCount());
assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).has("weight", 3.5).edgeCount());
assertEquals(2, v.query().labels("connect").adjacent(vs[6]).has("time", 6).edgeCount());
assertEquals(0, v.query().labels("connect").adjacent(vs[8]).has("time", 8).edgeCount());
assertEquals(2, v.query().labels().direction(BOTH).adjacent(vs[11]).edgeCount());
assertEquals(2, v.query().direction(BOTH).adjacent(vs[11]).edgeCount());
assertEquals(2, v.query().adjacent(vs[11]).edgeCount());
// v and vs[0] are not adjacent
assertEquals(0, v.query().adjacent(vs[0]).edgeCount());
assertEquals(0, v.query().labels().adjacent(vs[0]).edgeCount());
assertEquals(0, v.query().direction(BOTH).adjacent(vs[0]).edgeCount());
assertEquals(0, v.query().labels().direction(BOTH).adjacent(vs[0]).edgeCount());
assertEquals(edgesPerLabel, v.query().labels("connect").direction(OUT).edgeCount());
assertEquals(edgesPerLabel, v.query().labels("connect").direction(IN).edgeCount());
assertEquals(2 * edgesPerLabel, v.query().labels("connect").direction(BOTH).edgeCount());
assertEquals(edgesPerLabel, v.query().labels("connect").has("undefined", null).direction(OUT).edgeCount());
assertEquals(2 * (int) Math.ceil((noVertices - 1) / 4.0), Iterables.size(v.query().labels("connect", "friend", "knows").has("weight", 1.5).vertexIds()));
assertEquals(1, v.query().direction(IN).has("time", 1).edgeCount());
assertEquals(10, v.query().direction(OUT).interval("time", 4, 14).edgeCount());
assertEquals(9, v.query().direction(IN).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
assertEquals(9, v.query().direction(OUT).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
assertEquals(noVertices - 1, Iterables.size(v.query().direction(OUT).vertices()));
assertEquals(noVertices - 1, Iterables.size(v.query().direction(IN).vertices()));
for (final Direction dir : new Direction[] { IN, OUT }) {
vl = v.query().labels().direction(dir).interval("time", 3, 31).vertexIds();
vl.sort();
for (int i = 0; i < vl.size(); i++) assertEquals(vertexIdSubset[i], vl.getID(i));
}
assertCount(2 * (noVertices - 1), v.query().direction(Direction.BOTH).edges());
// Property queries
assertEquals(1, Iterables.size(v.query().properties()));
assertEquals(1, Iterables.size(v.query().keys("name").properties()));
// MultiQueries
results = tx.multiQuery(qvs).direction(IN).labels("connect").edges();
for (final Iterable<JanusGraphEdge> result : results.values()) assertEquals(1, Iterables.size(result));
results = tx.multiQuery(Sets.newHashSet(qvs)).labels("connect").edges();
for (final Iterable<JanusGraphEdge> result : results.values()) assertEquals(2, Iterables.size(result));
results = tx.multiQuery(qvs).labels("knows").edges();
for (final Iterable<JanusGraphEdge> result : results.values()) assertEquals(0, Iterables.size(result));
results = tx.multiQuery(qvs).edges();
for (final Iterable<JanusGraphEdge> result : results.values()) assertEquals(4, Iterables.size(result));
results2 = tx.multiQuery(qvs).properties();
for (final Iterable<JanusGraphVertexProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
results2 = tx.multiQuery(qvs).keys("name").properties();
for (final Iterable<JanusGraphVertexProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
// ##################################################
// End copied queries
// ##################################################
newTx();
v = Iterables.getOnlyElement(tx.query().has("name", "v").vertices());
assertNotNull(v);
assertEquals(2, v.query().has("weight", 1.5).interval("time", 10, 30).limit(2).vertexIds().size());
assertEquals(10, v.query().has("weight", 1.5).interval("time", 10, 30).vertexIds().size());
newTx();
v = Iterables.getOnlyElement(tx.query().has("name", "v").vertices());
assertNotNull(v);
assertEquals(2, v.query().has("weight", 1.5).interval("time", 10, 30).limit(2).edgeCount());
assertEquals(10, v.query().has("weight", 1.5).interval("time", 10, 30).edgeCount());
newTx();
// Test partially new vertex queries
final JanusGraphVertex[] qvs2 = new JanusGraphVertex[qvs.length + 2];
qvs2[0] = tx.addVertex();
for (int i = 0; i < qvs.length; i++) qvs2[i + 1] = getV(tx, qvs[i].longId());
qvs2[qvs2.length - 1] = tx.addVertex();
qvs2[0].addEdge("connect", qvs2[qvs2.length - 1]);
qvs2[qvs2.length - 1].addEdge("connect", qvs2[0]);
results = tx.multiQuery(qvs2).direction(IN).labels("connect").edges();
for (final Iterable<JanusGraphEdge> result : results.values()) assertEquals(1, Iterables.size(result));
}
use of org.janusgraph.core.VertexList in project janusgraph by JanusGraph.
the class JanusGraphPartitionGraphTest method testPartitionSpread.
private void testPartitionSpread(boolean flush, boolean batchCommit) {
Object[] options = { option(GraphDatabaseConfiguration.IDS_FLUSH), flush };
clopen(options);
int[] groupDegrees = { 10, 15, 10, 17, 10, 4, 7, 20, 11 };
int numVertices = setupGroupClusters(groupDegrees, batchCommit ? CommitMode.BATCH : CommitMode.PER_VERTEX);
// to track the "spread" of partition ids
IntSet partitionIds = new IntHashSet(numVertices);
for (int i = 0; i < groupDegrees.length; i++) {
JanusGraphVertex g = getOnlyVertex(tx.query().has("groupid", "group" + i));
assertCount(groupDegrees[i], g.edges(Direction.OUT, "contain"));
assertCount(groupDegrees[i], g.edges(Direction.IN, "member"));
assertCount(groupDegrees[i], g.query().direction(Direction.OUT).edges());
assertCount(groupDegrees[i], g.query().direction(Direction.IN).edges());
assertCount(groupDegrees[i] * 2, g.query().edges());
for (JanusGraphVertex o : g.query().direction(Direction.IN).labels("member").vertices()) {
int pid = getPartitionID(o);
partitionIds.add(pid);
assertEquals(g, getOnlyElement(o.query().direction(Direction.OUT).labels("member").vertices()));
VertexList vertexList = o.query().direction(Direction.IN).labels("contain").vertexIds();
assertEquals(1, vertexList.size());
assertEquals(pid, idManager.getPartitionId(vertexList.getID(0)));
assertEquals(g, vertexList.get(0));
}
}
if (flush || !batchCommit) {
// In these cases we would expect significant spread across partitions
// This is a probabilistic test that might fail
assertTrue(partitionIds.size() > numPartitions / 2);
} else {
// No spread in this case
assertEquals(1, partitionIds.size());
}
}
use of org.janusgraph.core.VertexList in project janusgraph by JanusGraph.
the class JanusGraphPartitionGraphTest method testVertexPartitioning.
@Test
public void testVertexPartitioning() {
Object[] options = { option(GraphDatabaseConfiguration.IDS_FLUSH), false };
clopen(options);
makeVertexIndexedUniqueKey("gid", Integer.class);
makeKey("sig", Integer.class);
mgmt.makePropertyKey("name").cardinality(Cardinality.LIST).dataType(String.class).make();
makeLabel("knows");
makeLabel("base");
mgmt.makeEdgeLabel("one").multiplicity(Multiplicity.ONE2ONE).make();
mgmt.makeVertexLabel("person").make();
mgmt.makeVertexLabel("group").partition().make();
finishSchema();
final Set<String> names = ImmutableSet.of("Marko", "Dan", "Stephen", "Daniel", "Josh", "Thad", "Pavel", "Matthias");
final int numG = 10;
final long[] gids = new long[numG];
for (int i = 0; i < numG; i++) {
JanusGraphVertex g = tx.addVertex("group");
g.property(VertexProperty.Cardinality.single, "gid", i);
g.property(VertexProperty.Cardinality.single, "sig", 0);
for (String n : names) {
g.property("name", n);
}
assertEquals(i, g.<Integer>value("gid").intValue());
assertEquals(0, g.<Integer>value("sig").intValue());
assertEquals("group", g.label());
assertCount(names.size(), g.properties("name"));
assertTrue(getId(g) > 0);
gids[i] = getId(g);
if (i > 0) {
g.addEdge("base", getV(tx, gids[0]));
}
if (i % 2 == 1) {
g.addEdge("one", getV(tx, gids[i - 1]));
}
}
for (int i = 0; i < numG; i++) {
JanusGraphVertex g = getV(tx, gids[i]);
assertCount(1, g.query().direction(Direction.BOTH).labels("one").edges());
assertCount(1, g.query().direction(i % 2 == 0 ? Direction.IN : Direction.OUT).labels("one").edges());
assertCount(0, g.query().direction(i % 2 == 1 ? Direction.IN : Direction.OUT).labels("one").edges());
if (i > 0) {
assertCount(1, g.query().direction(Direction.OUT).labels("base").edges());
} else {
assertCount(numG - 1, g.query().direction(Direction.IN).labels("base").edges());
}
}
newTx();
for (int i = 0; i < numG; i++) {
long gId = gids[i];
assertTrue(idManager.isPartitionedVertex(gId));
assertEquals(idManager.getCanonicalVertexId(gId), gId);
JanusGraphVertex g = getV(tx, gId);
final int canonicalPartition = getPartitionID(g);
assertEquals(g, getOnlyElement(tx.query().has("gid", i).vertices()));
assertEquals(i, g.<Integer>value("gid").intValue());
assertCount(names.size(), g.properties("name"));
// Verify that properties are distributed correctly
JanusGraphVertexProperty p = (JanusGraphVertexProperty) getOnlyElement(g.properties("gid"));
assertEquals(canonicalPartition, getPartitionID(p));
for (Iterator<VertexProperty<Object>> niter = g.properties("name"); niter.hasNext(); ) {
assertEquals(canonicalPartition, getPartitionID((JanusGraphVertex) niter.next().element()));
}
// Copied from above
assertCount(1, g.query().direction(Direction.BOTH).labels("one").edges());
assertCount(1, g.query().direction(i % 2 == 0 ? Direction.IN : Direction.OUT).labels("one").edges());
assertCount(0, g.query().direction(i % 2 == 1 ? Direction.IN : Direction.OUT).labels("one").edges());
if (i > 0) {
assertCount(1, g.query().direction(Direction.OUT).labels("base").edges());
} else {
assertCount(numG - 1, g.query().direction(Direction.IN).labels("base").edges());
}
}
clopen(options);
final int numTx = 100;
final int vPerTx = 10;
Multiset<Integer> partitions = HashMultiset.create();
for (int t = 1; t <= numTx; t++) {
JanusGraphVertex g1 = getV(tx, gids[0]), g2 = getV(tx, gids[1]);
assertNotNull(g1);
JanusGraphVertex[] vs = new JanusGraphVertex[vPerTx];
for (int vi = 0; vi < vPerTx; vi++) {
vs[vi] = tx.addVertex("person");
vs[vi].property(VertexProperty.Cardinality.single, "sig", t);
Edge e = vs[vi].addEdge("knows", g1);
e.property("sig", t);
e = g1.addEdge("knows", vs[vi]);
e.property("sig", t);
if (vi % 2 == 0) {
e = vs[vi].addEdge("knows", g2);
e.property("sig", t);
}
}
newTx();
// Verify that all elements are in the same partition
JanusGraphTransaction txx = graph.buildTransaction().readOnly().start();
g1 = getV(tx, gids[0]);
g2 = getV(tx, gids[1]);
int partition = -1;
for (int vi = 0; vi < vPerTx; vi++) {
assertTrue(vs[vi].hasId());
int pid = getPartitionID(vs[vi]);
if (partition < 0)
partition = pid;
else
assertEquals(partition, pid);
int numRelations = 0;
JanusGraphVertex v = getV(txx, vs[vi].longId());
for (JanusGraphRelation r : v.query().relations()) {
numRelations++;
assertEquals(partition, getPartitionID(r));
if (r instanceof JanusGraphEdge) {
JanusGraphVertex o = ((JanusGraphEdge) r).otherVertex(v);
assertTrue(o.equals(g1) || o.equals(g2));
}
}
assertEquals(3 + (vi % 2 == 0 ? 1 : 0), numRelations);
}
partitions.add(partition);
txx.commit();
}
// Verify spread across partitions; this number is a pessimistic lower bound but might fail since it is probabilistic
//
assertTrue(partitions.elementSet().size() >= 3);
newTx();
// Verify edge querying across partitions
JanusGraphVertex g1 = getV(tx, gids[0]);
assertEquals(0, g1.<Integer>value("gid").intValue());
assertEquals("group", g1.label());
assertCount(names.size(), g1.properties("name"));
assertCount(numTx * vPerTx, g1.query().direction(Direction.OUT).labels("knows").edges());
assertCount(numTx * vPerTx, g1.query().direction(Direction.IN).labels("knows").edges());
assertCount(numTx * vPerTx * 2, g1.query().direction(Direction.BOTH).labels("knows").edges());
assertCount(numTx * vPerTx + numG, tx.query().vertices());
newTx();
// Restrict to partitions
for (int t = 0; t < 10; t++) {
int numP = random.nextInt(3) + 1;
Set<Integer> parts = Sets.newHashSet();
int numV = 0;
while (parts.size() < numP) {
int part = Iterables.get(partitions.elementSet(), random.nextInt(partitions.elementSet().size()));
if (parts.add(part))
numV += partitions.count(part);
}
numV *= vPerTx;
int[] partitionArray = new int[numP];
int i = 0;
for (Integer part : parts) partitionArray[i++] = part;
JanusGraphTransaction tx2 = graph.buildTransaction().restrictedPartitions(partitionArray).readOnly().start();
// Copied from above
g1 = getV(tx2, gids[0]);
assertEquals(0, g1.<Integer>value("gid").intValue());
assertEquals("group", g1.label());
assertTrue(names.size() >= Iterators.size(g1.properties("name")));
assertCount(numV, g1.query().direction(Direction.OUT).labels("knows").edges());
assertCount(numV, g1.query().direction(Direction.IN).labels("knows").edges());
assertCount(numV * 2, g1.query().direction(Direction.BOTH).labels("knows").edges());
// Test local intersection
JanusGraphVertex g2 = getV(tx2, gids[1]);
VertexList v1 = g1.query().direction(Direction.IN).labels("knows").vertexIds();
VertexList v2 = g2.query().direction(Direction.IN).labels("knows").vertexIds();
assertEquals(numV, v1.size());
assertEquals(numV / 2, v2.size());
v1.sort();
v2.sort();
LongArrayList al1 = v1.getIDs();
LongArrayList al2 = v2.getIDs();
assertTrue(AbstractLongListUtil.isSorted(al1));
assertTrue(AbstractLongListUtil.isSorted(al2));
LongArrayList alr = AbstractLongListUtil.mergeJoin(al1, al2, false);
assertEquals(numV / 2, alr.size());
tx2.commit();
}
}
Aggregations