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.function; 19 20 import org.apache.giraph.function.vertex.ConsumerWithVertex; 21 import org.apache.giraph.function.vertex.SupplierFromVertex; 22 import org.apache.giraph.graph.Vertex; 23 import org.apache.hadoop.io.Writable; 24 import org.apache.hadoop.io.WritableComparable; 25 26 /** 27 * Default object transfer, intermediary between producers and consumers. 28 * 29 * Holds value temporarily in memory, so can be used only when producer and 30 * consumer are in the same context. Nulls it out after supplying, so each 31 * object is returned only once, and second consecutive call to 'get' will 32 * return null. 33 * 34 * Useful for both: 35 * 36 * - passing data from vertexReceive function of WorkerReceivePiece of previous 37 * Piece to vertexSend function WorkerSendPiece of next Piece, of the same 38 * vertex. 39 * - when value is set on the master, and later read in block logic 40 * (RepeatUntilBlock), or in a different Piece, either on worker or master. 41 * If it is read within the same piece - just use local field. 42 * 43 * @param <T> Type of object to transfer. 44 */ 45 public class ObjectTransfer<T> implements Supplier<T>, Consumer<T> { 46 /** value */ 47 private T value; 48 49 /** 50 * Constructor 51 * @param value initial value 52 */ 53 public ObjectTransfer(T value) { 54 this.value = value; 55 } 56 57 /** Constructor */ 58 public ObjectTransfer() { 59 } 60 61 @Override 62 public T get() { 63 T result = value; 64 value = null; 65 return result; 66 } 67 68 @Override 69 public void apply(T value) { 70 this.value = value; 71 } 72 73 @Override 74 public String toString() { 75 return getClass() + " [value=" + value + "]"; 76 } 77 78 /** 79 * To be called when needing to pass it as a Supplier - making it 80 * obvious that K, V and E on supplier side can be any types, 81 * and to make code work without compile errors/warnings. 82 * 83 * In Java7, some callsites might need explicit types: 84 * object.<LongWritable, DoubleWritable, Writable>castToSupplier() 85 * In Java8, object.castToSupplier() is always going to be enough. 86 * 87 * @param <I> Vertex id type 88 * @param <V> Vertex value type 89 * @param <E> Edge value type 90 * @return supplier from vertex 91 */ 92 // TODO Java8: cleanup callers 93 @SuppressWarnings("rawtypes") 94 public <I extends WritableComparable, V extends Writable, E extends Writable> 95 SupplierFromVertex<I, V, E, T> castToSupplier() { 96 return new SupplierFromVertex<I, V, E, T>() { 97 @Override 98 public T get(Vertex<I, V, E> vertex) { 99 return ObjectTransfer.this.get(); 100 } 101 }; 102 } 103 104 /** 105 * To be called when needing to pass it as a Consumer - making it 106 * obvious that K, V and E on consumer side can be any types, 107 * and to make code work without compile errors/warnings. 108 * 109 * In Java7, some callsites might need explicit types: 110 * object.<LongWritable, DoubleWritable, Writable>castToConsumer() 111 * In Java8, object.castToConsumer() is always going to be enough. 112 * 113 * @param <I> Vertex id type 114 * @param <V> Vertex value type 115 * @param <E> Edge value type 116 * @return consumer with vertex 117 */ 118 // TODO Java8: cleanup callers 119 @SuppressWarnings("rawtypes") 120 public <I extends WritableComparable, V extends Writable, E extends Writable> 121 ConsumerWithVertex<I, V, E, T> castToConsumer() { 122 return new ConsumerWithVertex<I, V, E, T>() { 123 @Override 124 public void apply(Vertex<I, V, E> vertex, T value) { 125 ObjectTransfer.this.apply(value); 126 } 127 }; 128 } 129 }