com.google.common.collect.PeekingIterator Java Examples

The following examples show how to use com.google.common.collect.PeekingIterator. 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: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
private void visitStatements(List<? extends StatementTree> statements) {
    boolean first = true;
    PeekingIterator<StatementTree> it =
            Iterators.<StatementTree>peekingIterator(statements.iterator());
    dropEmptyDeclarations();
    while (it.hasNext()) {
        StatementTree tree = it.next();
        builder.forcedBreak();
        if (!first) {
            builder.blankLineWanted(BlankLineWanted.PRESERVE);
        }
        markForPartialFormat();
        first = false;
        List<VariableTree> fragments = variableFragments(it, tree);
        if (!fragments.isEmpty()) {
            visitVariables(
                    fragments,
                    DeclarationKind.NONE,
                    canLocalHaveHorizontalAnnotations(fragments.get(0).getModifiers()));
        } else {
            scan(tree, null);
        }
    }
}
 
Example #2
Source File: DefaultSlabAllocator.java    From emodb with Apache License 2.0 6 votes vote down vote up
/** Compute how many slots in a slab will be used to do a default (new slab) allocation, and how many bytes it will consume
 *
 * @param slabSlotsUsed - number of available slots in a slab that have been used prior to this allocation
 * @param slabBytesUsed - number of bytes in a slab that have been used prior to this allocation
 * @param eventSizes - list of the size in bytes of all events that we want to insert into the slab
 *
 * @return a pair of integers, the left value is the number of slots that will be used by this allocation and the
 *         right value is the numb er of bytes that will be used by this allocation
 */
static Pair<Integer, Integer> defaultAllocationCount(int slabSlotsUsed, int slabBytesUsed, PeekingIterator<Integer> eventSizes) {
    int slabTotalSlotCount = slabSlotsUsed;
    int allocationSlotCount = 0;
    int slabTotalBytesUsed = slabBytesUsed;
    int allocationBytes = 0;
    while (eventSizes.hasNext()) {
        checkArgument(eventSizes.peek() <= Constants.MAX_EVENT_SIZE_IN_BYTES, "Event size (" + eventSizes.peek() + ") is greater than the maximum allowed (" + Constants.MAX_EVENT_SIZE_IN_BYTES + ") event size");
        if (slabTotalSlotCount + 1 <= Constants.MAX_SLAB_SIZE && slabTotalBytesUsed + eventSizes.peek() <= Constants.MAX_SLAB_SIZE_IN_BYTES) {
            slabTotalSlotCount++;
            allocationSlotCount++;
            int eventSize = eventSizes.next();
            slabTotalBytesUsed += eventSize;
            allocationBytes += eventSize;
        } else {
            break;
        }
    }
    return new ImmutablePair<>(allocationSlotCount, allocationBytes);
}
 
Example #3
Source File: EvaluatorFactory.java    From OpenModsLib with MIT License 6 votes vote down vote up
@Override
public IModifierStateTransition<Node> getStateForModifier(String modifier) {
	if (MODIFIER_OP.equals(modifier)) {
		return new SingleStateTransition.ForModifier<Node>() {

			@Override
			public Node createRootNode(Node child) {
				return child;
			}

			@Override
			public Node parseSymbol(IParserState<Node> state, PeekingIterator<Token> input) {
				Preconditions.checkState(input.hasNext(), "Unexpected end out input");
				final Token token = input.next();
				Preconditions.checkState(token.type == TokenType.OPERATOR, "Unexpected token, expected operator, got %s", token);
				NodeOp operator = OPERATORS.getOperator(token.value, OperatorArity.BINARY);
				if (operator == null) operator = OPERATORS.getOperator(token.value, OperatorArity.UNARY);
				if (operator == null) throw new IllegalArgumentException("Unknown operator: " + token.value);
				return new Node(operator);
			}

		};
	} else {
		throw new UnsupportedOperationException("Modifier: " + modifier);
	}
}
 
