Example usage for com.google.common.collect Iterables mergeSorted

List of usage examples for com.google.common.collect Iterables mergeSorted

Introduction

In this page you can find the example usage for com.google.common.collect Iterables mergeSorted.

Prototype

@Beta
public static <T> Iterable<T> mergeSorted(final Iterable<? extends Iterable<? extends T>> iterables,
        final Comparator<? super T> comparator) 

Source Link

Document

Returns an iterable over the merged contents of all given iterables .

Usage

From source file:org.openqa.selenium.logging.LogCombiner.java

public static LogEntries combine(LogEntries... entries) {
    return new LogEntries(Iterables.mergeSorted(Lists.newArrayList(entries), LOG_ENTRY_TIMESTAMP_COMPARATOR));
}

From source file:io.druid.timeline.UnionTimeLineLookup.java

@Override
public Iterable<TimelineObjectHolder<VersionType, ObjectType>> lookup(final Interval interval) {
    return Iterables.mergeSorted(Iterables.transform(delegates,
            new Function<TimelineLookup<VersionType, ObjectType>, Iterable<TimelineObjectHolder<VersionType, ObjectType>>>() {
                @Override//from  w  w  w. j a v  a2  s . c  o  m
                public Iterable<TimelineObjectHolder<VersionType, ObjectType>> apply(
                        TimelineLookup<VersionType, ObjectType> input) {
                    return input.lookup(interval);
                }
            }), new Comparator<TimelineObjectHolder<VersionType, ObjectType>>() {
                @Override
                public int compare(TimelineObjectHolder<VersionType, ObjectType> o1,
                        TimelineObjectHolder<VersionType, ObjectType> o2) {
                    return Comparators.intervalsByStartThenEnd().compare(o1.getInterval(), o2.getInterval());
                }
            });
}

From source file:merge.MergeSort.java

private Iterable<Integer> mergesort(Iterable<Integer> first, Iterable<Integer> second) {

    Iterable<Integer> all = Iterables.mergeSorted(ImmutableList.of(first, second), Ordering.natural());

    return all;//from   w w  w  .j ava  2s . c  o m
}

From source file:org.apache.druid.query.search.SearchBinaryFn.java

@Override
public Result<SearchResultValue> apply(Result<SearchResultValue> arg1, Result<SearchResultValue> arg2) {
    if (arg1 == null) {
        return arg2;
    }//ww w .  j a  va  2 s. c  o m

    if (arg2 == null) {
        return arg1;
    }

    final int limit = gran instanceof AllGranularity ? this.limit : -1;

    SearchResultValue arg1Vals = arg1.getValue();
    SearchResultValue arg2Vals = arg2.getValue();

    Iterable<SearchHit> merged = Iterables.mergeSorted(Arrays.asList(arg1Vals, arg2Vals),
            searchSortSpec.getComparator());

    int maxSize = arg1Vals.getValue().size() + arg2Vals.getValue().size();
    if (limit > 0) {
        maxSize = Math.min(limit, maxSize);
    }
    List<SearchHit> results = Lists.newArrayListWithExpectedSize(maxSize);

    SearchHit prev = null;
    for (SearchHit searchHit : merged) {
        if (prev == null) {
            prev = searchHit;
            continue;
        }
        if (prev.equals(searchHit)) {
            if (prev.getCount() != null && searchHit.getCount() != null) {
                prev = new SearchHit(prev.getDimension(), prev.getValue(),
                        prev.getCount() + searchHit.getCount());
            } else {
                prev = new SearchHit(prev.getDimension(), prev.getValue());
            }
        } else {
            results.add(prev);
            prev = searchHit;
            if (limit > 0 && results.size() >= limit) {
                break;
            }
        }
    }

    if (prev != null && (limit < 0 || results.size() < limit)) {
        results.add(prev);
    }

    final DateTime timestamp = gran instanceof AllGranularity ? arg1.getTimestamp()
            : gran.bucketStart(arg1.getTimestamp());

    return new Result<SearchResultValue>(timestamp, new SearchResultValue(results));
}

From source file:hudson.util.RunList.java

private Iterable<R> combine(Iterable<Iterable<R>> runLists) {
    return Iterables.mergeSorted(runLists, new Comparator<R>() {
        public int compare(R o1, R o2) {
            long lhs = o1.getTimeInMillis();
            long rhs = o2.getTimeInMillis();
            if (lhs > rhs)
                return -1;
            if (lhs < rhs)
                return 1;
            return 0;
        }//from w w w  .j  a  v  a2  s  . c om
    });
}

From source file:com.xiaomi.linden.cluster.ResultMerger.java

