Java Code Examples for org.apache.lucene.util.CollectionUtil#introSort()

The following examples show how to use org.apache.lucene.util.CollectionUtil#introSort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InternalHistogram.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public InternalAggregation doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    List<B> reducedBuckets = reduceBuckets(aggregations, reduceContext);

    // adding empty buckets if needed
    if (minDocCount == 0) {
        addEmptyBuckets(reducedBuckets, reduceContext);
    }

    if (order == InternalOrder.KEY_ASC) {
        // nothing to do, data are already sorted since shards return
        // sorted buckets and the merge-sort performed by reduceBuckets
        // maintains order
    } else if (order == InternalOrder.KEY_DESC) {
        // we just need to reverse here...
        List<B> reverse = new ArrayList<>(reducedBuckets);
        Collections.reverse(reverse);
        reducedBuckets = reverse;
    } else {
        // sorted by sub-aggregation, need to fall back to a costly n*log(n) sort
        CollectionUtil.introSort(reducedBuckets, order.comparator());
    }

    return getFactory().create(getName(), reducedBuckets, order, minDocCount, emptyBucketInfo, formatter, keyed, pipelineAggregators(),
            getMetaData());
}
 
Example 2
Source File: ElectMasterService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private List<DiscoveryNode> sortedMasterNodes(Iterable<DiscoveryNode> nodes) {
    List<DiscoveryNode> possibleNodes = CollectionUtils.iterableAsArrayList(nodes);
    if (possibleNodes.isEmpty()) {
        return null;
    }
    // clean non master nodes
    for (Iterator<DiscoveryNode> it = possibleNodes.iterator(); it.hasNext(); ) {
        DiscoveryNode node = it.next();
        if (!node.masterNode()) {
            it.remove();
        }
    }
    CollectionUtil.introSort(possibleNodes, nodeComparator);
    return possibleNodes;
}
 
Example 3
Source File: CommitPoints.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public CommitPoints(List<CommitPoint> commitPoints) {
    CollectionUtil.introSort(commitPoints, new Comparator<CommitPoint>() {
        @Override
        public int compare(CommitPoint o1, CommitPoint o2) {
            return (o2.version() < o1.version() ? -1 : (o2.version() == o1.version() ? 0 : 1));
        }
    });
    this.commitPoints = Collections.unmodifiableList(new ArrayList<>(commitPoints));
}
 
Example 4
Source File: ElectMasterService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the given nodes sorted by likelyhood of being elected as master, most likely first.
 * Non-master nodes are not removed but are rather put in the end
 */
public List<DiscoveryNode> sortByMasterLikelihood(Iterable<DiscoveryNode> nodes) {
    ArrayList<DiscoveryNode> sortedNodes = CollectionUtils.iterableAsArrayList(nodes);
    CollectionUtil.introSort(sortedNodes, nodeComparator);
    return sortedNodes;
}