Search in sources :

Example 61 with ITypedReferenceableInstance

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

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

the class AtlasDeleteHandlerV1Test method getEmployeeNameGuidMap.

private Map<String, String> getEmployeeNameGuidMap(final ITypedReferenceableInstance hrDept) throws AtlasException {
    Object refValue = hrDept.get("employees");
    Assert.assertTrue(refValue instanceof List);
    List<Object> employees = (List<Object>) refValue;
    Assert.assertEquals(employees.size(), 4);
    Map<String, String> nameGuidMap = new HashMap<String, String>() {

        {
            put("hr", hrDept.getId()._getId());
        }
    };
    for (Object listValue : employees) {
        Assert.assertTrue(listValue instanceof ITypedReferenceableInstance);
        ITypedReferenceableInstance employee = (ITypedReferenceableInstance) listValue;
        nameGuidMap.put((String) employee.get("name"), employee.getId()._getId());
    }
    return nameGuidMap;
}
Also used : HashMap(java.util.HashMap) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 63 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance 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)

Example 64 with ITypedReferenceableInstance

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

the class VertexLookupContext method findReferencedInstancesToPreLoad.

private void findReferencedInstancesToPreLoad(ITypedReferenceableInstance newInstance) throws AtlasException {
    //pre-load vertices for reference fields
    for (AttributeInfo info : newInstance.fieldMapping().fields.values()) {
        if (info.dataType().getTypeCategory() == TypeCategory.CLASS) {
            ITypedReferenceableInstance newAttributeValue = (ITypedReferenceableInstance) newInstance.get(info.name);
            addAdditionalInstance(newAttributeValue);
        }
        if (info.dataType().getTypeCategory() == TypeCategory.ARRAY) {
            IDataType elementType = ((DataTypes.ArrayType) info.dataType()).getElemType();
            if (elementType.getTypeCategory() == TypeCategory.CLASS) {
                List<ITypedReferenceableInstance> newElements = (List) newInstance.get(info.name);
                addAdditionalInstances(newElements);
            }
        }
        if (info.dataType().getTypeCategory() == TypeCategory.MAP) {
            IDataType elementType = ((DataTypes.MapType) info.dataType()).getValueType();
            if (elementType.getTypeCategory() == TypeCategory.CLASS) {
                Map<Object, ITypedReferenceableInstance> newAttribute = (Map<Object, ITypedReferenceableInstance>) newInstance.get(info.name);
                if (newAttribute != null) {
                    addAdditionalInstances(newAttribute.values());
                }
            }
        }
    }
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) List(java.util.List) IDataType(org.apache.atlas.typesystem.types.IDataType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 65 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance 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)

Aggregations

ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)142 Test (org.testng.annotations.Test)54 List (java.util.List)34 ArrayList (java.util.ArrayList)29 Id (org.apache.atlas.typesystem.persistence.Id)28 Referenceable (org.apache.atlas.typesystem.Referenceable)22 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)21 ImmutableList (com.google.common.collect.ImmutableList)20 ClassType (org.apache.atlas.typesystem.types.ClassType)19 AtlasException (org.apache.atlas.AtlasException)16 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)16 HashMap (java.util.HashMap)15 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)14 EntityResult (org.apache.atlas.model.legacy.EntityResult)12 IStruct (org.apache.atlas.typesystem.IStruct)10 Map (java.util.Map)9 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)9 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)9 RepositoryException (org.apache.atlas.repository.RepositoryException)9 BeforeTest (org.testng.annotations.BeforeTest)9