Example usage for com.google.common.collect PeekingIterator next

List of usage examples for com.google.common.collect PeekingIterator next

Introduction

In this page you can find the example usage for com.google.common.collect PeekingIterator next.

Prototype

@Override
E next();

Source Link

Document

The objects returned by consecutive calls to #peek() then #next() are guaranteed to be equal to each other.

Usage

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

public static RowResult<Value> mergeResults(PeekingIterator<RowResult<Value>> it,
        QuorumParameters.QuorumRequestParameters quorumRequestParameters) {
    Preconditions.checkArgument(it.hasNext());

    byte[] row = it.peek().getRowName();
    final SortedMap<byte[], Value> result = Maps.newTreeMap(UnsignedBytes.lexicographicalComparator());
    int failCount = 0;
    int succCount = 0;
    RuntimeException lastSuppressedException = null;

    while (it.hasNext() && Arrays.equals(it.peek().getRowName(), row)) {
        try {//from   w  w w  .  jav  a  2 s.  com
            for (Map.Entry<Cell, Value> e : it.next().getCells()) {
                final byte[] col = e.getKey().getColumnName();

                // Assert that there is not contradictory data
                if (result.containsKey(col) && e.getValue().getTimestamp() == result.get(col).getTimestamp()) {
                    assert Arrays.equals(result.get(col).getContents(), e.getValue().getContents());
                }

                if (!result.containsKey(col) || result.get(col).getTimestamp() < e.getValue().getTimestamp()) {
                    result.put(col, e.getValue());
                }
            }
            succCount++;
        } catch (RuntimeException e) {
            System.err.println("Could not read for rangeRequest.");
            failCount++;
            if (failCount >= quorumRequestParameters.getFailureFactor()) {
                throw Throwables.rewrapAndThrowUncheckedException("Could not get enough reads.", e);
            }
            lastSuppressedException = e;
        }
    }

    if (succCount < quorumRequestParameters.getSuccessFactor()) {
        if (lastSuppressedException != null) {
            throw lastSuppressedException;
        } else {
            throw new RuntimeException("Not enough reads for row " + Arrays.toString(row));
        }
    }

    return RowResult.create(row, result);
}

From source file:org.apache.cassandra.db.compaction.DateTieredCompactionStrategy.java

/**
 * Group files with similar min timestamp into buckets. Files with recent min timestamps are grouped together into
 * buckets designated to short timespans while files with older timestamps are grouped into buckets representing
 * longer timespans.//from   w ww.j a v a2  s  . co m
 * @param files pairs consisting of a file and its min timestamp
 * @param timeUnit
 * @param base
 * @param now
 * @return a list of buckets of files. The list is ordered such that the files with newest timestamps come first.
 *         Each bucket is also a list of files ordered from newest to oldest.
 */
@VisibleForTesting
static <T> List<List<T>> getBuckets(Collection<Pair<T, Long>> files, long timeUnit, int base, long now,
        long maxWindowSize) {
    // Sort files by age. Newest first.
    final List<Pair<T, Long>> sortedFiles = Lists.newArrayList(files);
    Collections.sort(sortedFiles, Collections.reverseOrder(new Comparator<Pair<T, Long>>() {
        public int compare(Pair<T, Long> p1, Pair<T, Long> p2) {
            return p1.right.compareTo(p2.right);
        }
    }));

    List<List<T>> buckets = Lists.newArrayList();
    Target target = getInitialTarget(now, timeUnit, maxWindowSize);
    PeekingIterator<Pair<T, Long>> it = Iterators.peekingIterator(sortedFiles.iterator());

    outerLoop: while (it.hasNext()) {
        while (!target.onTarget(it.peek().right)) {
            // If the file is too new for the target, skip it.
            if (target.compareToTimestamp(it.peek().right) < 0) {
                it.next();

                if (!it.hasNext())
                    break outerLoop;
            } else // If the file is too old for the target, switch targets.
                target = target.nextTarget(base);
        }
        List<T> bucket = Lists.newArrayList();
        while (target.onTarget(it.peek().right)) {
            bucket.add(it.next().left);

            if (!it.hasNext())
                break;
        }
        buckets.add(bucket);
    }

    return buckets;
}

From source file:net.tridentsdk.reflect.Injector.java

/**
 * Creates a new object which has injectable fields, using the injectable constructor
 *
 * @param clazz the class to instantiate
 * @param args the parameters, not including the injectable classes, in order of declaration
 * @param <T> the type to return//from   w w  w.j  av a 2s  .  c  o m
 * @return the new object
 */
