use of org.apache.atlas.util.AttributeValueMap in project atlas by apache.
the class GraphHelper method getInstancesForVertex.
// finds the instance(s) that correspond to the given vertex
private Collection<IndexedInstance> getInstancesForVertex(Map<String, AttributeValueMap> map, AtlasVertex foundVertex) {
for (Map.Entry<String, AttributeValueMap> entry : map.entrySet()) {
String propertyName = entry.getKey();
AttributeValueMap valueMap = entry.getValue();
Object vertexValue = foundVertex.getProperty(propertyName, Object.class);
Collection<IndexedInstance> instances = valueMap.get(vertexValue);
if (instances != null && instances.size() > 0) {
// the user requested).
return instances;
}
// try another attribute
}
return Collections.emptyList();
}
use of org.apache.atlas.util.AttributeValueMap in project atlas by apache.
the class GraphHelper method getVerticesForInstancesByUniqueAttribute.
/**
* Finds vertices that match at least one unique attribute of the instances specified. The AtlasVertex at a given index in the result corresponds
* to the IReferencableInstance at that same index that was passed in. The number of elements in the resultant list is guaranteed to match the
* number of instances that were passed in. If no vertex is found for a given instance, that entry will be null in the resultant list.
*
* @param classType
* @param instancesForClass
* @return
* @throws AtlasException
*/
public List<AtlasVertex> getVerticesForInstancesByUniqueAttribute(AtlasEntityType classType, List<? extends Referenceable> instancesForClass) throws AtlasException {
// For each attribute, need to figure out what values to search for and which instance(s)
// those values correspond to.
Map<String, AttributeValueMap> map = new HashMap<String, AttributeValueMap>();
for (AtlasAttribute attributeInfo : classType.getUniqAttributes().values()) {
String propertyKey = attributeInfo.getQualifiedName();
AttributeValueMap mapForAttribute = new AttributeValueMap();
for (int idx = 0; idx < instancesForClass.size(); idx++) {
Referenceable instance = instancesForClass.get(idx);
Object value = instance.get(attributeInfo.getName());
mapForAttribute.put(value, instance, idx);
}
map.put(propertyKey, mapForAttribute);
}
AtlasVertex[] result = new AtlasVertex[instancesForClass.size()];
if (map.isEmpty()) {
// no unique attributes
return Arrays.asList(result);
}
// construct gremlin query
AtlasGraphQuery query = graph.query();
query.has(Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getTypeName());
query.has(Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name());
List<AtlasGraphQuery> orChildren = new ArrayList<AtlasGraphQuery>();
// that matches the value in some instance.
for (Map.Entry<String, AttributeValueMap> entry : map.entrySet()) {
AtlasGraphQuery orChild = query.createChildQuery();
String propertyName = entry.getKey();
AttributeValueMap valueMap = entry.getValue();
Set<Object> values = valueMap.getAttributeValues();
if (values.size() == 1) {
orChild.has(propertyName, values.iterator().next());
} else if (values.size() > 1) {
orChild.in(propertyName, values);
}
orChildren.add(orChild);
}
if (orChildren.size() == 1) {
AtlasGraphQuery child = orChildren.get(0);
query.addConditionsFrom(child);
} else if (orChildren.size() > 1) {
query.or(orChildren);
}
Iterable<AtlasVertex> queryResult = query.vertices();
for (AtlasVertex matchingVertex : queryResult) {
Collection<IndexedInstance> matches = getInstancesForVertex(map, matchingVertex);
for (IndexedInstance wrapper : matches) {
result[wrapper.getIndex()] = matchingVertex;
}
}
return Arrays.asList(result);
}
use of org.apache.atlas.util.AttributeValueMap in project incubator-atlas by apache.
the class GraphHelper method getVerticesForInstancesByUniqueAttribute.
/**
* Finds vertices that match at least one unique attribute of the instances specified. The AtlasVertex at a given index in the result corresponds
* to the IReferencableInstance at that same index that was passed in. The number of elements in the resultant list is guaranteed to match the
* number of instances that were passed in. If no vertex is found for a given instance, that entry will be null in the resultant list.
*
* @param classType
* @param instancesForClass
* @return
* @throws AtlasException
*/
public List<AtlasVertex> getVerticesForInstancesByUniqueAttribute(ClassType classType, List<? extends IReferenceableInstance> instancesForClass) throws AtlasException {
// For each attribute, need to figure out what values to search for and which instance(s)
// those values correspond to.
Map<String, AttributeValueMap> map = new HashMap<String, AttributeValueMap>();
for (AttributeInfo attributeInfo : classType.fieldMapping().fields.values()) {
if (attributeInfo.isUnique) {
String propertyKey = getQualifiedFieldName(classType, attributeInfo.name);
AttributeValueMap mapForAttribute = new AttributeValueMap();
for (int idx = 0; idx < instancesForClass.size(); idx++) {
IReferenceableInstance instance = instancesForClass.get(idx);
Object value = instance.get(attributeInfo.name);
mapForAttribute.put(value, instance, idx);
}
map.put(propertyKey, mapForAttribute);
}
}
AtlasVertex[] result = new AtlasVertex[instancesForClass.size()];
if (map.isEmpty()) {
// no unique attributes
return Arrays.asList(result);
}
// construct gremlin query
AtlasGraphQuery query = graph.query();
query.has(Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getName());
query.has(Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name());
List<AtlasGraphQuery> orChildren = new ArrayList<AtlasGraphQuery>();
// that matches the value in some instance.
for (Map.Entry<String, AttributeValueMap> entry : map.entrySet()) {
AtlasGraphQuery orChild = query.createChildQuery();
String propertyName = entry.getKey();
AttributeValueMap valueMap = entry.getValue();
Set<Object> values = valueMap.getAttributeValues();
if (values.size() == 1) {
orChild.has(propertyName, values.iterator().next());
} else if (values.size() > 1) {
orChild.in(propertyName, values);
}
orChildren.add(orChild);
}
if (orChildren.size() == 1) {
AtlasGraphQuery child = orChildren.get(0);
query.addConditionsFrom(child);
} else if (orChildren.size() > 1) {
query.or(orChildren);
}
Iterable<AtlasVertex> queryResult = query.vertices();
for (AtlasVertex matchingVertex : queryResult) {
Collection<IndexedInstance> matches = getInstancesForVertex(map, matchingVertex);
for (IndexedInstance wrapper : matches) {
result[wrapper.getIndex()] = matchingVertex;
}
}
return Arrays.asList(result);
}
use of org.apache.atlas.util.AttributeValueMap in project incubator-atlas by apache.
the class GraphHelper method getInstancesForVertex.
// finds the instance(s) that correspond to the given vertex
private Collection<IndexedInstance> getInstancesForVertex(Map<String, AttributeValueMap> map, AtlasVertex foundVertex) {
for (Map.Entry<String, AttributeValueMap> entry : map.entrySet()) {
String propertyName = entry.getKey();
AttributeValueMap valueMap = entry.getValue();
Object vertexValue = foundVertex.getProperty(propertyName, Object.class);
Collection<IndexedInstance> instances = valueMap.get(vertexValue);
if (instances != null && instances.size() > 0) {
// the user requested).
return instances;
}
// try another attribute
}
return Collections.emptyList();
}
Aggregations