Example #4
Source File: CassandraVideoV2Dao.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
public Stream<VideoRecordingSize> streamRecordingSizeAsc(UUID placeId, boolean includeFavorites, boolean includeInProgress) {
	Iterator<VideoRecordingSize> it = null;
	if(includeFavorites){		
		List<PeekingIterator<VideoRecordingSize>> idIterators = new ArrayList<>(2);
		idIterators.add( peekingUuidIterator( placeRecordingIndexFavorite.selectRecordingSizeAsc(placeId, PlaceRecordingIndexV2Table.Type.RECORDING), placeRecordingIndexFavorite::getRecordingIdAndSizeAndFavorite ) );
		idIterators.add( peekingUuidIterator( placeRecordingIndex.selectRecordingSizeAsc(placeId, PlaceRecordingIndexV2Table.Type.RECORDING), placeRecordingIndex::getRecordingIdAndSizeAndFavorite ) );
		it = new UnionIterator(idIterators);
	}else{
		Iterator<VideoRecordingSize> favorites = recordingIdsIterator(placeRecordingIndexFavorite.selectRecordingSizeAsc(placeId, PlaceRecordingIndexV2Table.Type.RECORDING), placeRecordingIndexFavorite::getRecordingIdAndSizeAndFavorite);
		Iterator<VideoRecordingSize> allRecordings = recordingIdsIterator(placeRecordingIndex.selectRecordingSizeAsc(placeId, PlaceRecordingIndexV2Table.Type.RECORDING), placeRecordingIndex::getRecordingIdAndSizeAndFavorite );
		it = new DifferenceIterator(allRecordings, favorites);
		
	}
	Spliterator<VideoRecordingSize> matches = Spliterators.spliteratorUnknownSize(it, Spliterator.DISTINCT | Spliterator.SORTED);
	
	Stream<VideoRecordingSize> stream = StreamSupport.stream(matches, false);
	if(!includeInProgress) {
		stream = stream.filter(VideoRecordingSize::isCompletedRecording);
	}
	return stream;
}
 
Example #5
Source File: Numbers.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a set of integers into a set of contiguous closed ranges that equally represent the
 * input integers.
 * <p>
 * The resulting ranges will be in ascending order.
 * <p>
 * TODO(wfarner): Change this to return a canonicalized RangeSet.
 *
 * @param values Values to transform to ranges.
 * @return Closed ranges with identical members to the input set.
 */
public static Set<Range<Integer>> toRanges(Iterable<Integer> values) {
  ImmutableSet.Builder<Range<Integer>> builder = ImmutableSet.builder();

  PeekingIterator<Integer> iterator =
      Iterators.peekingIterator(Sets.newTreeSet(values).iterator());

  // Build ranges until there are no numbers left.
  while (iterator.hasNext()) {
    // Start a new range.
    int start = iterator.next();
    int end = start;
    // Increment the end until the range is non-contiguous.
    while (iterator.hasNext() && iterator.peek() == end + 1) {
      end++;
      iterator.next();
    }

    builder.add(Range.closed(start, end));
  }

  return builder.build();
}
 
Example #6
Source File: LocalProperties.java    From presto with Apache License 2.0 6 votes vote down vote up
/**
 * Attempt to match the desired properties to a sequence of known properties.
 * <p>
 * Returns a list of the same length as the original. Entries are:
 * - Optional.empty(): the property was satisfied completely
 * - non-empty: the (simplified) property that was not satisfied
 */
public static <T> List<Optional<LocalProperty<T>>> match(List<LocalProperty<T>> actuals, List<LocalProperty<T>> desired)
{
    // After normalizing actuals, each symbol should only appear once
    PeekingIterator<LocalProperty<T>> actualIterator = peekingIterator(normalizeAndPrune(actuals).iterator());

    Set<T> constants = new HashSet<>();
    boolean consumeMoreActuals = true;
    List<Optional<LocalProperty<T>>> result = new ArrayList<>(desired.size());
    for (LocalProperty<T> desiredProperty : desired) {
        while (consumeMoreActuals && actualIterator.hasNext() && desiredProperty.isSimplifiedBy(actualIterator.peek())) {
            constants.addAll(actualIterator.next().getColumns());
        }
        Optional<LocalProperty<T>> simplifiedDesired = desiredProperty.withConstants(constants);
        consumeMoreActuals &= simplifiedDesired.isEmpty(); // Only continue processing actuals if all previous desired properties were fully satisfied
        result.add(simplifiedDesired);
    }
    return result;
}
 
