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);
}
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;
}
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;
}
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);
}
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);
}
Aggregations