Search in sources :

Example 1 with EntryScoreComparator

use of org.apache.geode.cache.lucene.internal.distributed.TopEntries.EntryScoreComparator in project geode by apache.

the class TopEntriesCollectorManager method reduce.

@Override
public TopEntriesCollector reduce(Collection<TopEntriesCollector> collectors) {
    TopEntriesCollector mergedResult = new TopEntriesCollector(id, limit);
    if (collectors.isEmpty()) {
        return mergedResult;
    }
    final EntryScoreComparator scoreComparator = new TopEntries().new EntryScoreComparator();
    // orders a entry with higher score above a doc with lower score
    Comparator<ListScanner> entryListComparator = new Comparator<ListScanner>() {

        @Override
        public int compare(ListScanner l1, ListScanner l2) {
            EntryScore o1 = l1.peek();
            EntryScore o2 = l2.peek();
            return scoreComparator.compare(o1, o2);
        }
    };
    // The queue contains iterators for all bucket results. The queue puts the entry with the
    // highest score at the head
    // using score comparator.
    PriorityQueue<ListScanner> entryListsPriorityQueue;
    entryListsPriorityQueue = new PriorityQueue<ListScanner>(collectors.size(), Collections.reverseOrder(entryListComparator));
    for (IndexResultCollector collector : collectors) {
        logger.debug("Number of entries found in collector {} is {}", collector.getName(), collector.size());
        if (collector.size() > 0) {
            entryListsPriorityQueue.add(new ListScanner(((TopEntriesCollector) collector).getEntries().getHits()));
        }
    }
    logger.debug("Only {} count of entries will be reduced. Other entries will be ignored", limit);
    while (entryListsPriorityQueue.size() > 0 && limit > mergedResult.size()) {
        ListScanner scanner = entryListsPriorityQueue.remove();
        EntryScore entry = scanner.next();
        mergedResult.collect(entry);
        if (scanner.hasNext()) {
            entryListsPriorityQueue.add(scanner);
        }
    }
    logger.debug("Reduced size of {} is {}", mergedResult.getName(), mergedResult.size());
    return mergedResult;
}
Also used : IndexResultCollector(org.apache.geode.cache.lucene.internal.repository.IndexResultCollector) EntryScoreComparator(org.apache.geode.cache.lucene.internal.distributed.TopEntries.EntryScoreComparator) EntryScoreComparator(org.apache.geode.cache.lucene.internal.distributed.TopEntries.EntryScoreComparator) Comparator(java.util.Comparator)

Aggregations

Comparator (java.util.Comparator)1 EntryScoreComparator (org.apache.geode.cache.lucene.internal.distributed.TopEntries.EntryScoreComparator)1 IndexResultCollector (org.apache.geode.cache.lucene.internal.repository.IndexResultCollector)1