Example #7
Source File: AstyanaxEventReaderDAO.java    From emodb with Apache License 2.0 6 votes vote down vote up
void readAll(String channel, SlabFilter filter, EventSink sink, boolean weak) {
    // PeekingIterator is needed so that we can look ahead and see the next slab Id
    PeekingIterator<Column<ByteBuffer>> manifestColumns = Iterators.peekingIterator(readManifestForChannel(channel, weak));

    while (manifestColumns.hasNext()) {
        Column<ByteBuffer> manifestColumn = manifestColumns.next();
        ByteBuffer slabId = manifestColumn.getName();
        ByteBuffer nextSlabId = manifestColumns.hasNext() ? manifestColumns.peek().getName() : null;
        boolean open = manifestColumn.getBooleanValue();
        if (filter != null && !filter.accept(slabId, open, nextSlabId)) {
            continue;
        }
        if (!readSlab(channel, slabId, new SlabCursor(), open, sink)) {
            break;
        }
    }
}
 
Example #8
Source File: IndentationAwareCompletionPrefixProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private PeekingIterator<ILeafNode> createReversedLeafIterator(INode root, INode candidate, LinkedList<ILeafNode> sameGrammarElement) {
	EObject grammarElement = null;
	PeekingIterator<ILeafNode> iterator = Iterators.peekingIterator(Iterators.filter(root.getAsTreeIterable().reverse().iterator(), ILeafNode.class));
	// traverse until we find the current candidate
	while(iterator.hasNext()) {
		ILeafNode next = iterator.next();
		if (candidate.equals(next)) {
			break;
		} else if (next.getTotalLength() == 0) {
			EObject otherGrammarElement = tryGetGrammarElementAsRule(next);
			if (grammarElement == null) {
				grammarElement = otherGrammarElement;
			}
			if (otherGrammarElement.equals(grammarElement)) {
				sameGrammarElement.add(next);
			} else {
				sameGrammarElement.removeLast();
			}
		}
	}
	return iterator;
}
 
Example #9
Source File: StateManagerImplTest.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
private void expectStateTransitions(
    String taskId,
    ScheduleStatus initial,
    ScheduleStatus next,
    ScheduleStatus... others) {

  List<ScheduleStatus> statuses = ImmutableList.<ScheduleStatus>builder()
      .add(initial)
      .add(next)
      .add(others)
      .build();
  PeekingIterator<ScheduleStatus> it = Iterators.peekingIterator(statuses.iterator());
  while (it.hasNext()) {
    ScheduleStatus cur = it.next();
    try {
      eventSink.post(matchStateChange(taskId, cur, it.peek()));
    } catch (NoSuchElementException e) {
      // Expected.
    }
  }
}
 