public static <T> T newObject(Class<T> clazz, Object... args) {
    for (Map.Entry<Constructor, Class<?>[]> entry : findConstructors(clazz).entrySet()) {
        List<Object> arguments = Lists.newArrayList();
        Constructor constructor = entry.getKey();
        Class<?>[] constructorParameters = entry.getValue();
        PeekingIterator<Object> iterator = Iterators.peekingIterator(Iterators.forArray(args));

        if (!checkArray(args, constructorParameters))
            continue;

        for (Class<?> c : constructorParameters) {
            Producer<?> producer = injectors.get(c);
            Inject inject = (Inject) constructor.getAnnotation(Inject.class);

            if (iterator.hasNext()) {
                if (iterator.peek().getClass() == c) {
                    arguments.add(iterator.next());
                    continue;
                }
            }

            if (producer == null) {
                if (iterator.hasNext()) {
                    if (iterator.peek().getClass() == c) {
                        arguments.add(iterator.next());
                        continue;
                    }
                }

                TridentLogger.error(new IllegalArgumentException("Constructor " + clazz.getName() + "("
                        + Arrays.toString(constructorParameters).replaceAll("class ", "").replaceAll("\\[", "")
                                .replaceAll("\\]", "")
                        + ") " + "does not provide or registered parameter " + c.getName()));
                return null;
            } else {
                if (inject.meta() == Class.class) {
                    arguments.add(producer.produce());
                } else {
                    arguments.add(producer.produce(inject.meta()));
                }
            }
        }

        try {
            T t = (T) constructor.newInstance(arguments.toArray());
            for (Field field : findFields(clazz)) {
                setField(t, field);
            }
            return t;
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            TridentLogger.error(e);
            return null;
        }
    }

    try {
        T t = clazz.newInstance();
        for (Field field : findFields(clazz)) {
            setField(t, field);
        }
        return t;
    } catch (InstantiationException | IllegalAccessException e) {
        TridentLogger.error(e);
        return null;
    }
}

From source file:org.kiji.schema.impl.cassandra.CassandraKijiPartition.java

/**
 * Convert a set of (start-token, host) pairs into a set of (token-range, host) pairs.
 *
 * Package private for testing.//from ww w  . ja  v  a 2s  .  c o m
 *
 * @param startTokens The set of start tokens with hosts.
 * @return The token corresponding token ranges.
 */
static Map<Range<Long>, InetAddress> getTokenRanges(final SortedMap<Long, InetAddress> startTokens) {

    ImmutableMap.Builder<Range<Long>, InetAddress> tokenRangesBldr = ImmutableMap.builder();

    final PeekingIterator<Entry<Long, InetAddress>> startTokensItr = Iterators
            .peekingIterator(startTokens.entrySet().iterator());

    // Add a range for [-, firstStartToken) owned by the final key (the wrap-around range).
    // For more information on Casandra VNode token ranges:
    //    http://www.datastax.com/dev/blog/virtual-nodes-in-cassandra-1-2
    tokenRangesBldr.put(Range.lessThan(startTokens.firstKey()), startTokens.get(startTokens.lastKey()));

    while (startTokensItr.hasNext()) {
        Entry<Long, InetAddress> startToken = startTokensItr.next();
        if (!startTokensItr.hasNext()) {
            // The final start token
            // Add a range for [lastStartToken, )
            tokenRangesBldr.put(Range.atLeast(startToken.getKey()), startToken.getValue());
        } else {
            // Add a range for [thisStartToken, nextStartToken)
            tokenRangesBldr.put(Range.closedOpen(startToken.getKey(), startTokensItr.peek().getKey()),
                    startToken.getValue());
        }
    }

    final Map<Range<Long>, InetAddress> tokenRanges = tokenRangesBldr.build();

    // Check that the returned ranges are coherent; most importantly that all possible tokens fall
    // within the returned range set.

    if (startTokens.size() + 1 != tokenRanges.size()) {
        throw new InternalKijiError(
                String.format("Unexpected number of token ranges. start-tokens: %s, token-ranges: %s.",
                        startTokens.size(), tokenRanges.size()));
    }

    final RangeSet<Long> ranges = TreeRangeSet.create();
    for (Range<Long> tokenRange : tokenRanges.keySet()) {
        ranges.add(tokenRange);
    }

    if (!ranges.encloses(Range.closed(Long.MIN_VALUE, Long.MAX_VALUE))) {
        throw new InternalKijiError("Token range does not include all possible tokens.");
    }

    return tokenRanges;
}

