1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
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 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class HashMapEdges<I extends WritableComparable, E extends Writable> |
44 | |
extends ConfigurableOutEdges<I, E> |
45 | |
implements StrictRandomAccessOutEdges<I, E>, |
46 | |
MutableOutEdges<I, E> { |
47 | |
|
48 | |
private HashMap<I, E> edgeMap; |
49 | |
|
50 | |
@Override |
51 | |
public void initialize(Iterable<Edge<I, E>> edges) { |
52 | 0 | EdgeIterables.initialize(this, edges); |
53 | 0 | } |
54 | |
|
55 | |
@Override |
56 | |
public void initialize(int capacity) { |
57 | 0 | edgeMap = Maps.newHashMapWithExpectedSize(capacity); |
58 | 0 | } |
59 | |
|
60 | |
@Override |
61 | |
public void initialize() { |
62 | 0 | edgeMap = Maps.newHashMap(); |
63 | 0 | } |
64 | |
|
65 | |
@Override |
66 | |
public void add(Edge<I, E> edge) { |
67 | 0 | edgeMap.put(edge.getTargetVertexId(), edge.getValue()); |
68 | 0 | } |
69 | |
|
70 | |
@Override |
71 | |
public void remove(I targetVertexId) { |
72 | 0 | edgeMap.remove(targetVertexId); |
73 | 0 | } |
74 | |
|
75 | |
@Override |
76 | |
public E getEdgeValue(I targetVertexId) { |
77 | 0 | return edgeMap.get(targetVertexId); |
78 | |
} |
79 | |
|
80 | |
@Override |
81 | |
public void setEdgeValue(I targetVertexId, E edgeValue) { |
82 | 0 | if (edgeMap.containsKey(targetVertexId)) { |
83 | 0 | edgeMap.put(targetVertexId, edgeValue); |
84 | |
} |
85 | 0 | } |
86 | |
|
87 | |
@Override |
88 | |
public int size() { |
89 | 0 | return edgeMap.size(); |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public Iterator<Edge<I, E>> iterator() { |
94 | |
|
95 | |
|
96 | |
|
97 | 0 | return (Iterator) mutableIterator(); |
98 | |
} |
99 | |
|
100 | |
@Override |
101 | |
public Iterator<MutableEdge<I, E>> mutableIterator() { |
102 | 0 | return new Iterator<MutableEdge<I, E>>() { |
103 | |
|
104 | 0 | private Iterator<Map.Entry<I, E>> mapIterator = |
105 | 0 | edgeMap.entrySet().iterator(); |
106 | |
|
107 | 0 | private MapMutableEdge<I, E> representativeEdge = |
108 | |
new MapMutableEdge<I, E>(); |
109 | |
|
110 | |
@Override |
111 | |
public boolean hasNext() { |
112 | 0 | return mapIterator.hasNext(); |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
public MutableEdge<I, E> next() { |
117 | 0 | representativeEdge.setEntry(mapIterator.next()); |
118 | 0 | return representativeEdge; |
119 | |
} |
120 | |
|
121 | |
@Override |
122 | |
public void remove() { |
123 | 0 | mapIterator.remove(); |
124 | 0 | } |
125 | |
}; |
126 | |
} |
127 | |
|
128 | |
@Override |
129 | |
public void write(DataOutput out) throws IOException { |
130 | 0 | out.writeInt(edgeMap.size()); |
131 | 0 | for (Map.Entry<I, E> entry : edgeMap.entrySet()) { |
132 | 0 | entry.getKey().write(out); |
133 | 0 | entry.getValue().write(out); |
134 | 0 | } |
135 | 0 | } |
136 | |
|
137 | |
@Override |
138 | |
public void readFields(DataInput in) throws IOException { |
139 | 0 | int numEdges = in.readInt(); |
140 | 0 | initialize(numEdges); |
141 | 0 | for (int i = 0; i < numEdges; ++i) { |
142 | 0 | I targetVertexId = getConf().createVertexId(); |
143 | 0 | targetVertexId.readFields(in); |
144 | 0 | E edgeValue = getConf().createEdgeValue(); |
145 | 0 | edgeValue.readFields(in); |
146 | 0 | edgeMap.put(targetVertexId, edgeValue); |
147 | |
} |
148 | 0 | } |
149 | |
} |