Example #10
Source File: JavadocLexer.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Replaces whitespace after a {@code href=...>} token with an "optional link break." This allows
 * us to output either {@code <a href=foo>foo</a>} or {@code <a href=foo>\nfoo</a>}, depending on
 * how much space we have left on the line.
 * <p>
 * <p>This method must be called after {@link #joinAdjacentLiteralsAndAdjacentWhitespace}, as it
 * assumes that adjacent whitespace tokens have already been joined.
 */
private static ImmutableList<Token> optionalizeSpacesAfterLinks(List<Token> input) {
    ImmutableList.Builder<Token> output = ImmutableList.builder();

    for (PeekingIterator<Token> tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) {
        if (tokens.peek().getType() == LITERAL && tokens.peek().getValue().matches("^href=[^>]*>")) {
            output.add(tokens.next());

            if (tokens.peek().getType() == WHITESPACE) {
                output.add(new Token(OPTIONAL_LINE_BREAK, tokens.next().getValue()));
            }
        } else {
            output.add(tokens.next());
        }
    }

    return output.build();

/*
 * Note: We do not want to insert <p> tags inside <pre>. Fortunately, the formatter gets that
 * right without special effort on our part. The reason: Line breaks inside a <pre> section are
 * of type FORCED_NEWLINE rather than WHITESPACE.
 */
}
 
Example #11
Source File: MergingIterator.java    From lsmtree with Apache License 2.0 6 votes vote down vote up
@Override
protected Generation.Entry<K, V> computeNext() {
    if (heap.isEmpty()) {
        return endOfData();
    }

    PeekingIterator<EntryAndGenerationId<K,V>> first = heap.poll();
    EntryAndGenerationId<K,V> ret = first.next();
    if (first.hasNext()) {
        temp.add(first);
    }
    while (!heap.isEmpty() && keyComparator.compare(ret.entry.getKey(), heap.peek().peek().entry.getKey()) == 0) {
        PeekingIterator<EntryAndGenerationId<K, V>> iter = heap.poll();
        iter.next();
        if (iter.hasNext()) {
            temp.add(iter);
        }
    }
    heap.addAll(temp);
    temp.clear();
    return ret.entry;
}
 
Example #12
Source File: FixupFontDeclarations.java    From closure-stylesheets with Apache License 2.0 6 votes vote down vote up
/**
 * Computes the suffix of {@code xs} starting at the first node for which
 * {@code p} fails.
 *
 * {@code Iterables.concat(takeWhile(xs, p), dropWhile(xs, p)) = xs}
 */
private <T> Iterable<T> dropWhile(
    final Iterable<T> xs, final Predicate<? super T> p) {
  return new Iterable<T>() {
    @Override
    public Iterator<T> iterator() {
      PeekingIterator<T> xsi = Iterators.peekingIterator(xs.iterator());
      while (xsi.hasNext()) {
        if (p.apply(xsi.peek())) {
          break;
        }
        xsi.next();
      }
      return xsi;
    }
  };
}
 
Example #13
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private void visitStatements(List<? extends StatementTree> statements) {
    boolean first = true;
    PeekingIterator<StatementTree> it =
            Iterators.<StatementTree>peekingIterator(statements.iterator());
    dropEmptyDeclarations();
    while (it.hasNext()) {
        StatementTree tree = it.next();
        builder.forcedBreak();
        if (!first) {
            builder.blankLineWanted(BlankLineWanted.PRESERVE);
        }
        markForPartialFormat();
        first = false;
        List<VariableTree> fragments = variableFragments(it, tree);
        if (!fragments.isEmpty()) {
            visitVariables(
                    fragments,
                    DeclarationKind.NONE,
                    canLocalHaveHorizontalAnnotations(fragments.get(0).getModifiers()));
        } else {
            scan(tree, null);
        }
    }
}
 
Example #14
Source File: JavadocLexer.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Adjust indentation inside `<pre>{@code` blocks.
 * <p>
 * <p>Also trim leading and trailing blank lines, and move the trailing `}` to its own line.
 */
private static ImmutableList<Token> deindentPreCodeBlocks(List<Token> input) {
    ImmutableList.Builder<Token> output = ImmutableList.builder();
    for (PeekingIterator<Token> tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) {
        if (tokens.peek().getType() != PRE_OPEN_TAG) {
            output.add(tokens.next());
            continue;
        }

        output.add(tokens.next());
        List<Token> initialNewlines = new ArrayList<>();
        while (tokens.hasNext() && tokens.peek().getType() == FORCED_NEWLINE) {
            initialNewlines.add(tokens.next());
        }
        if (tokens.peek().getType() != LITERAL
                || !tokens.peek().getValue().matches("[ \t]*[{]@code")) {
            output.addAll(initialNewlines);
            output.add(tokens.next());
            continue;
        }

        deindentPreCodeBlock(output, tokens);
    }
    return output.build();
}
 
Example #15
Source File: IntersectionTest.java    From datawave with Apache License 2.0 6 votes vote down vote up
/**
 * Cannot intersect two disjoint shards.
 */
@Test
public void testIntersection_DifferentShardStreams() {
    // Build a peeking iterator for a left side term
    List<IndexMatch> leftMatches = buildIndexMatches("FIELD", "VALUE", "doc1", "doc2", "doc3");
    IndexInfo left = new IndexInfo(leftMatches);
    left.setNode(JexlNodeFactory.buildEQNode("FIELD", "VALUE"));
    Tuple2<String,IndexInfo> leftTuple = Tuples.tuple("20190314_0", left);
    PeekingIterator<Tuple2<String,IndexInfo>> leftIter = Iterators.peekingIterator(Collections.singleton(leftTuple).iterator());
    
    // Build a peeking iterator for a right side term.
    List<IndexMatch> rightMatches = buildIndexMatches("FIELD", "VALUE", "doc2", "doc3", "doc4");
    IndexInfo right = new IndexInfo(rightMatches);
    right.setNode(JexlNodeFactory.buildEQNode("FIELD", "VALUE"));
    Tuple2<String,IndexInfo> rightTuple = Tuples.tuple("20190314_1", right);
    PeekingIterator<Tuple2<String,IndexInfo>> rightIter = Iterators.peekingIterator(Collections.singleton(rightTuple).iterator());
    
    // Build the Intersection.
    IndexStream leftStream = ScannerStream.withData(leftIter, JexlNodeFactory.buildEQNode("FIELD", "VALUE"));
    IndexStream rightStream = ScannerStream.withData(rightIter, JexlNodeFactory.buildEQNode("FIELD", "VALUE"));
    List<IndexStream> indexStreams = Lists.newArrayList(leftStream, rightStream);
    
    Intersection intersection = new Intersection(indexStreams, new IndexInfo());
    assertFalse(intersection.hasNext());
}
 
Example #16
Source File: MutableProfile.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private void merge(List<Profile.ProfileNode> flatNodes,
        List<ProfileNode> destinationRootNodes) {
    destinationStack.push(destinationRootNodes);
    PeekingIterator<Profile.ProfileNode> i =
            Iterators.peekingIterator(flatNodes.iterator());
    while (i.hasNext()) {
        Profile.ProfileNode flatNode = i.next();
        int destinationDepth = destinationStack.size() - 1;
        for (int j = 0; j < destinationDepth - flatNode.getDepth(); j++) {
            // TODO optimize: faster way to pop multiple elements at once
            destinationStack.pop();
        }
        ProfileNode destinationNode = mergeOne(flatNode, destinationStack.getFirst());
        if (i.hasNext() && i.peek().getDepth() > flatNode.getDepth()) {
            destinationStack.push(destinationNode.childNodes);
        }
    }
}
 
Example #17
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
protected void visitStatements(List<? extends StatementTree> statements) {
  boolean first = true;
  PeekingIterator<StatementTree> it = Iterators.peekingIterator(statements.iterator());
  dropEmptyDeclarations();
  while (it.hasNext()) {
    StatementTree tree = it.next();
    builder.forcedBreak();
    if (!first) {
      builder.blankLineWanted(BlankLineWanted.PRESERVE);
    }
    markForPartialFormat();
    first = false;
    List<VariableTree> fragments = variableFragments(it, tree);
    if (!fragments.isEmpty()) {
      visitVariables(
          fragments,
          DeclarationKind.NONE,
          canLocalHaveHorizontalAnnotations(fragments.get(0).getModifiers()));
    } else {
      scan(tree, null);
    }
  }
}
 
Example #18
Source File: DatabusResource1.java    From emodb with Apache License 2.0 5 votes vote down vote up
private static <T> Iterator<T> streamingIterator(Iterator<T> iterator) {
    // Force the calculation of at least the first item in the iterator so that, if an exception occurs, we find
    // out before writing the HTTP response code & headers.  Otherwise we will at best report a 500 error instead
    // of applying Jersey exception mappings and maybe returning a 400 error etc.
    PeekingIterator<T> peekingIterator = Iterators.peekingIterator(iterator);
    if (peekingIterator.hasNext()) {
        peekingIterator.peek();
    }

    return new LoggingIterator<>(peekingIterator, _log);
}
 
Example #19
Source File: ChunkInputStreamTest.java    From accumulo-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadIntoBufferLargerThanChunks() throws IOException {
  ChunkInputStream cis = new ChunkInputStream();
  byte[] b = new byte[20];
  int read;
  PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(data.iterator());

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 8);
  assertEquals(new String(b, 0, read), "asdfjkl;");
  assertEquals(read = cis.read(b), -1);

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 10);
  assertEquals(new String(b, 0, read), "qwertyuiop");
  assertEquals(read = cis.read(b), -1);
  assertEquals(cis.getVisibilities().toString(), "[A&B, B&C, D]");
  cis.close();

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 16);
  assertEquals(new String(b, 0, read), "asdfjkl;asdfjkl;");
  assertEquals(read = cis.read(b), -1);
  assertEquals(cis.getVisibilities().toString(), "[A&B]");
  cis.close();

  cis.setSource(pi);
  assertEquals(read = cis.read(b), -1);
  cis.close();

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 8);
  assertEquals(new String(b, 0, read), "asdfjkl;");
  assertEquals(read = cis.read(b), -1);
  cis.close();

  assertFalse(pi.hasNext());
}
 