From source file:org.glowroot.ui.TraceCommonService.java

private static void writeEntries(JsonGenerator jg, List<Trace.Entry> entries) throws IOException {
    jg.writeStartArray();/*  ww w.  j a v a 2 s .  com*/
    PeekingIterator<Trace.Entry> i = Iterators.peekingIterator(entries.iterator());
    while (i.hasNext()) {
        Trace.Entry entry = i.next();
        int depth = entry.getDepth();
        jg.writeStartObject();
        writeJson(entry, jg);
        int nextDepth = i.hasNext() ? i.peek().getDepth() : 0;
        if (nextDepth > depth) {
            jg.writeArrayFieldStart("childEntries");
        } else if (nextDepth < depth) {
            jg.writeEndObject();
            for (int j = depth; j > nextDepth; j--) {
                jg.writeEndArray();
                jg.writeEndObject();
            }
        } else {
            jg.writeEndObject();
        }
    }
    jg.writeEndArray();
}

From source file:org.calrissian.mango.collect.CloseableIterators.java

/**
 * Returns a {@code PeekingCloseableIterator} backed by the given closeable iterator.
 *
 * Calls to peek do not change the state of the iterator.  The subsequent call to next
 * after peeking will always return the same value.
 *//*from   www.  j a v  a  2 s  .  c  o m*/
public static <T> PeekingCloseableIterator<T> peekingIterator(final CloseableIterator<T> iterator) {
    final PeekingIterator<T> peeking = Iterators.peekingIterator(iterator);
    return new PeekingCloseableIterator<T>() {
        @Override
        public void closeQuietly() {
            iterator.closeQuietly();
        }

        @Override
        public void close() throws IOException {
            iterator.close();
        }

        @Override
        public T peek() {
            return peeking.peek();
        }

        @Override
        public T next() {
            return peeking.next();
        }

        @Override
        public void remove() {
            peeking.remove();
        }

        @Override
        public boolean hasNext() {
            return peeking.hasNext();
        }
    };
}

From source file:com.palantir.atlasdb.keyvalue.impl.InMemoryKeyValueService.java

private static Iterator<Entry<Key, byte[]>> takeCell(final PeekingIterator<Entry<Key, byte[]>> iter,
        final Key key) {
    return new AbstractIterator<Entry<Key, byte[]>>() {
        @Override/*  w w  w .ja  v a  2s  . c  o m*/
        protected Entry<Key, byte[]> computeNext() {
            if (!iter.hasNext()) {
                return endOfData();
            }
            Entry<Key, byte[]> next = iter.peek();
            Key nextKey = next.getKey();
            if (nextKey.matchesCell(key)) {
                return iter.next();
            }
            return endOfData();
        }
    };
}

From source file:org.apache.cassandra.db.rows.Rows.java

/**
 * Given the result ({@code merged}) of merging multiple {@code inputs}, signals the difference between
 * each input and {@code merged} to {@code diffListener}.
 * <p>// w ww . j a  v  a  2  s . co  m
 * Note that this method doesn't only emit cells etc where there's a difference. The listener is informed
 * of every corresponding entity between the merged and input rows, including those that are equal.
 *
 * @param diffListener the listener to which to signal the differences between the inputs and the merged result.
 * @param merged the result of merging {@code inputs}.
 * @param inputs the inputs whose merge yielded {@code merged}.
 */
