@Override public void run() { fp.project(Predicates.in(21)); } });
@Test public void testIdentityFieldPartitionerSetPredicate() { FieldPartitioner<String, String> fp = new IdentityFieldPartitioner<String>("str", "str_copy", String.class, 50); In<String> s = Predicates.in("a", "b"); Assert.assertEquals(s, fp.project(s)); Assert.assertEquals(s, fp.projectStrict(s)); }
@Test public void testIdentityFieldPartitionerRangePredicate() { FieldPartitioner<String, String> fp = new IdentityFieldPartitioner<String>("str", "str_copy", String.class, 50); Range<String> r = Ranges.openClosed("a", "b"); Assert.assertEquals(r, fp.project(r)); Assert.assertEquals(r, fp.projectStrict(r)); }
@Override public void run() { fp.project(Predicates.in(12)); } });
@Test @SuppressWarnings("unchecked") public void testHashFieldPartitionerRangePredicate() { FieldPartitioner<Object, Integer> fp = new HashFieldPartitioner("name", 50); // cannot enumerate all inputs, so we can't calculate the set of potential // hash values other than all hash values mod the number of buckets Assert.assertNull(fp.project((Predicate)Ranges.open("a", "b"))); Assert.assertNull(fp.projectStrict((Predicate) Ranges.open("a", "b"))); }
@Override public void run() { fp.project(Ranges.atMost("cc")); } });
@Test public void testYearFieldPartitionerSetPredicate() { FieldPartitioner<Long, Integer> fp = new YearFieldPartitioner("timestamp", "year"); // A single year Assert.assertEquals(Predicates.in(2013), fp.project(Predicates.in(sepInstant, octInstant))); Assert.assertNull(fp.projectStrict(Predicates.in(sepInstant))); // Multiple years Assert.assertEquals(Predicates.in(2012, 2013), fp.project(Predicates.in(sepInstant - ONE_YEAR_MILLIS, octInstant))); Assert.assertNull(fp.projectStrict( Predicates.in(sepInstant - ONE_YEAR_MILLIS, octInstant))); }
@Override public void run() { fp.project(Predicates.in("cc")); } });
@Test public void testDateFormatPartitionerSetPredicate() { FieldPartitioner<Long, String> fp = new DateFormatPartitioner("timestamp", "date", "yyyy-MM-dd"); Assert.assertEquals( Predicates.in("2013-09-12", "2013-10-12", "2013-11-11"), fp.project(Predicates.in(sepInstant, octInstant, novInstant))); Assert.assertNull(fp.projectStrict( Predicates.in(sepInstant, octInstant, novInstant))); }
@Override public void run() { fp.project(Ranges.openClosed(5, 21)); } });
@Test public void testLongFixedSizeRangeFieldPartitionerSetPredicate() { final FieldPartitioner<Long, Long> fp = new LongFixedSizeRangeFieldPartitioner("num", 5); Assert.assertEquals(Predicates.in(5L, 15L), fp.project(Predicates.in(5L, 6L, 15L, 16L))); // null if no full range is included Assert.assertNull(fp.projectStrict(Predicates.in(5L, 6L, 15L, 16L))); Assert.assertEquals(Predicates.in(5L), fp.projectStrict(Predicates.in(5L, 6L, 7L, 8L, 9L, 15L, 16L))); Assert.assertEquals(Predicates.in(5L, 15L), fp.projectStrict(Predicates.in( 4L, 5L, 6L, 7L, 8L, 9L, 10L, 15L, 16L, 17L, 18L, 19L, 20L))); }
List<FieldPartitioner> compatible = Lists.newArrayList(); for (FieldPartitioner fp : fps) { Predicate<?> projected = fp.project(constraint); if (projected instanceof Range) { current = addProjected(projected, fp.getName(), current); } else if (compatible.size() == 1) { FieldPartitioner fp = compatible.get(0); return addProjected(fp.project(constraint), fp.getName(), current); } else { return new SetGroupIterator(constraint, compatible, current);
@Test public void testRangeFieldPartitionerSetPredicate() { final FieldPartitioner<String, String> fp = new RangeFieldPartitioner("str", new String[]{"a", "b", "c"}); Assert.assertEquals(Predicates.in("a"), fp.project(Predicates.in("0"))); Assert.assertEquals(Predicates.in("a"), fp.project(Predicates.in("a"))); Assert.assertEquals(Predicates.in("b"), fp.project(Predicates.in("aa"))); Assert.assertEquals(Predicates.in("a", "b"), fp.project(Predicates.in("a", "aa", "b"))); TestHelpers.assertThrows("Cannot project endpoint outside of bounds", IllegalArgumentException.class, new Runnable() { @Override public void run() { fp.project(Predicates.in("cc")); } }); // cannot enumerate all of the potential input, so no satisfied projection Assert.assertNull(fp.projectStrict(Predicates.in("0"))); Assert.assertNull(fp.projectStrict(Predicates.in("a"))); Assert.assertNull(fp.projectStrict(Predicates.in("aa"))); Assert.assertNull(fp.projectStrict(Predicates.in("a", "aa", "b"))); Assert.assertNull(fp.projectStrict(Predicates.in("cc"))); }
@Test public void testDateFormatPartitionerRangePredicate() { FieldPartitioner<Long, String> fp = new DateFormatPartitioner("timestamp", "date", "yyyy-MM-dd"); Predicate<String> projected = fp.project( Ranges.open(octInstant, octInstant + ONE_DAY_MILLIS)); Assert.assertEquals(Ranges.closed("2013-10-12", "2013-10-13"), projected); }
fp.project(Predicates.in(7, 11, 13))); Assert.assertEquals(Predicates.in(0, 1, 2), fp.project(Predicates.in(7, 11, 22, 33, 44, 13))); Assert.assertEquals(Predicates.in(0, 2), fp.project(Predicates.in(7, 13))); Assert.assertEquals(Predicates.in(0), fp.project(Predicates.in(7))); Assert.assertEquals(Predicates.in(2), fp.project(Predicates.in(13)));
@Test @SuppressWarnings("unchecked") public void testHashFieldPartitionerSetPredicate() { FieldPartitioner<Object, Integer> fp = new HashFieldPartitioner("name", 50); Assert.assertEquals(Predicates.in(fp.apply("a"), fp.apply("b")), fp.project((Predicate)Predicates.in("a", "b"))); // the set of inputs that result in a particular value is not closed Assert.assertNull(fp.projectStrict((Predicate) Predicates.in("a"))); }
@Test public void testIntRangeFieldPartitionerSetPredicate() { final FieldPartitioner<Integer, Integer> fp = new IntRangeFieldPartitioner("num", 5, 10, 15, 20); Assert.assertEquals(Predicates.in(1, 3), fp.project(Predicates.in(6, 7, 16, 17))); TestHelpers.assertThrows("Should not project invalid set", IllegalArgumentException.class, new Runnable() { @Override public void run() { fp.project(Predicates.in(21)); } }); // null if no full range is included Assert.assertNull(fp.projectStrict(Predicates.in(6, 7, 16, 17))); Assert.assertEquals(Predicates.in(1), fp.projectStrict(Predicates.in(6, 7, 8, 9, 10, 16, 17))); Assert.assertEquals(Predicates.in(1, 3), fp.projectStrict(Predicates.in( 5, 6, 7, 8, 9, 10, 16, 17, 18, 19, 20))); // doesn't complain about values that are too large Assert.assertEquals(Predicates.in(1), fp.projectStrict(Predicates.in(6, 7, 8, 9, 10, 16, 17, 22))); }
@Test public void testLongFixedSizeRangeFieldPartitionerRangePredicate() { final FieldPartitioner<Long, Long> fp = new LongFixedSizeRangeFieldPartitioner("num", 5); Assert.assertEquals(Ranges.closed(-5L, 10L), fp.project(Ranges.open(-2L, 15L))); Assert.assertEquals(Ranges.closed(5L, 10L), fp.project(Ranges.open(5L, 15L))); Assert.assertEquals(Ranges.closed(5L, 10L), fp.project(Ranges.open(4L, 15L))); Assert.assertEquals(Ranges.closed(0L, 15L), fp.project(Ranges.closed(4L, 15L))); Assert.assertEquals(Ranges.closed(5L, 20L), fp.project(Ranges.openClosed(5L, 21L))); Assert.assertEquals(Ranges.atMost(15L), fp.project(Ranges.atMost(15L))); Assert.assertEquals(Ranges.atMost(20L), fp.project(Ranges.lessThan(21L))); Assert.assertEquals(Ranges.atLeast(10L), fp.project(Ranges.atLeast(14L))); Assert.assertEquals(Ranges.singleton(10L), fp.projectStrict(Ranges.open(5L, 15L))); Assert.assertNull(fp.projectStrict(Ranges.open(5L, 14L))); Assert.assertEquals(Ranges.atMost(10L), fp.projectStrict(Ranges.atMost(15L))); Assert.assertEquals(Ranges.atMost(15L), fp.projectStrict(Ranges.lessThan(21L))); Assert.assertEquals(Ranges.atLeast(15L), fp.projectStrict(Ranges.atLeast(14L))); }
fp.project(Ranges.atMost("a"))); Assert.assertEquals(Predicates.in("a", "b"), fp.project(Ranges.closedOpen("a", "b"))); Assert.assertEquals(Predicates.in("a", "b"), fp.project(Ranges.closedOpen("a", "aa"))); Assert.assertEquals(Predicates.in("a", "b", "c"), fp.project(Ranges.closedOpen("a", "ba"))); Assert.assertEquals(Predicates.in("a", "b", "c"), fp.project(Ranges.closedOpen("0", "c"))); Assert.assertEquals(Predicates.in("c"), fp.project(Ranges.atLeast("c")));
final FieldPartitioner<Integer, Integer> fp = new IntRangeFieldPartitioner("num", 5, 10, 15, 20); Assert.assertEquals(Ranges.closed(1, 2), fp.project(Ranges.open(5, 15))); Assert.assertEquals(Ranges.closed(0, 2), fp.project(Ranges.open(4, 15))); Assert.assertEquals(Ranges.closed(0, 3), fp.project(Ranges.open(4, 21))); TestHelpers.assertThrows("Should not project an invalid range", IllegalArgumentException.class, new Runnable() {