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.io;
2021import java.io.IOException;
22import org.apache.giraph.edge.Edge;
23import org.apache.giraph.edge.EdgeFactory;
24import org.apache.hadoop.io.Writable;
25import org.apache.hadoop.io.WritableComparable;
26import org.apache.hadoop.mapreduce.InputSplit;
27import org.apache.hadoop.mapreduce.TaskAttemptContext;
2829/**30 * An EdgeReader that creates the opposite direction edge for each edge read.31 * Used to create an undirected graph from a directed input.32 * This class is a decorator around any other EdgeReader.33 *34 * @param <I> Vertex id35 * @param <E> Edge Value36 */37publicclass ReverseEdgeDuplicator<I extends WritableComparable,
38 E extends Writable> extends EdgeReader<I, E> {
39/** The underlying EdgeReader to wrap */40privatefinal EdgeReader<I, E> baseReader;
4142/** Whether the reverse edge stored currently is valid */43privateboolean haveReverseEdge = true;
44/** Reverse of the edge last read */45private Edge<I, E> reverseEdge;
46/** Reverse source of last edge, in other words last edge's target */47private I reverseSourceId;
4849/**50 * Constructor51 * @param baseReader EdgeReader to wrap52 */53publicReverseEdgeDuplicator(EdgeReader<I, E> baseReader) {
54this.baseReader = baseReader;
55 }
5657/**58 * Get wrapped EdgeReader59 * @return EdgeReader60 */61public EdgeReader<I, E> getBaseReader() {
62return baseReader;
63 }
6465 @Override
66publicvoid initialize(InputSplit inputSplit, TaskAttemptContext context)
67throws IOException, InterruptedException {
68 baseReader.initialize(inputSplit, context);
69 haveReverseEdge = true;
70 }
7172 @Override
73publicboolean nextEdge() throws IOException, InterruptedException {
74boolean result = true;
75if (haveReverseEdge) {
76 result = baseReader.nextEdge();
77 haveReverseEdge = false;
78 } else {
79 Edge<I, E> currentEdge = baseReader.getCurrentEdge();
80 reverseSourceId = currentEdge.getTargetVertexId();
81 reverseEdge = EdgeFactory.create(baseReader.getCurrentSourceId(),
82 currentEdge.getValue());
83 haveReverseEdge = true;
84 }
85return result;
86 }
8788 @Override
89public I getCurrentSourceId() throws IOException, InterruptedException {
90if (haveReverseEdge) {
91return reverseSourceId;
92 } else {
93return baseReader.getCurrentSourceId();
94 }
95 }
9697 @Override
98public Edge<I, E> getCurrentEdge() throws IOException, InterruptedException {
99if (haveReverseEdge) {
100return reverseEdge;
101 } else {
102return baseReader.getCurrentEdge();
103 }
104 }
105106 @Override
107publicvoid close() throws IOException {
108 baseReader.close();
109 }
110111 @Override
112publicfloat getProgress() throws IOException, InterruptedException {
113return baseReader.getProgress();
114 }
115 }