This project has retired. For details please refer to its Attic page.
MutableEdgesIterable 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.edge;
20  
21  import org.apache.giraph.graph.Vertex;
22  import org.apache.hadoop.io.Writable;
23  import org.apache.hadoop.io.WritableComparable;
24  
25  import java.util.Iterator;
26  
27  /**
28   * Helper class to provide a mutable iterable over the edges when the chosen
29   * {@link OutEdges} doesn't offer a specialized one.
30   *
31   * @param <I> Vertex id
32   * @param <E> Edge value
33   */
34  public class MutableEdgesIterable<I extends WritableComparable,
35      E extends Writable> implements Iterable<MutableEdge<I, E>> {
36    /** Vertex that owns the out-edges. */
37    private Vertex<I, ?, E> vertex;
38  
39    /**
40     * Constructor.
41     *
42     * @param vertex Owning vertex
43     */
44    public MutableEdgesIterable(Vertex<I, ?, E> vertex) {
45      this.vertex = vertex;
46    }
47  
48    @Override
49    public Iterator<MutableEdge<I, E>> iterator() {
50      final MutableEdgesWrapper<I, E> mutableEdgesWrapper =
51          MutableEdgesWrapper.wrap((OutEdges<I, E>) vertex.getEdges(),
52              vertex.getConf());
53      vertex.setEdges(mutableEdgesWrapper);
54  
55      return new Iterator<MutableEdge<I, E>>() {
56        /** Iterator over the old edges. */
57        private Iterator<Edge<I, E>> oldEdgesIterator =
58            mutableEdgesWrapper.getOldEdgesIterator();
59        /** New edges data structure. */
60        private OutEdges<I, E> newEdges = mutableEdgesWrapper.getNewEdges();
61  
62        @Override
63        public boolean hasNext() {
64          // If the current edge is not null,
65          // we need to add it to the new edges.
66          Edge<I, E> currentEdge = mutableEdgesWrapper.getCurrentEdge();
67          if (currentEdge != null) {
68            newEdges.add(currentEdge);
69            mutableEdgesWrapper.setCurrentEdge(null);
70          }
71          if (!oldEdgesIterator.hasNext()) {
72            vertex.setEdges(newEdges);
73            return false;
74          } else {
75            return true;
76          }
77        }
78  
79        @Override
80        public MutableEdge<I, E> next() {
81          // If the current edge is not null,
82          // we need to add it to the new edges.
83          MutableEdge<I, E> currentEdge =
84              mutableEdgesWrapper.getCurrentEdge();
85          if (currentEdge != null) {
86            newEdges.add(currentEdge);
87          }
88          // Read the next edge and return it.
89          currentEdge = (MutableEdge<I, E>) oldEdgesIterator.next();
90          mutableEdgesWrapper.setCurrentEdge(currentEdge);
91          return currentEdge;
92        }
93  
94        @Override
95        public void remove() {
96          // Set the current edge to null, so that it's not added to the
97          // new edges.
98          mutableEdgesWrapper.setCurrentEdge(null);
99          mutableEdgesWrapper.decrementEdges();
100       }
101     };
102   }
103 }