Example #20
Source File: LoopingDatasetFinderSource.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Advance an iterator until the next value is larger than the reference.
 * @return the last value polled if it is equal to reference, or null otherwise.
 */
@Nullable
private <T extends URNIdentified> T advanceUntilLargerThan(PeekingIterator<T> it, String reference) {
  if (reference == null) {
    return null;
  }

  int comparisonResult = -1;
  while (it.hasNext() && (comparisonResult = lexicographicalComparator.compare(it.peek(), reference)) < 0) {
    it.next();
  }
  return comparisonResult == 0 ? it.next() : null;
}
 
Example #21
Source File: CompactorScanner.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
private void skipToNextColumn(Cell cell, PeekingIterator<Map.Entry<Cell, Optional<Cell>>> iter) {
    boolean isFamilyDelete = CellUtils.isFamilyDeleteCell(cell);
    while (iter.hasNext()
            && CellUtil.matchingFamily(iter.peek().getKey(), cell)
            && (CellUtil.matchingQualifier(iter.peek().getKey(), cell) || isFamilyDelete)) {
        iter.next();
    }
}
 
Example #22
Source File: AstyanaxEventReaderDAO.java    From emodb with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the ordered manifest for a channel.  The read can either be weak or strong.  A weak read will use CL1
 * and may use the cached oldest slab from a previous strong call to improve performance.  A strong read will use
 * CL local_quorum and will always read the entire manifest row.  This makes a weak read significantly faster than a
 * strong read but also means the call is not guaranteed to return the entire manifest.  Because of this at least
 * every 10 seconds a weak read for a channel is automatically promoted to a strong read.
 *
 * The vast majority of calls to this method are performed during a "peek" or "poll" operation.  Since these are
 * typically called repeatedly a weak call provides improved performance while guaranteeing that at least every
 * 10 seconds the manifest is strongly read so no slabs are missed over time.  Calls which must guarantee
 * the full manifest should explicitly request strong consistency.
 */
