This project has retired. For details please refer to its Attic page.
SumReduce xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.giraph.reducers.impl;
19  
20  import java.io.DataInput;
21  import java.io.DataOutput;
22  import java.io.IOException;
23  
24  import org.apache.giraph.reducers.ReduceSameTypeOperation;
25  import org.apache.giraph.types.ops.DoubleTypeOps;
26  import org.apache.giraph.types.ops.IntTypeOps;
27  import org.apache.giraph.types.ops.LongTypeOps;
28  import org.apache.giraph.types.ops.NumericTypeOps;
29  import org.apache.giraph.types.ops.TypeOpsUtils;
30  import org.apache.hadoop.io.DoubleWritable;
31  import org.apache.hadoop.io.IntWritable;
32  import org.apache.hadoop.io.LongWritable;
33  import org.apache.hadoop.io.Writable;
34  
35  /**
36   * Reducer for calculating sum of values
37   * @param <T> Value type
38   */
39  public class SumReduce<T extends Writable>
40      extends ReduceSameTypeOperation<T> {
41    /** DoubleWritable specialization */
42    public static final SumReduce<DoubleWritable> DOUBLE =
43        new SumReduce<>(DoubleTypeOps.INSTANCE);
44    /** LongWritable specialization */
45    public static final SumReduce<LongWritable> LONG =
46        new SumReduce<>(LongTypeOps.INSTANCE);
47    /** IntWritable specialization */
48    public static final SumReduce<IntWritable> INT =
49        new SumReduce<>(IntTypeOps.INSTANCE);
50  
51    /** Value type operations */
52    private NumericTypeOps<T> typeOps;
53  
54    /** Constructor used for deserialization only */
55    public SumReduce() {
56    }
57  
58    /**
59     * Constructor
60     * @param typeOps Value type operations
61     */
62    public SumReduce(NumericTypeOps<T> typeOps) {
63      this.typeOps = typeOps;
64    }
65  
66    @Override
67    public T createInitialValue() {
68      return typeOps.createZero();
69    }
70  
71    @Override
72    public T reduce(T curValue, T valueToReduce) {
73      typeOps.plusInto(curValue, valueToReduce);
74      return curValue;
75    }
76  
77    @Override
78    public void write(DataOutput out) throws IOException {
79      TypeOpsUtils.writeTypeOps(typeOps, out);
80    }
81  
82    @Override
83    public void readFields(DataInput in) throws IOException {
84      typeOps = TypeOpsUtils.readTypeOps(in);
85    }
86  }