public static LindenResult merge(final LindenSearchRequest lindenRequest, final List<LindenResult> resultList) {
    if (resultList == null || resultList.isEmpty()) {
        return EMPTY_RESULT;
    }// w  w  w .ja va2  s  .c  o  m

    Iterator<LindenResult> iterator = resultList.iterator();
    int failureCount = 0;
    LindenResult failedResult = null;
    while (iterator.hasNext()) {
        LindenResult result = iterator.next();
        if (!result.isSuccess()) {
            failureCount++;
            failedResult = result;
            iterator.remove();
        }
    }
    if (resultList.isEmpty()) {
        if (failureCount == 1) {
            LOGGER.error("The shard failed for search request {}", lindenRequest.toString());
            return failedResult;
        }
        LOGGER.error("All shards failed for search request {}", lindenRequest.toString());
        return ALL_SHARDS_FAILED_RESULT;
    }

    LindenResult mergedResult;
    if (lindenRequest.isSetGroupParam()) {
        //merge results for grouping search
        mergedResult = mergeGroupSearch(lindenRequest, resultList);
    } else {
        //merge results for normal searching
        mergedResult = new LindenResult().setTotalHits(0).setQueryInfo(resultList.get(0).queryInfo);
        List<List<LindenHit>> hits = new ArrayList<>();
        for (LindenResult result : resultList) {
            mergedResult.totalHits += result.getTotalHits();
            hits.add(result.getHits());
        }
        //merge LindenHit
        List<LindenSortField> sortFields = lindenRequest.isSetSort() ? lindenRequest.getSort().getFields()
                : null;
        Iterable<LindenHit> mergedHits = Iterables.mergeSorted(hits, new LindenHitCmp(sortFields));
        List<LindenHit> topNHits = Lists.newArrayList(mergedHits);
        if (lindenRequest.getOffset() <= topNHits.size()) {
            List<LindenHit> subHits = topNHits.subList(lindenRequest.getOffset(),
                    Math.min(lindenRequest.getOffset() + lindenRequest.getLength(), topNHits.size()));
            mergedResult.setHits(subHits);
        } else {
            mergedResult.setHits(new ArrayList<LindenHit>());
        }
    }

    // Merge facet result
    if (lindenRequest.isSetFacet()) {
        mergeFacet(lindenRequest, resultList, mergedResult);
    }

    if (failureCount > 0) {
        mergedResult.setError(failureCount + " shards failed.");
    }
    return mergedResult;
}

From source file:com.pinterest.pinlater.backends.common.PinLaterBackendUtils.java

/**
 * Merges multiple iterables into a list using the comparing logic provided by the comparator.
 * The returned list will only include the first n merged items, where n is the limit specified.
 *
 * @param iterablesToMerge  The iterables to be merged.
 * @param comparator        Comparator specifying the comparison logic between the iterables.
 * @param limit             Max number of results that will be returned.
 * @param <T>               Iterable item type.
 * @param <S>               Comparator between iterate items type.
 * @return List of the first n merged results.
 *///from  w  ww.  ja v a 2s.  co m
public static <T, S extends Comparator<T>> List<T> mergeIntoList(
        Iterable<? extends Iterable<T>> iterablesToMerge, S comparator, int limit) {
    // Perform a k-way merge on the collections and return the result in an ArrayList.
    List<T> mergedCols = Lists.newLinkedList();
    Iterator<T> mergeIterator = Iterables.mergeSorted(iterablesToMerge, comparator).iterator();
    while (mergeIterator.hasNext() && mergedCols.size() < limit) {
        mergedCols.add(mergeIterator.next());
    }
    return mergedCols;
}

From source file:com.xiaomi.linden.cluster.ResultMerger.java

