This project has retired. For details please refer to its Attic page.
MaxMessageCombiner 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.combiner;
19  
20  import org.apache.giraph.types.ops.DoubleTypeOps;
21  import org.apache.giraph.types.ops.FloatTypeOps;
22  import org.apache.giraph.types.ops.IntTypeOps;
23  import org.apache.giraph.types.ops.LongTypeOps;
24  import org.apache.giraph.types.ops.NumericTypeOps;
25  import org.apache.hadoop.io.DoubleWritable;
26  import org.apache.hadoop.io.FloatWritable;
27  import org.apache.hadoop.io.IntWritable;
28  import org.apache.hadoop.io.LongWritable;
29  import org.apache.hadoop.io.WritableComparable;
30  
31  /**
32   * Message combiner which calculates max of all messages.
33   *
34   * @param <M> Message type
35   */
36  public class MaxMessageCombiner<M extends WritableComparable>
37      implements MessageCombiner<WritableComparable, M> {
38    /** DoubleWritable specialization */
39    public static final MaxMessageCombiner<DoubleWritable> DOUBLE =
40        new MaxMessageCombiner<>(DoubleTypeOps.INSTANCE);
41    /** DoubleWritable specialization */
42    public static final MaxMessageCombiner<FloatWritable> FLOAT =
43        new MaxMessageCombiner<>(FloatTypeOps.INSTANCE);
44    /** LongWritable specialization */
45    public static final MaxMessageCombiner<LongWritable> LONG =
46        new MaxMessageCombiner<>(LongTypeOps.INSTANCE);
47    /** IntWritable specialization */
48    public static final MaxMessageCombiner<IntWritable> INT =
49        new MaxMessageCombiner<>(IntTypeOps.INSTANCE);
50  
51    /** Value type operations */
52    private final NumericTypeOps<M> typeOps;
53  
54    /**
55     * Constructor
56     * @param typeOps Value type operations
57     */
58    public MaxMessageCombiner(NumericTypeOps<M> typeOps) {
59      this.typeOps = typeOps;
60    }
61  
62    @Override
63    public void combine(
64        WritableComparable vertexIndex, M originalMessage, M messageToCombine) {
65      if (originalMessage.compareTo(messageToCombine) < 0) {
66        typeOps.set(originalMessage, messageToCombine);
67      }
68    }
69  
70    @Override
71    public M createInitialMessage() {
72      return typeOps.createZero();
73    }
74  }