Search in sources :

Example 1 with AttributeInfo

use of org.apache.atlas.typesystem.types.AttributeInfo in project incubator-atlas by apache.

the class DeleteHandler 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 AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Deleting {}", string(instanceVertex));
    }
    String typeName = GraphHelper.getTypeName(instanceVertex);
    IDataType type = typeSystem.getDataType(IDataType.class, typeName);
    FieldMapping fieldMapping = getFieldMapping(type);
    for (AttributeInfo attributeInfo : fieldMapping.fields.values()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Deleting attribute {} for {}", attributeInfo.name, string(instanceVertex));
        }
        String edgeLabel = GraphHelper.getEdgeLabel(type, attributeInfo);
        switch(attributeInfo.dataType().getTypeCategory()) {
            case CLASS:
                //If its class attribute, delete the reference
                deleteEdgeReference(instanceVertex, edgeLabel, DataTypes.TypeCategory.CLASS, attributeInfo.isComposite);
                break;
            case STRUCT:
                //If its struct attribute, delete the reference
                deleteEdgeReference(instanceVertex, edgeLabel, DataTypes.TypeCategory.STRUCT, false);
                break;
            case ARRAY:
                //For array attribute, if the element is struct/class, delete all the references
                IDataType elementType = ((DataTypes.ArrayType) attributeInfo.dataType()).getElemType();
                DataTypes.TypeCategory elementTypeCategory = elementType.getTypeCategory();
                if (elementTypeCategory == DataTypes.TypeCategory.STRUCT || elementTypeCategory == DataTypes.TypeCategory.CLASS) {
                    Iterator<AtlasEdge> edges = graphHelper.getOutGoingEdgesByLabel(instanceVertex, edgeLabel);
                    if (edges != null) {
                        while (edges.hasNext()) {
                            AtlasEdge edge = edges.next();
                            deleteEdgeReference(edge, elementType.getTypeCategory(), attributeInfo.isComposite, false);
                        }
                    }
                }
                break;
            case MAP:
                //For map attribute, if the value type is struct/class, delete all the references
                DataTypes.MapType mapType = (DataTypes.MapType) attributeInfo.dataType();
                DataTypes.TypeCategory valueTypeCategory = mapType.getValueType().getTypeCategory();
                String propertyName = GraphHelper.getQualifiedFieldName(type, attributeInfo.name);
                if (valueTypeCategory == DataTypes.TypeCategory.STRUCT || valueTypeCategory == DataTypes.TypeCategory.CLASS) {
                    List<String> keys = GraphHelper.getListProperty(instanceVertex, propertyName);
                    if (keys != null) {
                        for (String key : keys) {
                            String mapEdgeLabel = GraphHelper.getQualifiedNameForMapKey(edgeLabel, key);
                            deleteEdgeReference(instanceVertex, mapEdgeLabel, valueTypeCategory, attributeInfo.isComposite);
                        }
                    }
                }
        }
    }
    deleteVertex(instanceVertex, force);
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) FieldMapping(org.apache.atlas.typesystem.types.FieldMapping) IDataType(org.apache.atlas.typesystem.types.IDataType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) DataTypes(org.apache.atlas.typesystem.types.DataTypes)

Example 2 with AttributeInfo

use of org.apache.atlas.typesystem.types.AttributeInfo in project incubator-atlas by apache.

the class DeleteHandler method deleteEdgeBetweenVertices.

/**
     * Deletes the edge between outvertex and inVertex. The edge is for attribute attributeName of outVertex
     * @param outVertex
     * @param inVertex
     * @param attributeName
     * @throws AtlasException
     */
