Search in sources :

Example 6 with IReferenceableInstance

use of org.apache.atlas.typesystem.IReferenceableInstance 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 7 with IReferenceableInstance

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

the class AtlasObjectIdConverter method fromV1ToV2.

@Override
public Object fromV1ToV2(Object v1Obj, AtlasType type, AtlasFormatConverter.ConverterContext converterContext) throws AtlasBaseException {
    Object ret = null;
    if (v1Obj != null) {
        if (v1Obj instanceof Id) {
            Id id = (Id) v1Obj;
            ret = new AtlasObjectId(id._getId(), id.getTypeName());
        } else if (v1Obj instanceof IReferenceableInstance) {
            IReferenceableInstance refInst = (IReferenceableInstance) v1Obj;
            String guid = refInst.getId()._getId();
            ret = new AtlasObjectId(guid, refInst.getTypeName());
            if (!converterContext.entityExists(guid) && hasAnyAssignedAttribute(refInst)) {
                AtlasEntityType entityType = typeRegistry.getEntityTypeByName(refInst.getTypeName());
                AtlasEntityFormatConverter converter = (AtlasEntityFormatConverter) converterRegistry.getConverter(TypeCategory.ENTITY);
                AtlasEntity entity = converter.fromV1ToV2(v1Obj, entityType, converterContext);
                converterContext.addReferredEntity(entity);
            }
        }
    }
    return ret;
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Id(org.apache.atlas.typesystem.persistence.Id) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 8 with IReferenceableInstance

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

the class NotificationEntityChangeListener method notifyOfEntityEvent.

// send notification of entity change
private void notifyOfEntityEvent(Collection<ITypedReferenceableInstance> entityDefinitions, EntityNotification.OperationType operationType) throws AtlasException {
    List<EntityNotification> messages = new LinkedList<>();
    for (IReferenceableInstance entityDefinition : entityDefinitions) {
        Referenceable entity = new Referenceable(entityDefinition);
        Map<String, Object> attributesMap = entity.getValuesMap();
        List<String> entityNotificationAttrs = getNotificationAttributes(entity.getTypeName());
        if (MapUtils.isNotEmpty(attributesMap) && CollectionUtils.isNotEmpty(entityNotificationAttrs)) {
            for (String entityAttr : attributesMap.keySet()) {
                if (!entityNotificationAttrs.contains(entityAttr)) {
                    entity.setNull(entityAttr);
                }
            }
        }
        EntityNotificationImpl notification = new EntityNotificationImpl(entity, operationType, getAllTraits(entity, typeSystem));
        messages.add(notification);
    }
    notificationInterface.send(NotificationInterface.NotificationType.ENTITIES, messages);
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) EntityNotificationImpl(org.apache.atlas.notification.entity.EntityNotificationImpl) Referenceable(org.apache.atlas.typesystem.Referenceable) EntityNotification(org.apache.atlas.notification.entity.EntityNotification) LinkedList(java.util.LinkedList)

Example 9 with IReferenceableInstance

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

the class ObjectGraphTraversal method processReferenceableInstance.

void processReferenceableInstance(Object val) throws AtlasException {
    if (val == null || !(val instanceof IReferenceableInstance || val instanceof Id)) {
        return;
    }
    if (val instanceof Id) {
        Id id = (Id) val;
        if (id.isUnassigned()) {
            add(id, null);
        }
        return;
    }
    IReferenceableInstance ref = (IReferenceableInstance) val;
    Id id = ref.getId();
    if (id.isUnassigned()) {
        add(id, ref);
        if (!processedIds.contains(id)) {
            processedIds.add(id);
            processStruct(val);
            ImmutableList<String> traits = ref.getTraits();
            for (String trait : traits) {
                processStruct(ref.getTrait(trait));
            }
        }
    }
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) Id(org.apache.atlas.typesystem.persistence.Id)

Example 10 with IReferenceableInstance

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

the class GraphHelper method getVerticesForInstancesByUniqueAttribute.

/**
 * Finds vertices that match at least one unique attribute of the instances specified.  The AtlasVertex at a given index in the result corresponds
 * to the IReferencableInstance at that same index that was passed in.  The number of elements in the resultant list is guaranteed to match the
 * number of instances that were passed in.  If no vertex is found for a given instance, that entry will be null in the resultant list.
 *
 * @param classType
 * @param instancesForClass
 * @return
 * @throws AtlasException
 */
public List<AtlasVertex> getVerticesForInstancesByUniqueAttribute(ClassType classType, List<? extends IReferenceableInstance> instancesForClass) throws AtlasException {
    // For each attribute, need to figure out what values to search for and which instance(s)
    // those values correspond to.
    Map<String, AttributeValueMap> map = new HashMap<String, AttributeValueMap>();
    for (AttributeInfo attributeInfo : classType.fieldMapping().fields.values()) {
        if (attributeInfo.isUnique) {
            String propertyKey = getQualifiedFieldName(classType, attributeInfo.name);
            AttributeValueMap mapForAttribute = new AttributeValueMap();
            for (int idx = 0; idx < instancesForClass.size(); idx++) {
                IReferenceableInstance instance = instancesForClass.get(idx);
                Object value = instance.get(attributeInfo.name);
                mapForAttribute.put(value, instance, idx);
            }
            map.put(propertyKey, mapForAttribute);
        }
    }
    AtlasVertex[] result = new AtlasVertex[instancesForClass.size()];
    if (map.isEmpty()) {
        // no unique attributes
        return Arrays.asList(result);
    }
    // construct gremlin query
    AtlasGraphQuery query = graph.query();
    query.has(Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getName());
    query.has(Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name());
    List<AtlasGraphQuery> orChildren = new ArrayList<AtlasGraphQuery>();
    // that matches the value in some instance.
    for (Map.Entry<String, AttributeValueMap> entry : map.entrySet()) {
        AtlasGraphQuery orChild = query.createChildQuery();
        String propertyName = entry.getKey();
        AttributeValueMap valueMap = entry.getValue();
        Set<Object> values = valueMap.getAttributeValues();
        if (values.size() == 1) {
            orChild.has(propertyName, values.iterator().next());
        } else if (values.size() > 1) {
            orChild.in(propertyName, values);
        }
        orChildren.add(orChild);
    }
    if (orChildren.size() == 1) {
        AtlasGraphQuery child = orChildren.get(0);
        query.addConditionsFrom(child);
    } else if (orChildren.size() > 1) {
        query.or(orChildren);
    }
    Iterable<AtlasVertex> queryResult = query.vertices();
    for (AtlasVertex matchingVertex : queryResult) {
        Collection<IndexedInstance> matches = getInstancesForVertex(map, matchingVertex);
        for (IndexedInstance wrapper : matches) {
            result[wrapper.getIndex()] = matchingVertex;
        }
    }
    return Arrays.asList(result);
}
Also used : AttributeValueMap(org.apache.atlas.util.AttributeValueMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtlasGraphQuery(org.apache.atlas.repository.graphdb.AtlasGraphQuery) AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) IndexedInstance(org.apache.atlas.util.IndexedInstance) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashMap(java.util.HashMap) HashBiMap(com.google.common.collect.HashBiMap) AttributeValueMap(org.apache.atlas.util.AttributeValueMap)

Aggregations

IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)21 ArrayList (java.util.ArrayList)11 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)9 List (java.util.List)7 Id (org.apache.atlas.typesystem.persistence.Id)7 Test (org.testng.annotations.Test)7 ImmutableList (com.google.common.collect.ImmutableList)5 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)5 BeforeTest (org.testng.annotations.BeforeTest)5 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)4 Referenceable (org.apache.atlas.typesystem.Referenceable)4 HashMap (java.util.HashMap)3 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)3 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 AtlasException (org.apache.atlas.AtlasException)2 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)2 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)2 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)2