@SuppressWarnings("unchecked") @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH", justification="Null case checked by precondition") private Object partitionValue(E object, @Nullable Map<String, Object> provided, FieldPartitioner fp) { if (fp instanceof ProvidedFieldPartitioner) { String name = fp.getName(); Preconditions.checkArgument( (provided != null) && provided.containsKey(name), "Cannot construct key, missing provided value: %s", name); return provided.get(name); } else { return fp.apply(get(object, fp.getSourceName())); } }
private <S, T> T valueFor(FieldPartitioner<S, T> fp) { if (values.containsKey(fp.getName())) { return Conversions.convert(values.get(fp.getName()), fp.getType()); } else if (values.containsKey(fp.getSourceName())) { return fp.apply(Conversions.convert( values.get(fp.getSourceName()), fp.getSourceType())); } else { throw new IllegalStateException( "Cannot create StorageKey, missing data for field:" + fp.getName()); } } }
@SuppressWarnings("unchecked") private <S, T> T valueFor(FieldPartitioner<S, T> fp) { if (values.containsKey(fp.getName())) { return Conversions.convert(values.get(fp.getName()), SchemaUtil.getPartitionType(fp, schema)); } else if (values.containsKey(fp.getSourceName())) { return fp.apply(Conversions.convert(values.get(fp.getSourceName()), SchemaUtil.getSourceType(fp, schema))); } else { throw new IllegalStateException( "Cannot create Key, missing data for field:" + fp.getName()); } } }
@Override @SuppressWarnings("unchecked") public MarkerRange.Builder update( MarkerRange.Builder current, Object item) { for (FieldPartitioner fp : fields) { Object value = fp.apply(item); current.addToStart(fp.getName(), value); current.addToEnd(fp.getName(), value); } return current; } }
/** * <p> * Construct a partition key for the given entity, reusing the supplied key if * not null. * </p> * <p> * This is a convenient way to find the partition that a given entity is * written to, or to find a partition using objects from the entity domain. * </p> */ @SuppressWarnings("unchecked") public static <E> PartitionKey partitionKeyForEntity(PartitionStrategy strategy, E entity, EntityAccessor<E> accessor, @Nullable PartitionKey reuseKey) { List<FieldPartitioner> fieldPartitioners = Accessor.getDefault().getFieldPartitioners(strategy); PartitionKey key = (reuseKey == null ? new PartitionKey(new Object[fieldPartitioners.size()]) : reuseKey); for (int i = 0; i < fieldPartitioners.size(); i++) { FieldPartitioner fp = fieldPartitioners.get(i); key.set(i, fp.apply(accessor.get(entity, fp.getSourceName()))); } return key; }
@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"))); }
/** * Return the value of a {@code FieldPartitioner} field for this {@link Marker}. * * If the {@code Marker} has a value for the field's name, that value is * returned using {@link Marker#getAs(java.lang.String, java.lang.Class)}. If * the {@code Marker} only has a value for the the source field name, then * that value is retrieved using * {@link org.kitesdk.data.spi.Marker#getAs(java.lang.String, * java.lang.Class)} and the field's transformation is applied to it as the source value. * * @param fp a {@code FieldPartitioner} * @return the value of the field for this {@code marker}, or null * @since 0.9.0 */ @Nullable public <S, T> T valueFor(FieldPartitioner<S, T> fp) { if (has(fp.getName())) { return getAs(fp.getName(), fp.getType()); } else if (has(fp.getSourceName())) { return fp.apply(getAs(fp.getSourceName(), fp.getSourceType())); } else { return null; } }
hash50.apply("green"), 2013, 9, 1, "green")); Assert.assertEquals(Sets.newHashSet("number"), c.minimizeFor(key).keySet());
@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()); }
.add("id_hash", hashFunc.apply(ids[0])).add("id_copy", ids[0]) .build(); Marker nov0 = new Marker.Builder() .add("year", 2013).add("month", 11).add("day", 11) .add("id_hash", hashFunc.apply(ids[0])).add("id_copy", ids[0]) .build(); .add("id_hash", hashFunc.apply(ids[1])).add("id_copy", ids[1]) .build(); Marker nov1 = new Marker.Builder() .add("year", 2013).add("month", 11).add("day", 11) .add("id_hash", hashFunc.apply(ids[1])).add("id_copy", ids[1]) .build(); assertIterableEquals(Sets.newHashSet( .add("id_hash", hashFunc.apply(ids[0])).add("id_copy", ids[0]) .add("id_component", "com.company.Main") .build(); Marker nov2 = new Marker.Builder() .add("year", 2013).add("month", 11).add("day", 11) .add("id_hash", hashFunc.apply(ids[0])).add("id_copy", ids[0]) .add("id_component", "com.company.Main") .build(); Marker sep3 = new Marker.Builder() .add("year", 2013).add("month", 9).add("day", 12) .add("id_hash", hashFunc.apply(ids[1])).add("id_copy", ids[1]) .add("id_component", "com.company.Main") .build(); Marker nov3 = new Marker.Builder()