Search in sources :

Example 36 with Id

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

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

the class GraphToTypedInstanceMapper method mapVertexToClassReference.

private Object mapVertexToClassReference(AtlasVertex instanceVertex, AttributeInfo attributeInfo, String relationshipLabel, IDataType dataType, AtlasEdge optionalEdge) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Finding edge for {} -> label {} ", instanceVertex, relationshipLabel);
    }
    AtlasEdge edge = null;
    if (optionalEdge == null) {
        edge = graphHelper.getEdgeForLabel(instanceVertex, relationshipLabel);
    } else {
        edge = optionalEdge;
    }
    if (GraphHelper.elementExists(edge)) {
        final AtlasVertex referenceVertex = edge.getInVertex();
        final String guid = GraphHelper.getSingleValuedProperty(referenceVertex, Constants.GUID_PROPERTY_KEY, String.class);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Found vertex {} for label {} with guid {}", referenceVertex, relationshipLabel, guid);
        }
        if (attributeInfo.isComposite) {
            //Also, when you retrieve a type's instance, you get the complete object graph of the composites
            LOG.debug("Found composite, mapping vertex to instance");
            ITypedReferenceableInstance cached = RequestContext.get().getInstanceV1(guid);
            if (cached != null) {
                return cached;
            }
            return mapGraphToTypedInstance(guid, referenceVertex);
        } else {
            String state = GraphHelper.getStateAsString(referenceVertex);
            Id referenceId = new Id(guid, GraphHelper.getSingleValuedProperty(referenceVertex, Constants.VERSION_PROPERTY_KEY, Integer.class), dataType.getName(), state);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found non-composite, adding id {} ", referenceId);
            }
            return referenceId;
        }
    }
    return null;
}
Also used : BigInteger(java.math.BigInteger) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Id(org.apache.atlas.typesystem.persistence.Id) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 38 with Id

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

the class TypedInstanceToGraphMapper method getClassVertex.

private <V, E> AtlasVertex<V, E> getClassVertex(ITypedReferenceableInstance typedReference) throws EntityNotFoundException {
    AtlasVertex<V, E> referenceVertex = null;
    Id id = null;
    if (typedReference != null) {
        id = getExistingId(typedReference);
        referenceVertex = idToVertexMap.get(id);
        if (referenceVertex == null && id.isAssigned()) {
            referenceVertex = graphHelper.getVertexForGUID(id.id);
        }
    }
    return referenceVertex;
}
Also used : Id(org.apache.atlas.typesystem.persistence.Id)

Example 39 with Id

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

the class SoftDeleteHandlerV1Test method assertMaxForTestDisconnectBidirectionalReferences.

@Override
protected void assertMaxForTestDisconnectBidirectionalReferences(final Map<String, String> nameGuidMap) throws Exception {
    // Verify that the Department.employees reference to the deleted employee
    // was disconnected.
    ITypedReferenceableInstance hrDept = metadataService.getEntityDefinition(nameGuidMap.get("hr"));
    List<ITypedReferenceableInstance> employees = (List<ITypedReferenceableInstance>) hrDept.get("employees");
    Assert.assertEquals(employees.size(), 4);
    String maxGuid = nameGuidMap.get("Max");
    for (ITypedReferenceableInstance employee : employees) {
        if (employee.getId()._getId().equals(maxGuid)) {
            assertEquals(employee.getId().getState(), Id.EntityState.DELETED);
        }
    }
    // Verify that the Manager.subordinates still references deleted employee
    ITypedReferenceableInstance jane = metadataService.getEntityDefinition(nameGuidMap.get("Jane"));
    List<ITypedReferenceableInstance> subordinates = (List<ITypedReferenceableInstance>) jane.get("subordinates");
    assertEquals(subordinates.size(), 2);
    for (ITypedReferenceableInstance subordinate : subordinates) {
        if (subordinate.getId()._getId().equals(maxGuid)) {
            assertEquals(subordinate.getId().getState(), Id.EntityState.DELETED);
        }
    }
    // Verify that max's Person.mentor unidirectional reference to john was disconnected.
    ITypedReferenceableInstance john = metadataService.getEntityDefinition(nameGuidMap.get("John"));
    Id mentor = (Id) john.get("mentor");
    assertEquals(mentor._getId(), maxGuid);
    assertEquals(mentor.getState(), Id.EntityState.DELETED);
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) List(java.util.List) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Id(org.apache.atlas.typesystem.persistence.Id)

Example 40 with Id

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

the class QuickStartIT method testViewIsAdded.

@Test
public void testViewIsAdded() throws AtlasServiceException, JSONException {
    Referenceable view = atlasClientV1.getEntity(QuickStart.VIEW_TYPE, AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, QuickStart.PRODUCT_DIM_VIEW);
    assertEquals(QuickStart.PRODUCT_DIM_VIEW, view.get(AtlasClient.NAME));
    Id productDimId = getTable(QuickStart.PRODUCT_DIM_TABLE).getId();
    Id inputTableId = ((List<Id>) view.get(QuickStart.INPUT_TABLES_ATTRIBUTE)).get(0);
    assertEquals(productDimId, inputTableId);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) List(java.util.List) Id(org.apache.atlas.typesystem.persistence.Id) Test(org.testng.annotations.Test)

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