This project has retired. For details please refer to its Attic page.
MutableEdgesWrapper 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.conf.ImmutableClassesGiraphConfiguration;
22  import org.apache.hadoop.io.Writable;
23  import org.apache.hadoop.io.WritableComparable;
24  
25  import java.io.DataInput;
26  import java.io.DataOutput;
27  import java.io.IOException;
28  import java.util.Iterator;
29  
30  /**
31   * Helper class that wraps the current out-edges and inserts them into a new
32   * data structure as they are iterated over.
33   * Used by Vertex to provide a mutable iterator when the chosen
34   * {@link OutEdges} doesn't offer a specialized one.
35   * The edges are "unwrapped" back to the chosen {@link OutEdges} data
36   * structure as soon as possible: either when the iterator is exhausted,
37   * or after compute() if iteration has been terminated early.
38   *
39   * @param <I> Vertex id
40   * @param <E> Edge value
41   */
42  public class MutableEdgesWrapper<I extends WritableComparable,
43      E extends Writable> implements OutEdges<I, E> {
44    /** New edges data structure (initially empty). */
45    private final OutEdges<I, E> newEdges;
46    /** Iterator over the old edges. */
47    private final Iterator<Edge<I, E>> oldEdgesIterator;
48    /** Last edge that was returned during iteration. */
49    private MutableEdge<I, E> currentEdge;
50    /** Number of edges. */
51    private int numEdges;
52  
53    /**
54     * Private constructor: instantiation happens through the {@code wrap()}
55     * factory method.
56     *
57     * @param oldEdges Current out-edges
58     * @param newEdges New (empty) edges data structure
59     */
60    private MutableEdgesWrapper(OutEdges<I, E> oldEdges,
61                                OutEdges<I, E> newEdges) {
62      oldEdgesIterator = oldEdges.iterator();
63      this.newEdges = newEdges;
64      numEdges = oldEdges.size();
65    }
66  
67    /**
68     * Factory method to create a new wrapper over the existing out-edges.
69     *
70     * @param edges Current out-edges
71     * @param conf Configuration
72     * @param <I> Vertex id
73     * @param <E> Edge value
74     * @return The wrapped edges
75     */
76    public static <I extends WritableComparable, E extends Writable>
77    MutableEdgesWrapper<I, E> wrap(
78        OutEdges<I, E> edges,
79        ImmutableClassesGiraphConfiguration<I, ?, E> conf) {
80      MutableEdgesWrapper<I, E> wrapper = new MutableEdgesWrapper<I, E>(
81          edges, conf.createAndInitializeOutEdges(edges.size()));
82      return wrapper;
83    }
84  
85    /**
86     * Moves all the remaining edges to the new data structure, and returns it.
87     *
88     * @return The new {@link OutEdges} data structure.
89     */
90    public OutEdges<I, E> unwrap() {
91      if (currentEdge != null) {
92        newEdges.add(currentEdge);
93        currentEdge = null;
94      }
95      while (oldEdgesIterator.hasNext()) {
96        newEdges.add(oldEdgesIterator.next());
97      }
98      return newEdges;
99    }
100 
101   /**
102    * Get the new {@link OutEdges} data structure.
103    *
104    * @return New edges
105    */
106   public OutEdges<I, E> getNewEdges() {
107     return newEdges;
108   }
109 
110   /**
111    * Get the iterator over the old edges data structure.
112    *
113    * @return Old edges iterator
114    */
115   public Iterator<Edge<I, E>> getOldEdgesIterator() {
116     return oldEdgesIterator;
117   }
118 
119   /**
120    * Get the last edge returned by the mutable iterator.
121    *
122    * @return Last edge iterated on
123    */
124   public MutableEdge<I, E> getCurrentEdge() {
125     return currentEdge;
126   }
127 
128   /**
129    * Set the last edge returned by the mutable iterator.
130    *
131    * @param edge Last edge iterated on
132    */
133   public void setCurrentEdge(MutableEdge<I, E> edge) {
134     currentEdge = edge;
135   }
136 
137   /**
138    * Decrement the number of edges (to account for a deletion from the mutable
139    * iterator).
140    */
141   public void decrementEdges() {
142     --numEdges;
143   }
144 
145   @Override
146   public void initialize(Iterable<Edge<I, E>> edges) {
147     throw new IllegalStateException("initialize: MutableEdgesWrapper should " +
148         "never be initialized.");
149   }
150 
151   @Override
152   public void initialize(int capacity) {
153     throw new IllegalStateException("initialize: MutableEdgesWrapper should " +
154         "never be initialized.");
155   }
156 
157   @Override
158   public void initialize() {
159     throw new IllegalStateException("initialize: MutableEdgesWrapper should " +
160         "never be initialized.");
161   }
162 
163   @Override
164   public void add(Edge<I, E> edge) {
165     unwrap().add(edge);
166   }
167 
168   @Override
169   public void remove(I targetVertexId) {
170     unwrap().remove(targetVertexId);
171   }
172 
173   @Override
174   public int size() {
175     return numEdges;
176   }
177 
178   @Override
179   public Iterator<Edge<I, E>> iterator() {
180     return unwrap().iterator();
181   }
182 
183   @Override
184   public void write(DataOutput out) throws IOException {
185     throw new IllegalStateException("write: MutableEdgesWrapper should " +
186         "never be serialized.");
187   }
188 
189   @Override
190   public void readFields(DataInput in) throws IOException {
191     throw new IllegalStateException("readFields: MutableEdgesWrapper should " +
192         "never be deserialized.");
193   }
194 }