public static void diff(RowDiffListener diffListener, Row merged, Row... inputs) {
    Clustering clustering = merged.clustering();
    LivenessInfo mergedInfo = merged.primaryKeyLivenessInfo().isEmpty() ? null
            : merged.primaryKeyLivenessInfo();
    Row.Deletion mergedDeletion = merged.deletion().isLive() ? null : merged.deletion();
    for (int i = 0; i < inputs.length; i++) {
        Row input = inputs[i];
        LivenessInfo inputInfo = input == null || input.primaryKeyLivenessInfo().isEmpty() ? null
                : input.primaryKeyLivenessInfo();
        Row.Deletion inputDeletion = input == null || input.deletion().isLive() ? null : input.deletion();

        if (mergedInfo != null || inputInfo != null)
            diffListener.onPrimaryKeyLivenessInfo(i, clustering, mergedInfo, inputInfo);
        if (mergedDeletion != null || inputDeletion != null)
            diffListener.onDeletion(i, clustering, mergedDeletion, inputDeletion);
    }

    List<Iterator<ColumnData>> inputIterators = new ArrayList<>(1 + inputs.length);
    inputIterators.add(merged.iterator());
    for (Row row : inputs)
        inputIterators.add(row == null ? Collections.emptyIterator() : row.iterator());

    Iterator<?> iter = MergeIterator.get(inputIterators, ColumnData.comparator,
            new MergeIterator.Reducer<ColumnData, Object>() {
                ColumnData mergedData;
                ColumnData[] inputDatas = new ColumnData[inputs.length];

                public void reduce(int idx, ColumnData current) {
                    if (idx == 0)
                        mergedData = current;
                    else
                        inputDatas[idx - 1] = current;
                }

                protected Object getReduced() {
                    for (int i = 0; i != inputDatas.length; i++) {
                        ColumnData input = inputDatas[i];
                        if (mergedData != null || input != null) {
                            ColumnDefinition column = (mergedData != null ? mergedData : input).column;
                            if (column.isSimple()) {
                                diffListener.onCell(i, clustering, (Cell) mergedData, (Cell) input);
                            } else {
                                ComplexColumnData mergedData = (ComplexColumnData) this.mergedData;
                                ComplexColumnData inputData = (ComplexColumnData) input;
                                if (mergedData == null) {
                                    // Everything in inputData has been shadowed
                                    if (!inputData.complexDeletion().isLive())
                                        diffListener.onComplexDeletion(i, clustering, column, null,
                                                inputData.complexDeletion());
                                    for (Cell inputCell : inputData)
                                        diffListener.onCell(i, clustering, null, inputCell);
                                } else if (inputData == null) {
                                    // Everything in inputData is new
                                    if (!mergedData.complexDeletion().isLive())
                                        diffListener.onComplexDeletion(i, clustering, column,
                                                mergedData.complexDeletion(), null);
                                    for (Cell mergedCell : mergedData)
                                        diffListener.onCell(i, clustering, mergedCell, null);
                                } else {

                                    if (!mergedData.complexDeletion().isLive()
                                            || !inputData.complexDeletion().isLive())
                                        diffListener.onComplexDeletion(i, clustering, column,
                                                mergedData.complexDeletion(), inputData.complexDeletion());

                                    PeekingIterator<Cell> mergedCells = Iterators
                                            .peekingIterator(mergedData.iterator());
                                    PeekingIterator<Cell> inputCells = Iterators
                                            .peekingIterator(inputData.iterator());
                                    while (mergedCells.hasNext() && inputCells.hasNext()) {
                                        int cmp = column.cellPathComparator().compare(mergedCells.peek().path(),
                                                inputCells.peek().path());
                                        if (cmp == 0)
                                            diffListener.onCell(i, clustering, mergedCells.next(),
                                                    inputCells.next());
                                        else if (cmp < 0)
                                            diffListener.onCell(i, clustering, mergedCells.next(), null);
                                        else // cmp > 0
                                            diffListener.onCell(i, clustering, null, inputCells.next());
                                    }
                                    while (mergedCells.hasNext())
                                        diffListener.onCell(i, clustering, mergedCells.next(), null);
                                    while (inputCells.hasNext())
                                        diffListener.onCell(i, clustering, null, inputCells.next());
                                }
                            }
                        }

                    }
                    return null;
                }

                protected void onKeyChange() {
                    mergedData = null;
                    Arrays.fill(inputDatas, null);
                }
            });

    while (iter.hasNext())
        iter.next();
}

From source file:com.metamx.common.guava.MergeIterator.java

@Override
public T next() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    }// w  w  w.j a v a2 s .com

    PeekingIterator<T> retIt = pQueue.remove();
    T retVal = retIt.next();

    if (retIt.hasNext()) {
        pQueue.add(retIt);
    }

    return retVal;
}

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

@Override
public T next() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    }// w  w w  .  j av a2 s .c o  m
    PeekingIterator<T> it = iterators.get(0);
    T next = it.next();
    // more elements?
    if (it.hasNext()) {
        adjustFirst();
    } else {
        // remove from list of iterators
        iterators.remove(0);
    }
    // fetch next iterator?
    if (comparator.compare(next, lastPeek) >= 0) {
        fetchNextIterator();
    }
    return next;
}