Search in sources :

Example 1 with AtlasType

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

the class EntityGraphMapper method removeUnusedArrayEntries.

//Removes unused edges from the old collection, compared to the new collection
private List<AtlasEdge> removeUnusedArrayEntries(AtlasAttribute attribute, List<AtlasEdge> currentEntries, List<AtlasEdge> newEntries) throws AtlasBaseException {
    if (CollectionUtils.isNotEmpty(currentEntries)) {
        AtlasStructType entityType = attribute.getDefinedInType();
        AtlasType entryType = ((AtlasArrayType) attribute.getAttributeType()).getElementType();
        if (AtlasGraphUtilsV1.isReference(entryType)) {
            Collection<AtlasEdge> edgesToRemove = CollectionUtils.subtract(currentEntries, newEntries);
            if (CollectionUtils.isNotEmpty(edgesToRemove)) {
                List<AtlasEdge> additionalElements = new ArrayList<>();
                for (AtlasEdge edge : edgesToRemove) {
                    boolean deleted = deleteHandler.deleteEdgeReference(edge, entryType.getTypeCategory(), attribute.isOwnedRef(), true);
                    if (!deleted) {
                        additionalElements.add(edge);
                    }
                }
                return additionalElements;
            }
        }
    }
    return Collections.emptyList();
}
Also used : AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasType(org.apache.atlas.type.AtlasType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 2 with AtlasType

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

the class EntityGraphMapper method mapArrayValue.

public List mapArrayValue(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapArrayValue({})", ctx);
    }
    AtlasAttribute attribute = ctx.getAttribute();
    List newElements = (List) ctx.getValue();
    AtlasArrayType arrType = (AtlasArrayType) attribute.getAttributeType();
    AtlasType elementType = arrType.getElementType();
    boolean isReference = AtlasGraphUtilsV1.isReference(elementType);
    AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute();
    Cardinality cardinality = attribute.getAttributeDef().getCardinality();
    List<Object> newElementsCreated = new ArrayList<>();
    List<Object> currentElements;
    if (isRelationshipAttribute(attribute)) {
        currentElements = getArrayElementsUsingRelationship(ctx.getReferringVertex(), attribute, elementType);
    } else {
        currentElements = getArrayElementsProperty(elementType, ctx.getReferringVertex(), ctx.getVertexProperty());
    }
    if (CollectionUtils.isNotEmpty(newElements)) {
        if (cardinality == SET) {
            newElements = (List) newElements.stream().distinct().collect(Collectors.toList());
        }
        for (int index = 0; index < newElements.size(); index++) {
            AtlasEdge existingEdge = getEdgeAt(currentElements, index, elementType);
            AttributeMutationContext arrCtx = new AttributeMutationContext(ctx.getOp(), ctx.getReferringVertex(), ctx.getAttribute(), newElements.get(index), ctx.getVertexProperty(), elementType, existingEdge);
            Object newEntry = mapCollectionElementsToVertex(arrCtx, context);
            if (isReference && newEntry instanceof AtlasEdge && inverseRefAttribute != null) {
                // Update the inverse reference value.
                AtlasEdge newEdge = (AtlasEdge) newEntry;
                addInverseReference(inverseRefAttribute, newEdge, getRelationshipAttributes(ctx.getValue()));
            }
            newElementsCreated.add(newEntry);
        }
    }
    if (isReference) {
        List<AtlasEdge> additionalEdges = removeUnusedArrayEntries(attribute, (List) currentElements, (List) newElementsCreated, ctx.getReferringVertex());
        newElementsCreated.addAll(additionalEdges);
    }
    // for dereference on way out
    setArrayElementsProperty(elementType, ctx.getReferringVertex(), ctx.getVertexProperty(), newElementsCreated);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapArrayValue({})", ctx);
    }
    return newElementsCreated;
}
Also used : AtlasArrayType(org.apache.atlas.type.AtlasArrayType) Cardinality(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality) AtlasType(org.apache.atlas.type.AtlasType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute)

Example 3 with AtlasType

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

the class EntityGraphMapper method mapObjectIdValueUsingRelationship.

