use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.
the class GraphToTypedInstanceMapper method mapVertexToAttribute.
private void mapVertexToAttribute(AtlasVertex instanceVertex, ITypedInstance typedInstance, AttributeInfo attributeInfo) throws AtlasException {
if (LOG.isDebugEnabled()) {
LOG.debug("Mapping attributeInfo {}", attributeInfo.name);
}
final IDataType dataType = attributeInfo.dataType();
final String vertexPropertyName = GraphHelper.getQualifiedFieldName(typedInstance, attributeInfo);
String relationshipLabel = GraphHelper.getEdgeLabel(typedInstance, attributeInfo);
switch(dataType.getTypeCategory()) {
case PRIMITIVE:
mapVertexToPrimitive(instanceVertex, typedInstance, attributeInfo);
// add only if vertex has this attribute
break;
case ENUM:
Object propertyValue = GraphHelper.getProperty(instanceVertex, vertexPropertyName);
if (propertyValue == null) {
return;
}
typedInstance.set(attributeInfo.name, dataType.convert(propertyValue, Multiplicity.REQUIRED));
break;
case ARRAY:
mapVertexToArrayInstance(instanceVertex, typedInstance, attributeInfo, vertexPropertyName);
break;
case MAP:
mapVertexToMapInstance(instanceVertex, typedInstance, attributeInfo, vertexPropertyName);
break;
case STRUCT:
ITypedStruct structInstance = mapVertexToStructInstance(instanceVertex, (StructType) attributeInfo.dataType(), relationshipLabel, null);
typedInstance.set(attributeInfo.name, structInstance);
break;
case TRAIT:
// do NOTHING - handled in class
break;
case CLASS:
AtlasEdge nullEdge = null;
Object idOrInstance = mapVertexToClassReference(instanceVertex, attributeInfo, relationshipLabel, attributeInfo.dataType(), nullEdge);
if (idOrInstance != null) {
typedInstance.set(attributeInfo.name, idOrInstance);
}
break;
default:
break;
}
}
use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.
the class GraphToTypedInstanceMapper method mapVertexToClassReference.
private Object mapVertexToClassReference(AtlasVertex instanceVertex, AttributeInfo attributeInfo, String relationshipLabel, IDataType dataType, AtlasEdge optionalEdge) throws AtlasException {
if (LOG.isDebugEnabled()) {
LOG.debug("Finding edge for {} -> label {} ", instanceVertex, relationshipLabel);
}
AtlasEdge edge = null;
if (optionalEdge == null) {
edge = graphHelper.getEdgeForLabel(instanceVertex, relationshipLabel);
} else {
edge = optionalEdge;
}
if (GraphHelper.elementExists(edge)) {
final AtlasVertex referenceVertex = edge.getInVertex();
final String guid = GraphHelper.getSingleValuedProperty(referenceVertex, Constants.GUID_PROPERTY_KEY, String.class);
if (LOG.isDebugEnabled()) {
LOG.debug("Found vertex {} for label {} with guid {}", referenceVertex, relationshipLabel, guid);
}
if (attributeInfo.isComposite) {
// Also, when you retrieve a type's instance, you get the complete object graph of the composites
LOG.debug("Found composite, mapping vertex to instance");
ITypedReferenceableInstance cached = RequestContext.get().getInstanceV1(guid);
if (cached != null) {
return cached;
}
return mapGraphToTypedInstance(guid, referenceVertex);
} else {
String state = GraphHelper.getStateAsString(referenceVertex);
Id referenceId = new Id(guid, GraphHelper.getSingleValuedProperty(referenceVertex, Constants.VERSION_PROPERTY_KEY, Integer.class), dataType.getName(), state);
if (LOG.isDebugEnabled()) {
LOG.debug("Found non-composite, adding id {} ", referenceId);
}
return referenceId;
}
}
return null;
}
use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.
the class GraphToTypedInstanceMapper method getReferredEntity.
public ITypedInstance getReferredEntity(String edgeId, IDataType<?> referredType) throws AtlasException {
final AtlasEdge edge = getGraph().getEdge(edgeId);
if (edge != null) {
final AtlasVertex referredVertex = edge.getInVertex();
if (referredVertex != null) {
switch(referredType.getTypeCategory()) {
case STRUCT:
if (LOG.isDebugEnabled()) {
LOG.debug("Found struct instance vertex {}, mapping to instance {} ", referredVertex, referredType.getName());
}
StructType structType = (StructType) referredType;
ITypedStruct instance = structType.createInstance();
Map<String, AttributeInfo> fields = structType.fieldMapping().fields;
mapVertexToInstance(referredVertex, instance, fields);
return instance;
case CLASS:
// TODO isComposite handling for class loads
return GraphHelper.getIdFromVertex(referredType.getName(), referredVertex);
default:
throw new UnsupportedOperationException("Loading " + referredType.getTypeCategory() + " is not supported");
}
}
}
return null;
}
use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.
the class EntityGraphMapper method addInverseReference.
private void addInverseReference(AttributeMutationContext ctx, AtlasAttribute inverseAttribute, AtlasEdge edge) throws AtlasBaseException {
AtlasStructType inverseType = inverseAttribute.getDefinedInType();
String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(inverseType, inverseAttribute.getName());
AtlasVertex vertex = edge.getOutVertex();
AtlasVertex inverseVertex = edge.getInVertex();
String inverseEdgeLabel = AtlasGraphUtilsV1.getEdgeLabel(propertyName);
AtlasEdge inverseEdge = graphHelper.getEdgeForLabel(inverseVertex, inverseEdgeLabel);
AtlasEdge newEdge;
try {
newEdge = graphHelper.getOrCreateEdge(inverseVertex, vertex, inverseEdgeLabel);
} catch (RepositoryException e) {
throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
}
boolean inverseUpdated = true;
switch(inverseAttribute.getAttributeType().getTypeCategory()) {
case OBJECT_ID_TYPE:
if (inverseEdge != null) {
if (!inverseEdge.equals(newEdge)) {
// Disconnect old reference
deleteHandler.deleteEdgeReference(inverseEdge, inverseAttribute.getAttributeType().getTypeCategory(), inverseAttribute.isOwnedRef(), true);
} else {
// Edge already exists for this attribute between these vertices.
inverseUpdated = false;
}
}
break;
case ARRAY:
// Add edge ID to property value
List<String> elements = inverseVertex.getProperty(propertyName, List.class);
if (elements == null) {
elements = new ArrayList<>();
elements.add(newEdge.getId().toString());
inverseVertex.setProperty(propertyName, elements);
} else {
if (!elements.contains(newEdge.getId().toString())) {
elements.add(newEdge.getId().toString());
inverseVertex.setProperty(propertyName, elements);
} else {
// Property value list already contains the edge ID.
inverseUpdated = false;
}
}
break;
default:
break;
}
if (inverseUpdated) {
updateModificationMetadata(inverseVertex);
AtlasObjectId inverseEntityId = new AtlasObjectId(AtlasGraphUtilsV1.getIdFromVertex(inverseVertex), inverseType.getTypeName());
RequestContextV1.get().recordEntityUpdate(inverseEntityId);
}
}
use of org.apache.atlas.repository.graphdb.AtlasEdge in project atlas by apache.
the class DeleteHandlerV1 method deleteVertex.
protected void deleteVertex(AtlasVertex instanceVertex, boolean force) throws AtlasBaseException {
// Update external references(incoming edges) to this vertex
if (LOG.isDebugEnabled()) {
LOG.debug("Setting the external references to {} to null(removing edges)", string(instanceVertex));
}
for (AtlasEdge edge : (Iterable<AtlasEdge>) instanceVertex.getEdges(AtlasEdgeDirection.IN)) {
AtlasEntity.Status edgeState = getState(edge);
if (edgeState == AtlasEntity.Status.ACTIVE) {
// Delete only the active edge references
AtlasAttribute attribute = getAttributeForEdge(edge.getLabel());
// TODO use delete edge instead??
deleteEdgeBetweenVertices(edge.getOutVertex(), edge.getInVertex(), attribute);
}
}
_deleteVertex(instanceVertex, force);
}
Aggregations