Search in sources :

Example 6 with RangeSet

use of com.google.common.collect.RangeSet in project druid by druid-io.

the class DimFilterUtils method filterShards.

/**
   * Filter the given iterable of objects by removing any object whose ShardSpec, obtained from the converter function,
   * does not fit in the RangeSet of the dimFilter {@link DimFilter#getDimensionRangeSet(String)}. The returned set
   * contains the filtered objects in the same order as they appear in input.
   *
   * DimensionRangedCache stores the RangeSets of different dimensions for the dimFilter. It should be re-used
   * between calls with the same dimFilter to save redundant calls of {@link DimFilter#getDimensionRangeSet(String)}
   * on same dimensions.
   *
   * @param dimFilter The filter to use
   * @param input The iterable of objects to be filtered
   * @param converter The function to convert T to ShardSpec that can be filtered by
   * @param dimensionRangeCache The cache of RangeSets of different dimensions for the dimFilter
   * @param <T> This can be any type, as long as transform function is provided to convert this to ShardSpec
   * @return The set of filtered object, in the same order as input
   */
public static <T> Set<T> filterShards(DimFilter dimFilter, Iterable<T> input, Function<T, ShardSpec> converter, Map<String, Optional<RangeSet<String>>> dimensionRangeCache) {
    Set<T> retSet = Sets.newLinkedHashSet();
    for (T obj : input) {
        ShardSpec shard = converter.apply(obj);
        boolean include = true;
        if (dimFilter != null && shard != null) {
            Map<String, Range<String>> domain = shard.getDomain();
            for (Map.Entry<String, Range<String>> entry : domain.entrySet()) {
                Optional<RangeSet<String>> optFilterRangeSet = dimensionRangeCache.get(entry.getKey());
                if (optFilterRangeSet == null) {
                    RangeSet<String> filterRangeSet = dimFilter.getDimensionRangeSet(entry.getKey());
                    optFilterRangeSet = Optional.fromNullable(filterRangeSet);
                    dimensionRangeCache.put(entry.getKey(), optFilterRangeSet);
                }
                if (optFilterRangeSet.isPresent() && optFilterRangeSet.get().subRangeSet(entry.getValue()).isEmpty()) {
                    include = false;
                }
            }
        }
        if (include) {
            retSet.add(obj);
        }
    }
    return retSet;
}
Also used : RangeSet(com.google.common.collect.RangeSet) Range(com.google.common.collect.Range) Map(java.util.Map) HashMap(java.util.HashMap) ShardSpec(io.druid.timeline.partition.ShardSpec)

Aggregations

RangeSet (com.google.common.collect.RangeSet)6 ImmutableRangeSet (com.google.common.collect.ImmutableRangeSet)4 Test (org.junit.Test)4 ImmutableList (com.google.common.collect.ImmutableList)1 Range (com.google.common.collect.Range)1 Pair (io.druid.java.util.common.Pair)1 AndDimFilter (io.druid.query.filter.AndDimFilter)1 BoundDimFilter (io.druid.query.filter.BoundDimFilter)1 DimFilter (io.druid.query.filter.DimFilter)1 NotDimFilter (io.druid.query.filter.NotDimFilter)1 OrDimFilter (io.druid.query.filter.OrDimFilter)1 ShardSpec (io.druid.timeline.partition.ShardSpec)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1