use of org.apache.beam.sdk.io.range.OffsetRange in project beam by apache.
the class OffsetRangeTrackerTest method testCheckDoneAfterTryClaimRightBeforeEndOfRange.
@Test
public void testCheckDoneAfterTryClaimRightBeforeEndOfRange() {
OffsetRangeTracker tracker = new OffsetRangeTracker(new OffsetRange(100, 200));
assertTrue(tracker.tryClaim(150L));
assertTrue(tracker.tryClaim(175L));
assertTrue(tracker.tryClaim(199L));
tracker.checkDone();
}
use of org.apache.beam.sdk.io.range.OffsetRange in project beam by apache.
the class OffsetRangeTrackerTest method testSmallRangeWithLargeValue.
@Test
public void testSmallRangeWithLargeValue() throws Exception {
OffsetRangeTracker tracker = new OffsetRangeTracker(new OffsetRange(123456789012345677L, 123456789012345679L));
assertTrue(tracker.tryClaim(123456789012345677L));
SplitResult res = tracker.trySplit(0.5);
assertEquals(new OffsetRange(123456789012345677L, 123456789012345678L), res.getPrimary());
assertEquals(new OffsetRange(123456789012345678L, 123456789012345679L), res.getResidual());
tracker = new OffsetRangeTracker(new OffsetRange(123456789012345681L, 123456789012345683L));
assertTrue(tracker.tryClaim(123456789012345681L));
res = tracker.trySplit(0.5);
assertEquals(new OffsetRange(123456789012345681L, 123456789012345682L), res.getPrimary());
assertEquals(new OffsetRange(123456789012345682L, 123456789012345683L), res.getResidual());
}
use of org.apache.beam.sdk.io.range.OffsetRange in project beam by apache.
the class PCollectionViewsTest method assertNonEmptyRangesAndPositions.
private static void assertNonEmptyRangesAndPositions(Iterable<OffsetRange> ranges, Map<OffsetRange, Integer> nonOverlappingRangesToNumElementsPerPosition) {
for (Map.Entry<OffsetRange, Integer> entry : nonOverlappingRangesToNumElementsPerPosition.entrySet()) {
assertNotEquals(0, (int) entry.getValue());
}
ListMultimap<Long, Integer> positions = ArrayListMultimap.create();
for (OffsetRange range : ranges) {
for (long i = range.getFrom(); i < range.getTo(); ++i) {
positions.put(i, 0);
}
}
int position = 0;
for (Long key : new TreeSet<>(positions.keySet())) {
int size = positions.get(key).size();
positions.replaceValues(key, Lists.newArrayList(IntStream.range(position, position + size).iterator()));
position += size;
}
for (int i = 0; i < position; ++i) {
KV<Long, Integer> computedPosition = computePositionForIndex(nonOverlappingRangesToNumElementsPerPosition, i);
assertEquals(i, (int) positions.get(computedPosition.getKey()).get(computedPosition.getValue()));
}
assertThrows(IndexOutOfBoundsException.class, () -> computePositionForIndex(nonOverlappingRangesToNumElementsPerPosition, -1));
int totalNumberOfElements = computeTotalNumElements(nonOverlappingRangesToNumElementsPerPosition);
assertEquals(position, totalNumberOfElements);
assertThrows(IndexOutOfBoundsException.class, () -> computePositionForIndex(nonOverlappingRangesToNumElementsPerPosition, totalNumberOfElements));
}
Aggregations