@VisibleForTesting static Consumer<List<BatchElement<Integer, TimestampRange>>> consumer(TimestampService delegate) { return batch -> { long totalTimestamps = batch.stream().mapToLong(BatchElement::argument).reduce(0, Math::addExact); long startInclusive = 0; long endExclusive = 0; for (BatchElement<Integer, TimestampRange> element : batch) { int timestampsRequired = element.argument(); if (element.argument() <= endExclusive - startInclusive) { element.result().set(createExclusiveRange(startInclusive, startInclusive + timestampsRequired)); startInclusive += timestampsRequired; totalTimestamps -= timestampsRequired; } else { TimestampRange requested = getFreshTimestampsFromDelegate( delegate, Ints.saturatedCast(totalTimestamps)); startInclusive = requested.getLowerBound(); endExclusive = Math.addExact(requested.getUpperBound(), 1); int toTake = Math.min(Ints.checkedCast(endExclusive - startInclusive), timestampsRequired); element.result().set(createExclusiveRange(startInclusive, startInclusive + toTake)); startInclusive += toTake; totalTimestamps -= timestampsRequired; } } }; }
@VisibleForTesting static Consumer<List<BatchElement<Integer, TimestampRange>>> consumer(TimestampService delegate) { return batch -> { long totalTimestamps = batch.stream().mapToLong(BatchElement::argument).reduce(0, Math::addExact); long startInclusive = 0; long endExclusive = 0; for (BatchElement<Integer, TimestampRange> element : batch) { int timestampsRequired = element.argument(); if (element.argument() <= endExclusive - startInclusive) { element.result().set(createExclusiveRange(startInclusive, startInclusive + timestampsRequired)); startInclusive += timestampsRequired; totalTimestamps -= timestampsRequired; } else { TimestampRange requested = getFreshTimestampsFromDelegate( delegate, Ints.saturatedCast(totalTimestamps)); startInclusive = requested.getLowerBound(); endExclusive = Math.addExact(requested.getUpperBound(), 1); int toTake = Math.min(Ints.checkedCast(endExclusive - startInclusive), timestampsRequired); element.result().set(createExclusiveRange(startInclusive, startInclusive + toTake)); startInclusive += toTake; totalTimestamps -= timestampsRequired; } } }; }