Search in sources :

Example 46 with AtlasVertex

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

the class GraphBackedMetadataRepositoryDeleteTestBase method testUpdateEntity_MultiplicityOneNonCompositeReference.

@Test
public void testUpdateEntity_MultiplicityOneNonCompositeReference() throws Exception {
    String hrDeptGuid = createHrDeptGraph();
    ITypedReferenceableInstance hrDept = repositoryService.getEntityDefinition(hrDeptGuid);
    Map<String, String> nameGuidMap = getEmployeeNameGuidMap(hrDept);
    ITypedReferenceableInstance john = repositoryService.getEntityDefinition(nameGuidMap.get("John"));
    Id johnGuid = john.getId();
    ITypedReferenceableInstance max = repositoryService.getEntityDefinition(nameGuidMap.get("Max"));
    String maxGuid = max.getId()._getId();
    AtlasVertex vertex = GraphHelper.getInstance().getVertexForGUID(maxGuid);
    Long creationTimestamp = GraphHelper.getSingleValuedProperty(vertex, Constants.TIMESTAMP_PROPERTY_KEY, Long.class);
    Assert.assertNotNull(creationTimestamp);
    Long modificationTimestampPreUpdate = GraphHelper.getSingleValuedProperty(vertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class);
    Assert.assertNotNull(modificationTimestampPreUpdate);
    ITypedReferenceableInstance jane = repositoryService.getEntityDefinition(nameGuidMap.get("Jane"));
    Id janeId = jane.getId();
    // Update max's mentor reference to john.
    ClassType personType = typeSystem.getDataType(ClassType.class, "Person");
    ITypedReferenceableInstance maxEntity = personType.createInstance(max.getId());
    maxEntity.set("mentor", johnGuid);
    EntityResult entityResult = updatePartial(maxEntity);
    assertEquals(entityResult.getUpdateEntities().size(), 1);
    assertTrue(entityResult.getUpdateEntities().contains(maxGuid));
    // Verify the update was applied correctly - john should now be max's mentor.
    max = repositoryService.getEntityDefinition(maxGuid);
    ITypedReferenceableInstance refTarget = (ITypedReferenceableInstance) max.get("mentor");
    Assert.assertEquals(refTarget.getId()._getId(), johnGuid._getId());
    // Verify modification timestamp was updated.
    vertex = GraphHelper.getInstance().getVertexForGUID(maxGuid);
    Long modificationTimestampPostUpdate = GraphHelper.getSingleValuedProperty(vertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class);
    Assert.assertNotNull(modificationTimestampPostUpdate);
    Assert.assertTrue(creationTimestamp < modificationTimestampPostUpdate);
    // Update max's mentor reference to jane.
    maxEntity.set("mentor", janeId);
    entityResult = updatePartial(maxEntity);
    assertEquals(entityResult.getUpdateEntities().size(), 1);
    assertTrue(entityResult.getUpdateEntities().contains(maxGuid));
    // Verify the update was applied correctly - jane should now be max's mentor.
    max = repositoryService.getEntityDefinition(maxGuid);
    refTarget = (ITypedReferenceableInstance) max.get("mentor");
    Assert.assertEquals(refTarget.getId()._getId(), janeId._getId());
    // Verify modification timestamp was updated.
    vertex = GraphHelper.getInstance().getVertexForGUID(maxGuid);
    Long modificationTimestampPost2ndUpdate = GraphHelper.getSingleValuedProperty(vertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class);
    Assert.assertNotNull(modificationTimestampPost2ndUpdate);
    Assert.assertTrue(modificationTimestampPostUpdate < modificationTimestampPost2ndUpdate);
    ITypedReferenceableInstance julius = repositoryService.getEntityDefinition(nameGuidMap.get("Julius"));
    Id juliusId = julius.getId();
    maxEntity = personType.createInstance(max.getId());
    maxEntity.set("manager", juliusId);
    entityResult = updatePartial(maxEntity);
    // Verify julius' subordinates were updated.
    assertEquals(entityResult.getUpdateEntities().size(), 3);
    assertTrue(entityResult.getUpdateEntities().contains(maxGuid));
    assertTrue(entityResult.getUpdateEntities().containsAll(Arrays.asList(maxGuid, janeId._getId(), juliusId._getId())));
    // Verify the update was applied correctly - julius should now be max's manager.
    max = repositoryService.getEntityDefinition(maxGuid);
    refTarget = (ITypedReferenceableInstance) max.get("manager");
    Assert.assertEquals(refTarget.getId()._getId(), juliusId._getId());
    Assert.assertEquals(refTarget.getId()._getId(), juliusId._getId());
    julius = repositoryService.getEntityDefinition(nameGuidMap.get("Julius"));
    Object object = julius.get("subordinates");
    Assert.assertTrue(object instanceof List);
    List<ITypedReferenceableInstance> refValues = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refValues.size(), 1);
    Assert.assertTrue(refValues.contains(max.getId()));
    assertTestUpdateEntity_MultiplicityOneNonCompositeReference(janeId._getId());
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Id(org.apache.atlas.typesystem.persistence.Id) EntityResult(org.apache.atlas.model.legacy.EntityResult) Test(org.testng.annotations.Test)

