1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
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.commons.lang3.tuple.Pair; |
25 | |
import org.apache.giraph.reducers.ReduceOperation; |
26 | |
import org.apache.giraph.utils.WritableUtils; |
27 | |
import org.apache.giraph.writable.tuple.PairWritable; |
28 | |
import org.apache.hadoop.io.Writable; |
29 | |
import com.google.common.base.Preconditions; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public class PairReduce<S1, R1 extends Writable, S2, R2 extends Writable> |
41 | |
implements ReduceOperation<Pair<S1, S2>, PairWritable<R1, R2>> { |
42 | |
|
43 | |
private ReduceOperation<S1, R1> reduce1; |
44 | |
|
45 | |
private ReduceOperation<S2, R2> reduce2; |
46 | |
|
47 | |
|
48 | 0 | public PairReduce() { |
49 | 0 | } |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public PairReduce( |
57 | 0 | ReduceOperation<S1, R1> reduce1, ReduceOperation<S2, R2> reduce2) { |
58 | 0 | this.reduce1 = reduce1; |
59 | 0 | this.reduce2 = reduce2; |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
@Override |
64 | |
public PairWritable<R1, R2> createInitialValue() { |
65 | 0 | return new PairWritable<>( |
66 | 0 | reduce1.createInitialValue(), reduce2.createInitialValue()); |
67 | |
} |
68 | |
|
69 | |
@Override |
70 | |
public PairWritable<R1, R2> reduce( |
71 | |
PairWritable<R1, R2> curValue, Pair<S1, S2> valueToReduce) { |
72 | 0 | Preconditions.checkState( |
73 | 0 | curValue.getLeft() == |
74 | 0 | reduce1.reduce(curValue.getLeft(), valueToReduce.getLeft())); |
75 | 0 | Preconditions.checkState( |
76 | 0 | curValue.getRight() == |
77 | 0 | reduce2.reduce(curValue.getRight(), valueToReduce.getRight())); |
78 | 0 | return curValue; |
79 | |
} |
80 | |
|
81 | |
@Override |
82 | |
public PairWritable<R1, R2> reduceMerge( |
83 | |
PairWritable<R1, R2> curValue, PairWritable<R1, R2> valueToReduce) { |
84 | 0 | Preconditions.checkState( |
85 | 0 | curValue.getLeft() == |
86 | 0 | reduce1.reduceMerge(curValue.getLeft(), valueToReduce.getLeft())); |
87 | 0 | Preconditions.checkState( |
88 | 0 | curValue.getRight() == |
89 | 0 | reduce2.reduceMerge(curValue.getRight(), valueToReduce.getRight())); |
90 | 0 | return curValue; |
91 | |
} |
92 | |
|
93 | |
@Override |
94 | |
public void write(DataOutput out) throws IOException { |
95 | 0 | WritableUtils.writeWritableObject(reduce1, out); |
96 | 0 | WritableUtils.writeWritableObject(reduce2, out); |
97 | 0 | } |
98 | |
|
99 | |
@Override |
100 | |
public void readFields(DataInput in) throws IOException { |
101 | 0 | reduce1 = WritableUtils.readWritableObject(in, null); |
102 | 0 | reduce2 = WritableUtils.readWritableObject(in, null); |
103 | 0 | } |
104 | |
} |