protected void deleteEdgeBetweenVertices(AtlasVertex outVertex, AtlasVertex inVertex, String attributeName) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Removing edge from {} to {} with attribute name {}", string(outVertex), string(inVertex), attributeName);
    }
    String typeName = GraphHelper.getTypeName(outVertex);
    String outId = GraphHelper.getGuid(outVertex);
    Id.EntityState state = GraphHelper.getState(outVertex);
    if ((outId != null && RequestContext.get().isDeletedEntity(outId)) || state == Id.EntityState.DELETED) {
        //If the reference vertex is marked for deletion, skip updating the reference
        return;
    }
    IDataType type = typeSystem.getDataType(IDataType.class, typeName);
    AttributeInfo attributeInfo = getFieldMapping(type).fields.get(attributeName);
    String propertyName = GraphHelper.getQualifiedFieldName(type, attributeName);
    String edgeLabel = EDGE_LABEL_PREFIX + propertyName;
    AtlasEdge edge = null;
    switch(attributeInfo.dataType().getTypeCategory()) {
        case CLASS:
            //If its class attribute, its the only edge between two vertices
            if (attributeInfo.multiplicity.nullAllowed()) {
                edge = graphHelper.getEdgeForLabel(outVertex, edgeLabel);
                if (shouldUpdateReverseAttribute) {
                    GraphHelper.setProperty(outVertex, propertyName, null);
                }
            } else {
                // Cannot unset a required attribute.
                throw new NullRequiredAttributeException("Cannot unset required attribute " + GraphHelper.getQualifiedFieldName(type, attributeName) + " on " + GraphHelper.getVertexDetails(outVertex) + " edge = " + edgeLabel);
            }
            break;
        case ARRAY:
            //If its array attribute, find the right edge between the two vertices and update array property
            List<String> elements = GraphHelper.getListProperty(outVertex, propertyName);
            if (elements != null) {
                //Make a copy, else list.remove reflects on titan.getProperty()
                elements = new ArrayList<>(elements);
                for (String elementEdgeId : elements) {
                    AtlasEdge elementEdge = graphHelper.getEdgeByEdgeId(outVertex, edgeLabel, elementEdgeId);
                    if (elementEdge == null) {
                        continue;
                    }
                    AtlasVertex elementVertex = elementEdge.getInVertex();
                    if (elementVertex.equals(inVertex)) {
                        edge = elementEdge;
                        //TODO element.size includes deleted items as well. should exclude
                        if (!attributeInfo.multiplicity.nullAllowed() && elements.size() <= attributeInfo.multiplicity.lower) {
                            // Deleting this edge would violate the attribute's lower bound.
                            throw new NullRequiredAttributeException("Cannot remove array element from required attribute " + GraphHelper.getQualifiedFieldName(type, attributeName) + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(elementEdge));
                        }
                        if (shouldUpdateReverseAttribute) {
                            //but when column is deleted, table will not reference the deleted column
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Removing edge {} from the array attribute {}", string(elementEdge), attributeName);
                            }
                            // Remove all occurrences of the edge ID from the list.
                            // This prevents dangling edge IDs (i.e. edge IDs for deleted edges)
                            // from the remaining in the list if there are duplicates.
                            elements.removeAll(Collections.singletonList(elementEdge.getId().toString()));
                            GraphHelper.setProperty(outVertex, propertyName, elements);
                            break;
                        }
                    }
                }
            }
            break;
        case MAP:
            //If its map attribute, find the right edge between two vertices and update map property
            List<String> keys = GraphHelper.getListProperty(outVertex, propertyName);
            if (keys != null) {
                //Make a copy, else list.remove reflects on titan.getProperty()
                keys = new ArrayList<>(keys);
                for (String key : keys) {
                    String keyPropertyName = GraphHelper.getQualifiedNameForMapKey(propertyName, key);
                    String mapEdgeId = GraphHelper.getSingleValuedProperty(outVertex, keyPropertyName, String.class);
                    AtlasEdge mapEdge = graphHelper.getEdgeByEdgeId(outVertex, keyPropertyName, mapEdgeId);
                    if (mapEdge != null) {
                        AtlasVertex mapVertex = mapEdge.getInVertex();
                        if (mapVertex.getId().toString().equals(inVertex.getId().toString())) {
                            //TODO keys.size includes deleted items as well. should exclude
                            if (attributeInfo.multiplicity.nullAllowed() || keys.size() > attributeInfo.multiplicity.lower) {
                                edge = mapEdge;
                            } else {
                                // Deleting this entry would violate the attribute's lower bound.
                                throw new NullRequiredAttributeException("Cannot remove map entry " + keyPropertyName + " from required attribute " + GraphHelper.getQualifiedFieldName(type, attributeName) + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(mapEdge));
                            }
                            if (shouldUpdateReverseAttribute) {
                                //remove this key
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("Removing edge {}, key {} from the map attribute {}", string(mapEdge), key, attributeName);
                                }
                                keys.remove(key);
                                GraphHelper.setProperty(outVertex, propertyName, keys);
                                GraphHelper.setProperty(outVertex, keyPropertyName, null);
                            }
                            break;
                        }
                    }
                }
            }
            break;
        case STRUCT:
        case TRAIT:
            break;
        default:
            throw new IllegalStateException("There can't be an edge from " + GraphHelper.getVertexDetails(outVertex) + " to " + GraphHelper.getVertexDetails(inVertex) + " with attribute name " + attributeName + " which is not class/array/map attribute");
    }
    if (edge != null) {
        deleteEdge(edge, false);
        RequestContext requestContext = RequestContext.get();
        GraphHelper.setProperty(outVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, requestContext.getRequestTime());
        GraphHelper.setProperty(outVertex, Constants.MODIFIED_BY_KEY, requestContext.getUser());
        requestContext.recordEntityUpdate(outId);
    }
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) Id(org.apache.atlas.typesystem.persistence.Id) RequestContext(org.apache.atlas.RequestContext) IDataType(org.apache.atlas.typesystem.types.IDataType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) NullRequiredAttributeException(org.apache.atlas.typesystem.exception.NullRequiredAttributeException)

