Search in sources :

Example 1 with AtlasMapType

use of org.apache.atlas.type.AtlasMapType in project atlas by apache.

the class EntityGraphMapper method removeUnusedMapEntries.

// Remove unused entries from map
private Map<String, Object> removeUnusedMapEntries(AtlasAttribute attribute, AtlasVertex vertex, String propertyName, Map<String, Object> currentMap, Map<String, Object> newMap) throws AtlasException, AtlasBaseException {
    AtlasMapType mapType = (AtlasMapType) attribute.getAttributeType();
    Map<String, Object> additionalMap = new HashMap<>();
    for (String currentKey : currentMap.keySet()) {
        boolean shouldDeleteKey = !newMap.containsKey(currentKey);
        if (AtlasGraphUtilsV1.isReference(mapType.getValueType())) {
            // Delete the edge reference if its not part of new edges created/updated
            AtlasEdge currentEdge = (AtlasEdge) currentMap.get(currentKey);
            if (!newMap.values().contains(currentEdge)) {
                boolean deleted = deleteHandler.deleteEdgeReference(currentEdge, mapType.getValueType().getTypeCategory(), attribute.isOwnedRef(), true, vertex);
                if (!deleted) {
                    additionalMap.put(currentKey, currentEdge);
                    shouldDeleteKey = false;
                }
            }
        }
        if (shouldDeleteKey) {
            String propertyNameForKey = GraphHelper.getQualifiedNameForMapKey(propertyName, GraphHelper.encodePropertyKey(currentKey));
            GraphHelper.setProperty(vertex, propertyNameForKey, null);
        }
    }
    return additionalMap;
}
Also used : AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Example 2 with AtlasMapType

use of org.apache.atlas.type.AtlasMapType in project atlas by apache.

the class EntityGraphMapper method mapMapValue.

private Map<String, Object> mapMapValue(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapMapValue({})", ctx);
    }
    @SuppressWarnings("unchecked") Map<Object, Object> newVal = (Map<Object, Object>) ctx.getValue();
    Map<String, Object> newMap = new HashMap<>();
    AtlasMapType mapType = (AtlasMapType) ctx.getAttrType();
    try {
        AtlasAttribute attribute = ctx.getAttribute();
        List<String> currentKeys = GraphHelper.getListProperty(ctx.getReferringVertex(), ctx.getVertexProperty());
        Map<String, Object> currentMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(currentKeys)) {
            for (String key : currentKeys) {
                String propertyNameForKey = GraphHelper.getQualifiedNameForMapKey(ctx.getVertexProperty(), GraphHelper.encodePropertyKey(key));
                Object propertyValueForKey = getMapValueProperty(mapType.getValueType(), ctx.getReferringVertex(), propertyNameForKey);
                currentMap.put(key, propertyValueForKey);
            }
        }
        if (MapUtils.isNotEmpty(newVal)) {
            boolean isReference = AtlasGraphUtilsV1.isReference(mapType.getValueType());
            AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute();
            for (Map.Entry<Object, Object> entry : newVal.entrySet()) {
                String key = entry.getKey().toString();
                String propertyName = GraphHelper.getQualifiedNameForMapKey(ctx.getVertexProperty(), GraphHelper.encodePropertyKey(key));
                AtlasEdge existingEdge = getEdgeIfExists(mapType, currentMap, key);
                AttributeMutationContext mapCtx = new AttributeMutationContext(ctx.getOp(), ctx.getReferringVertex(), attribute, entry.getValue(), propertyName, mapType.getValueType(), existingEdge);
                // Add/Update/Remove property value
                Object newEntry = mapCollectionElementsToVertex(mapCtx, context);
                setMapValueProperty(mapType.getValueType(), ctx.getReferringVertex(), propertyName, newEntry);
                newMap.put(key, newEntry);
                // update the inverse reference value.
                if (isReference && newEntry instanceof AtlasEdge && inverseRefAttribute != null) {
                    AtlasEdge newEdge = (AtlasEdge) newEntry;
                    addInverseReference(inverseRefAttribute, newEdge, getRelationshipAttributes(ctx.getValue()));
                }
            }
        }
        Map<String, Object> finalMap = removeUnusedMapEntries(attribute, ctx.getReferringVertex(), ctx.getVertexProperty(), currentMap, newMap);
        for (Object newEntry : newMap.values()) {
            updateInConsistentOwnedMapVertices(ctx, mapType, newEntry);
        }
        Set<String> newKeys = new LinkedHashSet<>(newMap.keySet());
        newKeys.addAll(finalMap.keySet());
        // for dereference on way out
        GraphHelper.setListProperty(ctx.getReferringVertex(), ctx.getVertexProperty(), new ArrayList<>(newKeys));
    } catch (AtlasException e) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapMapValue({})", ctx);
    }
    return newMap;
}
Also used : AtlasException(org.apache.atlas.AtlasException) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasMapType(org.apache.atlas.type.AtlasMapType) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException)

