1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.mapping.translate;
2021import org.apache.giraph.conf.DefaultImmutableClassesGiraphConfigurable;
22import org.apache.giraph.worker.BspServiceWorker;
23import org.apache.giraph.worker.LocalData;
24import org.apache.hadoop.io.ByteWritable;
25import org.apache.hadoop.io.DoubleWritable;
26import org.apache.hadoop.io.FloatWritable;
27import org.apache.hadoop.io.IntWritable;
28import org.apache.hadoop.io.LongWritable;
29import org.apache.hadoop.io.NullWritable;
30import org.apache.hadoop.io.Writable;
3132/**33 * Basic implementation of Translate Edge34 * where I = LongWritable & B = ByteWritable35 *36 * @param <E> edge value type37 */38 @SuppressWarnings("unchecked")
39publicclass LongByteTranslateEdge<E extends Writable>
40extendsDefaultImmutableClassesGiraphConfigurable41implements TranslateEdge<LongWritable, E> {
4243/** Local data used for targetId translation of edge */44private LocalData<LongWritable,
45 ? extends Writable, E, ByteWritable> localData;
4647 @Override
48publicvoid initialize(BspServiceWorker<LongWritable,
49 ? extends Writable, E> service) {
50 localData = (LocalData<LongWritable, ? extends Writable, E, ByteWritable>)
51 service.getLocalData();
52 }
5354 @Override
55public LongWritable translateId(LongWritable targetId) {
56 LongWritable translatedId = new LongWritable();
57 translatedId.set(targetId.get());
58 localData.getMappingStoreOps().embedTargetInfo(translatedId);
59return translatedId;
60 }
6162 @Override
63public E cloneValue(E edgeValue) {
64// If vertex input does not have create edges,65// then you can use LongByteTranslateEdge directly66thrownew UnsupportedOperationException();
67 }
6869/**70 * Correct implementation of cloneValue when edgevalue = nullwritable71 */72publicstaticclassNoEdgeValue73extends LongByteTranslateEdge<NullWritable> {
74 @Override
75public NullWritable cloneValue(NullWritable edgeValue) {
76return NullWritable.get();
77 }
78 }
7980/**81 * Correct implementation of cloneValue when edgevalue = intwritable82 */83publicstaticclassIntEdgeValue84extends LongByteTranslateEdge<IntWritable> {
85 @Override
86public IntWritable cloneValue(IntWritable edgeValue) {
87returnnew IntWritable(edgeValue.get());
88 }
89 }
9091/**92 * Correct implementation of cloneValue when edgevalue = longwritable93 */94publicstaticclassLongEdgeValue95extends LongByteTranslateEdge<LongWritable> {
96 @Override
97public LongWritable cloneValue(LongWritable edgeValue) {
98returnnew LongWritable(edgeValue.get());
99 }
100 }
101102/**103 * Correct implementation of cloneValue when edgevalue = floatwritable104 */105publicstaticclassFloatEdgeValue106extends LongByteTranslateEdge<FloatWritable> {
107 @Override
108public FloatWritable cloneValue(FloatWritable edgeValue) {
109returnnew FloatWritable(edgeValue.get());
110 }
111 }
112113/**114 * Correct implementation of cloneValue when edgevalue = doublewritable115 */116publicstaticclassDoubleEdgeValue117extends LongByteTranslateEdge<DoubleWritable> {
118 @Override
119public DoubleWritable cloneValue(DoubleWritable edgeValue) {
120returnnew DoubleWritable(edgeValue.get());
121 }
122 }
123 }