Commit a9aa5b0e by Sarath Subramanian

ATLAS-3034: Perf enhancement to avoid unnecessary lookup when creating new relationships

parent de7fe47a
...@@ -59,6 +59,10 @@ public interface AtlasRelationshipStore { ...@@ -59,6 +59,10 @@ public interface AtlasRelationshipStore {
AtlasEdge getOrCreate(AtlasVertex end1Vertex, AtlasVertex end2Vertex, AtlasRelationship relationship) throws AtlasBaseException; AtlasEdge getOrCreate(AtlasVertex end1Vertex, AtlasVertex end2Vertex, AtlasRelationship relationship) throws AtlasBaseException;
AtlasEdge getRelationship(AtlasVertex end1Vertex, AtlasVertex end2Vertex, AtlasRelationship relationship);
AtlasEdge createRelationship(AtlasVertex end1Vertex, AtlasVertex end2Vertex, AtlasRelationship relationship) throws AtlasBaseException;
/** /**
* Retrieve a relationship if it exists or creates a new relationship instance. * Retrieve a relationship if it exists or creates a new relationship instance.
* @param relationship relationship instance definition * @param relationship relationship instance definition
......
...@@ -819,7 +819,6 @@ public class EntityGraphMapper { ...@@ -819,7 +819,6 @@ public class EntityGraphMapper {
AtlasType type = typeRegistry.getType(AtlasGraphUtilsV2.getTypeName(entityVertex)); AtlasType type = typeRegistry.getType(AtlasGraphUtilsV2.getTypeName(entityVertex));
AtlasRelationshipEdgeDirection edgeDirection = ctx.getAttribute().getRelationshipEdgeDirection(); AtlasRelationshipEdgeDirection edgeDirection = ctx.getAttribute().getRelationshipEdgeDirection();
String edgeLabel = ctx.getAttribute().getRelationshipEdgeLabel();
if (type instanceof AtlasEntityType) { if (type instanceof AtlasEntityType) {
AtlasEntityType entityType = (AtlasEntityType) type; AtlasEntityType entityType = (AtlasEntityType) type;
...@@ -844,10 +843,17 @@ public class EntityGraphMapper { ...@@ -844,10 +843,17 @@ public class EntityGraphMapper {
fromVertex = entityVertex; fromVertex = entityVertex;
toVertex = attributeVertex; toVertex = attributeVertex;
} }
boolean relationshipExists = isRelationshipExists(fromVertex, toVertex, edgeLabel);
ret = getOrCreateRelationship(fromVertex, toVertex, relationshipName, relationshipAttributes); ret = getOrCreateRelationship(fromVertex, toVertex, relationshipName, relationshipAttributes);
boolean isCreated = GraphHelper.getCreatedTime(ret) == RequestContext.get().getRequestTime();
if (isCreated) {
// if relationship did not exist before and new relationship was created
// record entity update on both relationship vertices
recordEntityUpdate(attributeVertex);
}
// for import use the relationship guid provided // for import use the relationship guid provided
if (RequestContext.get().isImportInProgress()) { if (RequestContext.get().isImportInProgress()) {
String relationshipGuid = getRelationshipGuid(ctx.getValue()); String relationshipGuid = getRelationshipGuid(ctx.getValue());
...@@ -856,12 +862,6 @@ public class EntityGraphMapper { ...@@ -856,12 +862,6 @@ public class EntityGraphMapper {
AtlasGraphUtilsV2.setEncodedProperty(ret, RELATIONSHIP_GUID_PROPERTY_KEY, relationshipGuid); AtlasGraphUtilsV2.setEncodedProperty(ret, RELATIONSHIP_GUID_PROPERTY_KEY, relationshipGuid);
} }
} }
// if relationship did not exist before and new relationship was created
// record entity update on both relationship vertices
if (!relationshipExists) {
recordEntityUpdate(attributeVertex);
}
} }
} else { } else {
// use legacy way to create/update edges // use legacy way to create/update edges
...@@ -1851,22 +1851,6 @@ public class EntityGraphMapper { ...@@ -1851,22 +1851,6 @@ public class EntityGraphMapper {
return relationshipStore.getOrCreate(end1Vertex, end2Vertex, new AtlasRelationship(relationshipName, relationshipAttributes)); return relationshipStore.getOrCreate(end1Vertex, end2Vertex, new AtlasRelationship(relationshipName, relationshipAttributes));
} }
private boolean isRelationshipExists(AtlasVertex fromVertex, AtlasVertex toVertex, String edgeLabel) {
boolean ret = false;
Iterator<AtlasEdge> edges = graphHelper.getOutGoingEdgesByLabel(fromVertex, edgeLabel);
while (edges != null && edges.hasNext()) {
AtlasEdge edge = edges.next();
AtlasVertex inVertex = edge.getInVertex();
if (inVertex != null && StringUtils.equals(getIdFromVertex(inVertex), getIdFromVertex(toVertex))) {
ret = true;
}
}
return ret;
}
private void recordEntityUpdate(AtlasVertex vertex) throws AtlasBaseException { private void recordEntityUpdate(AtlasVertex vertex) throws AtlasBaseException {
RequestContext req = RequestContext.get(); RequestContext req = RequestContext.get();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment