This project has retired. For details please refer to its Attic page.
DefaultVertex 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  package org.apache.giraph.graph;
19  
20  import org.apache.giraph.conf.DefaultImmutableClassesGiraphConfigurable;
21  import org.apache.giraph.edge.Edge;
22  import org.apache.giraph.utils.Trimmable;
23  import org.apache.giraph.edge.MultiRandomAccessOutEdges;
24  import org.apache.giraph.edge.MutableEdge;
25  import org.apache.giraph.edge.MutableEdgesIterable;
26  import org.apache.giraph.edge.MutableEdgesWrapper;
27  import org.apache.giraph.edge.MutableOutEdges;
28  import org.apache.giraph.edge.OutEdges;
29  import org.apache.giraph.edge.StrictRandomAccessOutEdges;
30  import org.apache.hadoop.io.Writable;
31  import org.apache.hadoop.io.WritableComparable;
32  
33  import com.google.common.collect.UnmodifiableIterator;
34  
35  import java.util.Iterator;
36  
37  /**
38   * Class which holds vertex id, data and edges.
39   *
40   * @param <I> Vertex id
41   * @param <V> Vertex data
42   * @param <E> Edge data
43   */
44  public class DefaultVertex<I extends WritableComparable,
45      V extends Writable, E extends Writable>
46      extends DefaultImmutableClassesGiraphConfigurable<I, V, E>
47      implements Vertex<I, V, E>, Trimmable {
48    /** Vertex id. */
49    private I id;
50    /** Vertex value. */
51    private V value;
52    /** Outgoing edges. */
53    private OutEdges<I, E> edges;
54    /** If true, do not do anymore computation on this vertex. */
55    private boolean halt;
56  
57    @Override
58    public void initialize(I id, V value, Iterable<Edge<I, E>> edges) {
59      this.id = id;
60      this.value = value;
61      setEdges(edges);
62    }
63  
64    @Override
65    public void initialize(I id, V value) {
66      this.id = id;
67      this.value = value;
68      this.edges = getConf().createAndInitializeOutEdges(0);
69    }
70  
71    @Override
72    public void setEdges(Iterable<Edge<I, E>> edges) {
73      // If the iterable is actually an instance of OutEdges,
74      // we simply take the reference.
75      // Otherwise, we initialize a new OutEdges.
76      if (edges instanceof OutEdges) {
77        this.edges = (OutEdges<I, E>) edges;
78      } else {
79        this.edges = getConf().createAndInitializeOutEdges(edges);
80      }
81    }
82  
83    @Override
84    public I getId() {
85      return id;
86    }
87  
88    @Override
89    public V getValue() {
90      return value;
91    }
92  
93    @Override
94    public void setValue(V value) {
95      this.value = value;
96    }
97  
98    @Override
99    public Iterable<Edge<I, E>> getEdges() {
100     return edges;
101   }
102 
103   @Override
104   public Iterable<MutableEdge<I, E>> getMutableEdges() {
105     // If the OutEdges implementation has a specialized mutable iterator,
106     // we use that; otherwise, we build a new data structure as we iterate
107     // over the current edges.
108     if (edges instanceof MutableOutEdges) {
109       return new Iterable<MutableEdge<I, E>>() {
110         @Override
111         public Iterator<MutableEdge<I, E>> iterator() {
112           return ((MutableOutEdges<I, E>) edges).mutableIterator();
113         }
114       };
115     } else {
116       return new MutableEdgesIterable<I, E>(this);
117     }
118   }
119 
120   @Override
121   public void unwrapMutableEdges() {
122     if (edges instanceof MutableEdgesWrapper) {
123       edges = ((MutableEdgesWrapper<I, E>) edges).unwrap();
124     }
125   }
126 
127   @Override
128   public int getNumEdges() {
129     return edges.size();
130   }
131 
132   @Override
133   public E getEdgeValue(I targetVertexId) {
134     // If the OutEdges implementation has a specialized random-access
135     // method, we use that; otherwise, we scan the edges.
136     if (edges instanceof StrictRandomAccessOutEdges) {
137       return ((StrictRandomAccessOutEdges<I, E>) edges)
138           .getEdgeValue(targetVertexId);
139     } else {
140       for (Edge<I, E> edge : edges) {
141         if (edge.getTargetVertexId().equals(targetVertexId)) {
142           return edge.getValue();
143         }
144       }
145       return null;
146     }
147   }
148 
149   @Override
150   public void setEdgeValue(I targetVertexId, E edgeValue) {
151     // If the OutEdges implementation has a specialized random-access
152     // method, we use that; otherwise, we scan the edges.
153     if (edges instanceof StrictRandomAccessOutEdges) {
154       ((StrictRandomAccessOutEdges<I, E>) edges).setEdgeValue(
155           targetVertexId, edgeValue);
156     } else {
157       for (MutableEdge<I, E> edge : getMutableEdges()) {
158         if (edge.getTargetVertexId().equals(targetVertexId)) {
159           edge.setValue(edgeValue);
160         }
161       }
162     }
163   }
164 
165   @Override
166   public Iterable<E> getAllEdgeValues(final I targetVertexId) {
167     // If the OutEdges implementation has a specialized random-access
168     // method, we use that; otherwise, we scan the edges.
169     if (edges instanceof MultiRandomAccessOutEdges) {
170       return ((MultiRandomAccessOutEdges<I, E>) edges)
171           .getAllEdgeValues(targetVertexId);
172     } else {
173       return new Iterable<E>() {
174         @Override
175         public Iterator<E> iterator() {
176           return new UnmodifiableIterator<E>() {
177             /** Iterator over all edges. */
178             private Iterator<Edge<I, E>> edgeIterator = edges.iterator();
179             /** Last matching edge found. */
180             private Edge<I, E> currentEdge;
181 
182             @Override
183             public boolean hasNext() {
184               while (edgeIterator.hasNext()) {
185                 currentEdge = edgeIterator.next();
186                 if (currentEdge.getTargetVertexId().equals(targetVertexId)) {
187                   return true;
188                 }
189               }
190               return false;
191             }
192 
193             @Override
194             public E next() {
195               return currentEdge.getValue();
196             }
197           };
198         }
199       };
200     }
201   }
202 
203   @Override
204   public void voteToHalt() {
205     halt = true;
206   }
207 
208   @Override
209   public void wakeUp() {
210     halt = false;
211   }
212 
213   @Override
214   public boolean isHalted() {
215     return halt;
216   }
217 
218   @Override
219   public void trim() {
220     if (edges instanceof Trimmable) {
221       ((Trimmable) edges).trim();
222     }
223   }
224 
225   @Override
226   public void addEdge(Edge<I, E> edge) {
227     edges.add(edge);
228   }
229 
230   @Override
231   public void removeEdges(I targetVertexId) {
232     edges.remove(targetVertexId);
233   }
234 
235   @Override
236   public String toString() {
237     return "Vertex(id=" + getId() + ",value=" + getValue() +
238         ",#edges=" + getNumEdges() + ")";
239   }
240 }