All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.mapreduce.aggregation.impl.CountAggregation Maven / Gradle / Ivy

There is a newer version: 5.4.0
Show newest version
/*
 * Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.mapreduce.aggregation.impl;

import com.hazelcast.mapreduce.Collator;
import com.hazelcast.mapreduce.Combiner;
import com.hazelcast.mapreduce.CombinerFactory;
import com.hazelcast.mapreduce.Mapper;
import com.hazelcast.mapreduce.Reducer;
import com.hazelcast.mapreduce.ReducerFactory;
import com.hazelcast.mapreduce.aggregation.Supplier;

import java.util.Map;

/**
 * The predefined count aggregation.
 *
 * @param    the key input type
 * @param  the value input type
 */
public class CountAggregation
        implements AggType {

    @Override
    public Collator, Long> getCollator() {
        return new Collator, Long>() {
            @Override
            public Long collate(Iterable> values) {
                long count = 0;
                for (Map.Entry entry : values) {
                    count += entry.getValue();
                }
                return count;
            }
        };
    }

    @Override
    public Mapper getMapper(Supplier supplier) {
        return new SupplierConsumingMapper(supplier);
    }

    @Override
    public CombinerFactory getCombinerFactory() {
        return new CountCombinerFactory();
    }

    @Override
    public ReducerFactory getReducerFactory() {
        return new CountReducerFactory();
    }

    /**
     * Count CombinerFactory
     *
     * @param  the key type
     */
    static final class CountCombinerFactory
            extends AbstractAggregationCombinerFactory {

        @Override
        public Combiner newCombiner(Key key) {
            return new CountCombiner();
        }

        @Override
        public int getId() {
            return AggregationsDataSerializerHook.COUNT_COMBINER_FACTORY;
        }
    }

    /**
     * Count ReducerFactory
     *
     * @param  the key type
     */
    static final class CountReducerFactory
            extends AbstractAggregationReducerFactory {

        @Override
        public Reducer newReducer(Key key) {
            return new CountReducer();
        }

        @Override
        public int getId() {
            return AggregationsDataSerializerHook.COUNT_REDUCER_FACTORY;
        }
    }

    /**
     * Sum Combiner
     */
    private static final class CountCombiner
            extends Combiner {

        private long chunkCount;

        @Override
        public void combine(Object value) {
            chunkCount++;
        }

        @Override
        public Long finalizeChunk() {
            long value = chunkCount;
            chunkCount = 0;
            return value;
        }
    }

    /**
     * Count Reducer
     */
    private static final class CountReducer
            extends Reducer {

        private long count;

        @Override
        public void reduce(Long value) {
            count += value;
        }

        @Override
        public Long finalizeReduce() {
            return count;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy