Search in sources :

Example 1 with IReferenceableInstance

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

the class MapIds method processNode.

@Override
public void processNode(ObjectGraphWalker.Node nd) throws AtlasException {
    IReferenceableInstance ref = null;
    Id id = null;
    if (nd.attributeName == null) {
        ref = (IReferenceableInstance) nd.instance;
        Id newId = idToNewIdMap.get(ref.getId());
        if (newId != null) {
            ((ReferenceableInstance) ref).replaceWithNewId(newId);
        }
    } else if (nd.aInfo.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS) {
        if (nd.value != null && nd.value instanceof IReferenceableInstance) {
            Id oldId = ((IReferenceableInstance) nd.value).getId();
            Id newId = idToNewIdMap.get(oldId);
            /*
                 * Replace Instances with Ids, irrespective of whether they map to newIds or not.
                 */
            newId = newId == null ? oldId : newId;
            nd.instance.set(nd.attributeName, newId);
        }
    } else if (nd.aInfo.dataType().getTypeCategory() == DataTypes.TypeCategory.ARRAY) {
        DataTypes.ArrayType aT = (DataTypes.ArrayType) nd.aInfo.dataType();
        Object v = aT.mapIds((ImmutableCollection) nd.value, nd.aInfo.multiplicity, idToNewIdMap);
        nd.instance.set(nd.attributeName, v);
    } else if (nd.aInfo.dataType().getTypeCategory() == DataTypes.TypeCategory.MAP) {
        DataTypes.MapType mT = (DataTypes.MapType) nd.aInfo.dataType();
        Object v = mT.mapIds((ImmutableMap) nd.value, nd.aInfo.multiplicity, idToNewIdMap);
        nd.instance.set(nd.attributeName, v);
    }
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) ImmutableMap(com.google.common.collect.ImmutableMap) DataTypes(org.apache.atlas.typesystem.types.DataTypes)

Example 2 with IReferenceableInstance

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

the class AtlasDeleteHandlerV1Test method testDeleteByUniqueAttribute.

@Test
public void testDeleteByUniqueAttribute() throws Exception {
    // Create a table entity, with 3 composite column entities
    init();
    final AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
    EntityMutationResponse dbCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(dbEntity), false);
    final AtlasEntity tableEntity = TestUtilsV2.createTableEntity(dbEntity);
    AtlasEntity.AtlasEntitiesWithExtInfo entitiesInfo = new AtlasEntity.AtlasEntitiesWithExtInfo(tableEntity);
    final AtlasEntity columnEntity1 = TestUtilsV2.createColumnEntity(tableEntity);
    entitiesInfo.addReferredEntity(columnEntity1);
    final AtlasEntity columnEntity2 = TestUtilsV2.createColumnEntity(tableEntity);
    entitiesInfo.addReferredEntity(columnEntity2);
    final AtlasEntity columnEntity3 = TestUtilsV2.createColumnEntity(tableEntity);
    entitiesInfo.addReferredEntity(columnEntity3);
    tableEntity.setAttribute(COLUMNS_ATTR_NAME, Arrays.asList(AtlasTypeUtil.getAtlasObjectId(columnEntity1), AtlasTypeUtil.getAtlasObjectId(columnEntity2), AtlasTypeUtil.getAtlasObjectId(columnEntity3)));
    init();
    final EntityMutationResponse tblCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    final AtlasEntityHeader column1Created = tblCreationResponse.getCreatedEntityByTypeNameAndAttribute(COLUMN_TYPE, NAME, (String) columnEntity1.getAttribute(NAME));
    final AtlasEntityHeader column2Created = tblCreationResponse.getCreatedEntityByTypeNameAndAttribute(COLUMN_TYPE, NAME, (String) columnEntity2.getAttribute(NAME));
    final AtlasEntityHeader column3Created = tblCreationResponse.getCreatedEntityByTypeNameAndAttribute(COLUMN_TYPE, NAME, (String) columnEntity3.getAttribute(NAME));
    // Retrieve the table entities from the Repository, to get their guids and the composite column guids.
    ITypedReferenceableInstance tableInstance = metadataService.getEntityDefinitionReference(TestUtils.TABLE_TYPE, NAME, (String) tableEntity.getAttribute(NAME));
    List<IReferenceableInstance> columns = (List<IReferenceableInstance>) tableInstance.get(COLUMNS_ATTR_NAME);
    // Delete column
    String colId = columns.get(0).getId()._getId();
    String tableId = tableInstance.getId()._getId();
    init();
    Map<String, Object> uniqueAttrs = new HashMap<>();
    uniqueAttrs.put(NAME, column1Created.getAttribute(NAME));
    AtlasEntityType columnType = typeRegistry.getEntityTypeByName(COLUMN_TYPE);
    EntityMutationResponse deletionResponse = entityStore.deleteByUniqueAttributes(columnType, uniqueAttrs);
    assertEquals(deletionResponse.getDeletedEntities().size(), 1);
    assertEquals(deletionResponse.getDeletedEntities().get(0).getGuid(), colId);
    assertEquals(deletionResponse.getUpdatedEntities().size(), 1);
    assertEquals(deletionResponse.getUpdatedEntities().get(0).getGuid(), tableId);
    assertEntityDeleted(colId);
}
Also used : HashMap(java.util.HashMap) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with IReferenceableInstance

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

the class MemRepository method create.

/**
 * 1. traverse the Object Graph from  i and create idToNewIdMap : Map[Id, Id],
 *    also create old Id to Instance Map: oldIdToInstance : Map[Id, IInstance]
 *   - traverse reference Attributes, List[ClassType], Maps where Key/value is ClassType
 *   - traverse Structs
 *   - traverse Traits.
 * 1b. Ensure that every newId has an associated Instance.
 * 2. Traverse oldIdToInstance map create newInstances : List[ITypedReferenceableInstance]
 *    - create a ITypedReferenceableInstance.
 *      replace any old References ( ids or object references) with new Ids.
 * 3. Traverse over newInstances
 *    - ask ClassStore to assign a position to the Id.
 *      - for Instances with Traits, assign a position for each Trait
 *    - invoke store on the nwInstance.
 *
 * Recovery:
 * - on each newInstance, invoke releaseId and delete on its ClassStore and Traits' Stores.
 *
 * @param i
 * @return
 * @throws org.apache.atlas.repository.RepositoryException
 */
public ITypedReferenceableInstance create(IReferenceableInstance i) throws RepositoryException {
    DiscoverInstances discoverInstances = new DiscoverInstances(this);
    /*
         * Step 1: traverse the Object Graph from  i and create idToNewIdMap : Map[Id, Id],
         *    also create old Id to Instance Map: oldIdToInstance : Map[Id, IInstance]
         *   - traverse reference Attributes, List[ClassType], Maps where Key/value is ClassType
         *   - traverse Structs
         *   - traverse Traits.
         */
    try {
        new ObjectGraphWalker(typeSystem, discoverInstances, i).walk();
    } catch (AtlasException me) {
        throw new RepositoryException("TypeSystem error when walking the ObjectGraph", me);
    }
    /*
         * Step 1b: Ensure that every newId has an associated Instance.
         */
    for (Id oldId : discoverInstances.idToNewIdMap.keySet()) {
        if (!discoverInstances.idToInstanceMap.containsKey(oldId)) {
            throw new RepositoryException(String.format("Invalid Object Graph: " + "Encountered an unassignedId %s that is not associated with an Instance", oldId));
        }
    }
    /* Step 2: Traverse oldIdToInstance map create newInstances :
        List[ITypedReferenceableInstance]
         * - create a ITypedReferenceableInstance.
         *   replace any old References ( ids or object references) with new Ids.
        */
    List<ITypedReferenceableInstance> newInstances = new ArrayList<>();
    ITypedReferenceableInstance retInstance = null;
    Set<ClassType> classTypes = new TreeSet<>();
    Set<TraitType> traitTypes = new TreeSet<>();
    for (IReferenceableInstance transientInstance : discoverInstances.idToInstanceMap.values()) {
        try {
            ClassType cT = typeSystem.getDataType(ClassType.class, transientInstance.getTypeName());
            ITypedReferenceableInstance newInstance = cT.convert(transientInstance, Multiplicity.REQUIRED);
            newInstances.add(newInstance);
            classTypes.add(cT);
            for (String traitName : newInstance.getTraits()) {
                TraitType tT = typeSystem.getDataType(TraitType.class, traitName);
                traitTypes.add(tT);
            }
            if (newInstance.getId() == i.getId()) {
                retInstance = newInstance;
            }
            /*
                 * Now replace old references with new Ids
                 */
            MapIds mapIds = new MapIds(discoverInstances.idToNewIdMap);
            new ObjectGraphWalker(typeSystem, mapIds, newInstances).walk();
        } catch (AtlasException me) {
            throw new RepositoryException(String.format("Failed to create Instance(id = %s", transientInstance.getId()), me);
        }
    }
    /*
         * 3. Acquire Class and Trait Storage locks.
         * - acquire them in a stable order (super before subclass, classes before traits
         */
    for (ClassType cT : classTypes) {
        HierarchicalTypeStore st = typeStores.get(cT.getName());
        st.acquireWriteLock();
    }
    for (TraitType tT : traitTypes) {
        HierarchicalTypeStore st = typeStores.get(tT.getName());
        st.acquireWriteLock();
    }
    /*
         * 4. Traverse over newInstances
         *    - ask ClassStore to assign a position to the Id.
         *      - for Instances with Traits, assign a position for each Trait
         *    - invoke store on the nwInstance.
         */
    try {
        for (ITypedReferenceableInstance instance : newInstances) {
            HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
            st.assignPosition(instance.getId());
            for (String traitName : instance.getTraits()) {
                HierarchicalTypeStore tt = typeStores.get(traitName);
                tt.assignPosition(instance.getId());
            }
        }
        for (ITypedReferenceableInstance instance : newInstances) {
            HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
            st.store((ReferenceableInstance) instance);
            for (String traitName : instance.getTraits()) {
                HierarchicalTypeStore tt = typeStores.get(traitName);
                tt.store((ReferenceableInstance) instance);
            }
        }
    } catch (RepositoryException re) {
        for (ITypedReferenceableInstance instance : newInstances) {
            HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
            st.releaseId(instance.getId());
        }
        throw re;
    } finally {
        for (ClassType cT : classTypes) {
            HierarchicalTypeStore st = typeStores.get(cT.getName());
            st.releaseWriteLock();
        }
        for (TraitType tT : traitTypes) {
            HierarchicalTypeStore st = typeStores.get(tT.getName());
            st.releaseWriteLock();
        }
    }
    return retInstance;
}
Also used : DiscoverInstances(org.apache.atlas.repository.DiscoverInstances) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) TraitType(org.apache.atlas.typesystem.types.TraitType) ObjectGraphWalker(org.apache.atlas.typesystem.types.ObjectGraphWalker) ArrayList(java.util.ArrayList) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasException(org.apache.atlas.AtlasException) ClassType(org.apache.atlas.typesystem.types.ClassType) MapIds(org.apache.atlas.typesystem.persistence.MapIds) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) TreeSet(java.util.TreeSet) Id(org.apache.atlas.typesystem.persistence.Id)

Example 4 with IReferenceableInstance

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

the class TypedInstanceToGraphMapper method createVerticesAndDiscoverInstances.

private TypeUtils.Pair<List<ITypedReferenceableInstance>, List<ITypedReferenceableInstance>> createVerticesAndDiscoverInstances(Collection<IReferenceableInstance> instances) throws AtlasException {
    List<ITypedReferenceableInstance> instancesToCreate = new ArrayList<>();
    List<ITypedReferenceableInstance> instancesToUpdate = new ArrayList<>();
    Map<Id, AtlasVertex> foundVertices = findExistingVertices(instances);
    // cache all the ids
    idToVertexMap.putAll(foundVertices);
    Set<Id> processedIds = new HashSet<>();
    for (IReferenceableInstance instance : instances) {
        Id id = instance.getId();
        if (processedIds.contains(id)) {
            continue;
        }
        AtlasVertex instanceVertex = foundVertices.get(id);
        ClassType classType = typeSystem.getDataType(ClassType.class, instance.getTypeName());
        if (instanceVertex == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Creating new vertex for instance {}", instance.toShortString());
            }
            ITypedReferenceableInstance newInstance = classType.convert(instance, Multiplicity.REQUIRED);
            instanceVertex = graphHelper.createVertexWithIdentity(newInstance, classType.getAllSuperTypeNames());
            instancesToCreate.add(newInstance);
            // Map only unique attributes for cases of circular references
            mapInstanceToVertex(newInstance, instanceVertex, classType.fieldMapping().fields, true, Operation.CREATE);
            idToVertexMap.put(id, instanceVertex);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Re-using existing vertex {} for instance {}", string(instanceVertex), instance.toShortString());
            }
            if (!(instance instanceof ITypedReferenceableInstance)) {
                throw new IllegalStateException(String.format("%s is not of type ITypedReferenceableInstance", instance.toShortString()));
            }
            ITypedReferenceableInstance existingInstance = (ITypedReferenceableInstance) instance;
            instancesToUpdate.add(existingInstance);
        }
        processedIds.add(id);
    }
    return TypeUtils.Pair.of(instancesToCreate, instancesToUpdate);
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) Id(org.apache.atlas.typesystem.persistence.Id) HashSet(java.util.HashSet)

