1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.utils; |
20 | |
|
21 | |
import static org.apache.hadoop.util.ReflectionUtils.newInstance; |
22 | |
|
23 | |
import java.io.ByteArrayInputStream; |
24 | |
import java.io.ByteArrayOutputStream; |
25 | |
import java.io.DataInput; |
26 | |
import java.io.DataInputStream; |
27 | |
import java.io.DataOutput; |
28 | |
import java.io.DataOutputStream; |
29 | |
import java.io.IOException; |
30 | |
import java.lang.reflect.InvocationTargetException; |
31 | |
import java.util.ArrayList; |
32 | |
import java.util.List; |
33 | |
|
34 | |
import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration; |
35 | |
import org.apache.giraph.edge.Edge; |
36 | |
import org.apache.giraph.edge.OutEdges; |
37 | |
import org.apache.giraph.factories.ValueFactory; |
38 | |
import org.apache.giraph.graph.Vertex; |
39 | |
import org.apache.giraph.zk.ZooKeeperExt; |
40 | |
import org.apache.giraph.zk.ZooKeeperExt.PathStat; |
41 | |
import org.apache.hadoop.conf.Configuration; |
42 | |
import org.apache.hadoop.io.NullWritable; |
43 | |
import org.apache.hadoop.io.Writable; |
44 | |
import org.apache.hadoop.io.WritableComparable; |
45 | |
import org.apache.zookeeper.CreateMode; |
46 | |
import org.apache.zookeeper.KeeperException; |
47 | |
import org.apache.zookeeper.ZooDefs.Ids; |
48 | |
import org.apache.zookeeper.data.Stat; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class WritableUtils { |
54 | |
|
55 | |
|
56 | |
|
57 | 0 | private WritableUtils() { } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public static <W extends Writable> W createWritable(Class<W> klass) { |
67 | 0 | return createWritable(klass, null); |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public static <W extends Writable> W createWritable( |
79 | |
Class<W> klass, |
80 | |
ImmutableClassesGiraphConfiguration configuration) { |
81 | |
W result; |
82 | 0 | if (NullWritable.class.equals(klass)) { |
83 | 0 | result = (W) NullWritable.get(); |
84 | |
} else { |
85 | 0 | result = ReflectionUtils.newInstance(klass); |
86 | |
} |
87 | 0 | ConfigurationUtils.configureIfPossible(result, configuration); |
88 | 0 | return result; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public static void readFieldsFromByteArray( |
99 | |
byte[] byteArray, Writable... writableObjects) { |
100 | 0 | DataInputStream inputStream = |
101 | |
new DataInputStream(new ByteArrayInputStream(byteArray)); |
102 | |
try { |
103 | 0 | for (Writable writableObject : writableObjects) { |
104 | 0 | writableObject.readFields(inputStream); |
105 | |
} |
106 | 0 | } catch (IOException e) { |
107 | 0 | throw new IllegalStateException( |
108 | |
"readFieldsFromByteArray: IOException", e); |
109 | 0 | } |
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public static void readFieldsFromZnode(ZooKeeperExt zkExt, |
122 | |
String zkPath, |
123 | |
boolean watch, |
124 | |
Stat stat, |
125 | |
Writable... writableObjects) { |
126 | |
try { |
127 | 0 | byte[] zkData = zkExt.getData(zkPath, false, stat); |
128 | 0 | readFieldsFromByteArray(zkData, writableObjects); |
129 | 0 | } catch (KeeperException e) { |
130 | 0 | throw new IllegalStateException( |
131 | |
"readFieldsFromZnode: KeeperException on " + zkPath, e); |
132 | 0 | } catch (InterruptedException e) { |
133 | 0 | throw new IllegalStateException( |
134 | |
"readFieldsFromZnode: InterrruptedStateException on " + zkPath, e); |
135 | 0 | } |
136 | 0 | } |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public static byte[] writeToByteArray(Writable... writableObjects) { |
145 | 0 | ByteArrayOutputStream outputStream = |
146 | |
new ByteArrayOutputStream(); |
147 | 0 | DataOutput output = new DataOutputStream(outputStream); |
148 | |
try { |
149 | 0 | for (Writable writableObject : writableObjects) { |
150 | 0 | writableObject.write(output); |
151 | |
} |
152 | 0 | } catch (IOException e) { |
153 | 0 | throw new IllegalStateException( |
154 | |
"writeToByteArray: IOStateException", e); |
155 | 0 | } |
156 | 0 | return outputStream.toByteArray(); |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public static void readFieldsFromByteArrayWithSize( |
168 | |
byte[] byteArray, Writable writableObject, boolean unsafe) { |
169 | |
ExtendedDataInput extendedDataInput; |
170 | 0 | if (unsafe) { |
171 | 0 | extendedDataInput = new UnsafeByteArrayInputStream(byteArray); |
172 | |
} else { |
173 | 0 | extendedDataInput = new ExtendedByteArrayDataInput(byteArray); |
174 | |
} |
175 | |
try { |
176 | 0 | extendedDataInput.readInt(); |
177 | 0 | writableObject.readFields(extendedDataInput); |
178 | 0 | } catch (IOException e) { |
179 | 0 | throw new IllegalStateException( |
180 | |
"readFieldsFromByteArrayWithSize: IOException", e); |
181 | 0 | } |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
public static byte[] writeToByteArrayWithSize(Writable writableObject, |
193 | |
boolean unsafe) { |
194 | 0 | return writeToByteArrayWithSize(writableObject, null, unsafe); |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
public static byte[] writeToByteArrayWithSize(Writable writableObject, |
207 | |
byte[] buffer, |
208 | |
boolean unsafe) { |
209 | |
ExtendedDataOutput extendedDataOutput; |
210 | 0 | if (unsafe) { |
211 | 0 | extendedDataOutput = new UnsafeByteArrayOutputStream(buffer); |
212 | |
} else { |
213 | 0 | extendedDataOutput = new ExtendedByteArrayDataOutput(buffer); |
214 | |
} |
215 | |
try { |
216 | 0 | extendedDataOutput.writeInt(-1); |
217 | 0 | writableObject.write(extendedDataOutput); |
218 | 0 | extendedDataOutput.writeInt(0, extendedDataOutput.getPos()); |
219 | 0 | } catch (IOException e) { |
220 | 0 | throw new IllegalStateException("writeToByteArrayWithSize: " + |
221 | |
"IOException", e); |
222 | 0 | } |
223 | |
|
224 | 0 | return extendedDataOutput.getByteArray(); |
225 | |
} |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
public static PathStat writeToZnode(ZooKeeperExt zkExt, |
237 | |
String zkPath, |
238 | |
int version, |
239 | |
Writable... writableObjects) { |
240 | |
try { |
241 | 0 | byte[] byteArray = writeToByteArray(writableObjects); |
242 | 0 | return zkExt.createOrSetExt(zkPath, |
243 | |
byteArray, |
244 | |
Ids.OPEN_ACL_UNSAFE, |
245 | |
CreateMode.PERSISTENT, |
246 | |
true, |
247 | |
version); |
248 | 0 | } catch (KeeperException e) { |
249 | 0 | throw new IllegalStateException( |
250 | |
"writeToZnode: KeeperException on " + zkPath, e); |
251 | 0 | } catch (InterruptedException e) { |
252 | 0 | throw new IllegalStateException( |
253 | |
"writeToZnode: InterruptedException on " + zkPath, e); |
254 | |
} |
255 | |
} |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
public static byte[] writeListToByteArray( |
264 | |
List<? extends Writable> writableList) { |
265 | 0 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
266 | 0 | DataOutput output = new DataOutputStream(outputStream); |
267 | |
try { |
268 | 0 | output.writeInt(writableList.size()); |
269 | 0 | for (Writable writable : writableList) { |
270 | 0 | writable.write(output); |
271 | 0 | } |
272 | 0 | } catch (IOException e) { |
273 | 0 | throw new IllegalStateException( |
274 | |
"writeListToByteArray: IOException", e); |
275 | 0 | } |
276 | 0 | return outputStream.toByteArray(); |
277 | |
} |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
public static PathStat writeListToZnode( |
289 | |
ZooKeeperExt zkExt, |
290 | |
String zkPath, |
291 | |
int version, |
292 | |
List<? extends Writable> writableList) { |
293 | |
try { |
294 | 0 | return zkExt.createOrSetExt( |
295 | |
zkPath, |
296 | 0 | writeListToByteArray(writableList), |
297 | |
Ids.OPEN_ACL_UNSAFE, |
298 | |
CreateMode.PERSISTENT, |
299 | |
true, |
300 | |
version); |
301 | 0 | } catch (KeeperException e) { |
302 | 0 | throw new IllegalStateException( |
303 | |
"writeListToZnode: KeeperException on " + zkPath, e); |
304 | 0 | } catch (InterruptedException e) { |
305 | 0 | throw new IllegalStateException( |
306 | |
"writeListToZnode: InterruptedException on " + zkPath, e); |
307 | |
} |
308 | |
} |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
public static <T extends Writable> List<T> readListFieldsFromByteArray( |
320 | |
byte[] byteArray, |
321 | |
Class<? extends T> writableClass, |
322 | |
Configuration conf) { |
323 | |
try { |
324 | 0 | DataInputStream inputStream = |
325 | |
new DataInputStream(new ByteArrayInputStream(byteArray)); |
326 | 0 | int size = inputStream.readInt(); |
327 | 0 | List<T> writableList = new ArrayList<T>(size); |
328 | 0 | for (int i = 0; i < size; ++i) { |
329 | 0 | T writable = newInstance(writableClass, conf); |
330 | 0 | writable.readFields(inputStream); |
331 | 0 | writableList.add(writable); |
332 | |
} |
333 | 0 | return writableList; |
334 | 0 | } catch (IOException e) { |
335 | 0 | throw new IllegalStateException( |
336 | |
"readListFieldsFromZnode: IOException", e); |
337 | |
} |
338 | |
} |
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
public static <T extends Writable> List<T> readListFieldsFromZnode( |
353 | |
ZooKeeperExt zkExt, |
354 | |
String zkPath, |
355 | |
boolean watch, |
356 | |
Stat stat, |
357 | |
Class<? extends T> writableClass, |
358 | |
Configuration conf) { |
359 | |
try { |
360 | 0 | byte[] zkData = zkExt.getData(zkPath, false, stat); |
361 | 0 | return WritableUtils.<T>readListFieldsFromByteArray(zkData, |
362 | |
writableClass, conf); |
363 | 0 | } catch (KeeperException e) { |
364 | 0 | throw new IllegalStateException( |
365 | |
"readListFieldsFromZnode: KeeperException on " + zkPath, e); |
366 | 0 | } catch (InterruptedException e) { |
367 | 0 | throw new IllegalStateException( |
368 | |
"readListFieldsFromZnode: InterruptedException on " + zkPath, |
369 | |
e); |
370 | |
} |
371 | |
} |
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
public static void writeExtendedDataOutput( |
380 | |
ExtendedDataOutput extendedDataOutput, DataOutput out) |
381 | |
throws IOException { |
382 | 0 | out.writeInt(extendedDataOutput.getPos()); |
383 | 0 | out.write( |
384 | 0 | extendedDataOutput.getByteArray(), 0, extendedDataOutput.getPos()); |
385 | 0 | } |
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
public static ExtendedDataOutput readExtendedDataOutput(DataInput in, |
395 | |
ImmutableClassesGiraphConfiguration conf) throws IOException { |
396 | 0 | int size = in.readInt(); |
397 | 0 | byte[] buf = new byte[size]; |
398 | 0 | in.readFully(buf); |
399 | 0 | return conf.createExtendedDataOutput(buf, size); |
400 | |
} |
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
public static <I extends WritableComparable, V extends Writable, |
416 | |
E extends Writable> byte[] writeVertexToByteArray( |
417 | |
Vertex<I, V, E> vertex, |
418 | |
byte[] buffer, |
419 | |
boolean unsafe, |
420 | |
ImmutableClassesGiraphConfiguration<I, V, E> conf) { |
421 | |
ExtendedDataOutput extendedDataOutput; |
422 | 0 | if (unsafe) { |
423 | 0 | extendedDataOutput = new UnsafeByteArrayOutputStream(buffer); |
424 | |
} else { |
425 | 0 | extendedDataOutput = new ExtendedByteArrayDataOutput(buffer); |
426 | |
} |
427 | |
try { |
428 | 0 | extendedDataOutput.writeInt(-1); |
429 | 0 | writeVertexToDataOutput(extendedDataOutput, vertex, conf); |
430 | 0 | extendedDataOutput.writeInt(0, extendedDataOutput.getPos()); |
431 | 0 | } catch (IOException e) { |
432 | 0 | throw new IllegalStateException("writeVertexToByteArray: " + |
433 | |
"IOException", e); |
434 | 0 | } |
435 | |
|
436 | 0 | return extendedDataOutput.getByteArray(); |
437 | |
} |
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
public static <I extends WritableComparable, V extends Writable, |
452 | |
E extends Writable> byte[] writeVertexToByteArray( |
453 | |
Vertex<I, V, E> vertex, |
454 | |
boolean unsafe, |
455 | |
ImmutableClassesGiraphConfiguration<I, V, E> conf) { |
456 | 0 | return writeVertexToByteArray(vertex, null, unsafe, conf); |
457 | |
} |
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
public static <I extends WritableComparable, V extends Writable, |
473 | |
E extends Writable> void reinitializeVertexFromByteArray( |
474 | |
byte[] byteArray, |
475 | |
Vertex<I, V, E> vertex, |
476 | |
boolean unsafe, |
477 | |
ImmutableClassesGiraphConfiguration<I, V, E> conf) { |
478 | |
ExtendedDataInput extendedDataInput; |
479 | 0 | if (unsafe) { |
480 | 0 | extendedDataInput = new UnsafeByteArrayInputStream(byteArray); |
481 | |
} else { |
482 | 0 | extendedDataInput = new ExtendedByteArrayDataInput(byteArray); |
483 | |
} |
484 | |
try { |
485 | 0 | extendedDataInput.readInt(); |
486 | 0 | reinitializeVertexFromDataInput(extendedDataInput, vertex, conf); |
487 | 0 | } catch (IOException e) { |
488 | 0 | throw new IllegalStateException( |
489 | |
"readFieldsFromByteArrayWithSize: IOException", e); |
490 | 0 | } |
491 | 0 | } |
492 | |
|
493 | |
|
494 | |
|
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
public static <I extends WritableComparable, E extends Writable> |
503 | |
void writeEdge(DataOutput out, Edge<I, E> edge) throws IOException { |
504 | 0 | edge.getTargetVertexId().write(out); |
505 | 0 | edge.getValue().write(out); |
506 | 0 | } |
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
public static <I extends WritableComparable, E extends Writable> |
518 | |
void readEdge(DataInput in, Edge<I, E> edge) throws IOException { |
519 | 0 | edge.getTargetVertexId().readFields(in); |
520 | 0 | edge.getValue().readFields(in); |
521 | 0 | } |
522 | |
|
523 | |
|
524 | |
|
525 | |
|
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
@SuppressWarnings("unchecked") |
536 | |
public static <I extends WritableComparable, V extends Writable, |
537 | |
E extends Writable> void reinitializeVertexFromDataInput( |
538 | |
DataInput input, |
539 | |
Vertex<I, V, E> vertex, |
540 | |
ImmutableClassesGiraphConfiguration<I, V, E> conf) |
541 | |
throws IOException { |
542 | 0 | vertex.getId().readFields(input); |
543 | 0 | vertex.getValue().readFields(input); |
544 | 0 | ((OutEdges<I, E>) vertex.getEdges()).readFields(input); |
545 | 0 | if (input.readBoolean()) { |
546 | 0 | vertex.voteToHalt(); |
547 | |
} else { |
548 | 0 | vertex.wakeUp(); |
549 | |
} |
550 | 0 | } |
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
|
563 | |
public static <I extends WritableComparable, V extends Writable, |
564 | |
E extends Writable> Vertex<I, V, E> |
565 | |
readVertexFromDataInput( |
566 | |
DataInput input, |
567 | |
ImmutableClassesGiraphConfiguration<I, V, E> conf) |
568 | |
throws IOException { |
569 | 0 | Vertex<I, V, E> vertex = conf.createVertex(); |
570 | 0 | I id = conf.createVertexId(); |
571 | 0 | V value = conf.createVertexValue(); |
572 | 0 | OutEdges<I, E> edges = conf.createOutEdges(); |
573 | 0 | vertex.initialize(id, value, edges); |
574 | 0 | reinitializeVertexFromDataInput(input, vertex, conf); |
575 | 0 | return vertex; |
576 | |
} |
577 | |
|
578 | |
|
579 | |
|
580 | |
|
581 | |
|
582 | |
|
583 | |
|
584 | |
|
585 | |
|
586 | |
|
587 | |
|
588 | |
|
589 | |
@SuppressWarnings("unchecked") |
590 | |
public static <I extends WritableComparable, V extends Writable, |
591 | |
E extends Writable> void writeVertexToDataOutput( |
592 | |
DataOutput output, |
593 | |
Vertex<I, V, E> vertex, |
594 | |
ImmutableClassesGiraphConfiguration<I, V, E> conf) |
595 | |
throws IOException { |
596 | 0 | vertex.getId().write(output); |
597 | 0 | vertex.getValue().write(output); |
598 | 0 | ((OutEdges<I, E>) vertex.getEdges()).write(output); |
599 | 0 | output.writeBoolean(vertex.isHalted()); |
600 | 0 | } |
601 | |
|
602 | |
|
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
public static <T> void writeClass(Class<T> clazz, |
610 | |
DataOutput output) throws IOException { |
611 | 0 | output.writeBoolean(clazz != null); |
612 | 0 | if (clazz != null) { |
613 | 0 | output.writeUTF(clazz.getName()); |
614 | |
} |
615 | 0 | } |
616 | |
|
617 | |
|
618 | |
|
619 | |
|
620 | |
|
621 | |
|
622 | |
|
623 | |
|
624 | |
|
625 | |
@SuppressWarnings("unchecked") |
626 | |
public static <T> Class<T> readClass(DataInput input) throws IOException { |
627 | 0 | if (input.readBoolean()) { |
628 | 0 | String className = input.readUTF(); |
629 | |
try { |
630 | 0 | return (Class<T>) Class.forName(className); |
631 | 0 | } catch (ClassNotFoundException e) { |
632 | 0 | throw new IllegalStateException("readClass: No class found " + |
633 | |
className); |
634 | |
} |
635 | |
} else { |
636 | 0 | return null; |
637 | |
} |
638 | |
} |
639 | |
|
640 | |
|
641 | |
|
642 | |
|
643 | |
|
644 | |
|
645 | |
|
646 | |
public static void writeWritableObject( |
647 | |
Writable object, DataOutput output) |
648 | |
throws IOException { |
649 | 0 | output.writeBoolean(object != null); |
650 | 0 | if (object != null) { |
651 | 0 | output.writeUTF(object.getClass().getName()); |
652 | 0 | object.write(output); |
653 | |
} |
654 | 0 | } |
655 | |
|
656 | |
|
657 | |
|
658 | |
|
659 | |
|
660 | |
|
661 | |
|
662 | |
|
663 | |
|
664 | |
public static <T extends Writable> |
665 | |
T readWritableObject(DataInput input, |
666 | |
ImmutableClassesGiraphConfiguration conf) throws IOException { |
667 | 0 | if (input.readBoolean()) { |
668 | 0 | String className = input.readUTF(); |
669 | |
try { |
670 | 0 | T object = |
671 | 0 | (T) ReflectionUtils.newInstance(Class.forName(className), conf); |
672 | 0 | object.readFields(input); |
673 | 0 | return object; |
674 | 0 | } catch (ClassNotFoundException e) { |
675 | 0 | throw new IllegalStateException("readWritableObject: No class found " + |
676 | |
className); |
677 | |
} |
678 | |
} else { |
679 | 0 | return null; |
680 | |
} |
681 | |
} |
682 | |
|
683 | |
|
684 | |
|
685 | |
|
686 | |
|
687 | |
|
688 | |
|
689 | |
|
690 | |
|
691 | |
|
692 | |
public static void writeList(List<? extends Writable> list, DataOutput output) |
693 | |
throws IOException { |
694 | 0 | output.writeBoolean(list != null); |
695 | 0 | if (list != null) { |
696 | 0 | output.writeInt(list.size()); |
697 | 0 | Class<? extends Writable> clazz = null; |
698 | 0 | for (Writable element : list) { |
699 | 0 | output.writeBoolean(element == null); |
700 | 0 | if (element != null) { |
701 | 0 | if (element.getClass() != clazz) { |
702 | 0 | clazz = element.getClass(); |
703 | 0 | output.writeBoolean(true); |
704 | 0 | writeClass(clazz, output); |
705 | |
} else { |
706 | 0 | output.writeBoolean(false); |
707 | |
} |
708 | 0 | element.write(output); |
709 | |
} |
710 | 0 | } |
711 | |
} |
712 | 0 | } |
713 | |
|
714 | |
|
715 | |
|
716 | |
|
717 | |
|
718 | |
|
719 | |
|
720 | |
|
721 | |
public static List<? extends Writable> readList(DataInput input) |
722 | |
throws IOException { |
723 | |
try { |
724 | 0 | List<Writable> res = null; |
725 | 0 | if (input.readBoolean()) { |
726 | 0 | int size = input.readInt(); |
727 | 0 | res = new ArrayList<>(size); |
728 | 0 | Class<? extends Writable> clazz = null; |
729 | 0 | for (int i = 0; i < size; i++) { |
730 | 0 | boolean isNull = input.readBoolean(); |
731 | 0 | if (isNull) { |
732 | 0 | res.add(null); |
733 | |
} else { |
734 | 0 | boolean hasClassInfo = input.readBoolean(); |
735 | 0 | if (hasClassInfo) { |
736 | 0 | clazz = readClass(input); |
737 | |
} |
738 | 0 | Writable element = clazz.newInstance(); |
739 | 0 | element.readFields(input); |
740 | 0 | res.add(element); |
741 | |
} |
742 | |
} |
743 | |
} |
744 | 0 | return res; |
745 | |
|
746 | 0 | } catch (InstantiationException | IllegalAccessException e) { |
747 | 0 | throw new IllegalStateException("unable to instantiate object", e); |
748 | |
} |
749 | |
} |
750 | |
|
751 | |
|
752 | |
|
753 | |
|
754 | |
|
755 | |
|
756 | |
|
757 | |
|
758 | |
public static void writeIntArray(int[] array, DataOutput dataOutput) |
759 | |
throws IOException { |
760 | 0 | if (array != null) { |
761 | 0 | dataOutput.writeInt(array.length); |
762 | 0 | for (int r : array) { |
763 | 0 | dataOutput.writeInt(r); |
764 | |
} |
765 | |
} else { |
766 | 0 | dataOutput.writeInt(-1); |
767 | |
} |
768 | 0 | } |
769 | |
|
770 | |
|
771 | |
|
772 | |
|
773 | |
|
774 | |
|
775 | |
|
776 | |
public static int[] readIntArray(DataInput dataInput) |
777 | |
throws IOException { |
778 | 0 | int [] res = null; |
779 | 0 | int size = dataInput.readInt(); |
780 | 0 | if (size >= 0) { |
781 | 0 | res = new int[size]; |
782 | 0 | for (int i = 0; i < size; i++) { |
783 | 0 | res[i] = dataInput.readInt(); |
784 | |
} |
785 | |
} |
786 | 0 | return res; |
787 | |
} |
788 | |
|
789 | |
|
790 | |
|
791 | |
|
792 | |
|
793 | |
|
794 | |
|
795 | |
|
796 | |
public static void writeLongArray(DataOutput dataOutput, long[] array) |
797 | |
throws IOException { |
798 | 0 | if (array != null) { |
799 | 0 | dataOutput.writeInt(array.length); |
800 | 0 | for (long r : array) { |
801 | 0 | dataOutput.writeLong(r); |
802 | |
} |
803 | |
} else { |
804 | 0 | dataOutput.writeInt(-1); |
805 | |
} |
806 | 0 | } |
807 | |
|
808 | |
|
809 | |
|
810 | |
|
811 | |
|
812 | |
|
813 | |
public static long[] readLongArray(DataInput dataInput) |
814 | |
throws IOException { |
815 | 0 | long [] res = null; |
816 | 0 | int size = dataInput.readInt(); |
817 | 0 | if (size >= 0) { |
818 | 0 | res = new long[size]; |
819 | 0 | for (int i = 0; i < size; i++) { |
820 | 0 | res[i] = dataInput.readLong(); |
821 | |
} |
822 | |
} |
823 | 0 | return res; |
824 | |
} |
825 | |
|
826 | |
|
827 | |
|
828 | |
|
829 | |
|
830 | |
|
831 | |
|
832 | |
public static <T extends Enum<T>> void writeEnum(T enumValue, |
833 | |
DataOutput output) throws IOException { |
834 | 0 | writeClass( |
835 | 0 | enumValue != null ? enumValue.getDeclaringClass() : null, output); |
836 | 0 | if (enumValue != null) { |
837 | 0 | Varint.writeUnsignedVarInt(enumValue.ordinal(), output); |
838 | |
} |
839 | 0 | } |
840 | |
|
841 | |
|
842 | |
|
843 | |
|
844 | |
|
845 | |
|
846 | |
|
847 | |
public static <T extends Enum<T>> T readEnum(DataInput input) throws |
848 | |
IOException { |
849 | 0 | Class<T> clazz = readClass(input); |
850 | 0 | if (clazz != null) { |
851 | 0 | int ordinal = Varint.readUnsignedVarInt(input); |
852 | |
try { |
853 | 0 | T[] values = (T[]) clazz.getDeclaredMethod("values").invoke(null); |
854 | 0 | return values[ordinal]; |
855 | 0 | } catch (IllegalAccessException | IllegalArgumentException | |
856 | |
InvocationTargetException | NoSuchMethodException | |
857 | |
SecurityException e) { |
858 | 0 | throw new IOException("Cannot read enum", e); |
859 | |
} |
860 | |
} else { |
861 | 0 | return null; |
862 | |
} |
863 | |
} |
864 | |
|
865 | |
|
866 | |
|
867 | |
|
868 | |
|
869 | |
|
870 | |
|
871 | |
|
872 | |
|
873 | |
|
874 | |
|
875 | |
public static <T extends Writable> void copyInto(T from, T to) { |
876 | 0 | copyInto(from, to, false); |
877 | 0 | } |
878 | |
|
879 | |
|
880 | |
|
881 | |
|
882 | |
|
883 | |
|
884 | |
|
885 | |
|
886 | |
|
887 | |
|
888 | |
|
889 | |
|
890 | |
public static <T extends Writable> void copyInto( |
891 | |
T from, T to, boolean checkOverRead) { |
892 | |
try { |
893 | 0 | if (from.getClass() != to.getClass()) { |
894 | 0 | throw new RuntimeException( |
895 | 0 | "Trying to copy from " + from.getClass() + |
896 | 0 | " into " + to.getClass()); |
897 | |
} |
898 | |
|
899 | 0 | UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream(); |
900 | 0 | from.write(out); |
901 | 0 | if (checkOverRead) { |
902 | 0 | out.writeByte(0); |
903 | |
} |
904 | |
|
905 | 0 | UnsafeByteArrayInputStream in = |
906 | 0 | new UnsafeByteArrayInputStream(out.getByteArray(), 0, out.getPos()); |
907 | 0 | to.readFields(in); |
908 | |
|
909 | 0 | if (in.available() != (checkOverRead ? 1 : 0)) { |
910 | 0 | throw new RuntimeException( |
911 | 0 | "Serialization encountered issues with " + from.getClass() + ", " + |
912 | 0 | (in.available() - (checkOverRead ? 1 : 0)) + " fewer bytes read"); |
913 | |
} |
914 | 0 | } catch (IOException e) { |
915 | 0 | throw new RuntimeException(e); |
916 | 0 | } |
917 | 0 | } |
918 | |
|
919 | |
|
920 | |
|
921 | |
|
922 | |
|
923 | |
|
924 | |
|
925 | |
|
926 | |
|
927 | |
|
928 | |
|
929 | |
public static <T extends Writable> T createCopy( |
930 | |
UnsafeByteArrayOutputStream reusableOut, |
931 | |
UnsafeReusableByteArrayInput reusableIn, T original, |
932 | |
ImmutableClassesGiraphConfiguration conf) { |
933 | 0 | T copy = (T) createWritable(original.getClass(), conf); |
934 | |
|
935 | |
try { |
936 | 0 | reusableOut.reset(); |
937 | 0 | original.write(reusableOut); |
938 | 0 | reusableIn.initialize( |
939 | 0 | reusableOut.getByteArray(), 0, reusableOut.getPos()); |
940 | 0 | copy.readFields(reusableIn); |
941 | |
|
942 | 0 | if (reusableIn.available() != 0) { |
943 | 0 | throw new RuntimeException("Serialization of " + |
944 | 0 | original.getClass() + " encountered issues, " + |
945 | 0 | reusableIn.available() + " bytes left to be read"); |
946 | |
} |
947 | 0 | } catch (IOException e) { |
948 | 0 | throw new IllegalStateException( |
949 | |
"IOException occurred while trying to create a copy " + |
950 | 0 | original.getClass(), e); |
951 | 0 | } |
952 | 0 | return copy; |
953 | |
} |
954 | |
|
955 | |
|
956 | |
|
957 | |
|
958 | |
|
959 | |
|
960 | |
|
961 | |
|
962 | |
public static final <T extends Writable> T createCopy(T original) { |
963 | 0 | return (T) createCopy(original, original.getClass(), null); |
964 | |
} |
965 | |
|
966 | |
|
967 | |
|
968 | |
|
969 | |
|
970 | |
|
971 | |
|
972 | |
|
973 | |
|
974 | |
|
975 | |
public static final <T extends Writable> |
976 | |
T createCopy(T original, Class<? extends T> outputClass, |
977 | |
ImmutableClassesGiraphConfiguration conf) { |
978 | 0 | T result = WritableUtils.createWritable(outputClass, conf); |
979 | 0 | copyInto(original, result); |
980 | 0 | return result; |
981 | |
} |
982 | |
|
983 | |
|
984 | |
|
985 | |
|
986 | |
|
987 | |
|
988 | |
|
989 | |
|
990 | |
|
991 | |
|
992 | |
public static final <T extends Writable> |
993 | |
T createCopy(T original, ValueFactory<T> classFactory, |
994 | |
ImmutableClassesGiraphConfiguration conf) { |
995 | 0 | T result = classFactory.newInstance(); |
996 | 0 | copyInto(original, result); |
997 | 0 | return result; |
998 | |
} |
999 | |
|
1000 | |
|
1001 | |
|
1002 | |
|
1003 | |
|
1004 | |
|
1005 | |
|
1006 | |
public static int size(Writable w) { |
1007 | |
try { |
1008 | 0 | ExtendedByteArrayDataOutput out = new ExtendedByteArrayDataOutput(); |
1009 | 0 | w.write(out); |
1010 | 0 | return out.getPos(); |
1011 | 0 | } catch (IOException e) { |
1012 | 0 | throw new RuntimeException(e); |
1013 | |
} |
1014 | |
} |
1015 | |
|
1016 | |
|
1017 | |
|
1018 | |
|
1019 | |
|
1020 | |
|
1021 | |
|
1022 | |
|
1023 | |
|
1024 | |
public static <T extends Writable> byte[] toByteArray(T w) { |
1025 | |
try { |
1026 | 0 | ExtendedByteArrayDataOutput out = new ExtendedByteArrayDataOutput(); |
1027 | 0 | w.write(out); |
1028 | 0 | return out.toByteArray(); |
1029 | 0 | } catch (IOException e) { |
1030 | 0 | throw new RuntimeException(e); |
1031 | |
} |
1032 | |
} |
1033 | |
|
1034 | |
|
1035 | |
|
1036 | |
|
1037 | |
|
1038 | |
|
1039 | |
|
1040 | |
|
1041 | |
|
1042 | |
public static <T extends Writable> void fromByteArray(byte[] data, T to) { |
1043 | |
try { |
1044 | 0 | ExtendedByteArrayDataInput in = |
1045 | |
new ExtendedByteArrayDataInput(data, 0, data.length); |
1046 | 0 | to.readFields(in); |
1047 | |
|
1048 | 0 | if (in.available() != 0) { |
1049 | 0 | throw new RuntimeException( |
1050 | 0 | "Serialization encountered issues, " + in.available() + |
1051 | |
" bytes left to be read"); |
1052 | |
} |
1053 | 0 | } catch (IOException e) { |
1054 | 0 | throw new RuntimeException(e); |
1055 | 0 | } |
1056 | 0 | } |
1057 | |
|
1058 | |
|
1059 | |
|
1060 | |
|
1061 | |
|
1062 | |
|
1063 | |
|
1064 | |
|
1065 | |
|
1066 | |
public static <T extends Writable> byte[] toByteArrayUnsafe(T w) { |
1067 | |
try { |
1068 | 0 | UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream(); |
1069 | 0 | w.write(out); |
1070 | 0 | return out.toByteArray(); |
1071 | 0 | } catch (IOException e) { |
1072 | 0 | throw new RuntimeException(e); |
1073 | |
} |
1074 | |
} |
1075 | |
|
1076 | |
|
1077 | |
|
1078 | |
|
1079 | |
|
1080 | |
|
1081 | |
|
1082 | |
|
1083 | |
|
1084 | |
|
1085 | |
public static <T extends Writable> void fromByteArrayUnsafe( |
1086 | |
byte[] data, T to, UnsafeReusableByteArrayInput reusableInput) { |
1087 | |
try { |
1088 | 0 | reusableInput.initialize(data, 0, data.length); |
1089 | 0 | to.readFields(reusableInput); |
1090 | |
|
1091 | 0 | if (reusableInput.available() != 0) { |
1092 | 0 | throw new RuntimeException( |
1093 | 0 | "Serialization encountered issues, " + reusableInput.available() + |
1094 | |
" bytes left to be read"); |
1095 | |
} |
1096 | 0 | } catch (IOException e) { |
1097 | 0 | throw new RuntimeException(e); |
1098 | 0 | } |
1099 | 0 | } |
1100 | |
|
1101 | |
|
1102 | |
|
1103 | |
|
1104 | |
|
1105 | |
|
1106 | |
|
1107 | |
|
1108 | |
|
1109 | |
public static <T extends Writable> void writeIfNotNullAndObject(T object, |
1110 | |
DataOutput out) throws IOException { |
1111 | 0 | out.writeBoolean(object != null); |
1112 | 0 | if (object != null) { |
1113 | 0 | object.write(out); |
1114 | |
} |
1115 | 0 | } |
1116 | |
|
1117 | |
|
1118 | |
|
1119 | |
|
1120 | |
|
1121 | |
|
1122 | |
|
1123 | |
|
1124 | |
|
1125 | |
|
1126 | |
|
1127 | |
public static <T extends Writable> T readIfNotNullAndObject(T reusableObject, |
1128 | |
Class<T> objectClass, DataInput in) throws IOException { |
1129 | 0 | if (in.readBoolean()) { |
1130 | 0 | if (reusableObject == null) { |
1131 | 0 | reusableObject = ReflectionUtils.newInstance(objectClass); |
1132 | |
} |
1133 | 0 | reusableObject.readFields(in); |
1134 | 0 | return reusableObject; |
1135 | |
} else { |
1136 | 0 | return null; |
1137 | |
} |
1138 | |
} |
1139 | |
|
1140 | |
} |