This project has retired. For details please refer to its Attic page.
MaxPairReducer 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.TypeOps;
26  import org.apache.giraph.types.ops.TypeOpsUtils;
27  import org.apache.giraph.writable.tuple.PairWritable;
28  import org.apache.hadoop.io.Writable;
29  import org.apache.hadoop.io.WritableComparable;
30  
31  
32  /**
33   * Aggregating PairWritable<L, R>, by taking pair with
34   * largest second value.
35   *
36   * @param <L> Type of the left value
37   * @param <R> Type of the right value
38   */
39  public class MaxPairReducer<L extends Writable, R extends WritableComparable>
40      extends ReduceSameTypeOperation<PairWritable<L, R>> {
41  
42    /** Left value TypeOps */
43    private TypeOps<L> leftTypeOps;
44    /** Right value TypeOps */
45    private TypeOps<R> rightTypeOps;
46  
47    /** Constructor used for deserialization only */
48    public MaxPairReducer() {
49    }
50  
51    /**
52     * Constructor
53     * @param leftTypeOps Left value TypeOps
54     * @param rightTypeOps Right value TypeOps
55     */
56    public MaxPairReducer(TypeOps<L> leftTypeOps, TypeOps<R> rightTypeOps) {
57      this.leftTypeOps = leftTypeOps;
58      this.rightTypeOps = rightTypeOps;
59    }
60  
61    @Override
62    public PairWritable<L, R> reduce(
63        PairWritable<L, R> curValue, PairWritable<L, R> valueToReduce) {
64      if (valueToReduce.getRight().compareTo(curValue.getRight()) > 0) {
65        leftTypeOps.set(curValue.getLeft(), valueToReduce.getLeft());
66        rightTypeOps.set(curValue.getRight(), valueToReduce.getRight());
67      }
68      return curValue;
69    }
70  
71    @Override
72    public PairWritable<L, R> createInitialValue() {
73      return new PairWritable<L, R>(
74          leftTypeOps.create(), rightTypeOps.create());
75    }
76  
77    @Override
78    public void write(DataOutput out) throws IOException {
79      TypeOpsUtils.writeTypeOps(leftTypeOps, out);
80      TypeOpsUtils.writeTypeOps(rightTypeOps, out);
81    }
82  
83    @Override
84    public void readFields(DataInput in) throws IOException {
85      leftTypeOps = TypeOpsUtils.readTypeOps(in);
86      rightTypeOps = TypeOpsUtils.readTypeOps(in);
87    }
88  }