Search in sources :

Example 91 with AtlasVertex

use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.

the class EntityLineageService method entityExists.

private boolean entityExists(String guid) {
    boolean ret = false;
    Iterator<AtlasVertex> results = graph.query().has(Constants.GUID_PROPERTY_KEY, guid).vertices().iterator();
    while (results.hasNext()) {
        AtlasVertex entityVertex = results.next();
        List<String> superTypes = GraphHelper.getSuperTypeNames(entityVertex);
        ret = (CollectionUtils.isNotEmpty(superTypes)) ? superTypes.contains(AtlasClient.DATA_SET_SUPER_TYPE) : false;
    }
    return ret;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex)

Example 92 with AtlasVertex

use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.

the class DefaultGraphPersistenceStrategy method constructInstance.

@Override
public <U> U constructInstance(IDataType<U> dataType, Object value) {
    try {
        switch(dataType.getTypeCategory()) {
            case PRIMITIVE:
            case ENUM:
                return dataType.convert(value, Multiplicity.OPTIONAL);
            case ARRAY:
                DataTypes.ArrayType arrType = (DataTypes.ArrayType) dataType;
                IDataType<?> elemType = arrType.getElemType();
                ImmutableCollection.Builder result = ImmutableList.builder();
                List list = (List) value;
                for (Object listElement : list) {
                    Object collectionEntry = constructCollectionEntry(elemType, listElement);
                    if (collectionEntry != null) {
                        result.add(collectionEntry);
                    }
                }
                return (U) result.build();
            case MAP:
                // todo
                break;
            case STRUCT:
                AtlasVertex structVertex = (AtlasVertex) value;
                StructType structType = (StructType) dataType;
                ITypedStruct structInstance = structType.createInstance();
                TypeSystem.IdType idType = TypeSystem.getInstance().getIdType();
                if (dataType.getName().equals(idType.getName())) {
                    structInstance.set(idType.typeNameAttrName(), GraphHelper.getSingleValuedProperty(structVertex, typeAttributeName(), String.class));
                    structInstance.set(idType.idAttrName(), GraphHelper.getSingleValuedProperty(structVertex, idAttributeName(), String.class));
                    String stateValue = GraphHelper.getSingleValuedProperty(structVertex, stateAttributeName(), String.class);
                    if (stateValue != null) {
                        structInstance.set(idType.stateAttrName(), stateValue);
                    }
                    structInstance.set(idType.versionAttrName(), structVertex.getProperty(versionAttributeName(), Integer.class));
                } else {
                    metadataRepository.getGraphToInstanceMapper().mapVertexToInstance(structVertex, structInstance, structType.fieldMapping().fields);
                }
                return dataType.convert(structInstance, Multiplicity.OPTIONAL);
            case TRAIT:
                AtlasVertex traitVertex = (AtlasVertex) value;
                TraitType traitType = (TraitType) dataType;
                ITypedStruct traitInstance = traitType.createInstance();
                // todo - this is not right, we should load the Instance associated with this
                // trait. for now just loading the trait struct.
                // metadataRepository.getGraphToInstanceMapper().mapVertexToTraitInstance(
                //        traitVertex, dataType.getName(), , traitType, traitInstance);
                metadataRepository.getGraphToInstanceMapper().mapVertexToInstance(traitVertex, traitInstance, traitType.fieldMapping().fields);
                break;
            case CLASS:
                AtlasVertex classVertex = (AtlasVertex) value;
                String guid = classVertex.getProperty(Constants.GUID_PROPERTY_KEY, String.class);
                // Check if the instance we need was previously loaded.
                ITypedReferenceableInstance classInstance = RequestContext.get().getInstanceV1(guid);
                if (classInstance == null) {
                    classInstance = metadataRepository.getGraphToInstanceMapper().mapGraphToTypedInstance(guid, classVertex);
                }
                return dataType.convert(classInstance, Multiplicity.OPTIONAL);
            default:
                throw new UnsupportedOperationException("Load for type " + dataType + "is not supported");
        }
    } catch (AtlasException e) {
        LOG.error("error while constructing an instance", e);
    }
    return null;
}
Also used : ImmutableCollection(com.google.common.collect.ImmutableCollection) TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) StructType(org.apache.atlas.typesystem.types.StructType) TraitType(org.apache.atlas.typesystem.types.TraitType) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) AtlasException(org.apache.atlas.AtlasException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) DataTypes(org.apache.atlas.typesystem.types.DataTypes)

