@Override public void run() { emptyConstraints.from("id", null); } });
@Override public void run() { startRange.fromAfter("year", 2012).from("year", 2012); } });
@Override public AbstractRefinableView<E> from(String name, Comparable value) { return filter(constraints.from(name, value)); }
@Test public void testTimeAlignedWithPartitionBoundaries() { Constraints c = emptyConstraints.partitionedBy(strategy); Assert.assertFalse("Cannot be satisfied because timestamp is in middle of partition", c.from("timestamp", new DateTime(2013, 9, 1, 12, 0, DateTimeZone.UTC).getMillis()) .alignedWithBoundaries()); Assert.assertTrue("Should be satisfied because 'from' timestamp is on inclusive partition boundary", c.from("timestamp", new DateTime(2013, 9, 1, 0, 0, DateTimeZone.UTC).getMillis()) .alignedWithBoundaries()); Assert.assertFalse("Cannot be satisfied because 'from' timestamp is on exclusive partition boundary", c.fromAfter("timestamp", new DateTime(2013, 9, 1, 0, 0, DateTimeZone.UTC).getMillis()) .alignedWithBoundaries()); Assert.assertTrue("Should be satisfied because 'to' timestamp is on exclusive partition boundary", c.toBefore("timestamp", new DateTime(2013, 9, 1, 0, 0, DateTimeZone.UTC).getMillis()) .alignedWithBoundaries()); Assert.assertFalse("Cannot be satisfied because 'to' timestamp is on inclusive partition boundary", c.to("timestamp", new DateTime(2013, 9, 1, 0, 0, DateTimeZone.UTC).getMillis()) .alignedWithBoundaries()); Assert.assertTrue("Should be satisfied because 'from' timestamp is on inclusive " + "partition boundary and 'to' timestamp is on exclusive partition boundary", c.from("timestamp", new DateTime(2013, 9, 1, 0, 0, DateTimeZone.UTC).getMillis()) .toBefore("timestamp", new DateTime(2013, 9, 2, 0, 0, DateTimeZone.UTC).getMillis()) .alignedWithBoundaries()); }
@Test public void testFrom() throws Exception { Iterable<StorageKey> partitions = new FileSystemPartitionIterator( fileSystem, testDirectory, strategy, schema, emptyConstraints.from("timestamp", oct_24_2013).toKeyPredicate()); assertIterableEquals(keys.subList(16, 24), partitions); }
@Test public void testAddRangeConstraints() { Assert.assertEquals("Should add equality constraints", URI.create("view:file:/datasets/test?color=[green,)"), new URIBuilder("dataset:file:/datasets/test") .constraints(empty.from("color", "green")) .build()); Assert.assertEquals("Should add equality constraints", URI.create("view:file:/datasets/test?color=(,green]"), new URIBuilder("dataset:file:/datasets/test") .constraints(empty.to("color", "green")) .build()); Assert.assertEquals("Should add equality constraints", URI.create("view:file:/datasets/test?timestamp=[0,1405720705333)&color=(green,red]"), new URIBuilder("dataset:file:/datasets/test") .constraints(empty .from("timestamp", 0l).toBefore("timestamp", 1405720705333L) .fromAfter("color", "green").to("color", "red")) .build()); }
@Test public void testFrom() throws Exception { Iterable<StorageKey> partitions = new FileSystemPartitionIterator( fileSystem, testDirectory, strategy, schema, emptyConstraints.from("timestamp", oct_24_2013).toKeyPredicate()); assertIterableEquals(keys.subList(16, 24), partitions); }
@Test public void testRangeMidpointRefinementUsingRange() { final Constraints startRange = emptyConstraints .from("year", 2012).toBefore("year", 2014); // any method can be used with a valid midpoint to limit the range Assert.assertEquals("Can refine using from", Ranges.closedOpen(2013, 2014), startRange.from("year", 2013).get("year")); Assert.assertEquals("Can refine using fromAfter", Ranges.open(2013, 2014), startRange.fromAfter("year", 2013).get("year")); Assert.assertEquals("Can refine using to", Ranges.closed(2012, 2013), startRange.to("year", 2013).get("year")); Assert.assertEquals("Can refine using toBefore", Ranges.closedOpen(2012, 2013), startRange.toBefore("year", 2013).get("year")); }
@Test public void testDayRange() throws Exception { Predicate<StorageKey> predicate = emptyConstraints .from("timestamp", oct_24_2013) .to("timestamp", oct_24_2013_end) .toKeyPredicate(); Iterable<StorageKey> partitions = new FileSystemPartitionIterator( fileSystem, testDirectory, strategy, schema, predicate); assertIterableEquals(keys.subList(16, 17), partitions); }
@Test public void testLargerRange() throws Exception { Predicate<StorageKey> predicate = emptyConstraints .from("timestamp", oct_25_2012) .to("timestamp", oct_24_2013) .toKeyPredicate(); Iterable<StorageKey> partitions = new FileSystemPartitionIterator( fileSystem, testDirectory, strategy, schema, predicate); assertIterableEquals(keys.subList(5, 17), partitions); }
@Test public void testDayRange() throws Exception { Predicate<StorageKey> predicate = emptyConstraints .from("timestamp", oct_24_2013) .to("timestamp", oct_24_2013_end) .toKeyPredicate(); Iterable<StorageKey> partitions = new FileSystemPartitionIterator( fileSystem, testDirectory, strategy, schema, predicate); assertIterableEquals(keys.subList(16, 17), partitions); }
@Test public void testLargerRange() throws Exception { Predicate<StorageKey> predicate = emptyConstraints .from("timestamp", oct_25_2012) .to("timestamp", oct_24_2013) .toKeyPredicate(); Iterable <StorageKey> partitions = new FileSystemPartitionIterator( fileSystem, testDirectory, strategy, schema, predicate); assertIterableEquals(keys.subList(5, 17), partitions); }
@Test public void testRangeRefinementUsingExists() { final Constraints startRange = emptyConstraints .from("year", 2012).toBefore("year", 2014); Assert.assertEquals(startRange, startRange.with("year")); }
@Test public void testRangeRefinementUsingWith() { final Constraints startRange = emptyConstraints .from("year", 2012).toBefore("year", 2014); // Can only refine using a subset Assert.assertEquals("Refinement with a set produces a set", Predicates.in(Sets.newHashSet(2012, 2013)), startRange.with("year", 2013, 2012).get("year")); TestHelpers.assertThrows("Cannot refine with a value outside the range", IllegalArgumentException.class, new Runnable() { @Override public void run() { startRange.with("year", 2014); } }); }
@Test public void testPartitionFieldConstraintsMinimized() { GenericEvent e = new GenericEvent(); DateTime eventDateTime = new Instant(e.getTimestamp()) .toDateTime(DateTimeZone.UTC); // year and month constraints are for partition fields Constraints green = emptyConstraints .from("year", eventDateTime.getYear()) // will be removed .with("month", eventDateTime.getMonthOfYear()) // will be removed .with("color", "green"); StorageKey eventKey = new StorageKey.Builder(strategy) .add("timestamp", e.getTimestamp()) .add("id", e.getId()) .build(); Map<String, Predicate> minimized = green.minimizeFor(eventKey); Assert.assertEquals("Should have one unsatisfied constraint", 1, minimized.size()); Map.Entry<String, Predicate> entry = Iterables.getOnlyElement( minimized.entrySet()); Assert.assertEquals("Should be the color constraint", "color", entry.getKey()); Assert.assertEquals("Should match green", Predicates.in("green"), entry.getValue()); }
@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 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 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())); }