Example 3 with AtlasMapType

use of org.apache.atlas.type.AtlasMapType in project atlas by apache.

the class DeleteHandlerV1 method getOwnedVertices.

/**
 * Get the GUIDs and vertices for all composite entities owned/contained by the specified root entity AtlasVertex.
 * The graph is traversed from the root entity through to the leaf nodes of the containment graph.
 *
 * @param entityVertex the root entity vertex
 * @return set of VertexInfo for all composite entities
 * @throws AtlasException
 */
public Collection<GraphHelper.VertexInfo> getOwnedVertices(AtlasVertex entityVertex) throws AtlasBaseException {
    Map<String, GraphHelper.VertexInfo> vertexInfoMap = new HashMap<>();
    Stack<AtlasVertex> vertices = new Stack<>();
    vertices.push(entityVertex);
    while (vertices.size() > 0) {
        AtlasVertex vertex = vertices.pop();
        AtlasEntity.Status state = getState(vertex);
        if (state == DELETED) {
            // If the reference vertex is marked for deletion, skip it
            continue;
        }
        String guid = GraphHelper.getGuid(vertex);
        if (vertexInfoMap.containsKey(guid)) {
            continue;
        }
        AtlasObjectId entity = entityRetriever.toAtlasObjectId(vertex);
        String typeName = entity.getTypeName();
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
        if (entityType == null) {
            throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), typeName);
        }
        vertexInfoMap.put(guid, new GraphHelper.VertexInfo(entity, vertex));
        for (AtlasStructType.AtlasAttribute attributeInfo : entityType.getAllAttributes().values()) {
            if (!attributeInfo.isOwnedRef()) {
                continue;
            }
            String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(entityType, attributeInfo.getName());
            AtlasType attrType = attributeInfo.getAttributeType();
            switch(attrType.getTypeCategory()) {
                case OBJECT_ID_TYPE:
                    {
                        AtlasEdge edge = graphHelper.getEdgeForLabel(vertex, edgeLabel);
                        if (edge != null && getState(edge) == AtlasEntity.Status.ACTIVE) {
                            vertices.push(edge.getInVertex());
                        }
                    }
                    break;
                case ARRAY:
                    {
                        AtlasArrayType arrType = (AtlasArrayType) attrType;
                        if (arrType.getElementType().getTypeCategory() != TypeCategory.OBJECT_ID_TYPE) {
                            continue;
                        }
                        Iterator<AtlasEdge> edges = graphHelper.getOutGoingEdgesByLabel(vertex, edgeLabel);
                        if (edges != null) {
                            while (edges.hasNext()) {
                                AtlasEdge edge = edges.next();
                                if (edge != null && getState(edge) == AtlasEntity.Status.ACTIVE) {
                                    vertices.push(edge.getInVertex());
                                }
                            }
                        }
                    }
                    break;
                case MAP:
                    {
                        AtlasMapType mapType = (AtlasMapType) attrType;
                        TypeCategory valueTypeCategory = mapType.getValueType().getTypeCategory();
                        if (valueTypeCategory != TypeCategory.OBJECT_ID_TYPE) {
                            continue;
                        }
                        String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(entityType, attributeInfo.getName());
                        List<String> keys = vertex.getProperty(propertyName, List.class);
                        if (keys != null) {
                            for (String key : keys) {
                                String mapEdgeLabel = GraphHelper.getQualifiedNameForMapKey(edgeLabel, key);
                                AtlasEdge edge = graphHelper.getEdgeForLabel(vertex, mapEdgeLabel);
                                if (edge != null && getState(edge) == AtlasEntity.Status.ACTIVE) {
                                    vertices.push(edge.getInVertex());
                                }
                            }
                        }
                    }
                    break;
            }
        }
    }
    return vertexInfoMap.values();
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasArrayType(org.apache.atlas.type.AtlasArrayType) GraphHelper(org.apache.atlas.repository.graph.GraphHelper) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasMapType(org.apache.atlas.type.AtlasMapType) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) TypeCategory(org.apache.atlas.model.TypeCategory) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 4 with AtlasMapType

use of org.apache.atlas.type.AtlasMapType in project atlas by apache.

the class EntityGraphRetriever method mapVertexToAttribute.