private Iterator<Column<ByteBuffer>> readManifestForChannel(final String channel, final boolean weak) {
    final ByteBuffer oldestSlab = weak ? _oldestSlab.getIfPresent(channel) : null;
    final ConsistencyLevel consistency;

    RangeBuilder range = new RangeBuilder().setLimit(50);
    if (oldestSlab != null) {
        range.setStart(oldestSlab);
        consistency = ConsistencyLevel.CL_LOCAL_ONE;
    } else {
        consistency = ConsistencyLevel.CL_LOCAL_QUORUM;
    }

    final Iterator<Column<ByteBuffer>> manifestColumns = executePaginated(
            _keyspace.prepareQuery(ColumnFamilies.MANIFEST, consistency)
                    .getKey(channel)
                    .withColumnRange(range.build())
                    .autoPaginate(true));

    if (oldestSlab != null) {
        // Query was executed weakly using the cached oldest slab, so don't update the cache with an unreliable oldest value
        return manifestColumns;
    } else {
        PeekingIterator<Column<ByteBuffer>> peekingManifestColumns = Iterators.peekingIterator(manifestColumns);
        if (peekingManifestColumns.hasNext()) {
            // Cache the first slab returned from querying the full manifest column family since it is the oldest.
            cacheOldestSlabForChannel(channel, TimeUUIDSerializer.get().fromByteBuffer(peekingManifestColumns.peek().getName()));
            return peekingManifestColumns;
        } else {
            // Channel was completely empty.  Cache a TimeUUID for the current time.  This will cause future calls
            // to read at most 1 minute of tombstones until the cache expires 10 seconds later.
            cacheOldestSlabForChannel(channel, TimeUUIDs.newUUID());
            return Iterators.emptyIterator();
        }
    }
}
 
