use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.
the class EntityGraphRetriever method mapRelationshipArrayAttribute.
private List<AtlasObjectId> mapRelationshipArrayAttribute(AtlasVertex entityVertex, AtlasAttribute attribute) throws AtlasBaseException {
List<AtlasObjectId> ret = new ArrayList<>();
Iterator<AtlasEdge> edges = null;
if (attribute.getRelationshipEdgeDirection() == AtlasRelationshipEdgeDirection.IN) {
edges = graphHelper.getIncomingEdgesByLabel(entityVertex, attribute.getRelationshipEdgeLabel());
} else if (attribute.getRelationshipEdgeDirection() == AtlasRelationshipEdgeDirection.OUT) {
edges = graphHelper.getOutGoingEdgesByLabel(entityVertex, attribute.getRelationshipEdgeLabel());
}
if (edges != null) {
while (edges.hasNext()) {
AtlasEdge relationshipEdge = edges.next();
AtlasObjectId objectId = mapRelatedVertexToObjectId(entityVertex, relationshipEdge);
ret.add(objectId);
}
}
return ret;
}
use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.
the class TypedInstanceToGraphMapper method mapAttributeToVertex.
void mapAttributeToVertex(ITypedInstance typedInstance, AtlasVertex instanceVertex, AttributeInfo attributeInfo, Operation operation) throws AtlasException {
if (typedInstance.isValueSet(attributeInfo.name) || operation == Operation.CREATE) {
Object attrValue = typedInstance.get(attributeInfo.name);
if (LOG.isDebugEnabled()) {
LOG.debug("Mapping attribute {} = {}", attributeInfo.name, attrValue);
}
switch(attributeInfo.dataType().getTypeCategory()) {
case PRIMITIVE:
case ENUM:
mapPrimitiveOrEnumToVertex(typedInstance, instanceVertex, attributeInfo);
break;
case ARRAY:
mapArrayCollectionToVertex(typedInstance, instanceVertex, attributeInfo, operation);
break;
case MAP:
mapMapCollectionToVertex(typedInstance, instanceVertex, attributeInfo, operation);
break;
case STRUCT:
case CLASS:
String edgeLabel = graphHelper.getEdgeLabel(typedInstance, attributeInfo);
AtlasEdge currentEdge = graphHelper.getEdgeForLabel(instanceVertex, edgeLabel);
AtlasEdge newEdge = addOrUpdateReference(instanceVertex, attributeInfo, attributeInfo.dataType(), attrValue, currentEdge, edgeLabel, operation);
if (currentEdge != null && !currentEdge.equals(newEdge)) {
deleteHandler.deleteEdgeReference(currentEdge, attributeInfo.dataType().getTypeCategory(), attributeInfo.isComposite, true);
}
if (attributeInfo.reverseAttributeName != null && newEdge != null) {
addReverseReference(instanceVertex, attributeInfo.reverseAttributeName, newEdge);
}
break;
case TRAIT:
// do NOTHING - this is taken care of earlier
break;
default:
throw new IllegalArgumentException("Unknown type category: " + attributeInfo.dataType().getTypeCategory());
}
}
}
use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.
the class TypedInstanceToGraphMapper method addOrUpdateClassVertex.
/**
****************************************** CLASS *************************************************
*/
private AtlasEdge addOrUpdateClassVertex(AtlasVertex instanceVertex, AtlasEdge currentEdge, ITypedReferenceableInstance newAttributeValue, AttributeInfo attributeInfo, String edgeLabel) throws AtlasException {
AtlasVertex newReferenceVertex = getClassVertex(newAttributeValue);
if (!GraphHelper.elementExists(newReferenceVertex) && newAttributeValue != null) {
LOG.error("Could not find vertex for Class Reference {}", newAttributeValue);
throw new EntityNotFoundException("Could not find vertex for Class Reference " + newAttributeValue);
}
AtlasEdge newEdge = null;
if (GraphHelper.elementExists(currentEdge) && newAttributeValue != null) {
newEdge = updateClassEdge(instanceVertex, currentEdge, newAttributeValue, newReferenceVertex, attributeInfo, edgeLabel);
} else if (!GraphHelper.elementExists(currentEdge) && newAttributeValue != null) {
newEdge = addClassEdge(instanceVertex, newReferenceVertex, edgeLabel);
}
return newEdge;
}
use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.
the class TypedInstanceToGraphMapper method addOrUpdateStruct.
/**
****************************************** STRUCT *************************************************
*/
private AtlasEdge addOrUpdateStruct(AtlasVertex instanceVertex, AttributeInfo attributeInfo, ITypedStruct newAttributeValue, AtlasEdge currentEdge, String edgeLabel, Operation operation) throws AtlasException {
AtlasEdge newEdge = null;
if (GraphHelper.elementExists(currentEdge) && newAttributeValue != null) {
// update
updateStructVertex(newAttributeValue, currentEdge, operation);
newEdge = currentEdge;
} else if (!GraphHelper.elementExists(currentEdge) && newAttributeValue != null) {
// add
newEdge = addStructVertex(newAttributeValue, instanceVertex, attributeInfo, edgeLabel);
}
return newEdge;
}
use of org.apache.atlas.repository.graphdb.AtlasEdge in project incubator-atlas by apache.
the class TypedInstanceToGraphMapper method mapArrayCollectionToVertex.
/**
****************************************** ARRAY *************************************************
*/
private void mapArrayCollectionToVertex(ITypedInstance typedInstance, AtlasVertex instanceVertex, AttributeInfo attributeInfo, Operation operation) throws AtlasException {
if (LOG.isDebugEnabled()) {
LOG.debug("Mapping instance {} for array attribute {} vertex {}", typedInstance.toShortString(), attributeInfo.name, string(instanceVertex));
}
List newElements = (List) typedInstance.get(attributeInfo.name);
boolean newAttributeEmpty = (newElements == null || newElements.isEmpty());
IDataType elementType = ((DataTypes.ArrayType) attributeInfo.dataType()).getElemType();
String propertyName = GraphHelper.getQualifiedFieldName(typedInstance, attributeInfo);
List<Object> currentElements = GraphHelper.getArrayElementsProperty(elementType, instanceVertex, propertyName);
List<Object> newElementsCreated = new ArrayList<>();
if (!newAttributeEmpty) {
int index = 0;
for (; index < newElements.size(); index++) {
Object currentElement = (currentElements != null && index < currentElements.size()) ? currentElements.get(index) : null;
if (LOG.isDebugEnabled()) {
LOG.debug("Adding/updating element at position {}, current element {}, new element {}", index, currentElement, newElements.get(index));
}
Object newEntry = addOrUpdateCollectionEntry(instanceVertex, attributeInfo, elementType, newElements.get(index), currentElement, propertyName, operation);
newElementsCreated.add(newEntry);
}
}
if (GraphHelper.isReference(elementType)) {
if (attributeInfo.reverseAttributeName != null && newElementsCreated.size() > 0) {
// Set/add the new reference value(s) on the reverse reference.
for (Object newElement : newElementsCreated) {
if ((newElement instanceof AtlasEdge)) {
AtlasEdge newEdge = (AtlasEdge) newElement;
addReverseReference(instanceVertex, attributeInfo.reverseAttributeName, newEdge);
} else {
throw new AtlasException("Invalid array element type " + newElement.getClass().getName() + " - expected " + AtlasEdge.class.getName() + " for reference " + GraphHelper.getQualifiedFieldName(typedInstance, attributeInfo) + " on vertex " + GraphHelper.getVertexDetails(instanceVertex));
}
}
}
List<AtlasEdge> additionalEdges = removeUnusedEntries(instanceVertex, propertyName, (List) currentElements, (List) newElementsCreated, elementType, attributeInfo);
newElementsCreated.addAll(additionalEdges);
}
// for dereference on way out
GraphHelper.setArrayElementsProperty(elementType, instanceVertex, propertyName, newElementsCreated);
}
Aggregations