Search in sources :

Example 1 with DataSource

use of org.apache.helix.Criteria.DataSource in project helix by apache.

the class CriteriaEvaluator method evaluateCriteria.

/**
 * Examine persisted data to match wildcards in {@link Criteria}
 * @param recipientCriteria Criteria specifying the message destinations
 * @param manager connection to the persisted data
 * @return map of evaluated criteria
 */
public List<Map<String, String>> evaluateCriteria(Criteria recipientCriteria, HelixManager manager) {
    // get the data
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    List<HelixProperty> properties;
    DataSource dataSource = recipientCriteria.getDataSource();
    String resourceName = recipientCriteria.getResource();
    String instanceName = recipientCriteria.getInstanceName();
    switch(dataSource) {
        case EXTERNALVIEW:
            properties = getProperty(accessor, resourceName, keyBuilder.externalViews(), keyBuilder.externalView(resourceName), DataSource.EXTERNALVIEW.name());
            break;
        case IDEALSTATES:
            properties = getProperty(accessor, resourceName, keyBuilder.idealStates(), keyBuilder.idealStates(resourceName), DataSource.IDEALSTATES.name());
            break;
        case LIVEINSTANCES:
            properties = getProperty(accessor, instanceName, keyBuilder.liveInstances(), keyBuilder.liveInstance(instanceName), DataSource.LIVEINSTANCES.name());
            break;
        case INSTANCES:
            properties = getProperty(accessor, instanceName, keyBuilder.instances(), keyBuilder.instance(instanceName), DataSource.INSTANCES.name());
            break;
        default:
            return Lists.newArrayList();
    }
    // flatten the data
    List<ZNRecordRow> allRows = ZNRecordRow.flatten(HelixProperty.convertToList(properties));
    // save the matches
    Set<String> liveParticipants = accessor.getChildValuesMap(keyBuilder.liveInstances()).keySet();
    List<ZNRecordRow> result = Lists.newArrayList();
    for (ZNRecordRow row : allRows) {
        // The participant instance name is stored in the return value of either getRecordId() or getMapSubKey()
        if (rowMatches(recipientCriteria, row) && (liveParticipants.contains(row.getRecordId()) || liveParticipants.contains(row.getMapSubKey()))) {
            result.add(row);
        }
    }
    Set<Map<String, String>> selected = Sets.newHashSet();
    // deduplicate and convert the matches into the required format
    for (ZNRecordRow row : result) {
        Map<String, String> resultRow = new HashMap<String, String>();
        resultRow.put("instanceName", !recipientCriteria.getInstanceName().equals("") ? (!Strings.isNullOrEmpty(row.getMapSubKey()) ? row.getMapSubKey() : row.getRecordId()) : "");
        resultRow.put("resourceName", !recipientCriteria.getResource().equals("") ? row.getRecordId() : "");
        resultRow.put("partitionName", !recipientCriteria.getPartition().equals("") ? row.getMapKey() : "");
        resultRow.put("partitionState", !recipientCriteria.getPartitionState().equals("") ? row.getMapValue() : "");
        selected.add(resultRow);
    }
    logger.info("Query returned " + selected.size() + " rows");
    return Lists.newArrayList(selected);
}
Also used : HashMap(java.util.HashMap) DataSource(org.apache.helix.Criteria.DataSource) HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixProperty(org.apache.helix.HelixProperty) HashMap(java.util.HashMap) Map(java.util.Map) PropertyKey(org.apache.helix.PropertyKey)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 DataSource (org.apache.helix.Criteria.DataSource)1 HelixDataAccessor (org.apache.helix.HelixDataAccessor)1 HelixProperty (org.apache.helix.HelixProperty)1 PropertyKey (org.apache.helix.PropertyKey)1