Example 3 with AttributeInfo

use of org.apache.atlas.typesystem.types.AttributeInfo in project incubator-atlas by apache.

the class Gremlin3ExpressionFactory method generateHasExpression.

@Override
public GroovyExpression generateHasExpression(GraphPersistenceStrategies s, GroovyExpression parent, String propertyName, String symbol, GroovyExpression requiredValue, FieldInfo fInfo) throws AtlasException {
    AttributeInfo attrInfo = fInfo.attrInfo();
    IDataType attrType = attrInfo.dataType();
    GroovyExpression propertNameExpr = new LiteralExpression(propertyName);
    if (s.isPropertyValueConversionNeeded(attrType)) {
        // for some types, the logical value cannot be stored directly in
        // the underlying graph,
        // and conversion logic is needed to convert the persistent form of
        // the value
        // to the actual value. In cases like this, we generate a conversion
        // expression to
        // do this conversion and use the filter step to perform the
        // comparsion in the gremlin query
        GroovyExpression itExpr = getItVariable();
        GroovyExpression vertexExpr = new CastExpression(new FunctionCallExpression(itExpr, GET_METHOD), VERTEX_CLASS);
        GroovyExpression propertyValueExpr = new FunctionCallExpression(vertexExpr, VALUE_METHOD, propertNameExpr);
        GroovyExpression conversionExpr = s.generatePersisentToLogicalConversionExpression(propertyValueExpr, attrType);
        GroovyExpression propertyIsPresentExpression = new FunctionCallExpression(new FunctionCallExpression(vertexExpr, PROPERTY_METHOD, propertNameExpr), IS_PRESENT_METHOD);
        GroovyExpression valueMatchesExpr = new ComparisonExpression(conversionExpr, getGroovyOperator(symbol), requiredValue);
        GroovyExpression filterCondition = new LogicalExpression(propertyIsPresentExpression, LogicalOperator.AND, valueMatchesExpr);
        GroovyExpression filterFunction = new ClosureExpression(filterCondition);
        return new FunctionCallExpression(TraversalStepType.FILTER, parent, FILTER_METHOD, filterFunction);
    } else {
        GroovyExpression valueMatches = new FunctionCallExpression(getComparisonFunction(symbol), requiredValue);
        return new FunctionCallExpression(TraversalStepType.FILTER, parent, HAS_METHOD, propertNameExpr, valueMatches);
    }
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) ComparisonExpression(org.apache.atlas.groovy.ComparisonExpression) LogicalExpression(org.apache.atlas.groovy.LogicalExpression) LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) CastExpression(org.apache.atlas.groovy.CastExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) IDataType(org.apache.atlas.typesystem.types.IDataType) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 4 with AttributeInfo