Example 47 with AtlasVertex

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

the class AtlasClassificationDefStoreV1 method getByName.

@Override
public AtlasClassificationDef getByName(String name) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> AtlasClassificationDefStoreV1.getByName({})", name);
    }
    AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.TRAIT);
    if (vertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_NOT_FOUND, name);
    }
    vertex.getProperty(Constants.TYPE_CATEGORY_PROPERTY_KEY, TypeCategory.class);
    AtlasClassificationDef ret = toClassificationDef(vertex);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== AtlasClassificationDefStoreV1.getByName({}): {}", name, ret);
    }
    return ret;
}
Also used : AtlasClassificationDef(org.apache.atlas.model.typedef.AtlasClassificationDef) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex)

Example 48 with AtlasVertex

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

the class AtlasEntityChangeNotifier method doFullTextMapping.

private void doFullTextMapping(List<AtlasEntityHeader> atlasEntityHeaders) {
    if (CollectionUtils.isEmpty(atlasEntityHeaders)) {
        return;
    }
    try {
        if (!AtlasRepositoryConfiguration.isFullTextSearchEnabled()) {
            return;
        }
    } catch (AtlasException e) {
        LOG.warn("Unable to determine if FullText is disabled. Proceeding with FullText mapping");
    }
    for (AtlasEntityHeader atlasEntityHeader : atlasEntityHeaders) {
        String guid = atlasEntityHeader.getGuid();
        AtlasVertex atlasVertex = AtlasGraphUtilsV1.findByGuid(guid);
        if (atlasVertex == null) {
            continue;
        }
        try {
            String fullText = fullTextMapperV2.getIndexTextForEntity(guid);
            GraphHelper.setProperty(atlasVertex, Constants.ENTITY_TEXT_PROPERTY_KEY, fullText);
        } catch (AtlasBaseException e) {
            LOG.error("FullText mapping failed for Vertex[ guid = {} ]", guid, e);
        }
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) AtlasException(org.apache.atlas.AtlasException)

Example 49 with AtlasVertex

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

the class AtlasEntityStoreV1 method preCreateOrUpdate.

private EntityMutationContext preCreateOrUpdate(EntityStream entityStream, EntityGraphMapper entityGraphMapper, boolean isPartialUpdate) throws AtlasBaseException {
    EntityGraphDiscovery graphDiscoverer = new AtlasEntityGraphDiscoveryV1(typeRegistry, entityStream);
    EntityGraphDiscoveryContext discoveryContext = graphDiscoverer.discoverEntities();
    EntityMutationContext context = new EntityMutationContext(discoveryContext);
    for (String guid : discoveryContext.getReferencedGuids()) {
        AtlasVertex vertex = discoveryContext.getResolvedEntityVertex(guid);
        AtlasEntity entity = entityStream.getByGuid(guid);
        if (entity != null) {
            if (vertex != null) {
                // entity would be null if guid is not in the stream but referenced by an entity in the stream
                if (!isPartialUpdate) {
                    graphDiscoverer.validateAndNormalize(entity);
                } else {
                    graphDiscoverer.validateAndNormalizeForUpdate(entity);
                }
                AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName());
                String guidVertex = AtlasGraphUtilsV1.getIdFromVertex(vertex);
                if (!StringUtils.equals(guidVertex, guid)) {
                    // if entity was found by unique attribute
                    entity.setGuid(guidVertex);
                }
                context.addUpdated(guid, entity, entityType, vertex);
            } else {
                graphDiscoverer.validateAndNormalize(entity);
                AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName());
                //Create vertices which do not exist in the repository
                if ((entityStream instanceof EntityImportStream) && AtlasTypeUtil.isAssignedGuid(entity.getGuid())) {
                    vertex = entityGraphMapper.createVertexWithGuid(entity, entity.getGuid());
                } else {
                    vertex = entityGraphMapper.createVertex(entity);
                }
                discoveryContext.addResolvedGuid(guid, vertex);
                String generatedGuid = AtlasGraphUtilsV1.getIdFromVertex(vertex);
                entity.setGuid(generatedGuid);
                context.addCreated(guid, entity, entityType, vertex);
            }
            // during import, update the system attributes
            if (entityStream instanceof EntityImportStream) {
                entityGraphMapper.updateSystemAttributes(vertex, entity);
            }
        }
    }
    return context;
}
Also used : EntityGraphDiscovery(org.apache.atlas.repository.store.graph.EntityGraphDiscovery) EntityGraphDiscoveryContext(org.apache.atlas.repository.store.graph.EntityGraphDiscoveryContext) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 50 with AtlasVertex

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

the class AtlasEnumDefStoreV1 method getByName.

@Override
public AtlasEnumDef getByName(String name) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> AtlasEnumDefStoreV1.getByName({})", name);
    }
    AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.ENUM);
    if (vertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_NOT_FOUND, name);
    }
    vertex.getProperty(Constants.TYPE_CATEGORY_PROPERTY_KEY, TypeCategory.class);
    AtlasEnumDef ret = toEnumDef(vertex);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== AtlasEnumDefStoreV1.getByName({}): {}", name, ret);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEnumDef(org.apache.atlas.model.typedef.AtlasEnumDef)

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