Example #23
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * The parser expands multi-variable declarations into separate single-variable declarations. All
 * of the fragments in the original declaration have the same start position, so we use that as a
 * signal to collect them and preserve the multi-variable declaration in the output.
 * <p>
 * <p>e.g. {@code int x, y;} is parsed as {@code int x; int y;}.
 */
private List<VariableTree> variableFragments(PeekingIterator<? extends Tree> it, Tree first) {
    List<VariableTree> fragments = new ArrayList<>();
    if (first.getKind() == VARIABLE) {
        int start = getStartPosition(first);
        fragments.add((VariableTree) first);
        while (it.hasNext()
                && it.peek().getKind() == VARIABLE
                && getStartPosition(it.peek()) == start) {
            fragments.add((VariableTree) it.next());
        }
    }
    return fragments;
}
 
Example #24
Source File: FileDataQuery.java    From accumulo-examples with Apache License 2.0 5 votes vote down vote up
public ChunkInputStream getData(String hash) throws IOException {
  scanner.setRange(new Range(hash));
  scanner.setBatchSize(1);
  lastRefs.clear();
  PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(scanner.iterator());
  if (pi.hasNext()) {
    while (!pi.peek().getKey().getColumnFamily().equals(FileDataIngest.CHUNK_CF)) {
      lastRefs.add(pi.peek());
      pi.next();
    }
  }
  cis.clear();
  cis.setSource(pi);
  return cis;
}
 
Example #25
Source File: TimeLimitedIteratorTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Test
public void testMinimumTwo() {
    Iterator<Long> unlimitedIter = countForever();
    PeekingIterator<Long> limitedIter = TimeLimitedIterator.create(unlimitedIter, Duration.ZERO, 2);

    assertTrue(limitedIter.hasNext());
    assertEquals(limitedIter.next(), 0L);
    assertTrue(limitedIter.hasNext());
    assertEquals(limitedIter.next(), 1L);
    assertFalse(limitedIter.hasNext());

    assertEquals(unlimitedIter.next(), 2L);
}
 
