@Override public void run() { emptyConstraints.toBefore("id", null); } });
@Override public AbstractRefinableView<E> toBefore(String name, Comparable value) { return filter(constraints.toBefore(name, value)); }
@Override public void run() { startRange.toBefore("year", 2014).to("year", 2014); } });
@Test public void testBefore() throws Exception { Iterable <StorageKey> partitions = new FileSystemPartitionIterator( fileSystem, testDirectory, strategy, schema, emptyConstraints.toBefore("timestamp", oct_25_2012).toKeyPredicate()); assertIterableEquals(keys.subList(0, 5), partitions); }
@Test public void testBefore() throws Exception { Iterable <StorageKey> partitions = new FileSystemPartitionIterator( fileSystem, testDirectory, strategy, schema, emptyConstraints.toBefore("timestamp", oct_25_2012).toKeyPredicate()); assertIterableEquals(keys.subList(0, 5), partitions); }
@Test public void testNormalizeConstraintsIntervals() throws IOException { Constraints constraints = new Constraints(DatasetTestUtilities.OLD_VALUE_SCHEMA). toBefore("value", 12L); String normalizedConstraints = SignalManager.getNormalizedConstraints(constraints); Assert.assertEquals("value=(,12)", normalizedConstraints); }
@Test public void testRangeRefinementUsingExists() { final Constraints startRange = emptyConstraints .from("year", 2012).toBefore("year", 2014); Assert.assertEquals(startRange, startRange.with("year")); }
@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 testTimeRangeEdgeCases() { final long oct_24_2013 = new DateTime(2013, 10, 24, 0, 0, DateTimeZone.UTC).getMillis(); final long oct_25_2013 = new DateTime(2013, 10, 25, 0, 0, DateTimeZone.UTC).getMillis(); final long oct_24_2013_end = new DateTime(2013, 10, 24, 23, 59, 59, 999, DateTimeZone.UTC).getMillis(); //final long oct_24_2013_end = oct_25_2013 - 1; GenericEvent e = new GenericEvent(); e.timestamp = oct_25_2013; StorageKey key = accessor.keyFor(e, new StorageKey(timeOnly)); Constraints empty = emptyConstraints.partitionedBy(timeOnly); Constraints c = empty.with("timestamp", oct_24_2013); Assert.assertFalse("Should not match", c.toKeyPredicate().apply(key)); c = empty.toBefore("timestamp", oct_24_2013_end); LOG.info("Constraints: {}", c); e.timestamp = oct_25_2013; key = accessor.keyFor(e, key); Assert.assertFalse("Should not match toBefore", c.toKeyPredicate().apply(key)); }
@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 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 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 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 testBasicRangeRefinement() { Assert.assertEquals("Should be closed to closed", Ranges.closed(2012, 2014), emptyConstraints.from("year", 2012).to("year", 2014).get("year")); Assert.assertEquals("Should be closed to closed", Ranges.closed(2012, 2014), emptyConstraints.to("year", 2014).from("year", 2012).get("year")); Assert.assertEquals("Should be closed to open", Ranges.closedOpen(2012, 2014), emptyConstraints.from("year", 2012).toBefore("year", 2014).get("year")); Assert.assertEquals("Should be closed to open", Ranges.closedOpen(2012, 2014), emptyConstraints.toBefore("year", 2014).from("year", 2012).get("year")); Assert.assertEquals("Should be open to open", Ranges.open(2012, 2014), emptyConstraints.fromAfter("year", 2012).toBefore("year", 2014).get("year")); Assert.assertEquals("Should be open to open", Ranges.open(2012, 2014), emptyConstraints.toBefore("year", 2014).fromAfter("year", 2012).get("year")); Assert.assertEquals("Should be open to closed", Ranges.openClosed(2012, 2014), emptyConstraints.fromAfter("year", 2012).to("year", 2014).get("year")); Assert.assertEquals("Should be open to closed", Ranges.openClosed(2012, 2014), emptyConstraints.to("year", 2014).fromAfter("year", 2012).get("year")); }
@Test public void testExistsRefinement() { Constraints exists = emptyConstraints.with("id"); String id = UUID.randomUUID().toString(); Assert.assertNotNull("Refine with exists should work", exists.with("id")); Assert.assertEquals("Refine with exists should do nothing", exists, exists.with("id")); Assert.assertNotNull("Refine with with should work", exists.with("id", id)); Assert.assertEquals("Refine with with should produce with", Predicates.in(Sets.newHashSet(id)), exists.with("id", id).get("id")); Assert.assertNotNull("Refine with from should work", exists.from("id", "0")); Assert.assertEquals("Refine with from should produce Range", Ranges.atLeast("0"), exists.from("id", "0").get("id")); Assert.assertNotNull("Refine with fromAfter should work", exists.fromAfter("id", "0")); Assert.assertEquals("Refine with fromAfter should produce Range", Ranges.greaterThan("0"), exists.fromAfter("id", "0").get("id")); Assert.assertNotNull("Refine with to should work", exists.to("id", "0")); Assert.assertEquals("Refine with to should produce Range", Ranges.atMost("0"), exists.to("id", "0").get("id")); Assert.assertNotNull("Refine with toBefore should work", exists.toBefore("id", "0")); Assert.assertEquals("Refine with toBefore should produce Range", Ranges.lessThan("0"), exists.toBefore("id", "0").get("id")); }
@Test public void testBasicRanges() { // because we validate that the constraint is the guava range, no need to // test how it matches events Assert.assertEquals("Should be closed to pos-infinity", Ranges.atLeast(2013), emptyConstraints.from("year", 2013).get("year")); Assert.assertEquals("Should be open to pos-infinity", Ranges.greaterThan(2013), emptyConstraints.fromAfter("year", 2013).get("year")); Assert.assertEquals("Should be neg-infinity to closed", Ranges.atMost(2013), emptyConstraints.to("year", 2013).get("year")); Assert.assertEquals("Should be neg-infinity to open", Ranges.lessThan(2013), emptyConstraints.toBefore("year", 2013).get("year")); Assert.assertFalse("Non-empty constraints (from) should not be unbounded", emptyConstraints.from("year", 2013).isUnbounded()); Assert.assertFalse("Non-empty constraints (fromAfter) should not be unbounded", emptyConstraints.fromAfter("year", 2013).isUnbounded()); Assert.assertFalse("Non-empty constraints (to) should not be unbounded", emptyConstraints.to("year", 2013).isUnbounded()); Assert.assertFalse("Non-empty constraints (toBefore) should not be unbounded", emptyConstraints.toBefore("year", 2013).isUnbounded()); }
@Test public void testRangeEndpointRefinementUsingRange() { // using the current end-points final Constraints startRange = emptyConstraints.from("year", 2012).to("year", 2014); Assert.assertEquals(Ranges.closed(2012, 2014), startRange.to("year", 2014).get("year")); Assert.assertEquals(Ranges.closedOpen(2012, 2014), startRange.toBefore("year", 2014).get("year")); Assert.assertEquals(Ranges.closed(2012, 2014), startRange.from("year", 2012).get("year")); Assert.assertEquals(Ranges.openClosed(2012, 2014), startRange.fromAfter("year", 2012).get("year")); TestHelpers.assertThrows("Cannot change closed endpoint to open", IllegalArgumentException.class, new Runnable() { @Override public void run() { startRange.toBefore("year", 2014).to("year", 2014); } }); TestHelpers.assertThrows("Cannot change closed endpoint to open", IllegalArgumentException.class, new Runnable() { @Override public void run() { startRange.fromAfter("year", 2012).from("year", 2012); } }); }