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