Search in sources :

Example 16 with AtlasMapType

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

the class EntityDiscoveryService method isRelationshipAttribute.

private boolean isRelationshipAttribute(AtlasAttribute attribute) throws AtlasBaseException {
    boolean ret = true;
    AtlasType attrType = attribute.getAttributeType();
    if (attrType.getTypeCategory() == ARRAY) {
        attrType = ((AtlasArrayType) attrType).getElementType();
    } else if (attrType.getTypeCategory() == MAP) {
        attrType = ((AtlasMapType) attrType).getValueType();
    }
    if (attrType.getTypeCategory() != OBJECT_ID_TYPE) {
        ret = false;
    }
    return ret;
}
Also used : AtlasType(org.apache.atlas.type.AtlasType) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Example 17 with AtlasMapType

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

the class DeleteHandlerV1 method deleteTypeVertex.

/**
 * Deleting any type vertex. Goes over the complex attributes and removes the references
 * @param instanceVertex
 * @throws AtlasException
 */
protected void deleteTypeVertex(AtlasVertex instanceVertex, boolean force) throws AtlasBaseException {
    LOG.debug("Deleting {}", string(instanceVertex));
    String typeName = GraphHelper.getTypeName(instanceVertex);
    AtlasType parentType = typeRegistry.getType(typeName);
    if (parentType instanceof AtlasStructType) {
        AtlasStructType structType = (AtlasStructType) parentType;
        boolean isEntityType = (parentType instanceof AtlasEntityType);
        for (AtlasStructType.AtlasAttribute attributeInfo : structType.getAllAttributes().values()) {
            LOG.debug("Deleting attribute {} for {}", attributeInfo.getName(), string(instanceVertex));
            boolean isOwned = isEntityType && attributeInfo.isOwnedRef();
            AtlasType attrType = attributeInfo.getAttributeType();
            String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(structType, attributeInfo.getName());
            switch(attrType.getTypeCategory()) {
                case OBJECT_ID_TYPE:
                    // If its class attribute, delete the reference
                    deleteEdgeReference(instanceVertex, edgeLabel, attrType.getTypeCategory(), isOwned);
                    break;
                case STRUCT:
                    // If its struct attribute, delete the reference
                    deleteEdgeReference(instanceVertex, edgeLabel, attrType.getTypeCategory(), false);
                    break;
                case ARRAY:
                    // For array attribute, if the element is struct/class, delete all the references
                    AtlasArrayType arrType = (AtlasArrayType) attrType;
                    AtlasType elemType = arrType.getElementType();
                    if (AtlasGraphUtilsV1.isReference(elemType.getTypeCategory())) {
                        Iterator<AtlasEdge> edges = graphHelper.getOutGoingEdgesByLabel(instanceVertex, edgeLabel);
                        if (edges != null) {
                            while (edges.hasNext()) {
                                AtlasEdge edge = edges.next();
                                deleteEdgeReference(edge, elemType.getTypeCategory(), isOwned, false);
                            }
                        }
                    }
                    break;
                case MAP:
                    // For map attribute, if the value type is struct/class, delete all the references
                    AtlasMapType mapType = (AtlasMapType) attrType;
                    AtlasType keyType = mapType.getKeyType();
                    TypeCategory valueTypeCategory = mapType.getValueType().getTypeCategory();
                    String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(structType, attributeInfo.getName());
                    if (AtlasGraphUtilsV1.isReference(valueTypeCategory)) {
                        List<Object> keys = EntityGraphMapper.getArrayElementsProperty(keyType, instanceVertex, propertyName);
                        if (keys != null) {
                            for (Object key : keys) {
                                String mapEdgeLabel = GraphHelper.getQualifiedNameForMapKey(edgeLabel, (String) key);
                                deleteEdgeReference(instanceVertex, mapEdgeLabel, valueTypeCategory, isOwned);
                            }
                        }
                    }
            }
        }
    }
    deleteVertex(instanceVertex, force);
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasType(org.apache.atlas.type.AtlasType) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasMapType(org.apache.atlas.type.AtlasMapType) TypeCategory(org.apache.atlas.model.TypeCategory) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 18 with AtlasMapType

use of org.apache.atlas.type.AtlasMapType in project incubator-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);
                }
            }
        }
        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 19 with AtlasMapType

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

the class ExportService method addType.

private void addType(AtlasType type, ExportContext context) {
    if (type.getTypeCategory() == TypeCategory.PRIMITIVE) {
        return;
    }
    if (type instanceof AtlasArrayType) {
        AtlasArrayType arrayType = (AtlasArrayType) type;
        addType(arrayType.getElementType(), context);
    } else if (type instanceof AtlasMapType) {
        AtlasMapType mapType = (AtlasMapType) type;
        addType(mapType.getKeyType(), context);
        addType(mapType.getValueType(), context);
    } else if (type instanceof AtlasEntityType) {
        addEntityType((AtlasEntityType) type, context);
    } else if (type instanceof AtlasClassificationType) {
        addClassificationType((AtlasClassificationType) type, context);
    } else if (type instanceof AtlasStructType) {
        addStructType((AtlasStructType) type, context);
    } else if (type instanceof AtlasEnumType) {
        addEnumType((AtlasEnumType) type, context);
    }
}
Also used : AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasEnumType(org.apache.atlas.type.AtlasEnumType) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) 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