Example 93 with AtlasVertex

use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.

the class DeleteHandlerV1 method deleteEdgeBetweenVertices.

/**
     * Deletes the edge between outvertex and inVertex. The edge is for attribute attributeName of outVertex
     * @param outVertex
     * @param inVertex
     * @param attribute
     * @throws AtlasException
     */
protected void deleteEdgeBetweenVertices(AtlasVertex outVertex, AtlasVertex inVertex, AtlasAttribute attribute) throws AtlasBaseException {
    LOG.debug("Removing edge from {} to {} with attribute name {}", string(outVertex), string(inVertex), attribute.getName());
    String typeName = GraphHelper.getTypeName(outVertex);
    String outId = GraphHelper.getGuid(outVertex);
    AtlasObjectId objId = new AtlasObjectId(outId, typeName);
    AtlasEntity.Status state = AtlasGraphUtilsV1.getState(outVertex);
    if (state == AtlasEntity.Status.DELETED || (outId != null && RequestContextV1.get().isDeletedEntity(objId))) {
        //If the reference vertex is marked for deletion, skip updating the reference
        return;
    }
    AtlasStructType parentType = (AtlasStructType) typeRegistry.getType(typeName);
    String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(parentType, attribute.getName());
    String edgeLabel = EDGE_LABEL_PREFIX + propertyName;
    AtlasEdge edge = null;
    AtlasAttributeDef attrDef = attribute.getAttributeDef();
    AtlasType attrType = attribute.getAttributeType();
    switch(attrType.getTypeCategory()) {
        case OBJECT_ID_TYPE:
            //If its class attribute, its the only edge between two vertices
            if (attrDef.getIsOptional()) {
                edge = graphHelper.getEdgeForLabel(outVertex, edgeLabel);
                if (shouldUpdateInverseReferences) {
                    GraphHelper.setProperty(outVertex, propertyName, null);
                }
            } else {
                // Cannot unset a required attribute.
                throw new AtlasBaseException("Cannot unset required attribute " + propertyName + " on " + GraphHelper.vertexString(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 (!attrDef.getIsOptional() && elements.size() <= attrDef.getValuesMinCount()) {
                            // Deleting this edge would violate the attribute's lower bound.
                            throw new AtlasBaseException("Cannot remove array element from required attribute " + propertyName + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(elementEdge));
                        }
                        if (shouldUpdateInverseReferences) {
                            //if composite attribute, remove the reference as well. else, just remove the edge
                            //for example, when table is deleted, process still references the table
                            //but when column is deleted, table will not reference the deleted column
                            LOG.debug("Removing edge {} from the array attribute {}", string(elementEdge), attribute.getName());
                            // 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 (attrDef.getIsOptional() || keys.size() > attrDef.getValuesMinCount()) {
                                edge = mapEdge;
                            } else {
                                // Deleting this entry would violate the attribute's lower bound.
                                throw new AtlasBaseException("Cannot remove map entry " + keyPropertyName + " from required attribute " + propertyName + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(mapEdge));
                            }
                            if (shouldUpdateInverseReferences) {
                                //remove this key
                                LOG.debug("Removing edge {}, key {} from the map attribute {}", string(mapEdge), key, attribute.getName());
                                keys.remove(key);
                                GraphHelper.setProperty(outVertex, propertyName, keys);
                                GraphHelper.setProperty(outVertex, keyPropertyName, null);
                            }
                            break;
                        }
                    }
                }
            }
            break;
        case STRUCT:
        case CLASSIFICATION:
            break;
        default:
            throw new IllegalStateException("There can't be an edge from " + GraphHelper.getVertexDetails(outVertex) + " to " + GraphHelper.getVertexDetails(inVertex) + " with attribute name " + attribute.getName() + " which is not class/array/map attribute. found " + attrType.getTypeCategory().name());
    }
    if (edge != null) {
        deleteEdge(edge, false);
        RequestContextV1 requestContext = RequestContextV1.get();
        GraphHelper.setProperty(outVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, requestContext.getRequestTime());
        GraphHelper.setProperty(outVertex, Constants.MODIFIED_BY_KEY, requestContext.getUser());
        requestContext.recordEntityUpdate(new AtlasObjectId(outId, typeName));
    }
}
Also used : RequestContextV1(org.apache.atlas.RequestContextV1) 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) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)

