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

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

Introduction

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

Prototype

@Beta
public static <T> UnmodifiableIterator<T> mergeSorted(Iterable<? extends Iterator<? extends T>> iterators,
        Comparator<? super T> comparator) 

Source Link

Document

Returns an iterator over the merged contents of all given iterators , traversing every element of the input iterators.

Usage

From source file:io.druid.java.util.common.CloseableIterators.java

public static <T> CloseableIterator<T> mergeSorted(List<? extends CloseableIterator<? extends T>> iterators,
        Comparator<T> comparator) {
    Preconditions.checkNotNull(comparator);

    final Closer closer = Closer.create();
    iterators.forEach(closer::register);

    final Iterator<T> innerIterator = Iterators.mergeSorted(iterators, comparator);
    return wrap(innerIterator, closer);
}

From source file:io.druid.query.groupby.epinephelinae.Groupers.java

public static <KeyType extends Comparable<KeyType>> Iterator<Grouper.Entry<KeyType>> mergeIterators(
        final Iterable<Iterator<Grouper.Entry<KeyType>>> iterators, final boolean sorted) {
    if (sorted) {
        return Iterators.mergeSorted(iterators, ENTRY_COMPARATOR);
    } else {//from ww  w  .j av  a2 s.  co m
        return Iterators.concat(iterators.iterator());
    }
}

From source file:org.elasticsearch.common.collect.Iterators2.java

/** Return a merged view over several iterators, optionally deduplicating equivalent entries. */
public static <T> Iterator<T> mergeSorted(Iterable<Iterator<? extends T>> iterators,
        Comparator<? super T> comparator, boolean deduplicate) {
    Iterator<T> it = Iterators.mergeSorted(iterators, comparator);
    if (deduplicate) {
        it = deduplicateSorted(it, comparator);
    }//from  ww  w.j  a  v a 2s .com
    return it;
}

From source file:org.locationtech.geogig.model.RevObjects.java

/**
 * Creates and returns an iterator of the joint lists of {@link RevTree#trees() trees} and
 * {@link RevTree#features() features} of the given {@code RevTree} whose iteration order is
 * given by the provided {@code comparator}.
 * //ww w. j  a  v a  2 s.co  m
 * @return an iterator over the <b>direct</b> {@link RevTree#trees() trees} and
 *         {@link RevTree#features() feature} children collections, in the order mandated by the
 *         provided {@code comparator}
 */
public static Iterator<Node> children(RevTree tree, Comparator<Node> comparator) {
    checkNotNull(comparator);
    ImmutableList<Node> trees = tree.trees();
    ImmutableList<Node> features = tree.features();
    if (trees.isEmpty()) {
        return features.iterator();
    }
    if (features.isEmpty()) {
        return trees.iterator();
    }
    return Iterators.mergeSorted(ImmutableList.of(trees.iterator(), features.iterator()), comparator);
}

From source file:org.apache.jackrabbit.oak.plugins.document.ValueMap.java