Example #26
Source File: JavadocLexer.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Where the input has two consecutive line breaks between literals, insert a {@code <p>} tag
 * between the literals.
 * <p>
 * <p>This method must be called after {@link #joinAdjacentLiteralsAndAdjacentWhitespace}, as it
 * assumes that adjacent whitespace tokens have already been joined.
 */
private static ImmutableList<Token> inferParagraphTags(List<Token> input) {
    ImmutableList.Builder<Token> output = ImmutableList.builder();

    for (PeekingIterator<Token> tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) {
        if (tokens.peek().getType() == LITERAL) {
            output.add(tokens.next());

            if (tokens.peek().getType() == WHITESPACE
                    && hasMultipleNewlines(tokens.peek().getValue())) {
                output.add(tokens.next());

                if (tokens.peek().getType() == LITERAL) {
                    output.add(new Token(PARAGRAPH_OPEN_TAG, "<p>"));
                }
            }
        } else {
            // TODO(cpovirk): Or just `continue` from the <p> case and move this out of the `else`?
            output.add(tokens.next());
        }
    }

    return output.build();

/*
 * Note: We do not want to insert <p> tags inside <pre>. Fortunately, the formatter gets that
 * right without special effort on our part. The reason: Line breaks inside a <pre> section are
 * of type FORCED_NEWLINE rather than WHITESPACE.
 */
}
 
Example #27
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * The parser expands multi-variable declarations into separate single-variable declarations. All
 * of the fragments in the original declaration have the same start position, so we use that as a
 * signal to collect them and preserve the multi-variable declaration in the output.
 * <p>
 * <p>e.g. {@code int x, y;} is parsed as {@code int x; int y;}.
 */
private List<VariableTree> variableFragments(PeekingIterator<? extends Tree> it, Tree first) {
    List<VariableTree> fragments = new ArrayList<>();
    if (first.getKind() == VARIABLE) {
        int start = getStartPosition(first);
        fragments.add((VariableTree) first);
        while (it.hasNext()
                && it.peek().getKind() == VARIABLE
                && getStartPosition(it.peek()) == start) {
            fragments.add((VariableTree) it.next());
        }
    }
    return fragments;
}
 
Example #28
Source File: ChunkInputStream.java    From accumulo-examples with Apache License 2.0 5 votes vote down vote up
public void setSource(PeekingIterator<Entry<Key,Value>> in) throws IOException {
  if (source != null)
    throw new IOException("setting new source without closing old one");
  this.source = in;
  currentVis = new TreeSet<>();
  count = pos = 0;
  if (!source.hasNext()) {
    log.debug("source has no next");
    gotEndMarker = true;
    return;
  }

  // read forward until we reach a chunk
  Entry<Key,Value> entry = source.next();
  currentKey = entry.getKey();
  buf = entry.getValue().get();
  while (!currentKey.getColumnFamily().equals(FileDataIngest.CHUNK_CF)) {
    log.debug("skipping key: " + currentKey.toString());
    if (!source.hasNext())
      return;
    entry = source.next();
    currentKey = entry.getKey();
    buf = entry.getValue().get();
  }
  log.debug("starting chunk: " + currentKey.toString());
  count = buf.length;
  currentVis.add(currentKey.getColumnVisibility());
  currentChunk = FileDataIngest.bytesToInt(currentKey.getColumnQualifier().getBytes(), 4);
  currentChunkSize = FileDataIngest.bytesToInt(currentKey.getColumnQualifier().getBytes(), 0);
  gotEndMarker = false;
  if (buf.length == 0)
    gotEndMarker = true;
  if (currentChunk != 0) {
    source = null;
    throw new IOException("starting chunk number isn't 0 for " + currentKey.getRow());
  }
}
 
Example #29
Source File: LocalProperties.java    From presto with Apache License 2.0 5 votes vote down vote up
public static <T> List<LocalProperty<T>> stripLeadingConstants(List<? extends LocalProperty<T>> properties)
{
    PeekingIterator<? extends LocalProperty<T>> iterator = peekingIterator(properties.iterator());
    while (iterator.hasNext() && iterator.peek() instanceof ConstantProperty) {
        iterator.next();
    }
    return ImmutableList.copyOf(iterator);
}
 
Example #30
Source File: EntityHelper.java    From emodb with Apache License 2.0 5 votes vote down vote up
private static <T> Iterator<T> streamingIterator(InputStream in, TypeReference<T> typeReference) {
    PeekingIterator<T> iter = new JsonStreamingArrayParser<>(in, typeReference);

    // Fetch the first element in the result stream immediately, while still wrapped by the Ostrich retry logic.
    // If we can't get the first element then Ostrich should retry immediately.  If we fail to get subsequent
    // elements then clients must catch JsonStreamingEOFException and deal with it themselves.  They are highly
    // encouraged to use the DataStoreStreaming class which handles the restart logic automatically.
    if (iter.hasNext()) {
        iter.peek();
    }

    return iter;
}