Example 94 with AtlasVertex

use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.

the class EntityGraphMapper method createVertex.

private AtlasEdge createVertex(AtlasStruct struct, AtlasVertex referringVertex, String edgeLabel, EntityMutationContext context) throws AtlasBaseException {
    AtlasVertex vertex = createStructVertex(struct);
    mapAttributes(struct, vertex, CREATE, context);
    try {
        //TODO - Map directly in AtlasGraphUtilsV1
        return graphHelper.getOrCreateEdge(referringVertex, vertex, edgeLabel);
    } catch (RepositoryException e) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RepositoryException(org.apache.atlas.repository.RepositoryException)

Example 95 with AtlasVertex

use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.

the class IDBasedEntityResolver method resolveEntityReferences.

public EntityGraphDiscoveryContext resolveEntityReferences(EntityGraphDiscoveryContext context) throws AtlasBaseException {
    if (context == null) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, "IDBasedEntityResolver.resolveEntityReferences(): context is null");
    }
    EntityStream entityStream = context.getEntityStream();
    for (String guid : context.getReferencedGuids()) {
        boolean isAssignedGuid = AtlasTypeUtil.isAssignedGuid(guid);
        AtlasVertex vertex = isAssignedGuid ? AtlasGraphUtilsV1.findByGuid(guid) : null;
        if (vertex == null && !(entityStream instanceof EntityImportStream)) {
            // if not found in the store, look if the entity is present in the stream
            AtlasEntity entity = entityStream.getByGuid(guid);
            if (entity != null) {
                // look for the entity in the store using unique-attributes
                AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName());
                if (entityType == null) {
                    throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), entity.getTypeName());
                }
                vertex = AtlasGraphUtilsV1.findByUniqueAttributes(entityType, entity.getAttributes());
            } else if (!isAssignedGuid) {
                // for local-guids, entity must be in the stream
                throw new AtlasBaseException(AtlasErrorCode.REFERENCED_ENTITY_NOT_FOUND, guid);
            }
        }
        if (vertex != null) {
            context.addResolvedGuid(guid, vertex);
        } else {
            if (isAssignedGuid && !(entityStream instanceof EntityImportStream)) {
                throw new AtlasBaseException(AtlasErrorCode.REFERENCED_ENTITY_NOT_FOUND, guid);
            } else {
                context.addLocalGuidReference(guid);
            }
        }
    }
    return context;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Aggregations

AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)164 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)53 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)26 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)21 ArrayList (java.util.ArrayList)20 Test (org.testng.annotations.Test)19 HashMap (java.util.HashMap)16 Id (org.apache.atlas.typesystem.persistence.Id)14 Map (java.util.Map)13 List (java.util.List)12 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)12 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)12 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)12 AtlasException (org.apache.atlas.AtlasException)11 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)11 RepositoryException (org.apache.atlas.repository.RepositoryException)11 AtlasGraphQuery (org.apache.atlas.repository.graphdb.AtlasGraphQuery)11 AtlasType (org.apache.atlas.type.AtlasType)11 HashSet (java.util.HashSet)8 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)8