private static LindenResult mergeGroupSearch(LindenSearchRequest lindenRequest, List<LindenResult> resultList) {
    LindenResult mergedResult = resultList.get(0);
    if (!mergedResult.isSetHits()) {
        mergedResult.setHits(new ArrayList<LindenHit>());
    }//  w ww.j  a v  a 2  s. co  m
    String groupField = lindenRequest.getGroupParam().getGroupField();
    int innerLimit = lindenRequest.getGroupParam().getGroupInnerLimit();
    //traverse LindenResults from shards
    for (int i = 1; i < resultList.size(); ++i) {
        LindenResult subResult = resultList.get(i);
        if (!subResult.isSetHits()) {
            continue;
        }
        mergedResult.totalHits += subResult.totalHits;
        mergedResult.totalGroups = Math.max(mergedResult.totalGroups, subResult.totalGroups);
        mergedResult.totalGroupHits += subResult.totalGroupHits;
        //traverse groups in waiting LindenResult
        for (LindenHit subGroup : subResult.getHits()) {
            String groupName = subGroup.getFields().get(groupField);
            boolean isFound = false;
            //find the group in the merged groupList
            for (int j = 0; j < mergedResult.getHitsSize(); ++j) {
                LindenHit mergedHit = mergedResult.getHits().get(j);
                if (mergedHit.getFields().get(groupField).equals(groupName)) {
                    Iterable<LindenHit> groupIterable = Iterables.mergeSorted(
                            ImmutableList.of(subGroup.getGroupHits(), mergedHit.getGroupHits()),
                            new LindenHitCmp(null));
                    List<LindenHit> hits = Lists.newArrayList(Iterables.limit(groupIterable, innerLimit));
                    if (mergedHit.getScore() < subGroup.getScore()) {
                        mergedHit = subGroup;
                    }
                    mergedHit.setGroupHits(hits);
                    mergedResult.getHits().set(j, mergedHit);
                    isFound = true;
                    break;
                }
            }
            if (!isFound) {
                mergedResult.getHits().add(subGroup);
            }
        }
    }
    //sort the group by score
    Ordering<LindenHit> ordering = new Ordering<LindenHit>() {
        @Override
        public int compare(@Nullable LindenHit left, @Nullable LindenHit right) {
            return Double.compare(left.getScore(), right.getScore());
        }
    };
    List<LindenHit> orderedHits = ordering.greatestOf(mergedResult.getHits(), mergedResult.getHitsSize()); //offset -> offset+size groups
    int from = lindenRequest.getOffset();
    int size = lindenRequest.getLength();
    if (from < orderedHits.size()) {
        List<LindenHit> subHits = orderedHits.subList(from, Math.min(from + size, orderedHits.size()));
        mergedResult.setHits(subHits);
    } else {
        mergedResult.setHits(new ArrayList<LindenHit>());
    }
    return mergedResult;
}

From source file:org.opennms.newts.gsod.MergeSort.java

private Function<? super Iterable<Iterable<GSODLine>>, Iterable<GSODLine>> mergeSorter() {
    return new Function<Iterable<Iterable<GSODLine>>, Iterable<GSODLine>>() {

        @Override/*from   ww  w.  j  a va2  s.  com*/
        public Iterable<GSODLine> apply(Iterable<Iterable<GSODLine>> input) {
            return Iterables.mergeSorted(input, new Comparator<GSODLine>() {

                @Override
                public int compare(GSODLine o1, GSODLine o2) {
                    return o1.compareTo(o2);
                }
            });
        }

    };
}

From source file:com.spotify.heroic.metric.bigtable.BigtableBackend.java

private AsyncFuture<FetchData> fetchBatch(final FetchQuotaWatcher watcher, final MetricType type,
        final List<PreparedQuery> prepared, final BigtableConnection c) {
    final BigtableDataClient client = c.dataClient();

    final List<AsyncFuture<FetchData>> fetches = new ArrayList<>(prepared.size());

    for (final PreparedQuery p : prepared) {
        final AsyncFuture<List<FlatRow>> readRows = client.readRows(table,
                ReadRowsRequest.builder().rowKey(p.request.getRowKey())
                        .filter(RowFilter.chain(Arrays.asList(
                                RowFilter.newColumnRangeBuilder(p.request.getColumnFamily())
                                        .startQualifierOpen(p.request.getStartQualifierOpen())
                                        .endQualifierClosed(p.request.getEndQualifierClosed()).build(),
                                RowFilter.onlyLatestCell())))
                        .build());//from  ww  w .ja v a2  s. c  om

        final Function<FlatRow.Cell, Metric> transform = cell -> p.deserialize(cell.getQualifier(),
                cell.getValue());

        final QueryTrace.NamedWatch w = QueryTrace.watch(FETCH_SEGMENT);

        fetches.add(readRows.directTransform(result -> {
            final List<Iterable<Metric>> points = new ArrayList<>();

            for (final FlatRow row : result) {
                watcher.readData(row.getCells().size());
                points.add(Iterables.transform(row.getCells(), transform));
            }

            final QueryTrace trace = w.end();
            final ImmutableList<Long> times = ImmutableList.of(trace.getElapsed());
            final List<Metric> data = ImmutableList.copyOf(Iterables.mergeSorted(points, Metric.comparator()));
            final List<MetricCollection> groups = ImmutableList.of(MetricCollection.build(type, data));

            return FetchData.of(trace, times, groups);
        }));
    }

    return async.collect(fetches, FetchData.collect(FETCH)).directTransform(result -> {
        watcher.accessedRows(prepared.size());
        return result;
    });
}