use of org.apache.tinkerpop.gremlin.structure.Direction in project janusgraph by JanusGraph.
the class IndexRemoveJob method getQueries.
@Override
public List<SliceQuery> getQueries() {
if (isGlobalGraphIndex()) {
// Everything
return Collections.singletonList(new SliceQuery(BufferUtil.zeroBuffer(1), BufferUtil.oneBuffer(128)));
} else {
RelationTypeIndexWrapper wrapper = (RelationTypeIndexWrapper) index;
InternalRelationType wrappedType = wrapper.getWrappedType();
Direction direction = null;
for (Direction dir : Direction.values()) if (wrappedType.isUnidirected(dir))
direction = dir;
assert direction != null;
StandardJanusGraphTx tx = (StandardJanusGraphTx) graph.get().buildTransaction().readOnly().start();
try {
QueryContainer qc = new QueryContainer(tx);
qc.addQuery().type(wrappedType).direction(direction).relations();
return qc.getSliceQueries();
} finally {
tx.rollback();
}
}
}
use of org.apache.tinkerpop.gremlin.structure.Direction 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.apache.tinkerpop.gremlin.structure.Direction in project janusgraph by JanusGraph.
the class JanusGraphTest method simpleLogTest.
public void simpleLogTest(final boolean withLogFailure) throws InterruptedException {
final String userLogName = "test";
final Serializer serializer = graph.getDataSerializer();
final EdgeSerializer edgeSerializer = graph.getEdgeSerializer();
final TimestampProvider times = graph.getConfiguration().getTimestampProvider();
final Instant startTime = times.getTime();
clopen(option(SYSTEM_LOG_TRANSACTIONS), true, option(LOG_BACKEND, USER_LOG), (withLogFailure ? TestMockLog.class.getName() : LOG_BACKEND.getDefaultValue()), option(TestMockLog.LOG_MOCK_FAILADD, USER_LOG), withLogFailure, option(KCVSLog.LOG_READ_LAG_TIME, USER_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, USER_LOG), Duration.ofMillis(250), option(LOG_SEND_DELAY, USER_LOG), Duration.ofMillis(100), option(KCVSLog.LOG_READ_LAG_TIME, TRANSACTION_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, TRANSACTION_LOG), Duration.ofMillis(250), option(MAX_COMMIT_TIME), Duration.ofSeconds(1));
final String instanceId = graph.getConfiguration().getUniqueGraphId();
PropertyKey weight = tx.makePropertyKey("weight").dataType(Float.class).cardinality(Cardinality.SINGLE).make();
EdgeLabel knows = tx.makeEdgeLabel("knows").make();
JanusGraphVertex n1 = tx.addVertex("weight", 10.5);
tx.addProperties(knows, weight);
newTx();
final Instant[] txTimes = new Instant[4];
// Transaction with custom user log name
txTimes[0] = times.getTime();
JanusGraphTransaction tx2 = graph.buildTransaction().logIdentifier(userLogName).start();
JanusGraphVertex v1 = tx2.addVertex("weight", 111.1);
v1.addEdge("knows", v1);
tx2.commit();
final long v1id = getId(v1);
txTimes[1] = times.getTime();
tx2 = graph.buildTransaction().logIdentifier(userLogName).start();
JanusGraphVertex v2 = tx2.addVertex("weight", 222.2);
v2.addEdge("knows", getV(tx2, v1id));
tx2.commit();
final long v2id = getId(v2);
// Only read tx
tx2 = graph.buildTransaction().logIdentifier(userLogName).start();
v1 = getV(tx2, v1id);
assertEquals(111.1, v1.<Float>value("weight").doubleValue(), 0.01);
assertEquals(222.2, getV(tx2, v2).<Float>value("weight").doubleValue(), 0.01);
tx2.commit();
// Deleting transaction
txTimes[2] = times.getTime();
tx2 = graph.buildTransaction().logIdentifier(userLogName).start();
v2 = getV(tx2, v2id);
assertEquals(222.2, v2.<Float>value("weight").doubleValue(), 0.01);
v2.remove();
tx2.commit();
// Edge modifying transaction
txTimes[3] = times.getTime();
tx2 = graph.buildTransaction().logIdentifier(userLogName).start();
v1 = getV(tx2, v1id);
assertEquals(111.1, v1.<Float>value("weight").doubleValue(), 0.01);
final Edge e = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("knows").edges());
assertFalse(e.property("weight").isPresent());
e.property("weight", 44.4);
tx2.commit();
close();
final Instant endTime = times.getTime();
final ReadMarker startMarker = ReadMarker.fromTime(startTime);
final Log transactionLog = openTxLog();
final Log userLog = openUserLog(userLogName);
final EnumMap<LogTxStatus, AtomicInteger> txMsgCounter = new EnumMap<>(LogTxStatus.class);
for (final LogTxStatus status : LogTxStatus.values()) txMsgCounter.put(status, new AtomicInteger(0));
final AtomicInteger userLogMeta = new AtomicInteger(0);
transactionLog.registerReader(startMarker, new MessageReader() {
@Override
public void read(Message message) {
final Instant msgTime = message.getTimestamp();
assertTrue(msgTime.isAfter(startTime) || msgTime.equals(startTime));
assertNotNull(message.getSenderId());
final TransactionLogHeader.Entry txEntry = TransactionLogHeader.parse(message.getContent(), serializer, times);
final TransactionLogHeader header = txEntry.getHeader();
// System.out.println(header.getTimestamp(TimeUnit.MILLISECONDS));
assertTrue(header.getTimestamp().isAfter(startTime) || header.getTimestamp().equals(startTime));
assertTrue(header.getTimestamp().isBefore(msgTime) || header.getTimestamp().equals(msgTime));
assertNotNull(txEntry.getMetadata());
assertNull(txEntry.getMetadata().get(LogTxMeta.GROUPNAME));
final LogTxStatus status = txEntry.getStatus();
if (status == LogTxStatus.PRECOMMIT) {
assertTrue(txEntry.hasContent());
final Object logId = txEntry.getMetadata().get(LogTxMeta.LOG_ID);
if (logId != null) {
assertTrue(logId instanceof String);
assertEquals(userLogName, logId);
userLogMeta.incrementAndGet();
}
} else if (withLogFailure) {
assertTrue(status.isPrimarySuccess() || status == LogTxStatus.SECONDARY_FAILURE);
if (status == LogTxStatus.SECONDARY_FAILURE) {
final TransactionLogHeader.SecondaryFailures secFail = txEntry.getContentAsSecondaryFailures(serializer);
assertTrue(secFail.failedIndexes.isEmpty());
assertTrue(secFail.userLogFailure);
}
} else {
assertFalse(txEntry.hasContent());
assertTrue(status.isSuccess());
}
txMsgCounter.get(txEntry.getStatus()).incrementAndGet();
}
@Override
public void updateState() {
}
});
final EnumMap<Change, AtomicInteger> userChangeCounter = new EnumMap<>(Change.class);
for (final Change change : Change.values()) userChangeCounter.put(change, new AtomicInteger(0));
final AtomicInteger userLogMsgCounter = new AtomicInteger(0);
userLog.registerReader(startMarker, new MessageReader() {
@Override
public void read(Message message) {
final Instant msgTime = message.getTimestamp();
assertTrue(msgTime.isAfter(startTime) || msgTime.equals(startTime));
assertNotNull(message.getSenderId());
final StaticBuffer content = message.getContent();
assertTrue(content != null && content.length() > 0);
final TransactionLogHeader.Entry transactionEntry = TransactionLogHeader.parse(content, serializer, times);
final Instant txTime = transactionEntry.getHeader().getTimestamp();
assertTrue(txTime.isBefore(msgTime) || txTime.equals(msgTime));
assertTrue(txTime.isAfter(startTime) || txTime.equals(msgTime));
final long transactionId = transactionEntry.getHeader().getId();
assertTrue(transactionId > 0);
transactionEntry.getContentAsModifications(serializer).forEach(modification -> {
assertTrue(modification.state == Change.ADDED || modification.state == Change.REMOVED);
userChangeCounter.get(modification.state).incrementAndGet();
});
userLogMsgCounter.incrementAndGet();
}
@Override
public void updateState() {
}
});
Thread.sleep(4000);
assertEquals(5, txMsgCounter.get(LogTxStatus.PRECOMMIT).get());
assertEquals(4, txMsgCounter.get(LogTxStatus.PRIMARY_SUCCESS).get());
assertEquals(1, txMsgCounter.get(LogTxStatus.COMPLETE_SUCCESS).get());
assertEquals(4, userLogMeta.get());
if (withLogFailure)
assertEquals(4, txMsgCounter.get(LogTxStatus.SECONDARY_FAILURE).get());
else
assertEquals(4, txMsgCounter.get(LogTxStatus.SECONDARY_SUCCESS).get());
// User-Log
if (withLogFailure) {
assertEquals(0, userLogMsgCounter.get());
} else {
assertEquals(4, userLogMsgCounter.get());
assertEquals(7, userChangeCounter.get(Change.ADDED).get());
assertEquals(4, userChangeCounter.get(Change.REMOVED).get());
}
clopen(option(VERBOSE_TX_RECOVERY), true);
/*
Transaction Recovery
*/
final TransactionRecovery recovery = JanusGraphFactory.startTransactionRecovery(graph, startTime);
/*
Use user log processing framework
*/
final AtomicInteger userLogCount = new AtomicInteger(0);
final LogProcessorFramework userLogs = JanusGraphFactory.openTransactionLog(graph);
userLogs.addLogProcessor(userLogName).setStartTime(startTime).setRetryAttempts(1).addProcessor((tx, txId, changes) -> {
assertEquals(instanceId, txId.getInstanceId());
// Just some reasonable upper bound
assertTrue(txId.getTransactionId() > 0 && txId.getTransactionId() < 100);
final Instant txTime = txId.getTransactionTime();
// Times should be within a second
assertTrue((txTime.isAfter(startTime) || txTime.equals(startTime)) && (txTime.isBefore(endTime) || txTime.equals(endTime)), String.format("tx timestamp %s not between start %s and end time %s", txTime, startTime, endTime));
assertTrue(tx.containsRelationType("knows"));
assertTrue(tx.containsRelationType("weight"));
final EdgeLabel knows1 = tx.getEdgeLabel("knows");
final PropertyKey weight1 = tx.getPropertyKey("weight");
Instant txTimeMicro = txId.getTransactionTime();
int txNo;
if (txTimeMicro.isBefore(txTimes[1])) {
txNo = 1;
// v1 addition transaction
assertEquals(1, Iterables.size(changes.getVertices(Change.ADDED)));
assertEquals(0, Iterables.size(changes.getVertices(Change.REMOVED)));
assertEquals(1, Iterables.size(changes.getVertices(Change.ANY)));
assertEquals(2, Iterables.size(changes.getRelations(Change.ADDED)));
assertEquals(1, Iterables.size(changes.getRelations(Change.ADDED, knows1)));
assertEquals(1, Iterables.size(changes.getRelations(Change.ADDED, weight1)));
assertEquals(2, Iterables.size(changes.getRelations(Change.ANY)));
assertEquals(0, Iterables.size(changes.getRelations(Change.REMOVED)));
final JanusGraphVertex v = Iterables.getOnlyElement(changes.getVertices(Change.ADDED));
assertEquals(v1id, getId(v));
final VertexProperty<Float> p = Iterables.getOnlyElement(changes.getProperties(v, Change.ADDED, "weight"));
assertEquals(111.1, p.value().doubleValue(), 0.01);
assertEquals(1, Iterables.size(changes.getEdges(v, Change.ADDED, OUT)));
assertEquals(1, Iterables.size(changes.getEdges(v, Change.ADDED, BOTH)));
} else if (txTimeMicro.isBefore(txTimes[2])) {
txNo = 2;
// v2 addition transaction
assertEquals(1, Iterables.size(changes.getVertices(Change.ADDED)));
assertEquals(0, Iterables.size(changes.getVertices(Change.REMOVED)));
assertEquals(2, Iterables.size(changes.getVertices(Change.ANY)));
assertEquals(2, Iterables.size(changes.getRelations(Change.ADDED)));
assertEquals(1, Iterables.size(changes.getRelations(Change.ADDED, knows1)));
assertEquals(1, Iterables.size(changes.getRelations(Change.ADDED, weight1)));
assertEquals(2, Iterables.size(changes.getRelations(Change.ANY)));
assertEquals(0, Iterables.size(changes.getRelations(Change.REMOVED)));
final JanusGraphVertex v = Iterables.getOnlyElement(changes.getVertices(Change.ADDED));
assertEquals(v2id, getId(v));
final VertexProperty<Float> p = Iterables.getOnlyElement(changes.getProperties(v, Change.ADDED, "weight"));
assertEquals(222.2, p.value().doubleValue(), 0.01);
assertEquals(1, Iterables.size(changes.getEdges(v, Change.ADDED, OUT)));
assertEquals(1, Iterables.size(changes.getEdges(v, Change.ADDED, BOTH)));
} else if (txTimeMicro.isBefore(txTimes[3])) {
txNo = 3;
// v2 deletion transaction
assertEquals(0, Iterables.size(changes.getVertices(Change.ADDED)));
assertEquals(1, Iterables.size(changes.getVertices(Change.REMOVED)));
assertEquals(2, Iterables.size(changes.getVertices(Change.ANY)));
assertEquals(0, Iterables.size(changes.getRelations(Change.ADDED)));
assertEquals(2, Iterables.size(changes.getRelations(Change.REMOVED)));
assertEquals(1, Iterables.size(changes.getRelations(Change.REMOVED, knows1)));
assertEquals(1, Iterables.size(changes.getRelations(Change.REMOVED, weight1)));
assertEquals(2, Iterables.size(changes.getRelations(Change.ANY)));
final JanusGraphVertex v = Iterables.getOnlyElement(changes.getVertices(Change.REMOVED));
assertEquals(v2id, getId(v));
final VertexProperty<Float> p = Iterables.getOnlyElement(changes.getProperties(v, Change.REMOVED, "weight"));
assertEquals(222.2, p.value().doubleValue(), 0.01);
assertEquals(1, Iterables.size(changes.getEdges(v, Change.REMOVED, OUT)));
assertEquals(0, Iterables.size(changes.getEdges(v, Change.ADDED, BOTH)));
} else {
txNo = 4;
// v1 edge modification
assertEquals(0, Iterables.size(changes.getVertices(Change.ADDED)));
assertEquals(0, Iterables.size(changes.getVertices(Change.REMOVED)));
assertEquals(1, Iterables.size(changes.getVertices(Change.ANY)));
assertEquals(1, Iterables.size(changes.getRelations(Change.ADDED)));
assertEquals(1, Iterables.size(changes.getRelations(Change.REMOVED)));
assertEquals(1, Iterables.size(changes.getRelations(Change.REMOVED, knows1)));
assertEquals(2, Iterables.size(changes.getRelations(Change.ANY)));
final JanusGraphVertex v = Iterables.getOnlyElement(changes.getVertices(Change.ANY));
assertEquals(v1id, getId(v));
JanusGraphEdge e1 = Iterables.getOnlyElement(changes.getEdges(v, Change.REMOVED, Direction.OUT, "knows"));
assertFalse(e1.property("weight").isPresent());
assertEquals(v, e1.vertex(Direction.IN));
e1 = Iterables.getOnlyElement(changes.getEdges(v, Change.ADDED, Direction.OUT, "knows"));
assertEquals(44.4, e1.<Float>value("weight").doubleValue(), 0.01);
assertEquals(v, e1.vertex(Direction.IN));
}
// See only current state of graph in transaction
final JanusGraphVertex v11 = getV(tx, v1id);
assertNotNull(v11);
assertTrue(v11.isLoaded());
if (txNo != 2) {
// In the transaction that adds v2, v2 will be considered "loaded"
assertMissing(tx, v2id);
// assertTrue(txNo + " - " + v2, v2 == null || v2.isRemoved());
}
assertEquals(111.1, v11.<Float>value("weight").doubleValue(), 0.01);
assertCount(1, v11.query().direction(Direction.OUT).edges());
userLogCount.incrementAndGet();
}).build();
// wait
Thread.sleep(22000L);
recovery.shutdown();
long[] recoveryStats = ((StandardTransactionLogProcessor) recovery).getStatistics();
if (withLogFailure) {
assertEquals(1, recoveryStats[0]);
assertEquals(4, recoveryStats[1]);
} else {
assertEquals(5, recoveryStats[0]);
assertEquals(0, recoveryStats[1]);
}
userLogs.removeLogProcessor(userLogName);
userLogs.shutdown();
assertEquals(4, userLogCount.get());
}
use of org.apache.tinkerpop.gremlin.structure.Direction in project janusgraph by JanusGraph.
the class JanusGraphTest method testBatchPropertiesPrefetchingFromEdges.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 10, 15, Integer.MAX_VALUE })
public void testBatchPropertiesPrefetchingFromEdges(int txCacheSize) {
boolean inmemoryBackend = getConfig().get(STORAGE_BACKEND).equals("inmemory");
int numV = 10;
int expectedVerticesPrefetch = Math.min(txCacheSize, 4);
JanusGraphVertex mainVertex = graph.addVertex("id", 0);
for (int i = 1; i <= numV; i++) {
JanusGraphVertex adjacentVertex = graph.addVertex("id", i);
mainVertex.addEdge("knows", adjacentVertex, "id", i);
}
graph.tx().commit();
if (!inmemoryBackend) {
clopen(option(BATCH_PROPERTY_PREFETCHING), true, option(TX_CACHE_SIZE), txCacheSize);
}
GraphTraversalSource gts = graph.traversal();
for (Direction direction : Direction.values()) {
GraphTraversal<Vertex, Edge> graphEdgeTraversal = gts.V().has("id", 0).outE("knows").has("id", P.within(4, 5, 6, 7));
GraphTraversal<Vertex, Vertex> graphVertexTraversal;
switch(direction) {
case IN:
graphVertexTraversal = graphEdgeTraversal.inV();
break;
case OUT:
graphVertexTraversal = graphEdgeTraversal.outV();
break;
case BOTH:
graphVertexTraversal = graphEdgeTraversal.bothV();
break;
default:
throw new NotImplementedException("No implementation found for direction: " + direction.name());
}
TraversalMetrics traversalMetrics = graphVertexTraversal.has("id", P.within(4, 5, 6, 7)).values("id").profile().next();
Metrics janusGraphEdgeVertexStepMetrics = getStepMetrics(traversalMetrics, JanusGraphEdgeVertexStep.class);
if (inmemoryBackend) {
assertNull(janusGraphEdgeVertexStepMetrics);
} else {
assertNotNull(janusGraphEdgeVertexStepMetrics);
assertTrue(janusGraphEdgeVertexStepMetrics.getName().endsWith("(" + direction.name() + ")"));
if (expectedVerticesPrefetch > 1 && !OUT.equals(direction)) {
assertContains(janusGraphEdgeVertexStepMetrics, "multiPreFetch", "true");
// 4 is the number of retrieved IN vertices
boolean withAdditionalOutVertex = BOTH.equals(direction) && txCacheSize > 4;
assertContains(janusGraphEdgeVertexStepMetrics, "vertices", expectedVerticesPrefetch + (withAdditionalOutVertex ? 1 : 0));
} else {
assertNotContains(janusGraphEdgeVertexStepMetrics, "multiPreFetch", "true");
}
}
}
}
use of org.apache.tinkerpop.gremlin.structure.Direction in project janusgraph by JanusGraph.
the class StandardJanusGraphTx method getUniquenessLock.
private TransactionLock getUniquenessLock(final JanusGraphVertex out, final InternalRelationType type, final Object in) {
Multiplicity multiplicity = type.multiplicity();
TransactionLock uniqueLock = FakeLock.INSTANCE;
if (config.hasVerifyUniqueness() && multiplicity.isConstrained()) {
uniqueLock = null;
if (multiplicity == Multiplicity.SIMPLE) {
uniqueLock = getLock(out, type, in);
} else {
for (Direction dir : Direction.proper) {
if (multiplicity.isUnique(dir)) {
TransactionLock lock = getLock(dir == Direction.OUT ? out : in, type, dir);
if (uniqueLock == null)
uniqueLock = lock;
else
uniqueLock = new CombinerLock(uniqueLock, lock, times);
}
}
}
}
assert uniqueLock != null;
return uniqueLock;
}
Aggregations