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 > ( ) ; //build up an or expression to find vertices which match at least one of the unique attribute constraints //For each unique attributes, we add a within clause to match vertices that have a value of that attribute //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 ) ; }
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 .