use of org.apache.atlas.typesystem.types.AttributeInfo in project incubator-atlas by apache.

the class EntityAuditListener method pruneEntityAttributesForAudit.

private Map<String, Object> pruneEntityAttributesForAudit(ITypedReferenceableInstance entity) throws AtlasException {
    Map<String, Object> ret = null;
    Map<String, Object> entityAttributes = entity.getValuesMap();
    List<String> excludeAttributes = auditRepository.getAuditExcludeAttributes(entity.getTypeName());
    if (CollectionUtils.isNotEmpty(excludeAttributes) && MapUtils.isNotEmpty(entityAttributes)) {
        Map<String, AttributeInfo> attributeInfoMap = entity.fieldMapping().fields;
        for (String attrName : entityAttributes.keySet()) {
            Object attrValue = entityAttributes.get(attrName);
            AttributeInfo attrInfo = attributeInfoMap.get(attrName);
            if (excludeAttributes.contains(attrName)) {
                if (ret == null) {
                    ret = new HashMap<>();
                }
                ret.put(attrName, attrValue);
                entity.setNull(attrName);
            } else if (attrInfo.isComposite) {
                if (attrValue instanceof Collection) {
                    for (Object attribute : (Collection) attrValue) {
                        if (attribute instanceof ITypedReferenceableInstance) {
                            ret = pruneAttributes(ret, (ITypedReferenceableInstance) attribute);
                        }
                    }
                } else if (attrValue instanceof ITypedReferenceableInstance) {
                    ret = pruneAttributes(ret, (ITypedReferenceableInstance) attrValue);
                }
            }
        }
    }
    return ret;
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Collection(java.util.Collection)

Example 5 with AttributeInfo

use of org.apache.atlas.typesystem.types.AttributeInfo in project incubator-atlas by apache.

the class EntityAuditListener method restoreEntityAttributes.

private void restoreEntityAttributes(ITypedReferenceableInstance entity, Map<String, Object> prunedAttributes) throws AtlasException {
    if (MapUtils.isEmpty(prunedAttributes)) {
        return;
    }
    Map<String, Object> entityAttributes = entity.getValuesMap();
    if (MapUtils.isNotEmpty(entityAttributes)) {
        Map<String, AttributeInfo> attributeInfoMap = entity.fieldMapping().fields;
        for (String attrName : entityAttributes.keySet()) {
            Object attrValue = entityAttributes.get(attrName);
            AttributeInfo attrInfo = attributeInfoMap.get(attrName);
            if (prunedAttributes.containsKey(attrName)) {
                entity.set(attrName, prunedAttributes.get(attrName));
            } else if (attrInfo.isComposite) {
                if (attrValue instanceof Collection) {
                    for (Object attributeEntity : (Collection) attrValue) {
                        if (attributeEntity instanceof ITypedReferenceableInstance) {
                            restoreAttributes(prunedAttributes, (ITypedReferenceableInstance) attributeEntity);
                        }
                    }
                } else if (attrValue instanceof ITypedReferenceableInstance) {
                    restoreAttributes(prunedAttributes, (ITypedReferenceableInstance) attrValue);
                }
            }
        }
    }
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Collection(java.util.Collection)

Aggregations

AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)47 AtlasException (org.apache.atlas.AtlasException)26 HashMap (java.util.HashMap)6 IDataType (org.apache.atlas.typesystem.types.IDataType)6 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)5 FieldMapping (org.apache.atlas.typesystem.types.FieldMapping)5 Map (java.util.Map)4 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)4 HierarchicalType (org.apache.atlas.typesystem.types.HierarchicalType)4 Test (org.testng.annotations.Test)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)3 Id (org.apache.atlas.typesystem.persistence.Id)3 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 ClosureExpression (org.apache.atlas.groovy.ClosureExpression)2 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)2 GroovyExpression (org.apache.atlas.groovy.GroovyExpression)2 LiteralExpression (org.apache.atlas.groovy.LiteralExpression)2