Search in sources :

Example 31 with Id

use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.

the class TypedInstanceToGraphMapper method findExistingVertices.

private Map<Id, AtlasVertex> findExistingVertices(Collection<IReferenceableInstance> instances) throws AtlasException {
    VertexLookupContext context = new VertexLookupContext(this);
    Map<Id, AtlasVertex> result = new HashMap<>();
    for (IReferenceableInstance instance : instances) {
        context.addInstance(instance);
    }
    List<Id> instancesToLoad = new ArrayList<>(context.getInstancesToLoadByGuid());
    List<String> guidsToLoad = Lists.transform(instancesToLoad, new Function<Id, String>() {

        @Override
        public String apply(Id instance) {
            Id id = getExistingId(instance);
            return id.id;
        }
    });
    Map<String, AtlasVertex> instanceVertices = graphHelper.getVerticesForGUIDs(guidsToLoad);
    List<String> missingGuids = new ArrayList<>();
    for (int i = 0; i < instancesToLoad.size(); i++) {
        String guid = guidsToLoad.get(i);
        AtlasVertex instanceVertex = instanceVertices.get(guid);
        if (instanceVertex == null) {
            missingGuids.add(guid);
            continue;
        }
        Id instance = instancesToLoad.get(i);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Found vertex {} for instance {}", string(instanceVertex), instance);
        }
        result.put(instance, instanceVertex);
    }
    if (missingGuids.size() > 0) {
        throw new EntityNotFoundException("Could not find entities in the repository with the following GUIDs: " + missingGuids);
    }
    for (Map.Entry<ClassType, List<IReferenceableInstance>> entry : context.getInstancesToLoadByUniqueAttribute().entrySet()) {
        ClassType type = entry.getKey();
        List<IReferenceableInstance> instancesForClass = entry.getValue();
        List<AtlasVertex> correspondingVertices = graphHelper.getVerticesForInstancesByUniqueAttribute(type, instancesForClass);
        for (int i = 0; i < instancesForClass.size(); i++) {
            IReferenceableInstance inst = instancesForClass.get(i);
            AtlasVertex vertex = correspondingVertices.get(i);
            result.put(getExistingId(inst), vertex);
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ArrayList(java.util.ArrayList) List(java.util.List) Id(org.apache.atlas.typesystem.persistence.Id) HashMap(java.util.HashMap) Map(java.util.Map)

Example 32 with Id

use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.

the class VertexLookupContext method addAdditionalInstance.

private void addAdditionalInstance(ITypedReferenceableInstance instance) {
    if (instance == null) {
        return;
    }
    Id id = mapper.getExistingId(instance);
    if (!id.isAssigned()) {
        return;
    }
    guidsToLookup.add(id);
}
Also used : Id(org.apache.atlas.typesystem.persistence.Id)

Example 33 with Id

use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.

the class GraphHelper method createVertexWithIdentity.

public AtlasVertex createVertexWithIdentity(ITypedReferenceableInstance typedInstance, Set<String> superTypeNames) {
    final String guid = UUID.randomUUID().toString();
    final AtlasVertex vertexWithIdentity = createVertexWithoutIdentity(typedInstance.getTypeName(), new Id(guid, 0, typedInstance.getTypeName()), superTypeNames);
    // add identity
    setProperty(vertexWithIdentity, Constants.GUID_PROPERTY_KEY, guid);
    // add version information
    setProperty(vertexWithIdentity, Constants.VERSION_PROPERTY_KEY, typedInstance.getId().version);
    return vertexWithIdentity;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) Id(org.apache.atlas.typesystem.persistence.Id)

Example 34 with Id

use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.

the class GraphToTypedInstanceMapper method mapGraphToTypedInstance.

public ITypedReferenceableInstance mapGraphToTypedInstance(String guid, AtlasVertex instanceVertex) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        //place to add a check to see if there are any places that were missed.
        if (RequestContext.get().getInstanceV1(guid) != null) {
            LOG.warn("Looking up previously cached guid at: ", new Exception());
        }
        LOG.debug("Mapping graph root vertex {} to typed instance for guid {}", instanceVertex, guid);
    }
    String typeName = GraphHelper.getSingleValuedProperty(instanceVertex, Constants.ENTITY_TYPE_PROPERTY_KEY, String.class);
    List<String> traits = GraphHelper.getTraitNames(instanceVertex);
    String state = GraphHelper.getStateAsString(instanceVertex);
    String createdBy = GraphHelper.getCreatedByAsString(instanceVertex);
    String modifiedBy = GraphHelper.getModifiedByAsString(instanceVertex);
    Date createdTime = new Date(GraphHelper.getCreatedTime(instanceVertex));
    Date modifiedTime = new Date(GraphHelper.getModifiedTime(instanceVertex));
    AtlasSystemAttributes systemAttributes = new AtlasSystemAttributes(createdBy, modifiedBy, createdTime, modifiedTime);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Found createdBy : {} modifiedBy : {} createdTime: {} modifedTime: {}", createdBy, modifiedBy, createdTime, modifiedTime);
    }
    Id id = new Id(guid, Integer.parseInt(String.valueOf(GraphHelper.getProperty(instanceVertex, Constants.VERSION_PROPERTY_KEY))), typeName, state);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Created id {} for instance type {}", id, typeName);
    }
    ClassType classType = typeSystem.getDataType(ClassType.class, typeName);
    ITypedReferenceableInstance typedInstance = classType.createInstance(id, systemAttributes, traits.toArray(new String[traits.size()]));
    mapVertexToInstance(instanceVertex, typedInstance, classType.fieldMapping().fields);
    mapVertexToInstanceTraits(instanceVertex, typedInstance, traits);
    RequestContext.get().cache(typedInstance);
    return typedInstance;
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) AtlasSystemAttributes(org.apache.atlas.typesystem.persistence.AtlasSystemAttributes) Id(org.apache.atlas.typesystem.persistence.Id) ClassType(org.apache.atlas.typesystem.types.ClassType) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasException(org.apache.atlas.AtlasException) Date(java.util.Date)

