This project has retired. For details please refer to its Attic page.
HashMapEdges 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 com.google.common.collect.Maps;
22  
23  import org.apache.giraph.utils.EdgeIterables;
24  import org.apache.hadoop.io.Writable;
25  import org.apache.hadoop.io.WritableComparable;
26  
27  import java.io.DataInput;
28  import java.io.DataOutput;
29  import java.io.IOException;
30  import java.util.HashMap;
31  import java.util.Iterator;
32  import java.util.Map;
33  
34  /**
35   * {@link OutEdges} implementation backed by a {@link HashMap}.
36   * Parallel edges are not allowed.
37   * Note: this implementation is optimized for fast random access and mutations,
38   * but uses more space.
39   *
40   * @param <I> Vertex id
41   * @param <E> Edge value
42   */
43  public class HashMapEdges<I extends WritableComparable, E extends Writable>
44      extends ConfigurableOutEdges<I, E>
45      implements StrictRandomAccessOutEdges<I, E>,
46      MutableOutEdges<I, E> {
47    /** Map from target vertex id to edge value. */
48    private HashMap<I, E> edgeMap;
49  
50    @Override
51    public void initialize(Iterable<Edge<I, E>> edges) {
52      EdgeIterables.initialize(this, edges);
53    }
54  
55    @Override
56    public void initialize(int capacity) {
57      edgeMap = Maps.newHashMapWithExpectedSize(capacity);
58    }
59  
60    @Override
61    public void initialize() {
62      edgeMap = Maps.newHashMap();
63    }
64  
65    @Override
66    public void add(Edge<I, E> edge) {
67      edgeMap.put(edge.getTargetVertexId(), edge.getValue());
68    }
69  
70    @Override
71    public void remove(I targetVertexId) {
72      edgeMap.remove(targetVertexId);
73    }
74  
75    @Override
76    public E getEdgeValue(I targetVertexId) {
77      return edgeMap.get(targetVertexId);
78    }
79  
80    @Override
81    public void setEdgeValue(I targetVertexId, E edgeValue) {
82      if (edgeMap.containsKey(targetVertexId)) {
83        edgeMap.put(targetVertexId, edgeValue);
84      }
85    }
86  
87    @Override
88    public int size() {
89      return edgeMap.size();
90    }
91  
92    @Override
93    public Iterator<Edge<I, E>> iterator() {
94      // Returns an iterator that reuses objects.
95      // The downcast is fine because all concrete Edge implementations are
96      // mutable, but we only expose the mutation functionality when appropriate.
97      return (Iterator) mutableIterator();
98    }
99  
100   @Override
101   public Iterator<MutableEdge<I, E>> mutableIterator() {
102     return new Iterator<MutableEdge<I, E>>() {
103       /** Wrapped map iterator. */
104       private Iterator<Map.Entry<I, E>> mapIterator =
105           edgeMap.entrySet().iterator();
106       /** Representative edge object. */
107       private MapMutableEdge<I, E> representativeEdge =
108           new MapMutableEdge<I, E>();
109 
110       @Override
111       public boolean hasNext() {
112         return mapIterator.hasNext();
113       }
114 
115       @Override
116       public MutableEdge<I, E> next() {
117         representativeEdge.setEntry(mapIterator.next());
118         return representativeEdge;
119       }
120 
121       @Override
122       public void remove() {
123         mapIterator.remove();
124       }
125     };
126   }
127 
128   @Override
129   public void write(DataOutput out) throws IOException {
130     out.writeInt(edgeMap.size());
131     for (Map.Entry<I, E> entry : edgeMap.entrySet()) {
132       entry.getKey().write(out);
133       entry.getValue().write(out);
134     }
135   }
136 
137   @Override
138   public void readFields(DataInput in) throws IOException {
139     int numEdges = in.readInt();
140     initialize(numEdges);
141     for (int i = 0; i < numEdges; ++i) {
142       I targetVertexId = getConf().createVertexId();
143       targetVertexId.readFields(in);
144       E edgeValue = getConf().createEdgeValue();
145       edgeValue.readFields(in);
146       edgeMap.put(targetVertexId, edgeValue);
147     }
148   }
149 }