You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@atlas.apache.org by am...@apache.org on 2020/05/05 16:39:34 UTC

[atlas] 02/02: ATLAS-3762: Improve edge creation using genuine iterator. Part 2

This is an automated email from the ASF dual-hosted git repository.

amestry pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/atlas.git

commit 23aea76fff6c8f530816319649218583f4dfd091
Author: Ashutosh Mestry <am...@cloudera.com>
AuthorDate: Mon May 4 18:46:17 2020 -0700

    ATLAS-3762: Improve edge creation using genuine iterator. Part 2
---
 .../atlas/repository/graphdb/AtlasGraph.java       | 13 +++++++++--
 .../repository/graphdb/janus/AtlasJanusGraph.java  | 11 ++++++++++
 .../apache/atlas/repository/graph/GraphHelper.java | 20 +++++++++--------
 .../store/graph/v2/AtlasRelationshipStoreV2.java   | 25 +---------------------
 4 files changed, 34 insertions(+), 35 deletions(-)

diff --git a/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraph.java b/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraph.java
index 7bd5f2d..c016f63 100644
--- a/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraph.java
+++ b/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraph.java
@@ -48,10 +48,19 @@ public interface AtlasGraph<V, E> {
     AtlasEdge<V, E> addEdge(AtlasVertex<V, E> outVertex, AtlasVertex<V, E> inVertex, String label);
 
     /**
-     * Adds a vertex to the graph.
-     *
+     * Fetch edges between two vertices using relationshipLabel
+     * @param fromVertex
+     * @param toVertex
+     * @param relationshipLabel
      * @return
      */
+    AtlasEdge<V, E> getEdgeBetweenVertices(AtlasVertex fromVertex, AtlasVertex toVertex, String relationshipLabel);
+
+        /**
+         * Adds a vertex to the graph.
+         *
+         * @return
+         */
     AtlasVertex<V, E> addVertex();
 
     /**
diff --git a/graphdb/janus/src/main/java/org/apache/atlas/repository/graphdb/janus/AtlasJanusGraph.java b/graphdb/janus/src/main/java/org/apache/atlas/repository/graphdb/janus/AtlasJanusGraph.java
index a30dbc7..35274c9 100644
--- a/graphdb/janus/src/main/java/org/apache/atlas/repository/graphdb/janus/AtlasJanusGraph.java
+++ b/graphdb/janus/src/main/java/org/apache/atlas/repository/graphdb/janus/AtlasJanusGraph.java
@@ -172,6 +172,17 @@ public class AtlasJanusGraph implements AtlasGraph<AtlasJanusVertex, AtlasJanusE
     }
 
     @Override
+    public AtlasEdge getEdgeBetweenVertices(AtlasVertex fromVertex, AtlasVertex toVertex, String edgeLabel) {
+        GraphTraversal gt = V(fromVertex.getId()).outE(edgeLabel).where(__.otherV().hasId(toVertex.getId()));
+        Object o = gt.hasNext() ? gt.next() : null;
+        if (o == null) {
+            return null;
+        }
+
+        return GraphDbObjectFactory.createEdge(this, (Edge) o);
+    }
+
+    @Override
     public AtlasEdge<AtlasJanusVertex, AtlasJanusEdge> getEdge(String edgeId) {
         Iterator<Edge> it = getGraph().edges(edgeId);
         Edge           e  = getSingleElement(it, edgeId);
diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java
index b9e3a5e..b1368e5 100755
--- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java
+++ b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java
@@ -739,19 +739,21 @@ public final class GraphHelper {
 
     public static List<AtlasVertex> getPropagationEnabledClassificationVertices(AtlasVertex entityVertex) {
         List<AtlasVertex> ret   = new ArrayList<>();
-        Iterable          edges = entityVertex.query().direction(AtlasEdgeDirection.OUT).label(CLASSIFICATION_LABEL).edges();
+        if (entityVertex.hasEdges(AtlasEdgeDirection.OUT, CLASSIFICATION_LABEL)) {
+            Iterable edges = entityVertex.query().direction(AtlasEdgeDirection.OUT).label(CLASSIFICATION_LABEL).edges();
 
-        if (edges != null) {
-            Iterator<AtlasEdge> iterator = edges.iterator();
+            if (edges != null) {
+                Iterator<AtlasEdge> iterator = edges.iterator();
 
-            while (iterator.hasNext()) {
-                AtlasEdge edge = iterator.next();
+                while (iterator.hasNext()) {
+                    AtlasEdge edge = iterator.next();
 
-                if (edge != null) {
-                    AtlasVertex classificationVertex = edge.getInVertex();
+                    if (edge != null) {
+                        AtlasVertex classificationVertex = edge.getInVertex();
 
-                    if (isPropagationEnabled(classificationVertex)) {
-                        ret.add(classificationVertex);
+                        if (isPropagationEnabled(classificationVertex)) {
+                            ret.add(classificationVertex);
+                        }
                     }
                 }
             }
diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasRelationshipStoreV2.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasRelationshipStoreV2.java
index ab431bc..8d74489 100644
--- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasRelationshipStoreV2.java
+++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasRelationshipStoreV2.java
@@ -784,36 +784,13 @@ public class AtlasRelationshipStoreV2 implements AtlasRelationshipStore {
         AtlasEdge ret = null;
 
         if (toVertex.hasEdges(AtlasEdgeDirection.IN, relationshipLabel) && fromVertex.hasEdges(AtlasEdgeDirection.OUT, relationshipLabel)) {
-            long fromVertexOutgoingEdgeCount = graphHelper.getOutGoingEdgesCountByLabel(fromVertex, relationshipLabel);
-            long toVertexIncomingEdgeCount = graphHelper.getInComingEdgesCountByLabel(toVertex, relationshipLabel);
-            if (toVertexIncomingEdgeCount < fromVertexOutgoingEdgeCount) {
-                Iterator<AtlasEdge> edgesIteratorIn = graphHelper.getIncomingEdgesByLabel(toVertex, relationshipLabel);
-                ret = getActiveEdgeFromList(edgesIteratorIn, fromVertex.getId(), e -> e.getOutVertex().getId());
-            } else {
-                Iterator<AtlasEdge> edgesIteratorOut = graphHelper.getOutGoingEdgesByLabel(fromVertex, relationshipLabel);
-                ret = getActiveEdgeFromList(edgesIteratorOut, toVertex.getId(), e -> e.getInVertex().getId());
-            }
+            ret = graph.getEdgeBetweenVertices(fromVertex, toVertex, relationshipLabel);
         }
 
         RequestContext.get().endMetricRecord(metric);
         return ret;
     }
 
-    private AtlasEdge getActiveEdgeFromList(Iterator<AtlasEdge> edgesIterator, Object vertexIdToCompare, Function<AtlasEdge, Object> edgeIdFn) {
-        while (edgesIterator != null && edgesIterator.hasNext()) {
-            AtlasEdge edge = edgesIterator.next();
-            if (edge != null) {
-                Status status = graphHelper.getStatus(edge);
-
-                if ((status == null || status == ACTIVE) && edgeIdFn.apply(edge).equals(vertexIdToCompare)) {
-                    return edge;
-                }
-            }
-        }
-
-        return null;
-    }
-
     private Long getRelationshipVersion(AtlasRelationship relationship) {
         Long ret = relationship != null ? relationship.getVersion() : null;