Example 35 with Id

use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.

the class TypedInstanceToGraphMapper method addOrUpdateAttributesAndTraits.

private String addOrUpdateAttributesAndTraits(Operation operation, ITypedReferenceableInstance typedInstance) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Adding/Updating typed instance {}", typedInstance.toShortString());
    }
    Id id = typedInstance.getId();
    if (id == null) {
        // oops
        throw new RepositoryException("id cannot be null");
    }
    AtlasVertex instanceVertex = idToVertexMap.get(id);
    // add the attributes for the instance
    ClassType classType = typeSystem.getDataType(ClassType.class, typedInstance.getTypeName());
    final Map<String, AttributeInfo> fields = classType.fieldMapping().fields;
    mapInstanceToVertex(typedInstance, instanceVertex, fields, false, operation);
    if (Operation.CREATE.equals(operation)) {
        //TODO - Handle Trait updates
        addTraits(typedInstance, instanceVertex, classType);
    }
    return getId(typedInstance)._getId();
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RepositoryException(org.apache.atlas.repository.RepositoryException) Id(org.apache.atlas.typesystem.persistence.Id)

Aggregations

Id (org.apache.atlas.typesystem.persistence.Id)94 Referenceable (org.apache.atlas.typesystem.Referenceable)50 Test (org.testng.annotations.Test)37 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)28 List (java.util.List)17 ArrayList (java.util.ArrayList)12 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)12 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)12 ImmutableList (com.google.common.collect.ImmutableList)10 TraitType (org.apache.atlas.typesystem.types.TraitType)10 JSONObject (org.codehaus.jettison.json.JSONObject)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 AtlasServiceException (org.apache.atlas.AtlasServiceException)7 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)7 Struct (org.apache.atlas.typesystem.Struct)7 ClassType (org.apache.atlas.typesystem.types.ClassType)7 AtlasException (org.apache.atlas.AtlasException)6 EntityResult (org.apache.atlas.model.legacy.EntityResult)6 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)5