Class GroupAggregator<K,​V,​T,​E,​R>

  • Type Parameters:
    K - the type of the Map entry keys
    V - the type of the Map entry values
    T - the type of the value to extract from
    E - the type of the extracted value
    R - the type of the group aggregator result
    All Implemented Interfaces:
    ExternalizableLite, PortableObject, InvocableMap.EntryAggregator<K,​V,​Map<E,​R>>, InvocableMap.StreamingAggregator<K,​V,​Map<E,​Object>,​Map<E,​R>>, Serializable
    Direct Known Subclasses:
    GroupAggregator.Parallel

    public class GroupAggregator<K,​V,​T,​E,​R>
    extends ExternalizableHelper
    implements InvocableMap.StreamingAggregator<K,​V,​Map<E,​Object>,​Map<E,​R>>, ExternalizableLite, PortableObject
    The GroupAggregator provides an ability to split a subset of entries in an InvocableMap into a collection of non-intersecting subsets and then aggregate them separately and independently. The splitting (grouping) is performed using the results of the underlying ValueExtractor in such a way that two entries will belong to the same group if and only if the result of the corresponding extract call produces the same value or tuple (list of values). After the entries are split into the groups, the underlying aggregator is applied separately to each group. The result of the aggregation by the GroupAggregator is a Map that has distinct values (or tuples) as keys and results of the individual aggregation as values. Additionally, those results could be further reduced using an optional Filter object.

    Informally speaking, this aggregator is analogous to the SQL "group by" and "having" clauses. Note that the "having" Filter is applied independently on each server against the partial aggregation results; this generally implies that data affinity is required to ensure that all required data used to generate a given result exists within a single cache partition. In other words, the "group by" predicate should not span multiple partitions if the "having" clause is used.

    The GroupAggregator is somewhat similar to the DistinctValues aggregator, which returns back a list of distinct values (tuples) without performing any additional aggregation work.

    Unlike many other concrete EntryAggregator implementations that are constructed directly, instances of GroupAggregator should only be created using one of the factory methods: createInstance(extractor, aggregator), createInstance(extractor, aggregator, filter), createInstance(sMethod, aggregator) createInstance(sMethod, aggregator, filter)

    Since:
    Coherence 3.2
    Author:
    gg 2006.02.15, as 2014.11.09
    See Also:
    Serialized Form
    • Field Detail

      • m_extractor

        protected ValueExtractor<? super T,​? extends E> m_extractor
        The underlying ValueExtractor.
      • m_filter

        protected Filter m_filter
        The Filter object representing the "having" clause of this "group by" aggregator.
      • m_fInit

        protected transient boolean m_fInit
        Flag specifying whether this aggregator has been initialized.
      • m_fStreaming

        protected transient boolean m_fStreaming
        Flag specifying whether streaming optimizations can be used.
      • m_fParallel

        protected transient boolean m_fParallel
        Flag specifying whether parallel optimizations can be used.
      • m_mapResults

        protected transient Map<E,​Object> m_mapResults
        A map of partial results to aggregate.
    • Constructor Detail

      • GroupAggregator

        public GroupAggregator()
        Default constructor (necessary for the ExternalizableLite interface).
      • GroupAggregator

        protected GroupAggregator​(ValueExtractor<? super T,​? extends E> extractor,
                                  InvocableMap.EntryAggregator<? super K,​? super V,​R> aggregator,
                                  Filter filter)
        Construct a GroupAggregator based on a specified ValueExtractor and underlying EntryAggregator.
        Parameters:
        extractor - a ValueExtractor object that is used to split InvocableMap entries into non-intersecting subsets; may not be null
        aggregator - an EntryAggregator object; may not be null
        filter - an optional Filter object used to filter out results of individual group aggregation results
    • Method Detail

      • accumulate

        public boolean accumulate​(InvocableMap.Entry<? extends K,​? extends V> entry)
        Description copied from interface: InvocableMap.StreamingAggregator
        Accumulate one entry into the result.
        Specified by:
        accumulate in interface InvocableMap.StreamingAggregator<K,​V,​T,​E>
        Parameters:
        entry - the entry to accumulate into the aggregation result
        Returns:
        true to continue the aggregation, and false to signal to the caller that the result is ready and the aggregation can be short-circuited
      • combine

        public boolean combine​(Map<E,​Object> partialResult)
        Description copied from interface: InvocableMap.StreamingAggregator
        Merge another partial result into the result.
        Specified by:
        combine in interface InvocableMap.StreamingAggregator<K,​V,​T,​E>
        Parameters:
        partialResult - the partial result to merge
        Returns:
        true to continue the aggregation, and false to signal to the caller that the result is ready and the aggregation can be short-circuited
      • getExtractor

        public ValueExtractor<?,​? extends E> getExtractor()
        Obtain the underlying ValueExtractor.
        Returns:
        the underlying ValueExtractor
      • getAggregator

        public InvocableMap.EntryAggregator<? super K,​? super V,​R> getAggregator()
        Obtain the underlying EntryAggregator.
        Returns:
        the underlying EntryAggregator
      • ensureInitialized

        protected void ensureInitialized()
        Ensure that this aggregator is initialized.
      • isDelegateStreaming

        protected boolean isDelegateStreaming()
        Return true if the underlying aggregator is a StreamingAggregator.
        Returns:
        true if the underlying aggregator is a StreamingAggregator
      • isDelegateParallel

        protected boolean isDelegateParallel()
        Return true if the underlying aggregator is a ParallelAwareAggregator.
        Returns:
        true if the underlying aggregator is a ParallelAwareAggregator
      • throwingMerger

        protected static <T> BinaryOperator<T> throwingMerger()
      • readExternal

        public void readExternal​(DataInput in)
                          throws IOException
        Description copied from interface: ExternalizableLite
        Restore the contents of this object by loading the object's state from the passed DataInput object.
        Specified by:
        readExternal in interface ExternalizableLite
        Parameters:
        in - the DataInput stream to read data from in order to restore the state of this object
        Throws:
        IOException - if an I/O exception occurs
        NotActiveException - if the object is not in its initial state, and therefore cannot be deserialized into
      • writeExternal

        public void writeExternal​(DataOutput out)
                           throws IOException
        Description copied from interface: ExternalizableLite
        Save the contents of this object by storing the object's state into the passed DataOutput object.
        Specified by:
        writeExternal in interface ExternalizableLite
        Parameters:
        out - the DataOutput stream to write the state of this object to
        Throws:
        IOException - if an I/O exception occurs
      • readExternal

        public void readExternal​(PofReader in)
                          throws IOException
        Description copied from interface: PortableObject
        Restore the contents of a user type instance by reading its state using the specified PofReader object.
        Specified by:
        readExternal in interface PortableObject
        Parameters:
        in - the PofReader from which to read the object's state
        Throws:
        IOException - if an I/O error occurs
      • writeExternal

        public void writeExternal​(PofWriter out)
                           throws IOException
        Description copied from interface: PortableObject
        Save the contents of a POF user type instance by writing its state using the specified PofWriter object.
        Specified by:
        writeExternal in interface PortableObject
        Parameters:
        out - the PofWriter to which to write the object's state
        Throws:
        IOException - if an I/O error occurs
      • equals

        public boolean equals​(Object o)
        Compare the GroupAggregator with another object to determine equality.
        Overrides:
        equals in class Object
        Returns:
        true iff this GroupAggregator and the passed object are equivalent
      • hashCode

        public int hashCode()
        Determine a hash value for the GroupAggregator object according to the general Object.hashCode() contract.
        Overrides:
        hashCode in class Object
        Returns:
        an integer hash value for this GroupAggregator object
      • toString

        public String toString()
        Return a human-readable description for this GroupAggregator.
        Overrides:
        toString in class Object
        Returns:
        a String description of the GroupAggregator
      • createInstance

        public static <K,​V,​R> GroupAggregator<K,​V,​Object,​Object,​R> createInstance​(String sMethod,
                                                                                                                      InvocableMap.EntryAggregator<K,​V,​R> aggregator)
        Create an instance of GroupAggregator based on a specified method name(s) and an EntryAggregator.
        If the specified underlying aggregator is an instance of ParallelAwareAggregator, then a parallel-aware instance of the GroupAggregator will be created. Otherwise, the resulting GroupAggregator will not be parallel-aware and could be ill-suited for aggregations run against large partitioned caches.
        Parameters:
        sMethod - a method name or a comma-delimited sequence of names that results in a ReflectionExtractor or a MultiExtractor that will be used to split InvocableMap entries into distinct groups
        aggregator - an underlying EntryAggregator
      • createInstance

        public static <K,​V,​R> GroupAggregator<K,​V,​Object,​Object,​R> createInstance​(String sMethod,
                                                                                                                      InvocableMap.EntryAggregator<K,​V,​R> aggregator,
                                                                                                                      Filter filter)
        Create an instance of GroupAggregator based on a specified method name(s), an EntryAggregator and a result evaluation filter.
        If the specified underlying aggregator is an instance of ParallelAwareAggregator, then a parallel-aware instance of the GroupAggregator will be created. Otherwise, the resulting GroupAggregator will not be parallel-aware and could be ill-suited for aggregations run against large partitioned caches.
        Parameters:
        sMethod - a method name or a comma-delimited sequence of names that results in a ReflectionExtractor or a MultiExtractor that will be used to split InvocableMap entries into distinct groups
        aggregator - an underlying EntryAggregator
        filter - an optional Filter object that will be used to evaluate results of each individual group aggregation
      • createInstance

        public static <K,​V,​T,​E,​R> GroupAggregator<K,​V,​T,​E,​R> createInstance​(ValueExtractor<? super T,​? extends E> extractor,
                                                                                                                            InvocableMap.EntryAggregator<K,​V,​R> aggregator)
        Create an instance of GroupAggregator based on a specified extractor and an EntryAggregator.
        If the specified aggregator is an instance of ParallelAwareAggregator, then a parallel-aware instance of the GroupAggregator will be created. Otherwise, the resulting GroupAggregator will not be parallel-aware and could be ill-suited for aggregations run against large partitioned caches.
        Parameters:
        extractor - a ValueExtractor that will be used to split a set of InvocableMap entries into distinct groups
        aggregator - an underlying EntryAggregator
      • createInstance

        public static <K,​V,​T,​E,​R> GroupAggregator<K,​V,​T,​E,​R> createInstance​(ValueExtractor<? super T,​? extends E> extractor,
                                                                                                                            InvocableMap.EntryAggregator<? super K,​? super V,​R> aggregator,
                                                                                                                            Filter filter)
        Create an instance of GroupAggregator based on a specified extractor and an EntryAggregator and a result evaluation filter.
        If the specified aggregator is an instance of ParallelAwareAggregator, then a parallel-aware instance of the GroupAggregator will be created. Otherwise, the resulting GroupAggregator will not be parallel-aware and could be ill-suited for aggregations run against large partitioned caches.
        Parameters:
        extractor - a ValueExtractor that will be used to split a set of InvocableMap entries into distinct groups
        aggregator - an underlying EntryAggregator
        filter - an optional Filter object used to filter out results of individual group aggregation results