use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.
the class AtlasDeleteHandlerV1Test method testDeleteTargetOfRequiredMapReference.
@Test
public void testDeleteTargetOfRequiredMapReference() throws Exception {
// Define type for map value.
AtlasEntityDef mapValueDef = new AtlasEntityDef("RequiredMapValue", "RequiredMapValue_description", "1.0", Collections.<AtlasStructDef.AtlasAttributeDef>emptyList(), Collections.<String>emptySet());
AtlasStructDef.AtlasAttributeDef[] mapOwnerAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("map", "map<string,RequiredMapValue>", false, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
AtlasEntityDef mapOwnerDef = new AtlasEntityDef("RequiredMapOwner", "RequiredMapOwner_description", "1.0", Arrays.asList(mapOwnerAttributes), Collections.<String>emptySet());
AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.<AtlasEntityDef>of(mapValueDef, mapOwnerDef));
typeDefStore.createTypesDef(typesDef);
AtlasEntityType mapOwnerType = typeRegistry.getEntityTypeByName("RequiredMapOwner");
AtlasEntityType mapValueType = typeRegistry.getEntityTypeByName("RequiredMapValue");
// Create instances of RequiredMapOwner and RequiredMapValue.
// Set RequiredMapOwner.map with one entry that references RequiredMapValue instance.
AtlasEntity mapOwnerInstance = new AtlasEntity(mapOwnerType.getTypeName());
AtlasEntity mapValueInstance = new AtlasEntity(mapValueType.getTypeName());
mapOwnerInstance.setAttribute("map", Collections.singletonMap("value1", AtlasTypeUtil.getAtlasObjectId(mapValueInstance)));
AtlasEntity.AtlasEntitiesWithExtInfo entities = new AtlasEntity.AtlasEntitiesWithExtInfo();
entities.addReferredEntity(mapValueInstance);
entities.addEntity(mapOwnerInstance);
List<AtlasEntityHeader> createEntitiesResult = entityStore.createOrUpdate(new AtlasEntityStream(entities), false).getCreatedEntities();
Assert.assertEquals(createEntitiesResult.size(), 2);
List<String> guids = metadataService.getEntityList("RequiredMapOwner");
Assert.assertEquals(guids.size(), 1);
String mapOwnerGuid = guids.get(0);
guids = metadataService.getEntityList("RequiredMapValue");
Assert.assertEquals(guids.size(), 1);
String mapValueGuid = guids.get(0);
// Verify MapOwner.map attribute has expected value.
final AtlasEntity.AtlasEntityWithExtInfo mapOwnerInstance1 = entityStore.getById(mapOwnerGuid);
Object object = mapOwnerInstance1.getEntity().getAttribute("map");
Assert.assertNotNull(object);
Assert.assertTrue(object instanceof Map);
Map<String, AtlasObjectId> map = (Map<String, AtlasObjectId>) object;
Assert.assertEquals(map.size(), 1);
AtlasObjectId mapValueInstance1 = map.get("value1");
Assert.assertNotNull(mapValueInstance1);
Assert.assertEquals(mapValueInstance1.getGuid(), mapValueGuid);
String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(mapOwnerType, "map");
String mapEntryLabel = edgeLabel + "." + "value1";
AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(mapEntryLabel);
AtlasVertex mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerGuid);
object = mapOwnerVertex.getProperty(atlasEdgeLabel.getQualifiedMapKey(), Object.class);
Assert.assertNotNull(object);
// Verify deleting the target of required map attribute throws a AtlasBaseException.
try {
entityStore.deleteById(mapValueGuid);
Assert.fail(AtlasBaseException.class.getSimpleName() + " was expected but none thrown.");
} catch (Exception e) {
verifyExceptionThrown(e, AtlasBaseException.class);
}
}
use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.
the class AtlasGraphSONUtility method objectNodeFromElement.
/**
* Creates GraphSON for a single graph element.
*/
private ObjectNode objectNodeFromElement(final AtlasElement element) {
final boolean isEdge = element instanceof AtlasEdge;
final boolean showTypes = mode == AtlasGraphSONMode.EXTENDED;
final List<String> propertyKeys = isEdge ? this.edgePropertyKeys : this.vertexPropertyKeys;
final ElementPropertiesRule elementPropertyConfig = isEdge ? this.edgePropertiesRule : this.vertexPropertiesRule;
final ObjectNode jsonElement = createJSONMap(createPropertyMap(element, propertyKeys, elementPropertyConfig, normalized), propertyKeys, showTypes);
if ((isEdge && this.includeReservedEdgeId) || (!isEdge && this.includeReservedVertexId)) {
putObject(jsonElement, AtlasGraphSONTokens.INTERNAL_ID, element.getId());
}
// are graph implementations that have AtlasEdge extend from AtlasVertex
if (element instanceof AtlasEdge) {
final AtlasEdge edge = (AtlasEdge) element;
if (this.includeReservedEdgeId) {
putObject(jsonElement, AtlasGraphSONTokens.INTERNAL_ID, element.getId());
}
if (this.includeReservedEdgeType) {
jsonElement.put(AtlasGraphSONTokens.INTERNAL_TYPE, AtlasGraphSONTokens.EDGE);
}
if (this.includeReservedEdgeOutV) {
putObject(jsonElement, AtlasGraphSONTokens.INTERNAL_OUT_V, edge.getOutVertex().getId());
}
if (this.includeReservedEdgeInV) {
putObject(jsonElement, AtlasGraphSONTokens.INTERNAL_IN_V, edge.getInVertex().getId());
}
if (this.includeReservedEdgeLabel) {
jsonElement.put(AtlasGraphSONTokens.INTERNAL_LABEL, edge.getLabel());
}
} else if (element instanceof AtlasVertex) {
if (this.includeReservedVertexId) {
putObject(jsonElement, AtlasGraphSONTokens.INTERNAL_ID, element.getId());
}
if (this.includeReservedVertexType) {
jsonElement.put(AtlasGraphSONTokens.INTERNAL_TYPE, AtlasGraphSONTokens.VERTEX);
}
}
return jsonElement;
}
use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.
the class TypedInstanceToGraphMapper method mapTraitInstanceToVertex.
/******************************************** TRAITS ****************************************************/
void mapTraitInstanceToVertex(ITypedStruct traitInstance, IDataType entityType, AtlasVertex parentInstanceVertex) throws AtlasException {
// add a new AtlasVertex for the struct or trait instance
final String traitName = traitInstance.getTypeName();
AtlasVertex traitInstanceVertex = graphHelper.createVertexWithoutIdentity(traitInstance.getTypeName(), null, typeSystem.getDataType(TraitType.class, traitName).getAllSuperTypeNames());
if (LOG.isDebugEnabled()) {
LOG.debug("created vertex {} for trait {}", string(traitInstanceVertex), traitName);
}
// map all the attributes to this newly created AtlasVertex
mapInstanceToVertex(traitInstance, traitInstanceVertex, traitInstance.fieldMapping().fields, false, Operation.CREATE);
// add an edge to the newly created AtlasVertex from the parent
String relationshipLabel = GraphHelper.getTraitLabel(entityType.getName(), traitName);
graphHelper.getOrCreateEdge(parentInstanceVertex, traitInstanceVertex, relationshipLabel);
}
use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.
the class TypedInstanceToGraphMapper method findExistingVertices.
private Map<Id, AtlasVertex> findExistingVertices(Collection<IReferenceableInstance> instances) throws AtlasException {
VertexLookupContext context = new VertexLookupContext(this);
Map<Id, AtlasVertex> result = new HashMap<>();
for (IReferenceableInstance instance : instances) {
context.addInstance(instance);
}
List<Id> instancesToLoad = new ArrayList<>(context.getInstancesToLoadByGuid());
List<String> guidsToLoad = Lists.transform(instancesToLoad, new Function<Id, String>() {
@Override
public String apply(Id instance) {
Id id = getExistingId(instance);
return id.id;
}
});
Map<String, AtlasVertex> instanceVertices = graphHelper.getVerticesForGUIDs(guidsToLoad);
List<String> missingGuids = new ArrayList<>();
for (int i = 0; i < instancesToLoad.size(); i++) {
String guid = guidsToLoad.get(i);
AtlasVertex instanceVertex = instanceVertices.get(guid);
if (instanceVertex == null) {
missingGuids.add(guid);
continue;
}
Id instance = instancesToLoad.get(i);
if (LOG.isDebugEnabled()) {
LOG.debug("Found vertex {} for instance {}", string(instanceVertex), instance);
}
result.put(instance, instanceVertex);
}
if (missingGuids.size() > 0) {
throw new EntityNotFoundException("Could not find entities in the repository with the following GUIDs: " + missingGuids);
}
for (Map.Entry<ClassType, List<IReferenceableInstance>> entry : context.getInstancesToLoadByUniqueAttribute().entrySet()) {
ClassType type = entry.getKey();
List<IReferenceableInstance> instancesForClass = entry.getValue();
List<AtlasVertex> correspondingVertices = graphHelper.getVerticesForInstancesByUniqueAttribute(type, instancesForClass);
for (int i = 0; i < instancesForClass.size(); i++) {
IReferenceableInstance inst = instancesForClass.get(i);
AtlasVertex vertex = correspondingVertices.get(i);
result.put(getExistingId(inst), vertex);
}
}
return result;
}
use of org.apache.atlas.repository.graphdb.AtlasVertex in project incubator-atlas by apache.
the class GraphBackedMetadataRepositoryDeleteTestBase method testDeleteEntityWithDuplicateReferenceListElements.
@Test
public void testDeleteEntityWithDuplicateReferenceListElements() throws Exception {
// Create a table entity, with 2 composite column entities
Referenceable dbEntity = createDBEntity();
String dbGuid = createInstance(dbEntity);
Referenceable table1Entity = createTableEntity(dbGuid);
String tableName = TestUtils.randomString();
table1Entity.set(NAME, tableName);
Referenceable col1 = createColumnEntity();
col1.set(NAME, TestUtils.randomString());
Referenceable col2 = createColumnEntity();
col2.set(NAME, TestUtils.randomString());
// Populate columns reference list with duplicates.
table1Entity.set(COLUMNS_ATTR_NAME, ImmutableList.of(col1, col2, col1, col2));
ClassType dataType = typeSystem.getDataType(ClassType.class, table1Entity.getTypeName());
ITypedReferenceableInstance instance = dataType.convert(table1Entity, Multiplicity.REQUIRED);
TestUtils.resetRequestContext();
List<String> result = repositoryService.createEntities(instance).getCreatedEntities();
Assert.assertEquals(result.size(), 3);
ITypedReferenceableInstance entityDefinition = repositoryService.getEntityDefinition(TABLE_TYPE, NAME, tableName);
String tableGuid = entityDefinition.getId()._getId();
Object attrValue = entityDefinition.get(COLUMNS_ATTR_NAME);
assertTrue(attrValue instanceof List);
List<ITypedReferenceableInstance> columns = (List<ITypedReferenceableInstance>) attrValue;
Assert.assertEquals(columns.size(), 4);
TestUtils.resetRequestContext();
String columnGuid = columns.get(0).getId()._getId();
// Delete one of the columns.
EntityResult deleteResult = repositoryService.deleteEntities(Collections.singletonList(columnGuid));
Assert.assertEquals(deleteResult.getDeletedEntities().size(), 1);
Assert.assertTrue(deleteResult.getDeletedEntities().contains(columnGuid));
Assert.assertEquals(deleteResult.getUpdateEntities().size(), 1);
Assert.assertTrue(deleteResult.getUpdateEntities().contains(tableGuid));
// Verify the duplicate edge IDs were all removed from reference property list.
AtlasVertex tableVertex = GraphHelper.getInstance().getVertexForGUID(tableGuid);
String columnsPropertyName = GraphHelper.getQualifiedFieldName(dataType, COLUMNS_ATTR_NAME);
List columnsPropertyValue = tableVertex.getProperty(columnsPropertyName, List.class);
verifyTestDeleteEntityWithDuplicateReferenceListElements(columnsPropertyValue);
}
Aggregations