@Nonnull
static Map<Revision, String> create(@Nonnull final NodeDocument doc, @Nonnull final String property) {
    final SortedMap<Revision, String> map = doc.getLocalMap(property);
    if (doc.getPreviousRanges().isEmpty()) {
        return map;
    }//from   w  w w.  j a v a2s .c  om
    final Set<Map.Entry<Revision, String>> entrySet = new AbstractSet<Map.Entry<Revision, String>>() {

        @Override
        @Nonnull
        public Iterator<Map.Entry<Revision, String>> iterator() {

            final Comparator<? super Revision> c = map.comparator();
            final Iterator<NodeDocument> docs;
            if (map.isEmpty()) {
                docs = doc.getPreviousDocs(property, null).iterator();
            } else {
                // merge sort local map into maps of previous documents
                List<Iterator<NodeDocument>> iterators = new ArrayList<Iterator<NodeDocument>>(2);
                iterators.add(Iterators.singletonIterator(doc));
                iterators.add(doc.getPreviousDocs(property, null).iterator());
                docs = Iterators.mergeSorted(iterators, new Comparator<NodeDocument>() {
                    @Override
                    public int compare(NodeDocument o1, NodeDocument o2) {
                        Revision r1 = getFirstRevision(o1);
                        Revision r2 = getFirstRevision(o2);
                        return c.compare(r1, r2);
                    }

                    private Revision getFirstRevision(NodeDocument d) {
                        Map<Revision, String> values;
                        if (Objects.equal(d.getId(), doc.getId())) {
                            // return local map for main document
                            values = d.getLocalMap(property);
                        } else {
                            values = d.getValueMap(property);
                        }
                        return values.keySet().iterator().next();
                    }

                });
            }

            return new MergeSortedIterators<Map.Entry<Revision, String>>(
                    new Comparator<Map.Entry<Revision, String>>() {
                        @Override
                        public int compare(Map.Entry<Revision, String> o1, Map.Entry<Revision, String> o2) {
                            return c.compare(o1.getKey(), o2.getKey());
                        }
                    }) {
                @Override
                public Iterator<Map.Entry<Revision, String>> nextIterator() {
                    NodeDocument d = docs.hasNext() ? docs.next() : null;
                    if (d == null) {
                        return null;
                    }
                    Map<Revision, String> values;
                    if (Objects.equal(d.getId(), doc.getId())) {
                        // return local map for main document
                        values = d.getLocalMap(property);
                    } else {
                        values = d.getValueMap(property);
                    }
                    return values.entrySet().iterator();
                }

                @Override
                public String description() {
                    return "Revisioned values for property " + doc.getId() + "/" + property + ":";
                }
            };
        }

        @Override
        public int size() {
            int size = map.size();
            for (NodeDocument prev : doc.getPreviousDocs(property, null)) {
                size += prev.getValueMap(property).size();
            }
            return size;
        }
    };

    return new AbstractMap<Revision, String>() {

        private final Map<Revision, String> map = doc.getLocalMap(property);

        @Override
        @Nonnull
        public Set<Entry<Revision, String>> entrySet() {
            return entrySet;
        }

        @Override
        public String get(Object key) {
            Revision r = (Revision) key;
            // first check values map of this document
            if (map.containsKey(r)) {
                return map.get(r);
            }
            for (NodeDocument prev : doc.getPreviousDocs(property, r)) {
                String value = prev.getValueMap(property).get(r);
                if (value != null) {
                    return value;
                }
            }
            // not found or null
            return null;
        }

        @Override
        public boolean containsKey(Object key) {
            // check local map first
            if (map.containsKey(key)) {
                return true;
            }
            Revision r = (Revision) key;
            for (NodeDocument prev : doc.getPreviousDocs(property, r)) {
                if (prev.getValueMap(property).containsKey(key)) {
                    return true;
                }
            }
            return false;
        }
    };
}

From source file:com.facebook.presto.operator.MergeHashSort.java

/**
 * Rows with same hash value are guaranteed to be in the same result page.
 *///w  ww  .j a v  a  2  s . co m
public Iterator<Page> merge(List<Type> keyTypes, List<Type> allTypes, List<Iterator<Page>> channels) {
    List<Iterator<PagePosition>> channelIterators = channels.stream()
            .map(channel -> new SingleChannelPagePositions(channel, memoryContext.newLocalMemoryContext()))
            .collect(toList());

    int[] hashChannels = new int[keyTypes.size()];
    for (int i = 0; i < keyTypes.size(); i++) {
        hashChannels[i] = i;
    }
    HashGenerator hashGenerator = new InterpretedHashGenerator(keyTypes, hashChannels);

    return new PageRewriteIterator(hashGenerator, allTypes,
            Iterators.mergeSorted(channelIterators,
                    (PagePosition left, PagePosition right) -> comparePages(hashGenerator, left, right)),
            memoryContext.newLocalMemoryContext());
}

From source file:com.palantir.atlasdb.keyvalue.partition.util.PartitionedRangedIterator.java

private void prepareNextRange() {
    Preconditions.checkState(currentRange.hasNext());
    Preconditions.checkState(!getRowIterator().hasNext());
    ConsistentRingRangeRequest newRange = currentRange.next();
    closeCurrentRangeIterators();//from   w  ww.java2  s. c om
    currentRangeIterators = computeNextRange(newRange);
    Preconditions.checkState(!currentRangeIterators.isEmpty());
    rowIterator = Iterators.<RowResult<T>>peekingIterator(
            Iterators.mergeSorted(currentRangeIterators, RowResult.<T>getOrderingByRowName()));
}

From source file:org.apache.accumulo.tserver.log.RecoveryLogsIterator.java

/**
 * Iterates only over keys in the range [start,end].
 *//*  w  w  w.ja  va2  s  . co m*/
