Search in sources :

Example 11 with AtlasMapType

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

the class AtlasMapFormatConverter method fromV1ToV2.

@Override
public Map fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException {
    Map ret = null;
    if (v1Obj != null) {
        if (v1Obj instanceof Map) {
            AtlasMapType mapType = (AtlasMapType) type;
            AtlasType keyType = mapType.getKeyType();
            AtlasType valueType = mapType.getValueType();
            AtlasFormatConverter keyConverter = converterRegistry.getConverter(keyType.getTypeCategory());
            AtlasFormatConverter valueConverter = converterRegistry.getConverter(valueType.getTypeCategory());
            Map v1Map = (Map) v1Obj;
            ret = new HashMap<>();
            for (Object key : v1Map.keySet()) {
                Object value = v1Map.get(key);
                Object v2Key = keyConverter.fromV1ToV2(key, keyType, ctx);
                Object v2Value = valueConverter.fromV1ToV2(value, valueType, ctx);
                ret.put(v2Key, v2Value);
            }
        } else {
            throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map", v1Obj.getClass().getCanonicalName());
        }
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType) Map(java.util.Map) HashMap(java.util.HashMap) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Example 12 with AtlasMapType

use of org.apache.atlas.type.AtlasMapType in project 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 {
    if (LOG.isDebugEnabled()) {
        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()) {
            if (LOG.isDebugEnabled()) {
                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, instanceVertex);
                            }
                        }
                    }
                    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);
                            }
                        }
                    }
                    break;
            }
        }
    }
    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 13 with AtlasMapType

use of org.apache.atlas.type.AtlasMapType in project 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)

Example 14 with AtlasMapType

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

the class AtlasMapFormatConverter method isValidValueV1.

@Override
public boolean isValidValueV1(Object v1Obj, AtlasType type) {
    boolean ret = false;
    if (v1Obj == null) {
        return true;
    }
    if (type instanceof AtlasMapType && v1Obj instanceof Map) {
        AtlasMapType mapType = (AtlasMapType) type;
        AtlasType keyType = mapType.getKeyType();
        AtlasType valueType = mapType.getValueType();
        AtlasFormatConverter keyConverter = null;
        AtlasFormatConverter valueConverter = null;
        Map v1Map = (Map) v1Obj;
        try {
            keyConverter = converterRegistry.getConverter(keyType.getTypeCategory());
            valueConverter = converterRegistry.getConverter(valueType.getTypeCategory());
        } catch (AtlasBaseException excp) {
            LOG.warn("failed to get key/value converter. type={}", type.getTypeName(), excp);
            ret = false;
        }
        if (keyConverter != null && valueConverter != null) {
            // for empty map
            ret = true;
            for (Object key : v1Map.keySet()) {
                Object value = v1Map.get(key);
                ret = keyConverter.isValidValueV1(key, keyType) && valueConverter.isValidValueV1(value, valueType);
                if (!ret) {
                    break;
                }
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("AtlasArrayFormatConverter.isValidValueV1(type={}, value={}): {}", (v1Obj != null ? v1Obj.getClass().getCanonicalName() : null), v1Obj, ret);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType) Map(java.util.Map) HashMap(java.util.HashMap) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Example 15 with AtlasMapType

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

the class AtlasMapFormatConverter method fromV2ToV1.

@Override
public Map fromV2ToV1(Object v2Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException {
    Map ret = null;
    if (v2Obj != null) {
        if (v2Obj instanceof Map) {
            AtlasMapType mapType = (AtlasMapType) type;
            AtlasType keyType = mapType.getKeyType();
            AtlasType valueType = mapType.getValueType();
            AtlasFormatConverter keyConverter = converterRegistry.getConverter(keyType.getTypeCategory());
            AtlasFormatConverter valueConverter = converterRegistry.getConverter(valueType.getTypeCategory());
            Map v2Map = (Map) v2Obj;
            ret = new HashMap<>();
            for (Object key : v2Map.keySet()) {
                Object value = v2Map.get(key);
                Object v2Key = keyConverter.fromV2ToV1(key, keyType, ctx);
                Object v2Value = valueConverter.fromV2ToV1(value, valueType, ctx);
                ret.put(v2Key, v2Value);
            }
        } else {
            throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map", v2Obj.getClass().getCanonicalName());
        }
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType) 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