1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.giraph.types.ops.collections; |
19 | |
|
20 | |
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; |
21 | |
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; |
22 | |
import it.unimi.dsi.fastutil.ints.IntIterator; |
23 | |
import it.unimi.dsi.fastutil.longs.Long2ObjectMap; |
24 | |
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; |
25 | |
import it.unimi.dsi.fastutil.longs.LongIterator; |
26 | |
import it.unimi.dsi.fastutil.objects.Object2ObjectMap; |
27 | |
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; |
28 | |
import it.unimi.dsi.fastutil.objects.ObjectIterator; |
29 | |
|
30 | |
import java.io.DataInput; |
31 | |
import java.io.DataOutput; |
32 | |
import java.io.IOException; |
33 | |
import java.util.Collection; |
34 | |
import java.util.Iterator; |
35 | |
|
36 | |
import org.apache.giraph.types.ops.IntTypeOps; |
37 | |
import org.apache.giraph.types.ops.LongTypeOps; |
38 | |
import org.apache.giraph.types.ops.PrimitiveIdTypeOps; |
39 | |
import org.apache.giraph.utils.WritableUtils; |
40 | |
import org.apache.hadoop.io.IntWritable; |
41 | |
import org.apache.hadoop.io.LongWritable; |
42 | |
import org.apache.hadoop.io.Writable; |
43 | |
|
44 | |
import com.google.common.base.Preconditions; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | public abstract class Basic2ObjectMap<K, V> implements Writable { |
55 | |
|
56 | |
public abstract void clear(); |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public abstract int size(); |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public abstract boolean containsKey(K key); |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public abstract V put(K key, V value); |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public abstract V get(K key); |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public abstract V remove(K key); |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public abstract PrimitiveIdTypeOps<K> getKeyTypeOps(); |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public abstract Iterator<K> fastKeyIterator(); |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public abstract Iterator<V> valueIterator(); |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public abstract Collection<V> values(); |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
protected abstract class ReusableIterator<Iter extends Iterator<?>> |
126 | |
implements ResettableIterator<K> { |
127 | |
|
128 | |
protected Iter iter; |
129 | |
|
130 | 0 | protected final K reusableKey = getKeyTypeOps().create(); |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | 0 | public ReusableIterator(Iter iter) { |
137 | 0 | this.iter = iter; |
138 | 0 | } |
139 | |
|
140 | |
@Override |
141 | |
public boolean hasNext() { |
142 | 0 | return iter.hasNext(); |
143 | |
} |
144 | |
|
145 | |
@Override |
146 | |
public void remove() { |
147 | 0 | iter.remove(); |
148 | 0 | } |
149 | |
} |
150 | |
|
151 | |
|
152 | 0 | public static final class BasicInt2ObjectOpenHashMap<V> |
153 | |
extends Basic2ObjectMap<IntWritable, V> { |
154 | |
|
155 | |
private final Int2ObjectOpenHashMap<V> map; |
156 | |
|
157 | |
private final WritableWriter<V> valueWriter; |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | 0 | public BasicInt2ObjectOpenHashMap(WritableWriter<V> valueWriter) { |
165 | 0 | this.map = new Int2ObjectOpenHashMap<>(); |
166 | 0 | this.valueWriter = valueWriter; |
167 | 0 | } |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public BasicInt2ObjectOpenHashMap( |
176 | 0 | int capacity, WritableWriter<V> valueWriter) { |
177 | 0 | this.map = new Int2ObjectOpenHashMap<>(capacity); |
178 | 0 | this.valueWriter = valueWriter; |
179 | 0 | } |
180 | |
|
181 | |
@Override |
182 | |
public void clear() { |
183 | 0 | map.clear(); |
184 | 0 | } |
185 | |
|
186 | |
@Override |
187 | |
public int size() { |
188 | 0 | return map.size(); |
189 | |
} |
190 | |
|
191 | |
@Override |
192 | |
public boolean containsKey(IntWritable key) { |
193 | 0 | return map.containsKey(key.get()); |
194 | |
} |
195 | |
|
196 | |
@Override |
197 | |
public V put(IntWritable key, V value) { |
198 | 0 | return map.put(key.get(), value); |
199 | |
} |
200 | |
|
201 | |
@Override |
202 | |
public V get(IntWritable key) { |
203 | 0 | return map.get(key.get()); |
204 | |
} |
205 | |
|
206 | |
@Override |
207 | |
public V remove(IntWritable key) { |
208 | 0 | return map.remove(key.get()); |
209 | |
} |
210 | |
|
211 | |
@Override |
212 | |
public PrimitiveIdTypeOps<IntWritable> getKeyTypeOps() { |
213 | 0 | return IntTypeOps.INSTANCE; |
214 | |
} |
215 | |
|
216 | |
@Override |
217 | |
public Iterator<IntWritable> fastKeyIterator() { |
218 | 0 | return new ReusableIterator<IntIterator>(map.keySet().iterator()) { |
219 | |
@Override |
220 | |
public IntWritable next() { |
221 | 0 | reusableKey.set(iter.nextInt()); |
222 | 0 | return reusableKey; |
223 | |
} |
224 | |
|
225 | |
@Override |
226 | |
public void reset() { |
227 | 0 | iter = map.keySet().iterator(); |
228 | 0 | } |
229 | |
}; |
230 | |
} |
231 | |
|
232 | |
@Override |
233 | |
public Iterator<V> valueIterator() { |
234 | 0 | return map.values().iterator(); |
235 | |
} |
236 | |
|
237 | |
@Override |
238 | |
public Collection<V> values() { |
239 | 0 | return map.values(); |
240 | |
} |
241 | |
|
242 | |
@Override |
243 | |
public void write(DataOutput out) throws IOException { |
244 | 0 | out.writeInt(map.size()); |
245 | 0 | ObjectIterator<Int2ObjectMap.Entry<V>> iterator = |
246 | 0 | map.int2ObjectEntrySet().fastIterator(); |
247 | 0 | while (iterator.hasNext()) { |
248 | 0 | Int2ObjectMap.Entry<V> entry = iterator.next(); |
249 | 0 | out.writeInt(entry.getIntKey()); |
250 | 0 | valueWriter.write(out, entry.getValue()); |
251 | 0 | } |
252 | 0 | } |
253 | |
|
254 | |
@Override |
255 | |
public void readFields(DataInput in) |
256 | |
throws IOException { |
257 | 0 | int size = in.readInt(); |
258 | 0 | map.clear(); |
259 | 0 | map.trim(size); |
260 | 0 | while (size-- > 0) { |
261 | 0 | int key = in.readInt(); |
262 | 0 | V value = valueWriter.readFields(in); |
263 | 0 | map.put(key, value); |
264 | 0 | } |
265 | 0 | } |
266 | |
} |
267 | |
|
268 | |
|
269 | 0 | public static final class BasicLong2ObjectOpenHashMap<V> |
270 | |
extends Basic2ObjectMap<LongWritable, V> { |
271 | |
|
272 | |
private final Long2ObjectOpenHashMap<V> map; |
273 | |
|
274 | |
private final WritableWriter<V> valueWriter; |
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | 0 | public BasicLong2ObjectOpenHashMap(WritableWriter<V> valueWriter) { |
282 | 0 | this.map = new Long2ObjectOpenHashMap<>(); |
283 | 0 | this.valueWriter = valueWriter; |
284 | 0 | } |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
public BasicLong2ObjectOpenHashMap( |
293 | 0 | int capacity, WritableWriter<V> valueWriter) { |
294 | 0 | this.map = new Long2ObjectOpenHashMap<>(capacity); |
295 | 0 | this.valueWriter = valueWriter; |
296 | 0 | } |
297 | |
|
298 | |
@Override |
299 | |
public void clear() { |
300 | 0 | map.clear(); |
301 | 0 | } |
302 | |
|
303 | |
@Override |
304 | |
public int size() { |
305 | 0 | return map.size(); |
306 | |
} |
307 | |
|
308 | |
@Override |
309 | |
public boolean containsKey(LongWritable key) { |
310 | 0 | return map.containsKey(key.get()); |
311 | |
} |
312 | |
|
313 | |
@Override |
314 | |
public V put(LongWritable key, V value) { |
315 | 0 | return map.put(key.get(), value); |
316 | |
} |
317 | |
|
318 | |
@Override |
319 | |
public V get(LongWritable key) { |
320 | 0 | return map.get(key.get()); |
321 | |
} |
322 | |
|
323 | |
@Override |
324 | |
public V remove(LongWritable key) { |
325 | 0 | return map.remove(key.get()); |
326 | |
} |
327 | |
|
328 | |
@Override |
329 | |
public PrimitiveIdTypeOps<LongWritable> getKeyTypeOps() { |
330 | 0 | return LongTypeOps.INSTANCE; |
331 | |
} |
332 | |
|
333 | |
@Override |
334 | |
public Iterator<LongWritable> fastKeyIterator() { |
335 | 0 | return new ReusableIterator<LongIterator>(map.keySet().iterator()) { |
336 | |
@Override |
337 | |
public LongWritable next() { |
338 | 0 | reusableKey.set(iter.nextLong()); |
339 | 0 | return reusableKey; |
340 | |
} |
341 | |
|
342 | |
@Override |
343 | |
public void reset() { |
344 | 0 | iter = map.keySet().iterator(); |
345 | 0 | } |
346 | |
}; |
347 | |
} |
348 | |
|
349 | |
@Override |
350 | |
public Iterator<V> valueIterator() { |
351 | 0 | return map.values().iterator(); |
352 | |
} |
353 | |
|
354 | |
@Override |
355 | |
public Collection<V> values() { |
356 | 0 | return map.values(); |
357 | |
} |
358 | |
|
359 | |
@Override |
360 | |
public void write(DataOutput out) throws IOException { |
361 | 0 | Preconditions.checkState( |
362 | |
valueWriter != null, |
363 | |
"valueWriter is not provided" |
364 | |
); |
365 | |
|
366 | 0 | out.writeInt(map.size()); |
367 | 0 | ObjectIterator<Long2ObjectMap.Entry<V>> iterator = |
368 | 0 | map.long2ObjectEntrySet().fastIterator(); |
369 | 0 | while (iterator.hasNext()) { |
370 | 0 | Long2ObjectMap.Entry<V> entry = iterator.next(); |
371 | 0 | out.writeLong(entry.getLongKey()); |
372 | 0 | valueWriter.write(out, entry.getValue()); |
373 | 0 | } |
374 | 0 | } |
375 | |
|
376 | |
@Override |
377 | |
public void readFields(DataInput in) throws IOException { |
378 | 0 | Preconditions.checkState( |
379 | |
valueWriter != null, |
380 | |
"valueWriter is not provided" |
381 | |
); |
382 | |
|
383 | 0 | int size = in.readInt(); |
384 | 0 | map.clear(); |
385 | 0 | map.trim(size); |
386 | 0 | while (size-- > 0) { |
387 | 0 | long key = in.readLong(); |
388 | 0 | V value = valueWriter.readFields(in); |
389 | 0 | map.put(key, value); |
390 | 0 | } |
391 | 0 | } |
392 | |
} |
393 | |
|
394 | |
|
395 | 0 | public static final class BasicObject2ObjectOpenHashMap<K extends Writable, V> |
396 | |
extends Basic2ObjectMap<K, V> { |
397 | |
|
398 | |
private final Object2ObjectOpenHashMap<K, V> map; |
399 | |
|
400 | |
private final WritableWriter<K> keyWriter; |
401 | |
|
402 | |
private final WritableWriter<V> valueWriter; |
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
public BasicObject2ObjectOpenHashMap( |
411 | |
WritableWriter<K> keyWriter, |
412 | |
WritableWriter<V> valueWriter |
413 | 0 | ) { |
414 | 0 | this.map = new Object2ObjectOpenHashMap<>(); |
415 | 0 | this.keyWriter = keyWriter; |
416 | 0 | this.valueWriter = valueWriter; |
417 | 0 | } |
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
public BasicObject2ObjectOpenHashMap( |
427 | |
int capacity, |
428 | |
WritableWriter<K> keyWriter, |
429 | |
WritableWriter<V> valueWriter |
430 | 0 | ) { |
431 | 0 | this.map = new Object2ObjectOpenHashMap<>(capacity); |
432 | 0 | this.keyWriter = keyWriter; |
433 | 0 | this.valueWriter = valueWriter; |
434 | 0 | } |
435 | |
|
436 | |
@Override |
437 | |
public void clear() { |
438 | 0 | map.clear(); |
439 | 0 | } |
440 | |
|
441 | |
@Override |
442 | |
public int size() { |
443 | 0 | return map.size(); |
444 | |
} |
445 | |
|
446 | |
@Override |
447 | |
public boolean containsKey(K key) { |
448 | 0 | return map.containsKey(key); |
449 | |
} |
450 | |
|
451 | |
@Override |
452 | |
public V put(K key, V value) { |
453 | |
|
454 | 0 | K copyKey = WritableUtils.createCopy(key); |
455 | 0 | return map.put(copyKey, value); |
456 | |
} |
457 | |
|
458 | |
@Override |
459 | |
public V get(K key) { |
460 | 0 | return map.get(key); |
461 | |
} |
462 | |
|
463 | |
@Override |
464 | |
public V remove(K key) { |
465 | 0 | return map.remove(key); |
466 | |
} |
467 | |
|
468 | |
@Override |
469 | |
public PrimitiveIdTypeOps<K> getKeyTypeOps() { |
470 | 0 | throw new UnsupportedOperationException(); |
471 | |
} |
472 | |
|
473 | |
@Override |
474 | |
public Iterator<K> fastKeyIterator() { |
475 | 0 | return map.keySet().iterator(); |
476 | |
} |
477 | |
|
478 | |
@Override |
479 | |
public Iterator<V> valueIterator() { |
480 | 0 | return map.values().iterator(); |
481 | |
} |
482 | |
|
483 | |
@Override |
484 | |
public Collection<V> values() { |
485 | 0 | return map.values(); |
486 | |
} |
487 | |
|
488 | |
@Override |
489 | |
public void write(DataOutput out) throws IOException { |
490 | 0 | Preconditions.checkState( |
491 | |
keyWriter != null, |
492 | |
"keyWriter is not provided" |
493 | |
); |
494 | 0 | Preconditions.checkState( |
495 | |
valueWriter != null, |
496 | |
"valueWriter is not provided" |
497 | |
); |
498 | |
|
499 | 0 | out.writeInt(map.size()); |
500 | 0 | ObjectIterator<Object2ObjectMap.Entry<K, V>> iterator = |
501 | 0 | map.object2ObjectEntrySet().fastIterator(); |
502 | 0 | while (iterator.hasNext()) { |
503 | 0 | Object2ObjectMap.Entry<K, V> entry = iterator.next(); |
504 | 0 | keyWriter.write(out, entry.getKey()); |
505 | 0 | valueWriter.write(out, entry.getValue()); |
506 | 0 | } |
507 | 0 | } |
508 | |
|
509 | |
@Override |
510 | |
public void readFields(DataInput in) throws IOException { |
511 | 0 | Preconditions.checkState( |
512 | |
keyWriter != null, |
513 | |
"keyWriter is not provided" |
514 | |
); |
515 | 0 | Preconditions.checkState( |
516 | |
valueWriter != null, |
517 | |
"valueWriter is not provided" |
518 | |
); |
519 | |
|
520 | 0 | int size = in.readInt(); |
521 | 0 | map.clear(); |
522 | 0 | map.trim(size); |
523 | 0 | while (size-- > 0) { |
524 | 0 | K key = keyWriter.readFields(in); |
525 | 0 | V value = valueWriter.readFields(in); |
526 | 0 | map.put(key, value); |
527 | 0 | } |
528 | 0 | } |
529 | |
} |
530 | |
} |