use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project janusgraph by JanusGraph.
the class AdjacentVertexHasIdOptimizerStrategyTest method testAll.
@Test
public void testAll() {
makeSampleGraph();
// AdjacentVertexHasIdOptimizer out/in/both
assertOptimization(g.V(sv[0]).outE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).inV(), g.V(sv[0]).out("knows").hasId(vs[50]), optimizer);
assertOptimization(g.V(sv[0]).inE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).outV(), g.V(sv[0]).in("knows").hasId(vs[50]), optimizer);
assertOptimization(g.V(sv[0]).bothE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).otherV(), g.V(sv[0]).both("knows").hasId(vs[50]), optimizer);
// AdjacentVertexHasIdOptimizer outE/inE/bothE
assertOptimization(g.V(sv[0]).outE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).inV(), g.V(sv[0]).outE("knows").inV().hasId(vs[50]), optimizer);
assertOptimization(g.V(sv[0]).inE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).outV(), g.V(sv[0]).inE("knows").outV().hasId(vs[50]), optimizer);
assertOptimization(g.V(sv[0]).bothE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).otherV(), g.V(sv[0]).bothE("knows").otherV().hasId(vs[50]), optimizer);
// Result should stay the same
assertSameResultWithOptimizations(g.V(sv[0]).as("v1").out("knows").hasId(vs[50].id()).as("v2").select("v1", "v2").by("id"), AdjacentVertexHasIdOptimizerStrategy.instance());
// neq should not be optimized
assertOptimization(g.V(sv[0]).in().hasId(P.neq(vs[50])), g.V(sv[0]).in().hasId(P.neq(vs[50])), optimizer);
// 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[] loop1 = { 0 };
int[] loop2 = { 0 };
GraphTraversal t1 = g.V(vs[0], vs[1], vs[2]).repeat(__.inE("knows").has(ImplicitKey.ADJACENT_ID.name(), sv[0].id()).outV().out(// TINKERPOP-2342
"knows").sideEffect(e -> loop1[0] = e.loops()).has("id", loop1[0])).times(numV);
GraphTraversal t2 = g.V(vs[0], vs[1], vs[2]).repeat(__.inE("knows").outV().hasId(sv[0].id()).out(// TINKERPOP-2342
"knows").sideEffect(e -> loop2[0] = e.loops()).has("id", loop2[0])).times(numV);
assertOptimization(t1, t2, optimizer);
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project open-kilda by telstra.
the class FermaIslRepository method findByPathIds.
@Override
public Collection<Isl> findByPathIds(List<PathId> pathIds) {
List<String> pathIdAsStr = pathIds.stream().map(PathIdConverter.INSTANCE::toGraphProperty).collect(Collectors.toList());
List<? extends PathSegmentFrame> segmentFrames = framedGraph().traverse(g -> g.V().hasLabel(PathSegmentFrame.FRAME_LABEL).has(PathSegmentFrame.PATH_ID_PROPERTY, P.within(pathIdAsStr))).toListExplicit(PathSegmentFrame.class);
if (segmentFrames.isEmpty()) {
return emptyList();
}
List<Isl> result = new ArrayList<>();
segmentFrames.forEach(segmentFrame -> {
framedGraph().traverse(g -> g.E().hasLabel(IslFrame.FRAME_LABEL).has(IslFrame.SRC_SWITCH_ID_PROPERTY, SwitchIdConverter.INSTANCE.toGraphProperty(segmentFrame.getSrcSwitchId())).has(IslFrame.DST_SWITCH_ID_PROPERTY, SwitchIdConverter.INSTANCE.toGraphProperty(segmentFrame.getDestSwitchId())).has(IslFrame.SRC_PORT_PROPERTY, segmentFrame.getSrcPort()).has(IslFrame.DST_PORT_PROPERTY, segmentFrame.getDestPort())).frameExplicit(IslFrame.class).forEachRemaining(frame -> result.add(addIslConfigToIsl(new Isl(frame))));
});
return result;
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project open-kilda by telstra.
the class FermaMirrorGroupRepository method findFirstUnassignedGroupId.
@Override
public Optional<GroupId> findFirstUnassignedGroupId(SwitchId switchId, GroupId lowestGroupId, GroupId highestGroupId) {
String switchIdAsStr = SwitchIdConverter.INSTANCE.toGraphProperty(switchId);
Long lowestGroupIdAsLong = GroupIdConverter.INSTANCE.toGraphProperty(lowestGroupId);
Long highestGroupIdAsLong = GroupIdConverter.INSTANCE.toGraphProperty(highestGroupId);
try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(MirrorGroupFrame.FRAME_LABEL).has(MirrorGroupFrame.SWITCH_ID_PROPERTY, switchIdAsStr).has(MirrorGroupFrame.GROUP_ID_PROPERTY, P.gte(lowestGroupIdAsLong)).has(MirrorGroupFrame.GROUP_ID_PROPERTY, P.lt(highestGroupIdAsLong)).values(MirrorGroupFrame.GROUP_ID_PROPERTY).order().math("_ + 1").as("a").where(__.not(__.V().hasLabel(MirrorGroupFrame.FRAME_LABEL).has(MirrorGroupFrame.SWITCH_ID_PROPERTY, switchIdAsStr).values(MirrorGroupFrame.GROUP_ID_PROPERTY).where(P.eq("a")))).select("a").limit(1)).getRawTraversal()) {
if (traversal.hasNext()) {
return traversal.tryNext().map(l -> ((Double) l).longValue()).map(GroupIdConverter.INSTANCE::toEntityAttribute);
}
} catch (Exception e) {
throw new PersistenceException("Failed to traverse", e);
}
try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(MirrorGroupFrame.FRAME_LABEL).has(MirrorGroupFrame.SWITCH_ID_PROPERTY, switchIdAsStr).has(MirrorGroupFrame.GROUP_ID_PROPERTY, lowestGroupIdAsLong)).getRawTraversal()) {
if (!traversal.hasNext()) {
return Optional.of(lowestGroupId);
}
} catch (Exception e) {
throw new PersistenceException("Failed to traverse", e);
}
return Optional.empty();
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project open-kilda by telstra.
the class DatabaseSupportImpl method getPaths.
/**
* Get all possible paths between source and destination switches.
*
* @param src source switch ID
* @param dst destination switch ID
* @return list of PathInfoData objects
*/
@Override
@SuppressWarnings("unchecked")
public List<PathInfoData> getPaths(SwitchId src, SwitchId dst) {
return transactionManager.doInTransaction(() -> {
FramedGraph framedGraph = getGraph();
GraphTraversal<?, ?> rawTraversal = framedGraph.traverse(input -> input.V().hasLabel(SwitchFrame.FRAME_LABEL).has(SwitchFrame.SWITCH_ID_PROPERTY, src.toString()).repeat(__.outE(IslFrame.FRAME_LABEL).has(IslFrame.STATUS_PROPERTY, "active").inV().hasLabel(SwitchFrame.FRAME_LABEL).simplePath()).until(__.has(SwitchFrame.SWITCH_ID_PROPERTY, dst.toString()).or().loops().is(DEFAULT_DEPTH)).has(SwitchFrame.SWITCH_ID_PROPERTY, dst.toString()).path()).getRawTraversal();
List<PathInfoData> deserializedResults = new ArrayList<>();
while (rawTraversal.hasNext()) {
ImmutablePath tpPath = (ImmutablePath) rawTraversal.next();
List<PathNode> resultPath = new ArrayList<>();
int seqId = 0;
for (Object hop : tpPath) {
if (hop instanceof Edge) {
Edge edge = (Edge) hop;
Vertex srcVertex = edge.outVertex();
resultPath.add(new PathNode(new SwitchId((String) srcVertex.property(SwitchFrame.SWITCH_ID_PROPERTY).value()), (Integer) edge.property(IslFrame.SRC_PORT_PROPERTY).value(), seqId++, (Long) edge.property(IslFrame.LATENCY_PROPERTY).value()));
Vertex dstVertex = edge.inVertex();
resultPath.add(new PathNode(new SwitchId((String) dstVertex.property(SwitchFrame.SWITCH_ID_PROPERTY).value()), (Integer) edge.property(IslFrame.DST_PORT_PROPERTY).value(), seqId++, (Long) edge.property(IslFrame.LATENCY_PROPERTY).value()));
}
}
deserializedResults.add(new PathInfoData(0, resultPath));
}
return deserializedResults;
});
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has in project timbuctoo by HuygensING.
the class TinkerPopOperationsTest method deleteEntitySetsModified.
@Test
public void deleteEntitySetsModified() throws Exception {
Vres vres = createConfiguration();
Collection collection = vres.getCollection("testthings").get();
UUID id = UUID.randomUUID();
String idString = id.toString();
TinkerPopGraphManager graphManager = newGraph().withVertex(v -> v.withTimId(idString).withProperty("isLatest", true).withVre("test").withType("thing").withProperty("rev", 1).withIncomingRelation("VERSION_OF", "orig")).withVertex("orig", v -> v.withTimId(idString).withVre("test").withType("thing").withProperty("isLatest", false).withProperty("rev", 1)).wrap();
TinkerPopOperations instance = forGraphWrapperAndMappings(graphManager, vres);
long timeStamp = Instant.now().toEpochMilli();
String userId = "userId";
instance.deleteEntity(collection, id, new Change(timeStamp, userId, null));
assertThat(graphManager.getGraph().traversal().V().has("tim_id", idString).has("deleted", true).next().value("modified"), sameJSONAs(String.format("{\"timeStamp\": %s,\"userId\": \"%s\"}", timeStamp, userId)));
}
Aggregations