Search in sources :

Example 96 with AtlasVertex

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

the class GraphBackedTypeStore method getTypesFromVertices.

private TypesDef getTypesFromVertices(Iterator<AtlasVertex> vertices) throws AtlasException {
    ImmutableList.Builder<EnumTypeDefinition> enums = ImmutableList.builder();
    ImmutableList.Builder<StructTypeDefinition> structs = ImmutableList.builder();
    ImmutableList.Builder<HierarchicalTypeDefinition<ClassType>> classTypes = ImmutableList.builder();
    ImmutableList.Builder<HierarchicalTypeDefinition<TraitType>> traits = ImmutableList.builder();
    while (vertices.hasNext()) {
        AtlasVertex vertex = vertices.next();
        DataTypes.TypeCategory typeCategory = GraphHelper.getSingleValuedProperty(vertex, Constants.TYPE_CATEGORY_PROPERTY_KEY, TypeCategory.class);
        String typeName = GraphHelper.getSingleValuedProperty(vertex, Constants.TYPENAME_PROPERTY_KEY, String.class);
        String typeDescription = GraphHelper.getSingleValuedProperty(vertex, Constants.TYPEDESCRIPTION_PROPERTY_KEY, String.class);
        LOG.info("Restoring type {}.{}.{}", typeCategory, typeName, typeDescription);
        switch(typeCategory) {
            case ENUM:
                enums.add(getEnumType(vertex));
                break;
            case STRUCT:
                AttributeDefinition[] attributes = getAttributes(vertex, typeName);
                structs.add(new StructTypeDefinition(typeName, typeDescription, attributes));
                break;
            case CLASS:
                ImmutableSet<String> superTypes = getSuperTypes(vertex);
                attributes = getAttributes(vertex, typeName);
                classTypes.add(new HierarchicalTypeDefinition(ClassType.class, typeName, typeDescription, superTypes, attributes));
                break;
            case TRAIT:
                superTypes = getSuperTypes(vertex);
                attributes = getAttributes(vertex, typeName);
                traits.add(new HierarchicalTypeDefinition(TraitType.class, typeName, typeDescription, superTypes, attributes));
                break;
            default:
                throw new IllegalArgumentException("Unhandled type category " + typeCategory);
        }
    }
    return TypesUtil.getTypesDef(enums.build(), structs.build(), traits.build(), classTypes.build());
}
Also used : TypeCategory(org.apache.atlas.typesystem.types.DataTypes.TypeCategory) ImmutableList(com.google.common.collect.ImmutableList) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex)

Example 97 with AtlasVertex

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

the class GraphBackedTypeStore method findVertex.

/**
     * Find vertex for the given type category and name, else create new vertex
     * @param category
     * @param typeName
     * @return vertex
     */
AtlasVertex findVertex(DataTypes.TypeCategory category, String typeName) {
    LOG.debug("Finding AtlasVertex for {}.{}", category, typeName);
    Iterator results = graph.query().has(Constants.TYPENAME_PROPERTY_KEY, typeName).vertices().iterator();
    AtlasVertex vertex = null;
    if (results != null && results.hasNext()) {
        //There should be just one AtlasVertex with the given typeName
        vertex = (AtlasVertex) results.next();
    }
    return vertex;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) Iterator(java.util.Iterator)

Example 98 with AtlasVertex

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

the class EntityGraphMapper method addInverseReference.

private void addInverseReference(AttributeMutationContext ctx, AtlasAttribute inverseAttribute, AtlasEdge edge) throws AtlasBaseException {
    AtlasStructType inverseType = inverseAttribute.getDefinedInType();
    String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(inverseType, inverseAttribute.getName());
    AtlasVertex vertex = edge.getOutVertex();
    AtlasVertex inverseVertex = edge.getInVertex();
    String inverseEdgeLabel = AtlasGraphUtilsV1.getEdgeLabel(propertyName);
    AtlasEdge inverseEdge = graphHelper.getEdgeForLabel(inverseVertex, inverseEdgeLabel);
    AtlasEdge newEdge;
    try {
        newEdge = graphHelper.getOrCreateEdge(inverseVertex, vertex, inverseEdgeLabel);
    } catch (RepositoryException e) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
    }
    boolean inverseUpdated = true;
    switch(inverseAttribute.getAttributeType().getTypeCategory()) {
        case OBJECT_ID_TYPE:
            if (inverseEdge != null) {
                if (!inverseEdge.equals(newEdge)) {
                    // Disconnect old reference
                    deleteHandler.deleteEdgeReference(inverseEdge, inverseAttribute.getAttributeType().getTypeCategory(), inverseAttribute.isOwnedRef(), true);
                } else {
                    // Edge already exists for this attribute between these vertices.
                    inverseUpdated = false;
                }
            }
            break;
        case ARRAY:
            // Add edge ID to property value
            List<String> elements = inverseVertex.getProperty(propertyName, List.class);
            if (elements == null) {
                elements = new ArrayList<>();
                elements.add(newEdge.getId().toString());
                inverseVertex.setProperty(propertyName, elements);
            } else {
                if (!elements.contains(newEdge.getId().toString())) {
                    elements.add(newEdge.getId().toString());
                    inverseVertex.setProperty(propertyName, elements);
                } else {
                    // Property value list already contains the edge ID.
                    inverseUpdated = false;
                }
            }
            break;
        default:
            break;
    }
    if (inverseUpdated) {
        updateModificationMetadata(inverseVertex);
        AtlasObjectId inverseEntityId = new AtlasObjectId(AtlasGraphUtilsV1.getIdFromVertex(inverseVertex), inverseType.getTypeName());
        RequestContextV1.get().recordEntityUpdate(inverseEntityId);
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasStructType(org.apache.atlas.type.AtlasStructType) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 99 with AtlasVertex

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

the class EntityGraphMapper method deleteClassifications.

public void deleteClassifications(String guid) throws AtlasBaseException {
    AtlasVertex instanceVertex = AtlasGraphUtilsV1.findByGuid(guid);
    if (instanceVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);
    }
    List<String> traitNames = GraphHelper.getTraitNames(instanceVertex);
    deleteClassifications(guid, traitNames);
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex)

Example 100 with AtlasVertex

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

the class EntityGraphMapper method createVertexWithGuid.

public AtlasVertex createVertexWithGuid(AtlasEntity entity, String guid) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> createVertex({})", entity.getTypeName());
    }
    AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName());
    AtlasVertex ret = createStructVertex(entity);
    for (String superTypeName : entityType.getAllSuperTypes()) {
        AtlasGraphUtilsV1.addProperty(ret, Constants.SUPER_TYPES_PROPERTY_KEY, superTypeName);
    }
    AtlasGraphUtilsV1.setProperty(ret, Constants.GUID_PROPERTY_KEY, guid);
    AtlasGraphUtilsV1.setProperty(ret, Constants.VERSION_PROPERTY_KEY, getEntityVersion(entity));
    return ret;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) 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