private Object mapVertexToAttribute(AtlasVertex entityVertex, AtlasAttribute attribute, AtlasEntityExtInfo entityExtInfo) throws AtlasBaseException {
    Object ret = null;
    AtlasType attrType = attribute.getAttributeType();
    String vertexPropertyName = attribute.getQualifiedName();
    String edgeLabel = EDGE_LABEL_PREFIX + vertexPropertyName;
    boolean isOwnedAttribute = attribute.isOwnedRef();
    AtlasRelationshipEdgeDirection edgeDirection = attribute.getRelationshipEdgeDirection();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Mapping vertex {} to atlas entity {}.{}", entityVertex, attribute.getDefinedInDef().getName(), attribute.getName());
    }
    switch(attrType.getTypeCategory()) {
        case PRIMITIVE:
            ret = mapVertexToPrimitive(entityVertex, vertexPropertyName, attribute.getAttributeDef());
            break;
        case ENUM:
            ret = GraphHelper.getProperty(entityVertex, vertexPropertyName);
            break;
        case STRUCT:
            ret = mapVertexToStruct(entityVertex, edgeLabel, null, entityExtInfo);
            break;
        case OBJECT_ID_TYPE:
            ret = mapVertexToObjectId(entityVertex, edgeLabel, null, entityExtInfo, isOwnedAttribute, edgeDirection);
            break;
        case ARRAY:
            ret = mapVertexToArray(entityVertex, (AtlasArrayType) attrType, vertexPropertyName, entityExtInfo, isOwnedAttribute, edgeDirection);
            break;
        case MAP:
            ret = mapVertexToMap(entityVertex, (AtlasMapType) attrType, vertexPropertyName, entityExtInfo, isOwnedAttribute, edgeDirection);
            break;
        case CLASSIFICATION:
            // do nothing
            break;
    }
    return ret;
}
Also used : AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasRelationshipEdgeDirection(org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection) AtlasType(org.apache.atlas.type.AtlasType) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Example 5 with AtlasMapType

use of org.apache.atlas.type.AtlasMapType in project atlas by apache.

the class AtlasEntityStoreV1Test method validateAttribute.

private void validateAttribute(AtlasEntityExtInfo entityExtInfo, Object actual, Object expected, AtlasType attributeType, String attrName) throws AtlasBaseException, AtlasException {
    switch(attributeType.getTypeCategory()) {
        case OBJECT_ID_TYPE:
            Assert.assertTrue(actual instanceof AtlasObjectId);
            String guid = ((AtlasObjectId) actual).getGuid();
            Assert.assertTrue(AtlasTypeUtil.isAssignedGuid(guid), "expected assigned guid. found " + guid);
            break;
        case PRIMITIVE:
        case ENUM:
            Assert.assertEquals(actual, expected);
            break;
        case MAP:
            AtlasMapType mapType = (AtlasMapType) attributeType;
            AtlasType valueType = mapType.getValueType();
            Map actualMap = (Map) actual;
            Map expectedMap = (Map) expected;
            if (MapUtils.isNotEmpty(expectedMap)) {
                Assert.assertTrue(MapUtils.isNotEmpty(actualMap));
                // deleted entries are included in the attribute; hence use >=
                Assert.assertTrue(actualMap.size() >= expectedMap.size());
                for (Object key : expectedMap.keySet()) {
                    validateAttribute(entityExtInfo, actualMap.get(key), expectedMap.get(key), valueType, attrName);
                }
            }
            break;
        case ARRAY:
            AtlasArrayType arrType = (AtlasArrayType) attributeType;
            AtlasType elemType = arrType.getElementType();
            List actualList = (List) actual;
            List expectedList = (List) expected;
            if (CollectionUtils.isNotEmpty(expectedList)) {
                Assert.assertTrue(CollectionUtils.isNotEmpty(actualList));
                // actual list could have deleted entities. Hence size may not match.
                Assert.assertTrue(actualList.size() >= expectedList.size());
                for (int i = 0; i < expectedList.size(); i++) {
                    validateAttribute(entityExtInfo, actualList.get(i), expectedList.get(i), elemType, attrName);
                }
            }
            break;
        case STRUCT:
            AtlasStruct expectedStruct = (AtlasStruct) expected;
            AtlasStruct actualStruct = (AtlasStruct) actual;
            validateEntity(entityExtInfo, actualStruct, expectedStruct);
            break;
        default:
            Assert.fail("Unknown type category");
    }
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) List(java.util.List) ArrayList(java.util.ArrayList) TestUtilsV2.randomString(org.apache.atlas.TestUtilsV2.randomString) Map(java.util.Map) HashMap(java.util.HashMap) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Aggregations

AtlasMapType (org.apache.atlas.type.AtlasMapType)19 AtlasType (org.apache.atlas.type.AtlasType)13 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)9 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)9 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)6 AtlasStructType (org.apache.atlas.type.AtlasStructType)6 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)6 TypeCategory (org.apache.atlas.model.TypeCategory)4 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtlasException (org.apache.atlas.AtlasException)2 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)2 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)2 GraphHelper (org.apache.atlas.repository.graph.GraphHelper)2 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)2 AtlasClassificationType (org.apache.atlas.type.AtlasClassificationType)2