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;
19
20 import org.apache.hadoop.io.Writable;
21
22 /**
23 * Reduce operations defining how to reduce single values
24 * passed on workers, into partial values on workers, and then
25 * into a single global reduced value.
26 *
27 * Object should be thread safe. Most frequently it should be
28 * immutable object, so that functions can execute concurrently.
29 * Rarely when object is mutable
30 * ({@link org.apache.giraph.master.AggregatorReduceOperation}),
31 * i.e. stores reusable object inside, accesses should be synchronized.
32 *
33 * @param <S> Single value type, objects passed on workers
34 * @param <R> Reduced value type
35 */
36 public interface ReduceOperation<S, R extends Writable> extends Writable {
37 /**
38 * Return new reduced value which is neutral to reduce operation.
39 *
40 * @return Neutral value
41 */
42 R createInitialValue();
43 /**
44 * Add a new value.
45 * Needs to be commutative and associative
46 *
47 * Commonly, returned value should be same as curValue argument.
48 *
49 * @param curValue Partial value into which to reduce and store the result
50 * @param valueToReduce Single value to be reduced
51 * @return reduced value
52 */
53 R reduce(R curValue, S valueToReduce);
54 /**
55 * Add partially reduced value to current partially reduced value.
56 *
57 * Commonly, returned value should be same as curValue argument.
58 *
59 * @param curValue Partial value into which to reduce and store the result
60 * @param valueToReduce Partial value to be reduced
61 * @return reduced value
62 */
63 R reduceMerge(R curValue, R valueToReduce);
64 }