RecoveryLogsIterator(VolumeManager fs, List<Path> recoveryLogPaths, LogFileKey start, LogFileKey end)
        throws IOException {

    iterators = new ArrayList<>(recoveryLogPaths.size());

    try {
        for (Path log : recoveryLogPaths) {
            LOG.debug("Opening recovery log {}", log.getName());
            RecoveryLogReader rlr = new RecoveryLogReader(fs, log, start, end);
            if (rlr.hasNext()) {
                LOG.debug("Write ahead log {} has data in range {} {}", log.getName(), start, end);
                iterators.add(rlr);
            } else {
                LOG.debug("Write ahead log {} has no data in range {} {}", log.getName(), start, end);
                rlr.close();
            }
        }

        iter = Iterators.mergeSorted(iterators, (o1, o2) -> o1.getKey().compareTo(o2.getKey()));

    } catch (RuntimeException | IOException e) {
        try {
            close();
        } catch (Exception e2) {
            e.addSuppressed(e2);
        }
        throw e;
    }
}

From source file:org.locationtech.geogig.plumbing.merge.CheckMergeScenarioOp.java

@Override
protected Boolean _call() {
    if (commits.size() < 2) {
        return Boolean.FALSE;
    }/*from w w w .  j  a  va2 s.c  o m*/
    Optional<ObjectId> ancestor = command(FindCommonAncestor.class).setLeft(commits.get(0))
            .setRight(commits.get(1)).call();
    Preconditions.checkState(ancestor.isPresent(), "No ancestor commit could be found.");
    for (int i = 2; i < commits.size(); i++) {
        ancestor = command(FindCommonAncestor.class).setLeft(commits.get(i)).setRightId(ancestor.get()).call();
        Preconditions.checkState(ancestor.isPresent(), "No ancestor commit could be found.");
    }

    List<AutoCloseableIterator<DiffEntry>> commitDiffs = new ArrayList<AutoCloseableIterator<DiffEntry>>();
    try {
        // we organize the changes made for each path
        for (RevCommit commit : commits) {
            AutoCloseableIterator<DiffEntry> toMergeDiffs = command(DiffTree.class).setReportTrees(true)
                    .setOldTree(ancestor.get()).setNewTree(commit.getId()).setPreserveIterationOrder(true)
                    .call();
            commitDiffs.add(toMergeDiffs);
        }

        PeekingIterator<DiffEntry> merged = Iterators
                .peekingIterator(Iterators.mergeSorted(commitDiffs, DiffEntry.COMPARATOR));

        long progress = 0;
        while (merged.hasNext()) {
            List<DiffEntry> nextPath = nextPath(merged);
            getProgressListener().setProgress(++progress);
            if (hasConflicts(nextPath)) {
                return true;
            }
        }
    } finally {
        commitDiffs.forEach((iter) -> iter.close());
    }

    return false;

}

From source file:com.yandex.yoctodb.v1.immutable.V1CompositeDatabase.java

@Override
public void execute(@NotNull final Query query, @NotNull final DocumentProcessor processor) {
    final Iterator<ScoredDocument<?>> iterator;

    // Doing merging iff there is sorting
    if (query.hasSorting()) {
        final List<Iterator<? extends ScoredDocument<?>>> results = new ArrayList<>(databases.size());
        for (IndexedDatabase db : databases) {
            final BitSet docs = query.filteredUnlimited(db, bitSetPool);

            if (docs == null) {
                continue;
            }//w w w.j  a  va 2  s  .c  o m

            assert !docs.isEmpty();

            results.add(query.sortedUnlimited(docs, db, bitSetPool));
        }

        if (results.isEmpty()) {
            return;
        }

        iterator = Iterators.mergeSorted(results, SCORED_DOCUMENT_COMPARATOR);
    } else {
        iterator = Iterators.concat(new FilterResultIterator(query, databases.iterator(), bitSetPool));
    }

    // Skipping values
    if (query.getSkip() != 0) {
        Iterators.advance(iterator, query.getSkip());
    }

    // Limited
    final Iterator<ScoredDocument<?>> limitedIterator;
    if (query.getLimit() == Integer.MAX_VALUE) {
        limitedIterator = iterator;
    } else {
        limitedIterator = Iterators.limit(iterator, query.getLimit());
    }

    while (limitedIterator.hasNext()) {
        final ScoredDocument<?> document = limitedIterator.next();
        if (!processor.process(document.getDocument(), document.getDatabase())) {
            return;
        }
    }
}