@Override public void initialize() { if (key == null) { // restore transient objects from serializable versions PartitionStrategy strategy = PartitionStrategyParser.parse(strategyString); Schema schema = new Schema.Parser().parse(schemaString); this.key = new AvroStorageKey(strategy, schema); this.accessor = DataModelUtil.accessor(type, schema); if (constraints != null) { this.provided = Constraints .fromQueryMap(schema, strategy, constraints) .getProvidedValues(); } } count = 0; }
public Constraints fromAfter(String name, Comparable value) { SchemaUtil.checkTypeConsistency(schema, strategy, name, value); checkContained(name, value); Range added = Ranges.greaterThan(value); return new Constraints(this, name, combine(constraints.get(name), added)); }
/** * Adds a set of constraints. * * @param constraints a set of constraints * @return this builder for method chaining */ @VisibleForTesting URIBuilder constraints(Constraints constraints) { options.putAll(constraints.toQueryMap()); this.isView = !constraints.isUnbounded(); return this; }
EventQueue.invokeLater(new Runnable() { @Override public void run() { Constraints constraints = new Constraints(); constraints.displayGUI(); System.out.println("Hello world"); } });
/** * Get a {@link Predicate} for testing entity objects that match the given * {@link StorageKey}. * * @param <E> The type of entities to be matched * @param key a StorageKey for entities tested with the Predicate * @return a Predicate to test if entity objects satisfy this constraint set */ public <E> Predicate<E> toEntityPredicate(StorageKey key, EntityAccessor<E> accessor) { if (key != null) { Map<String, Predicate> predicates = minimizeFor(key); if (predicates.isEmpty()) { return alwaysTrue(); } return entityPredicate(predicates, schema, accessor, strategy); } return toEntityPredicate(accessor); }
@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 testNormalizeConstraintsOrderedKeys() throws IOException { Constraints constraints = new Constraints(DatasetTestUtilities.USER_SCHEMA). with("username", "kite").with("email", "kite@domain.com"); String normalizedConstraints = SignalManager.getNormalizedConstraints(constraints); Assert.assertEquals("email=kite%40domain.com&username=kite", normalizedConstraints); }
@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 testExists() { Constraints exists = emptyConstraints.with("id"); Assert.assertEquals("Should be Predicates.exists()", Predicates.exists(), exists.get("id")); GenericEvent event = new GenericEvent(); Assert.assertTrue("Should match event with non-null id", exists.toEntityPredicate(accessor).apply(event)); event.id = null; Assert.assertFalse("Should not match event with null id", exists.toEntityPredicate(accessor).apply(event)); Assert.assertNotNull("Should produce a key range", exists.toKeyRanges()); Assert.assertFalse("Non-empty constraints (exists) should not be unbounded", exists.isUnbounded()); }
@Test public void testQueryMapConversionWithEncodedCharacters() { Constraints with = emptyConstraints.with("id", "a/b,c"); Map<String, String> queryMap = with.toQueryMap(); Assert.assertEquals(1, queryMap.size()); Assert.assertEquals("a%2Fb%2Cc", queryMap.get("id")); Assert.assertEquals(with, Constraints.fromQueryMap(schema, strategy, queryMap)); }
@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); }
new HashFieldPartitioner("name", 50); Constraints c = new Constraints(schema) .with("number", 7, 14, 21, 28, 35, 42, 49) .with("color", "green", "orange") .from("created_at", new DateTime(2013, 1, 1, 0, 0, DateTimeZone.UTC).getMillis()) .toBefore("created_at", new DateTime(2014, 1, 1, 0, 0, DateTimeZone.UTC).getMillis()); hash50.apply("green"), 2013, 9, 1, "green")); Assert.assertEquals(Sets.newHashSet("number"), c.minimizeFor(key).keySet()); Constraints c2 = c.toBefore("created_at", new DateTime(2013, 10, 1, 0, 0, DateTimeZone.UTC).getMillis()); Assert.assertEquals(Sets.newHashSet("number"), c2.minimizeFor(key).keySet()); Constraints c3 = c.to("created_at", new DateTime(2013, 9, 1, 12, 0, DateTimeZone.UTC).getMillis()); Assert.assertEquals(Sets.newHashSet("number", "created_at"), c3.minimizeFor(key).keySet());
StorageKey key = accessor.keyFor(e, new StorageKey(strategy)); Constraints time = emptyConstraints.partitionedBy(strategy) .from("timestamp", START) .to("timestamp", START + 100000); Predicate<StorageKey> matchKeys = time.toKeyPredicate(); Predicate<GenericEvent> matchEvents = time.toEntityPredicate(accessor); Assert.assertTrue(matchKeys.apply(key)); Assert.assertTrue(matchEvents.apply(e)); Constraints timeAndUUID = time.with("id", e.getId()); Assert.assertTrue(timeAndUUID.toKeyPredicate().apply(key)); Assert.assertTrue(timeAndUUID.toEntityPredicate(accessor).apply(e)); Assert.assertFalse(time.toEntityPredicate(accessor).apply(e)); Assert.assertFalse(timeAndUUID.toEntityPredicate(accessor).apply(e)); Assert.assertTrue(time.toKeyPredicate().apply(key)); Assert.assertTrue(timeAndUUID.toKeyPredicate().apply(key)); Assert.assertFalse(time.toEntityPredicate(accessor).apply(e)); Assert.assertFalse(timeAndUUID.toEntityPredicate(accessor).apply(e)); Assert.assertTrue(time.toKeyPredicate().apply(key)); Assert.assertTrue(timeAndUUID.toKeyPredicate().apply(key)); Assert.assertFalse(time.toEntityPredicate(accessor).apply(e)); Assert.assertFalse(timeAndUUID.toEntityPredicate(accessor).apply(e)); Assert.assertFalse(time.toKeyPredicate().apply(key)); Assert.assertFalse(timeAndUUID.toKeyPredicate().apply(key));
@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()); }
.build(); Constraints c = emptyConstraints.with("id", "a", "b", "c"); c.with("color", "orange").partitionedBy(strategy).alignedWithBoundaries()); c.partitionedBy(hashStrategy).alignedWithBoundaries()); Assert.assertFalse("Cannot be satisfied by time filtering for timestamp", c.with("timestamp", new DateTime(2013, 9, 1, 12, 0, DateTimeZone.UTC).getMillis()) .partitionedBy(strategy).alignedWithBoundaries()); c.with("color", "green", "brown").partitionedBy(withColor) .alignedWithBoundaries()); Assert.assertFalse("Cannot be satisfied because equality doesn't hold", c.fromAfter("color", "blue").to("color", "red").partitionedBy(withColor) .alignedWithBoundaries()); c.partitionedBy(strategy).alignedWithBoundaries()); c.with("year", 2013).with("month", 9).with("day", 1) .partitionedBy(strategy).alignedWithBoundaries()); Assert.assertFalse("Should not ignore other constraints", c.with("year", 2013).with("month", 9).with("day", 1).from("timestamp", new DateTime(2013, 9, 1, 12, 0, DateTimeZone.UTC).getMillis()) .partitionedBy(strategy).alignedWithBoundaries());
@Test public void testWith() throws Exception { Iterable<StorageKey> partitions = new FileSystemPartitionIterator( fileSystem, testDirectory, strategy, schema, emptyConstraints.with("timestamp", oct_24_2013).toKeyPredicate()); assertIterableEquals(keys.subList(16, 17), partitions); }
@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)); }
@Override public DatasetWriter<E> newWriter() { final EntityBatch<E> wrappedWriter = dataset.getDao().newBatch(); if (constraints.isUnbounded()) { wrappedWriter.initialize(); return wrappedWriter; private Constraints.KeyPredicate keyPredicate = constraints.toKeyPredicate();
@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 testEmptyConstraintsEntityPredicate() { Assert.assertNotNull("Empty constraints should produce an entity predicate", emptyConstraints.toEntityPredicate(accessor)); Assert.assertTrue("Should match event", emptyConstraints.toEntityPredicate(accessor).apply(new GenericEvent())); Assert.assertFalse("Should not match null", emptyConstraints.toEntityPredicate(accessor).apply(null)); Assert.assertNotNull("Should produce an unbound key range", emptyConstraints.toKeyRanges()); Assert.assertTrue("Empty constraints should be unbounded", emptyConstraints.isUnbounded()); }