private AtlasEdge mapObjectIdValueUsingRelationship(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapObjectIdValueUsingRelationship({})", ctx);
    }
    AtlasVertex attributeVertex = context.getDiscoveryContext().getResolvedEntityVertex(getGuid(ctx.getValue()));
    AtlasVertex entityVertex = ctx.getReferringVertex();
    AtlasEdge ret;
    if (attributeVertex == null) {
        AtlasObjectId objectId = getObjectId(ctx.getValue());
        attributeVertex = (objectId != null) ? context.getDiscoveryContext().getResolvedEntityVertex(objectId) : null;
    }
    if (attributeVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_OBJECT_ID, (ctx.getValue() == null ? null : ctx.getValue().toString()));
    }
    String attributeName = ctx.getAttribute().getName();
    AtlasType type = typeRegistry.getType(AtlasGraphUtilsV1.getTypeName(entityVertex));
    AtlasRelationshipEdgeDirection edgeDirection = ctx.getAttribute().getRelationshipEdgeDirection();
    String edgeLabel = ctx.getAttribute().getRelationshipEdgeLabel();
    if (type instanceof AtlasEntityType) {
        AtlasEntityType entityType = (AtlasEntityType) type;
        // use relationship to create/update edges
        if (entityType.hasRelationshipAttribute(attributeName)) {
            Map<String, Object> relationshipAttributes = getRelationshipAttributes(ctx.getValue());
            if (ctx.getCurrentEdge() != null) {
                ret = updateRelationship(ctx.getCurrentEdge(), entityVertex, attributeVertex, edgeDirection, relationshipAttributes);
            } else {
                String relationshipName = graphHelper.getRelationshipDefName(entityVertex, entityType, attributeName);
                AtlasVertex fromVertex;
                AtlasVertex toVertex;
                if (edgeDirection == IN) {
                    fromVertex = attributeVertex;
                    toVertex = entityVertex;
                } else {
                    fromVertex = entityVertex;
                    toVertex = attributeVertex;
                }
                boolean relationshipExists = isRelationshipExists(fromVertex, toVertex, edgeLabel);
                ret = getOrCreateRelationship(fromVertex, toVertex, relationshipName, relationshipAttributes);
                // for import use the relationship guid provided
                if (context.isImport()) {
                    AtlasGraphUtilsV1.setProperty(ret, Constants.GUID_PROPERTY_KEY, getRelationshipGuid(ctx.getValue()));
                }
                // record entity update on both relationship vertices
                if (!relationshipExists) {
                    recordEntityUpdate(attributeVertex);
                }
            }
        } else {
            // use legacy way to create/update edges
            if (LOG.isDebugEnabled()) {
                LOG.debug("No RelationshipDef defined between {} and {} on attribute: {}", getTypeName(entityVertex), getTypeName(attributeVertex), attributeName);
            }
            ret = mapObjectIdValue(ctx, context);
        }
    } else {
        // if type is StructType having objectid as attribute
        ret = mapObjectIdValue(ctx, context);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapObjectIdValueUsingRelationship({})", ctx);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasRelationshipEdgeDirection(org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 4 with AtlasType

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

the class AtlasStructDefStoreV1 method updateByName.

@Override
public AtlasStructDef updateByName(String name, AtlasStructDef structDef) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> AtlasStructDefStoreV1.updateByName({}, {})", name, structDef);
    }
    AtlasStructDef existingDef = typeRegistry.getStructDefByName(name);
    AtlasAuthorizationUtils.verifyAccess(new AtlasTypeAccessRequest(AtlasPrivilege.TYPE_UPDATE, existingDef), "update struct-def ", name);
    validateType(structDef);
    AtlasType type = typeRegistry.getType(structDef.getName());
    if (type.getTypeCategory() != org.apache.atlas.model.TypeCategory.STRUCT) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_MATCH_FAILED, structDef.getName(), TypeCategory.STRUCT.name());
    }
    AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.STRUCT);
    if (vertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_NOT_FOUND, name);
    }
    AtlasStructDefStoreV1.updateVertexPreUpdate(structDef, (AtlasStructType) type, vertex, typeDefStore);
    AtlasStructDefStoreV1.updateVertexAddReferences(structDef, vertex, typeDefStore);
    AtlasStructDef ret = toStructDef(vertex);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== AtlasStructDefStoreV1.updateByName({}, {}): {}", name, structDef, ret);
    }
    return ret;
}
Also used : AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasTypeAccessRequest(org.apache.atlas.authorize.AtlasTypeAccessRequest) AtlasType(org.apache.atlas.type.AtlasType)

Example 5 with AtlasType

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

the class AtlasStructDefStoreV1 method preCreate.

@Override
public AtlasVertex preCreate(AtlasStructDef structDef) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> AtlasStructDefStoreV1.preCreate({})", structDef);
    }
    validateType(structDef);
    AtlasType type = typeRegistry.getType(structDef.getName());
    if (type.getTypeCategory() != org.apache.atlas.model.TypeCategory.STRUCT) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_MATCH_FAILED, structDef.getName(), TypeCategory.STRUCT.name());
    }
    AtlasVertex ret = typeDefStore.findTypeVertexByName(structDef.getName());
    if (ret != null) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_ALREADY_EXISTS, structDef.getName());
    }
    ret = typeDefStore.createTypeVertex(structDef);
    AtlasStructDefStoreV1.updateVertexPreCreate(structDef, (AtlasStructType) type, ret, typeDefStore);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== AtlasStructDefStoreV1.preCreate({}): {}", structDef, ret);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasType(org.apache.atlas.type.AtlasType)

Aggregations

AtlasType (org.apache.atlas.type.AtlasType)95 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)51 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)33 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)23 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)17 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)17 AtlasStructType (org.apache.atlas.type.AtlasStructType)16 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)15 AtlasMapType (org.apache.atlas.type.AtlasMapType)13 ArrayList (java.util.ArrayList)11 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)9 HashMap (java.util.HashMap)8 AtlasTypeAccessRequest (org.apache.atlas.authorize.AtlasTypeAccessRequest)8 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)8 AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)8 Map (java.util.Map)7 List (java.util.List)6 Collection (java.util.Collection)5 LinkedHashSet (java.util.LinkedHashSet)5 Set (java.util.Set)4