use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project sqlg by pietermartin.
the class TestHasLabelAndId method testOutEWithHasLabelAndId.
@Test
public void testOutEWithHasLabelAndId() {
Vertex a = this.sqlgGraph.addVertex(T.label, "A");
Vertex b = this.sqlgGraph.addVertex(T.label, "B");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
Vertex c = this.sqlgGraph.addVertex(T.label, "C");
Vertex c2 = this.sqlgGraph.addVertex(T.label, "C");
Vertex d = this.sqlgGraph.addVertex(T.label, "D");
Vertex d2 = this.sqlgGraph.addVertex(T.label, "D");
Edge ab = b.addEdge("ab", a);
Edge ab2 = b2.addEdge("ab", a);
Edge ac = c.addEdge("ac", a);
Edge ac2 = c2.addEdge("ac", a);
Edge ad = d.addEdge("ad", a);
Edge ad2 = d2.addEdge("ad", a);
this.sqlgGraph.tx().commit();
GraphTraversal<Edge, Vertex> traversal = this.sqlgGraph.traversal().E().hasLabel("ab").inV();
List<Vertex> vertices = traversal.toList();
Assert.assertEquals(2, vertices.size());
Assert.assertEquals(a, vertices.get(0));
Assert.assertEquals(a, vertices.get(1));
traversal = this.sqlgGraph.traversal().E().hasLabel("ab").inV().hasLabel("B");
vertices = traversal.toList();
Assert.assertEquals(0, vertices.size());
traversal = this.sqlgGraph.traversal().E().hasLabel("ab").inV().hasLabel("A");
vertices = traversal.toList();
Assert.assertEquals(2, vertices.size());
Assert.assertEquals(a, vertices.get(0));
Assert.assertEquals(a, vertices.get(1));
traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).inV().hasLabel("A", "B");
vertices = traversal.toList();
Assert.assertEquals(6, vertices.size());
Assert.assertTrue(vertices.stream().allMatch(v -> v.equals(a)));
traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).outV().hasLabel("C", "B");
vertices = traversal.toList();
Assert.assertEquals(4, vertices.size());
Assert.assertTrue(vertices.contains(b) && vertices.contains(b2) && vertices.contains(c) && vertices.contains(c2));
traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).outV().hasLabel("C", "B").has(T.id, P.eq(b));
vertices = traversal.toList();
Assert.assertEquals(1, vertices.size());
Assert.assertEquals(b, vertices.get(0));
traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).outV().hasLabel("C", "B").has(T.id, P.neq(b));
vertices = traversal.toList();
Assert.assertEquals(3, vertices.size());
Assert.assertTrue(vertices.contains(b2) && vertices.contains(c) && vertices.contains(c2));
traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).outV().hasLabel("C", "B").has(T.id, P.within(b.id(), b2.id(), c.id()));
vertices = traversal.toList();
Assert.assertEquals(3, vertices.size());
Assert.assertTrue(vertices.contains(b) && vertices.contains(b2) && vertices.contains(c));
traversal = this.sqlgGraph.traversal().E().has(T.label, P.within("ab", "ac", "ad")).outV().hasLabel("C", "B").has(T.id, P.without(b.id(), b2.id(), c.id()));
vertices = traversal.toList();
Assert.assertEquals(1, vertices.size());
Assert.assertEquals(c2, vertices.get(0));
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project janusgraph by JanusGraph.
the class JanusGraphMultiQueryStrategyTest method testMultiQuery.
@Test
public void testMultiQuery() {
clopen(option(USE_MULTIQUERY), true);
makeSampleGraph();
Traversal t = g.V(sv[0]).outE().inV().choose(__.inE("knows").has("weight", 0), __.inE("knows").has("weight", 1), __.inE("knows").has("weight", 2)).profile("~metrics");
assertNumStep(numV * 2, 2, (GraphTraversal) t, ChooseStep.class, JanusGraphVertexStep.class);
assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
t = g.V(sv[0]).outE().inV().union(__.inE("knows").has("weight", 0), __.inE("knows").has("weight", 1), __.inE("knows").has("weight", 2)).profile("~metrics");
assertNumStep(numV * 6, 2, (GraphTraversal) t, UnionStep.class, JanusGraphVertexStep.class);
assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
// repeat starts from vertex with id 0 and goes in to the sv[0] vertex then loops back out to the vertex with the next id
int[] loop = { 0 };
t = g.V(vs[0], vs[1], vs[2]).repeat(__.inE("knows").outV().hasId(sv[0].id()).out(// TINKERPOP-2342
"knows").sideEffect(e -> loop[0] = e.loops()).has("id", loop[0])).times(numV).profile("~metrics");
assertNumStep(3, 1, (GraphTraversal) t, RepeatStep.class);
assertEquals(numV - 1, loop[0]);
assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
t = g.V(vs[0], vs[1], vs[2]).optional(__.inE("knows").has("weight", 0)).profile("~metrics");
assertNumStep(12, 1, (GraphTraversal) t, OptionalStep.class);
assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
t = g.V(vs[0], vs[1], vs[2]).filter(__.inE("knows").has("weight", 0)).profile("~metrics");
assertNumStep(1, 1, (GraphTraversal) t, TraversalFilterStep.class);
assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
assertNumStep(superV * (numV / 5), 2, g.V().has("id", sid).outE("knows").has("weight", 1), JanusGraphStep.class, JanusGraphVertexStep.class);
assertNumStep(superV * (numV / 5 * 2), 2, g.V().has("id", sid).outE("knows").has("weight", P.between(1, 3)), JanusGraphStep.class, JanusGraphVertexStep.class);
assertNumStep(superV * 10, 2, g.V().has("id", sid).local(__.outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)).limit(10)), JanusGraphStep.class, JanusGraphVertexStep.class);
assertNumStep(superV * 10, 1, g.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", desc).limit(10)), JanusGraphStep.class);
assertNumStep(superV * 10, 0, g.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", desc).limit(10)), LocalStep.class);
assertNumStep(superV * numV, 2, g.V().has("id", sid).values("names"), JanusGraphStep.class, JanusGraphPropertiesStep.class);
// Verify traversal metrics when all reads are from cache (i.e. no backend queries)
t = g.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", desc).limit(10)).profile("~metrics");
assertCount(superV * 10, t);
assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
// Verify that properties also use multi query
t = g.V().has("id", sid).values("names").profile("~metrics");
assertCount(superV * numV, t);
assertTrue(queryProfilerAnnotationIsPresent(t, QueryProfiler.MULTIQUERY_ANNOTATION));
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project janusgraph by JanusGraph.
the class QueryTest method testMultipleIndexQueryWithLimits.
@Test
public void testMultipleIndexQueryWithLimits() {
JanusGraphManagement mgmt = graph.openManagement();
PropertyKey prop1Key = mgmt.makePropertyKey("prop1").dataType(String.class).make();
PropertyKey prop2Key = mgmt.makePropertyKey("prop2").dataType(String.class).make();
mgmt.buildIndex("prop1_idx", Vertex.class).addKey(prop1Key).buildCompositeIndex();
mgmt.buildIndex("prop2_idx", Vertex.class).addKey(prop2Key).buildCompositeIndex();
mgmt.commit();
// Creates 20 vertices with prop1=prop1val1, prop2=prop2val1
for (int i = 0; i < 20; i++) {
tx.addVertex().property("prop1", "prop1val1").element().property("prop2", "prop2val1");
}
// Creates an additional vertex with prop1=prop1val1, prop2=prop2val2
tx.addVertex().property("prop1", "prop1val1").element().property("prop2", "prop2val2");
tx.commit();
List<Vertex> res;
// Tests that queries for the single vertex containing prop1=prop1val1, prop2=prop2val2, are returned when limit(1) is applied
// Tests that single vertex containing prop1=prop1val1, prop2=prop2val2 is returned when indices are not used
res = graph.traversal().V().map(x -> x.get()).has("prop2", "prop2val2").has("prop1", "prop1val1").limit(1).toList();
assertEquals(1, res.size());
// Tests that single vertex containing prop1=prop1val1, prop2=prop2val2 is returned when only prop1 index is used
res = graph.traversal().V().has("prop1", "prop1val1").map(x -> x.get()).has("prop2", "prop2val2").limit(1).toList();
assertEquals(1, res.size());
// Tests that single vertex containing prop1=prop1val1, prop2=prop2val2 is returned when only prop2 index is used
res = graph.traversal().V().has("prop2", "prop2val2").map(x -> x.get()).has("prop1", "prop1val1").limit(1).toList();
assertEquals(1, res.size());
// Tests that JanusGraphStep strategy properly combines has() steps to use both indices
// Tests without limits
res = graph.traversal().V().has("prop1", "prop1val1").has("prop2", "prop2val2").toList();
assertEquals(1, res.size());
// Tests with limit
res = graph.traversal().V().has("prop1", "prop1val1").has("prop2", "prop2val2").limit(1).toList();
assertEquals(1, res.size());
res = graph.traversal().V().has("prop2", "prop2val2").has("prop1", "prop1val1").limit(1).toList();
assertEquals(1, res.size());
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project janusgraph by JanusGraph.
the class JanusGraphTest method testConcurrentConsistencyEnforcement.
/**
* Execute multiple identical transactions concurrently. Note that since these transactions are running in the same process,
* {@link org.janusgraph.diskstorage.locking.LocalLockMediator} is used to resolve lock contentions. If there is only
* one lock needed in the whole transaction, exactly one transaction shall succeed and others shall fail due to local
* lock contention. If there is more than one lock needed in the transaction, at most one transaction shall succeed
* and others shall fail due to local lock contention.
* @throws Exception
*/
@Test
public void testConcurrentConsistencyEnforcement() throws Exception {
PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make();
JanusGraphIndex nameIndex = mgmt.buildIndex("name", Vertex.class).addKey(name).unique().buildCompositeIndex();
mgmt.setConsistency(nameIndex, ConsistencyModifier.LOCK);
EdgeLabel married = mgmt.makeEdgeLabel("married").multiplicity(Multiplicity.ONE2ONE).make();
mgmt.setConsistency(married, ConsistencyModifier.LOCK);
mgmt.makeEdgeLabel("friend").multiplicity(Multiplicity.MULTI).make();
finishSchema();
JanusGraphVertex baseV = tx.addVertex("name", "base");
newTx();
final long baseVid = getId(baseV);
final String nameA = "a", nameB = "b";
final int parallelThreads = 4;
// Only one lock is needed
int[] results = executeParallelTransactions(tx -> {
final JanusGraphVertex a = tx.addVertex();
final JanusGraphVertex base = getV(tx, baseVid);
base.addEdge("married", a);
}, parallelThreads);
int numOfSuccess = results[0];
int numOfLockContentions = results[1];
assertEquals(1, numOfSuccess);
assertEquals(parallelThreads - 1, numOfLockContentions);
// Two locks are needed. Note that the order of adding/modifying/deleting elements might not be consistent with
// the order of real mutations during commit. Thus, it can be the case that one thread gets one lock and another
// thread gets another, and both fail because they are unable to get the other lock.
results = executeParallelTransactions(tx -> {
tx.addVertex("name", nameA);
final JanusGraphVertex b = tx.addVertex("name", nameB);
b.addEdge("friend", b);
}, parallelThreads);
numOfSuccess = results[0];
numOfLockContentions = results[1];
assertTrue(numOfSuccess <= 1);
assertEquals(parallelThreads - numOfSuccess, numOfLockContentions);
newTx();
final long numA = Iterables.size(tx.query().has("name", nameA).vertices());
final long numB = Iterables.size(tx.query().has("name", nameB).vertices());
assertTrue(numA <= 1);
assertTrue(numB <= 1);
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project open-kilda by telstra.
the class FermaMirrorGroupRepository method exists.
@Override
public boolean exists(SwitchId switchId, GroupId groupId) {
String switchIdAsStr = SwitchIdConverter.INSTANCE.toGraphProperty(switchId);
Long groupIdAsLong = GroupIdConverter.INSTANCE.toGraphProperty(groupId);
try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(FlowMeterFrame.FRAME_LABEL).has(MirrorGroupFrame.GROUP_ID_PROPERTY, groupIdAsLong).has(MirrorGroupFrame.SWITCH_ID_PROPERTY, switchIdAsStr)).getRawTraversal()) {
return traversal.hasNext();
} catch (Exception e) {
throw new PersistenceException("Failed to traverse", e);
}
}
Aggregations