Example 5 with IReferenceableInstance

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

the class TypedInstanceToGraphMapper method mapTypedInstanceToGraph.

void mapTypedInstanceToGraph(Operation operation, ITypedReferenceableInstance... typedInstances) throws AtlasException {
    RequestContext requestContext = RequestContext.get();
    Collection<IReferenceableInstance> allNewInstances = new ArrayList<>();
    for (ITypedReferenceableInstance typedInstance : typedInstances) {
        allNewInstances.addAll(walkClassInstances(typedInstance));
    }
    TypeUtils.Pair<List<ITypedReferenceableInstance>, List<ITypedReferenceableInstance>> instancesPair = createVerticesAndDiscoverInstances(allNewInstances);
    List<ITypedReferenceableInstance> entitiesToCreate = instancesPair.left;
    List<ITypedReferenceableInstance> entitiesToUpdate = instancesPair.right;
    FullTextMapper fulltextMapper = new FullTextMapper(this, graphToTypedInstanceMapper);
    switch(operation) {
        case CREATE:
            List<String> ids = addOrUpdateAttributesAndTraits(operation, entitiesToCreate);
            addFullTextProperty(entitiesToCreate, fulltextMapper);
            requestContext.recordEntityCreate(ids);
            break;
        case UPDATE_FULL:
        case UPDATE_PARTIAL:
            ids = addOrUpdateAttributesAndTraits(Operation.CREATE, entitiesToCreate);
            requestContext.recordEntityCreate(ids);
            ids = addOrUpdateAttributesAndTraits(operation, entitiesToUpdate);
            requestContext.recordEntityUpdate(ids);
            addFullTextProperty(entitiesToCreate, fulltextMapper);
            addFullTextProperty(entitiesToUpdate, fulltextMapper);
            break;
        default:
            throw new UnsupportedOperationException("Not handled - " + operation);
    }
    for (ITypedReferenceableInstance instance : typedInstances) {
        addToEntityCache(requestContext, instance);
    }
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.apache.atlas.RequestContext)

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