public MarkerRange complement() { if (start == Boundary.NEGATIVE_INFINITY && end == Boundary.POSITIVE_INFINITY) { throw new IllegalArgumentException("Cannot find complement of unbounded range."); } if (start == Boundary.NEGATIVE_INFINITY) { Boundary newStart = new Boundary(comparator, end.bound, !end.isInclusive); Boundary newEnd = Boundary.POSITIVE_INFINITY; return new MarkerRange(comparator, newStart, newEnd); } else if (end == Boundary.POSITIVE_INFINITY) { Boundary newStart = Boundary.NEGATIVE_INFINITY; Boundary newEnd = new Boundary(comparator, start.bound, !start.isInclusive); return new MarkerRange(comparator, newStart, newEnd); } return new MarkerRange(); // unbounded TODO: use set of ranges to improve efficiency }
public MarkerRange fromAfter(Marker start) { Preconditions.checkArgument(contains(start), "Start boundary is outside of this range"); return new MarkerRange(comparator, new Boundary(comparator, start, false), end); }
public MarkerRange toBefore(Marker end) { Preconditions.checkArgument(contains(end), "End boundary is outside of this range"); return new MarkerRange(comparator, start, new Boundary(comparator, end, false)); }
public MarkerRange to(Marker end) { Preconditions.checkArgument(contains(end), "End boundary is outside of this range"); return new MarkerRange(comparator, start, new Boundary(comparator, end, true)); }
public MarkerRange from(Marker start) { Preconditions.checkArgument(contains(start), "Start boundary is outside of this range"); return new MarkerRange(comparator, new Boundary(comparator, start, true), end); }
public MarkerRange build() { MarkerRange markerRange = new MarkerRange(comparator); if (start != null) { markerRange = markerRange.from(start.build()); } if (end != null) { markerRange = markerRange.to(end.build()); } return markerRange; } }
public MarkerRange intersection(MarkerRange other) { Boundary newStart = Ordering.from(new Boundary.LeftComparator()).max(start, other.start); Boundary newEnd = Ordering.from(new Boundary.RightComparator()).min(end, other.end); return new MarkerRange(comparator, newStart, newEnd); }
public MarkerRange of(Marker partial) { Preconditions.checkArgument(contains(partial), "Marker is outside of this range"); return new MarkerRange(comparator, new Boundary(comparator, partial, true), new Boundary(comparator, partial, true)); }
public MarkerRange span(MarkerRange other) { Boundary newStart = Ordering.from(new Boundary.LeftComparator()).min(start, other.start); Boundary newEnd = Ordering.from(new Boundary.RightComparator()).max(end, other.end); return new MarkerRange(comparator, newStart, newEnd); }
@Test public void testUnbounded() { Constraints c = emptyConstraints.partitionedBy(id); assertIterableEquals( Sets.newHashSet(new MarkerRange(idCmp)), c.toKeyRanges()); }
@Test public void testComplement() { final MarkerRange unbounded = new MarkerRange(comparator); final MarkerRange unboundedToNov1 = new MarkerRange(comparator).to(NOV_1); final MarkerRange oct12ToOct15 = new MarkerRange(comparator).from(OCT_12).to(OCT_15); final MarkerRange afterNov1ToUnbounded = new MarkerRange(comparator).fromAfter(NOV_1); TestHelpers.assertThrows("Can't find complement of unbounded range.", IllegalArgumentException.class, new Runnable() { @Override public void run() { unbounded.complement(); } }); assertEquals(afterNov1ToUnbounded, unboundedToNov1.complement()); assertEquals(unboundedToNov1, afterNov1ToUnbounded.complement()); assertEquals(unbounded, oct12ToOct15.complement()); } }
@Test public void testSingleSet() { Constraints c = emptyConstraints.partitionedBy(id) .with("component", "com.company.Main"); Marker main = new Marker.Builder("id_component", "com.company.Main").build(); MarkerRange actual = Iterables.getOnlyElement(c.toKeyRanges()); Assert.assertEquals(main, actual.getStart().getBound()); Assert.assertEquals(main, actual.getEnd().getBound()); Assert.assertEquals(new MarkerRange(idCmp).of(main), actual); c = emptyConstraints.partitionedBy(id).with("component", "com.company.Main", "com.company.SomeClass"); Marker sc = new Marker.Builder("id_component", "com.company.SomeClass").build(); assertIterableEquals( Sets.newHashSet( new MarkerRange(idCmp).of(main), new MarkerRange(idCmp).of(sc)), c.toKeyRanges()); }
@Test @SuppressWarnings("unchecked") public void testGroupSet() { String[] ids = new String[]{ UUID.randomUUID().toString(), UUID.randomUUID().toString()}; FieldPartitioner hashFunc = Accessor.getDefault().getFieldPartitioners(hash).get(0); Constraints c = emptyConstraints.partitionedBy(hash).with("id", ids[0]); Marker marker0 = new Marker.Builder() .add("id_hash", hashFunc.apply(ids[0])).add("id_copy", ids[0]).build(); MarkerRange actual = Iterables.getOnlyElement(c.toKeyRanges()); Assert.assertEquals(marker0, actual.getStart().getBound()); Assert.assertEquals(marker0, actual.getEnd().getBound()); c = emptyConstraints.partitionedBy(hash).with("id", (Object[]) ids); Marker marker1 = new Marker.Builder() .add("id_hash", hashFunc.apply(ids[1])).add("id_copy", ids[1]).build(); assertIterableEquals( Sets.newHashSet( new MarkerRange(hashCmp).of(marker0), new MarkerRange(hashCmp).of(marker1)), c.toKeyRanges()); }
@Test public void testOneSidedRange() { // Thu Sep 12 14:15:47 PDT 2013 Constraints c = emptyConstraints.partitionedBy(time) .from("timestamp", 1379020547042L); Marker sep = new Marker.Builder() .add("year", 2013).add("month", 9).add("day", 12).build(); assertIterableEquals(Sets.newHashSet(new MarkerRange(timeCmp).from(sep)), c.toKeyRanges()); }
@Test public void testHashRange() { Constraints c = emptyConstraints.partitionedBy(hash) .from("id", "0000").toBefore("id", "0001"); // note the lack of a hash field -- ranges cannot be projected through hash Marker start = new Marker.Builder("id_copy", "0000").build(); Marker stop = new Marker.Builder("id_copy", "0001").build(); Assert.assertEquals( new MarkerRange(hashCmp).from(start).to(stop), Iterables.getOnlyElement(c.toKeyRanges())); }
@Test public void testTimeSet() { Long[] timestamps = new Long[] { 1379020547042L, // Thu Sep 12 14:15:47 PDT 2013 1381612547042L, // Sat Oct 12 14:15:47 PDT 2013 1384204547042L // Mon Nov 11 13:15:47 PST 2013 }; Constraints c = emptyConstraints.partitionedBy(time) .with("timestamp", (Object[]) timestamps); Marker sep = new Marker.Builder().add("year", 2013).add("month", 9).add("day", 12).build(); Marker oct = new Marker.Builder().add("year", 2013).add("month", 10).add("day", 12).build(); Marker nov = new Marker.Builder().add("year", 2013).add("month", 11).add("day", 11).build(); assertIterableEquals(Sets.newHashSet( new MarkerRange(timeCmp).of(sep), new MarkerRange(timeCmp).of(oct), new MarkerRange(timeCmp).of(nov)), c.toKeyRanges()); }
@Test public void testGroupRange() { Constraints c = emptyConstraints.partitionedBy(num) .from("number", 5).toBefore("number", 18); Marker start = new Marker.Builder("id_number", 5).build(); Marker stop = new Marker.Builder("id_number", 18).build(); Assert.assertEquals( new MarkerRange(numCmp).from(start).to(stop), Iterables.getOnlyElement(c.toKeyRanges())); }
@Test public void testSpan() { final MarkerRange unbounded = new MarkerRange(comparator); final MarkerRange unboundedToNov1 = new MarkerRange(comparator).to(NOV_1); final MarkerRange oct12ToOct15 = new MarkerRange(comparator).from(OCT_12).to(OCT_15); final MarkerRange sept30ToOct15 = new MarkerRange(comparator).from(SEPT_30).to(OCT_15); final MarkerRange oct12ToNov1 = new MarkerRange(comparator).from(OCT_12).to(NOV_1); final MarkerRange sept30ToNov1 = new MarkerRange(comparator).from(SEPT_30).to(NOV_1); final MarkerRange sept30ToUnbounded = new MarkerRange(comparator).from(SEPT_30); final MarkerRange nov1ToUnbounded = new MarkerRange(comparator).from(NOV_1);
@Test public void testSingleRange() { Constraints c = emptyConstraints.partitionedBy(num2) .from("number", 5).toBefore("number", 18) .to("number2", 9); Marker start = new Marker.Builder("id_number", 5).build(); Marker stop = new Marker.Builder().add("id_number", 18).add("id_number2", 9).build(); Assert.assertEquals( new MarkerRange(num2Cmp).from(start).to(stop), Iterables.getOnlyElement(c.toKeyRanges())); }
@Test public void testTimeRange() { Long[] timestamps = new Long[] { 1379020547042L, // Thu Sep 12 14:15:47 PDT 2013 1381612547042L, // Sat Oct 12 14:15:47 PDT 2013 1384204547042L // Mon Nov 11 13:15:47 PST 2013 }; Constraints c = emptyConstraints.partitionedBy(time) .from("timestamp", timestamps[0]).to("timestamp", timestamps[2]); Marker sep = new Marker.Builder().add("year", 2013).add("month", 9).add("day", 12).build(); Marker nov = new Marker.Builder().add("year", 2013).add("month", 11).add("day", 11).build(); Assert.assertEquals( new MarkerRange(timeCmp).from(sep).to(nov), Iterables.getOnlyElement(c.toKeyRanges())); }