This project has retired. For details please refer to its Attic page.
EdgeReaderWrapper xref
View Javadoc

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  
19  package org.apache.giraph.io.iterables;
20  
21  import java.io.IOException;
22  
23  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
24  import org.apache.giraph.edge.Edge;
25  import org.apache.giraph.io.EdgeReader;
26  import org.apache.hadoop.io.Writable;
27  import org.apache.hadoop.io.WritableComparable;
28  import org.apache.hadoop.mapreduce.InputSplit;
29  import org.apache.hadoop.mapreduce.TaskAttemptContext;
30  
31  /**
32   * Wraps {@link GiraphReader} for edges into {@link EdgeReader}
33   *
34   * @param <I> Vertex id
35   * @param <E> Edge data
36   */
37  public class EdgeReaderWrapper<I extends WritableComparable,
38      E extends Writable> extends EdgeReader<I, E> {
39    /** Wrapped edge reader */
40    private GiraphReader<EdgeWithSource<I, E>> edgeReader;
41    /** {@link EdgeReader}-like wrapper of {@link #edgeReader} */
42    private IteratorToReaderWrapper<EdgeWithSource<I, E>> iterator;
43  
44    /**
45     * Constructor
46     *
47     * @param edgeReader GiraphReader for edges to wrap
48     */
49    public EdgeReaderWrapper(GiraphReader<EdgeWithSource<I, E>> edgeReader) {
50      this.edgeReader = edgeReader;
51      iterator = new IteratorToReaderWrapper<EdgeWithSource<I, E>>(edgeReader);
52    }
53  
54    @Override
55    public void setConf(
56        ImmutableClassesGiraphConfiguration<I, Writable, E> conf) {
57      super.setConf(conf);
58      conf.configureIfPossible(edgeReader);
59    }
60  
61    @Override
62    public boolean nextEdge() throws IOException, InterruptedException {
63      return iterator.nextObject();
64    }
65  
66    @Override
67    public I getCurrentSourceId() throws IOException, InterruptedException {
68      return iterator.getCurrentObject().getSourceVertexId();
69    }
70  
71    @Override
72    public Edge<I, E> getCurrentEdge() throws IOException, InterruptedException {
73      return iterator.getCurrentObject().getEdge();
74    }
75  
76    @Override
77    public void initialize(InputSplit inputSplit,
78        TaskAttemptContext context) throws IOException, InterruptedException {
79      edgeReader.initialize(inputSplit, context);
80    }
81  
82    @Override
83    public void close() throws IOException {
84      edgeReader.close();
85    }
86  
87    @Override
88    public float getProgress() throws IOException, InterruptedException {
89      return edgeReader.getProgress();
90    }
91  }