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 it.unimi.dsi.fastutil.longs.Long2DoubleMap; |
22 | |
import it.unimi.dsi.fastutil.longs.Long2DoubleOpenHashMap; |
23 | |
import it.unimi.dsi.fastutil.objects.ObjectIterator; |
24 | |
|
25 | |
import java.io.DataInput; |
26 | |
import java.io.DataOutput; |
27 | |
import java.io.IOException; |
28 | |
import java.util.Iterator; |
29 | |
|
30 | |
import org.apache.giraph.utils.EdgeIterables; |
31 | |
import org.apache.giraph.utils.Trimmable; |
32 | |
import org.apache.hadoop.io.DoubleWritable; |
33 | |
import org.apache.hadoop.io.LongWritable; |
34 | |
|
35 | |
import com.google.common.collect.UnmodifiableIterator; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class LongDoubleHashMapEdges |
46 | |
implements StrictRandomAccessOutEdges<LongWritable, DoubleWritable>, |
47 | |
ReuseObjectsOutEdges<LongWritable, DoubleWritable>, |
48 | |
MutableOutEdges<LongWritable, DoubleWritable>, Trimmable { |
49 | |
|
50 | |
private Long2DoubleOpenHashMap edgeMap; |
51 | |
|
52 | |
private DoubleWritable representativeEdgeValue; |
53 | |
|
54 | |
@Override |
55 | |
public void initialize(Iterable<Edge<LongWritable, DoubleWritable>> edges) { |
56 | 0 | EdgeIterables.initialize(this, edges); |
57 | 0 | } |
58 | |
|
59 | |
@Override |
60 | |
public void initialize(int capacity) { |
61 | 0 | edgeMap = new Long2DoubleOpenHashMap(capacity); |
62 | 0 | } |
63 | |
|
64 | |
@Override |
65 | |
public void initialize() { |
66 | 0 | edgeMap = new Long2DoubleOpenHashMap(); |
67 | 0 | } |
68 | |
|
69 | |
@Override |
70 | |
public void add(Edge<LongWritable, DoubleWritable> edge) { |
71 | 0 | edgeMap.put(edge.getTargetVertexId().get(), edge.getValue().get()); |
72 | 0 | } |
73 | |
|
74 | |
@Override |
75 | |
public void remove(LongWritable targetVertexId) { |
76 | 0 | edgeMap.remove(targetVertexId.get()); |
77 | 0 | } |
78 | |
|
79 | |
@Override |
80 | |
public DoubleWritable getEdgeValue(LongWritable targetVertexId) { |
81 | 0 | if (!edgeMap.containsKey(targetVertexId.get())) { |
82 | 0 | return null; |
83 | |
} |
84 | 0 | if (representativeEdgeValue == null) { |
85 | 0 | representativeEdgeValue = new DoubleWritable(); |
86 | |
} |
87 | 0 | representativeEdgeValue.set(edgeMap.get(targetVertexId.get())); |
88 | 0 | return representativeEdgeValue; |
89 | |
} |
90 | |
|
91 | |
@Override |
92 | |
public void setEdgeValue(LongWritable targetVertexId, |
93 | |
DoubleWritable edgeValue) { |
94 | 0 | if (edgeMap.containsKey(targetVertexId.get())) { |
95 | 0 | edgeMap.put(targetVertexId.get(), edgeValue.get()); |
96 | |
} |
97 | 0 | } |
98 | |
|
99 | |
@Override |
100 | |
public int size() { |
101 | 0 | return edgeMap.size(); |
102 | |
} |
103 | |
|
104 | |
@Override |
105 | |
public Iterator<Edge<LongWritable, DoubleWritable>> iterator() { |
106 | |
|
107 | 0 | return new UnmodifiableIterator<Edge<LongWritable, DoubleWritable>>() { |
108 | |
|
109 | 0 | private final ObjectIterator<Long2DoubleMap.Entry> mapIterator = |
110 | 0 | edgeMap.long2DoubleEntrySet().fastIterator(); |
111 | |
|
112 | 0 | private final ReusableEdge<LongWritable, DoubleWritable> |
113 | |
representativeEdge = |
114 | 0 | EdgeFactory.createReusable(new LongWritable(), new DoubleWritable()); |
115 | |
|
116 | |
@Override |
117 | |
public boolean hasNext() { |
118 | 0 | return mapIterator.hasNext(); |
119 | |
} |
120 | |
|
121 | |
@Override |
122 | |
public Edge<LongWritable, DoubleWritable> next() { |
123 | 0 | Long2DoubleMap.Entry nextEntry = mapIterator.next(); |
124 | 0 | representativeEdge.getTargetVertexId().set(nextEntry.getLongKey()); |
125 | 0 | representativeEdge.getValue().set(nextEntry.getDoubleValue()); |
126 | 0 | return representativeEdge; |
127 | |
} |
128 | |
}; |
129 | |
} |
130 | |
|
131 | |
@Override |
132 | |
public void trim() { |
133 | 0 | edgeMap.trim(); |
134 | 0 | } |
135 | |
|
136 | |
|
137 | 0 | private static class LongDoubleHashMapMutableEdge |
138 | |
extends DefaultEdge<LongWritable, DoubleWritable> { |
139 | |
|
140 | |
private Long2DoubleMap.Entry entry; |
141 | |
|
142 | |
|
143 | |
public LongDoubleHashMapMutableEdge() { |
144 | 0 | super(new LongWritable(), new DoubleWritable()); |
145 | 0 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public void setEntry(Long2DoubleMap.Entry entry) { |
153 | |
|
154 | 0 | getTargetVertexId().set(entry.getLongKey()); |
155 | 0 | getValue().set(entry.getDoubleValue()); |
156 | |
|
157 | 0 | this.entry = entry; |
158 | 0 | } |
159 | |
|
160 | |
@Override |
161 | |
public void setValue(DoubleWritable value) { |
162 | |
|
163 | 0 | getValue().set(value.get()); |
164 | |
|
165 | 0 | entry.setValue(value.get()); |
166 | 0 | } |
167 | |
} |
168 | |
|
169 | |
@Override |
170 | |
public Iterator<MutableEdge<LongWritable, DoubleWritable>> mutableIterator() { |
171 | 0 | return new Iterator<MutableEdge<LongWritable, DoubleWritable>>() { |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | 0 | private final ObjectIterator<Long2DoubleMap.Entry> mapIterator = |
178 | 0 | edgeMap.long2DoubleEntrySet().iterator(); |
179 | |
|
180 | 0 | private final LongDoubleHashMapMutableEdge representativeEdge = |
181 | |
new LongDoubleHashMapMutableEdge(); |
182 | |
|
183 | |
@Override |
184 | |
public boolean hasNext() { |
185 | 0 | return mapIterator.hasNext(); |
186 | |
} |
187 | |
|
188 | |
@Override |
189 | |
public MutableEdge<LongWritable, DoubleWritable> next() { |
190 | 0 | representativeEdge.setEntry(mapIterator.next()); |
191 | 0 | return representativeEdge; |
192 | |
} |
193 | |
|
194 | |
@Override |
195 | |
public void remove() { |
196 | 0 | mapIterator.remove(); |
197 | 0 | } |
198 | |
}; |
199 | |
} |
200 | |
|
201 | |
@Override |
202 | |
public void write(DataOutput out) throws IOException { |
203 | 0 | out.writeInt(edgeMap.size()); |
204 | 0 | for (Long2DoubleMap.Entry entry : edgeMap.long2DoubleEntrySet()) { |
205 | 0 | out.writeLong(entry.getLongKey()); |
206 | 0 | out.writeDouble(entry.getDoubleValue()); |
207 | 0 | } |
208 | 0 | } |
209 | |
|
210 | |
@Override |
211 | |
public void readFields(DataInput in) throws IOException { |
212 | 0 | int numEdges = in.readInt(); |
213 | 0 | initialize(numEdges); |
214 | 0 | for (int i = 0; i < numEdges; ++i) { |
215 | 0 | edgeMap.put(in.readLong(), in.readDouble()); |
216 | |
} |
217 | 0 | } |
218 | |
} |