-
Notifications
You must be signed in to change notification settings - Fork 187
/
StorageSerialization.java
1427 lines (1331 loc) · 41.3 KB
/
StorageSerialization.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015 LinkedIn Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
package com.linkedin.paldb.impl;
import com.linkedin.paldb.api.Configuration;
import com.linkedin.paldb.api.Serializer;
import com.linkedin.paldb.api.UnsupportedTypeException;
import com.linkedin.paldb.utils.DataInputOutput;
import com.linkedin.paldb.utils.LongPacker;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.EOFException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.xerial.snappy.Snappy;
/**
* Internal serialization implementation.
*/
public final class StorageSerialization {
//Buffer
private final DataInputOutput dataInputOutput = new DataInputOutput();
//Compression
private final boolean compression;
//Serializers
private Serializers serializers;
/**
* Default constructor with configuration.
* <p>
* Uses <code>Configuration.COMPRESSION_ENABLED</code> and <code>Configuration.KEY_COMPARATOR</code> values
* from the configuration.
*
* @param config configuration
*/
public StorageSerialization(Configuration config) {
this.compression = config.getBoolean(Configuration.COMPRESSION_ENABLED);
this.serializers = config.getSerializers();
}
/**
* Serializes the key object and returns it as a byte array.
*
* @param key key to serialize
* @return key as byte array
* @throws IOException if an io error occurs
*/
public byte[] serializeKey(Object key)
throws IOException {
if (key == null) {
throw new NullPointerException();
}
serializeObject(key, dataInputOutput.reset(), false);
return dataInputOutput.toByteArray();
}
/**
* Serializes the key and writes it into <code>dataOutput</code>.
*
* @param key key to serialize
* @param dataOutput data output
* @throws IOException if an io error occurs
*/
public void serializeKey(Object key, DataOutput dataOutput)
throws IOException {
serializeObject(key, dataOutput, false);
}
/**
* Serializes the value object and returns it as a byte array.
*
* @param value value to serialize
* @return value as byte array
* @throws IOException if an io error occurs
*/
public byte[] serializeValue(Object value)
throws IOException {
serializeObject(value, dataInputOutput.reset(), compression);
return dataInputOutput.toByteArray();
}
/**
* Serializes the value and writes it into <code>dataOutput</code>.
*
* @param value value to serialize
* @param dataOutput data output
* @throws IOException if an io error occurs
*/
public void serializeValue(Object value, DataOutput dataOutput)
throws IOException {
serializeObject(value, dataOutput, compression);
}
/**
* Serialization implementation.
*
* @param obj object to serialize
* @param useCompression use compression
* @return serialized object in bytes
* @throws IOException if an io error occurs
*/
private void serializeObject(Object obj, DataOutput dataOutput, boolean useCompression)
throws IOException {
//Cast to primitive arrays if necessary
if (obj != null && obj.getClass().isArray()) {
if (obj instanceof Integer[]) {
obj = (int[]) getPrimitiveArray((Object[]) obj);
} else if (obj instanceof Boolean[]) {
obj = (boolean[]) getPrimitiveArray((Object[]) obj);
} else if (obj instanceof Byte[]) {
obj = (byte[]) getPrimitiveArray((Object[]) obj);
} else if (obj instanceof Character[]) {
obj = (char[]) getPrimitiveArray((Object[]) obj);
} else if (obj instanceof Double[]) {
obj = (double[]) getPrimitiveArray((Object[]) obj);
} else if (obj instanceof Float[]) {
obj = (float[]) getPrimitiveArray((Object[]) obj);
} else if (obj instanceof Long[]) {
obj = (long[]) getPrimitiveArray((Object[]) obj);
} else if (obj instanceof Short[]) {
obj = (short[]) getPrimitiveArray((Object[]) obj);
} else if (obj instanceof Integer[][]) {
obj = (int[][]) getPrimitiveArray((Object[][]) obj);
} else if (obj instanceof Boolean[][]) {
obj = (boolean[][]) getPrimitiveArray((Object[][]) obj);
} else if (obj instanceof Byte[][]) {
obj = (byte[][]) getPrimitiveArray((Object[][]) obj);
} else if (obj instanceof Character[][]) {
obj = (char[][]) getPrimitiveArray((Object[][]) obj);
} else if (obj instanceof Double[][]) {
obj = (double[][]) getPrimitiveArray((Object[][]) obj);
} else if (obj instanceof Float[][]) {
obj = (float[][]) getPrimitiveArray((Object[][]) obj);
} else if (obj instanceof Long[][]) {
obj = (long[][]) getPrimitiveArray((Object[][]) obj);
} else if (obj instanceof Short[][]) {
obj = (short[][]) getPrimitiveArray((Object[][]) obj);
}
}
serialize(dataOutput, obj, useCompression);
}
/**
* Returns true if compression is enabled.
*
* @return true if enabled, false otherwise
*/
public boolean isCompressionEnabled() {
return compression;
}
// UTILITIES
private static Object getPrimitiveArray(Object[][] array) {
Class arrayClass = array.getClass().getComponentType().getComponentType();
if (!arrayClass.isPrimitive()) {
Class primitiveClass = getPrimitiveType(arrayClass);
int arrayLength = array.length;
Object primitiveArray = Array.newInstance(primitiveClass, arrayLength, 0);
for (int i = 0; i < arrayLength; i++) {
Object[] obj = array[i];
if (obj != null) {
Object innerArray = Array.newInstance(primitiveClass, obj.length);
for (int j = 0; j < obj.length; j++) {
Object iobj = obj[j];
if (iobj != null) {
Array.set(innerArray, j, iobj);
}
}
Array.set(primitiveArray, i, innerArray);
}
}
return primitiveArray;
}
return null;
}
private static Object getPrimitiveArray(Object[] array) {
Class arrayClass = array.getClass().getComponentType();
if (!arrayClass.isPrimitive()) {
Class primitiveClass = getPrimitiveType(arrayClass);
int arrayLength = array.length;
Object primitiveArray = Array.newInstance(primitiveClass, arrayLength);
for (int i = 0; i < arrayLength; i++) {
Object obj = array[i];
if (obj != null) {
Array.set(primitiveArray, i, obj);
}
}
return primitiveArray;
}
return array;
}
private static Class getPrimitiveType(Class type) {
if (!type.isPrimitive()) {
if (type.equals(Boolean.class)) {
return boolean.class;
} else if (type.equals(Integer.class)) {
return int.class;
} else if (type.equals(Short.class)) {
return short.class;
} else if (type.equals(Long.class)) {
return long.class;
} else if (type.equals(Byte.class)) {
return byte.class;
} else if (type.equals(Float.class)) {
return float.class;
} else if (type.equals(Double.class)) {
return double.class;
} else if (type.equals(Character.class)) {
return char.class;
}
}
throw new IllegalArgumentException("The type should be a wrapped primitive");
}
// SERIALIZATION
final static int NULL_ID = -1;
final static int NULL = 0;
final static int BOOLEAN_TRUE = 2;
final static int BOOLEAN_FALSE = 3;
final static int INTEGER_MINUS_1 = 4;
final static int INTEGER_0 = 5;
final static int INTEGER_1 = 6;
final static int INTEGER_2 = 7;
final static int INTEGER_3 = 8;
final static int INTEGER_4 = 9;
final static int INTEGER_5 = 10;
final static int INTEGER_6 = 11;
final static int INTEGER_7 = 12;
final static int INTEGER_8 = 13;
final static int INTEGER_255 = 14;
final static int INTEGER_PACK_NEG = 15;
final static int INTEGER_PACK = 16;
final static int LONG_MINUS_1 = 17;
final static int LONG_0 = 18;
final static int LONG_1 = 19;
final static int LONG_2 = 20;
final static int LONG_3 = 21;
final static int LONG_4 = 22;
final static int LONG_5 = 23;
final static int LONG_6 = 24;
final static int LONG_7 = 25;
final static int LONG_8 = 26;
final static int LONG_PACK_NEG = 27;
final static int LONG_PACK = 28;
final static int LONG_255 = 29;
final static int LONG_MINUS_MAX = 30;
final static int SHORT_MINUS_1 = 31;
final static int SHORT_0 = 32;
final static int SHORT_1 = 33;
final static int SHORT_255 = 34;
final static int SHORT_FULL = 35;
final static int BYTE_MINUS_1 = 36;
final static int BYTE_0 = 37;
final static int BYTE_1 = 38;
final static int BYTE_FULL = 39;
final static int CHAR = 40;
final static int FLOAT_MINUS_1 = 41;
final static int FLOAT_0 = 42;
final static int FLOAT_1 = 43;
final static int FLOAT_255 = 44;
final static int FLOAT_SHORT = 45;
final static int FLOAT_FULL = 46;
final static int DOUBLE_MINUS_1 = 47;
final static int DOUBLE_0 = 48;
final static int DOUBLE_1 = 49;
final static int DOUBLE_255 = 50;
final static int DOUBLE_SHORT = 51;
final static int DOUBLE_FULL = 52;
final static int DOUBLE_ARRAY = 53;
final static int BIGDECIMAL = 54;
final static int BIGINTEGER = 55;
final static int FLOAT_ARRAY = 56;
final static int INTEGER_MINUS_MAX = 57;
final static int SHORT_ARRAY = 58;
final static int BOOLEAN_ARRAY = 59;
final static int ARRAY_INT_B = 60;
final static int ARRAY_INT_S = 61;
final static int ARRAY_INT_I = 62;
final static int ARRAY_INT_PACKED = 63;
final static int ARRAY_LONG_B = 64;
final static int ARRAY_LONG_S = 65;
final static int ARRAY_LONG_I = 66;
final static int ARRAY_LONG_L = 67;
final static int ARRAY_LONG_PACKED = 68;
final static int CHAR_ARRAY = 69;
final static int BYTE_ARRAY = 70;
final static int STRING_ARRAY = 71;
final static int ARRAY_OBJECT = 72;
final static int STRING_EMPTY = 101;
final static int NOTUSED_STRING_C = 102;
final static int STRING = 103;
final static int ARRAY_INT_C = 104;
final static int ARRAY_LONG_C = 105;
final static int DOUBLE_ARRAY_C = 106;
final static int FLOAT_ARRAY_C = 107;
final static int CHAR_ARRAY_C = 108;
final static int BYTE_ARRAY_C = 109;
final static int SHORT_ARRAY_C = 110;
final static int INT_INT_ARRAY = 111;
final static int LONG_LONG_ARRAY = 112;
final static int CLASS = 113;
final static int CUSTOM = 114;
final static String EMPTY_STRING = "";
byte[] serialize(Object obj)
throws IOException {
return serialize(obj, false);
}
byte[] serialize(Object obj, boolean compress)
throws IOException {
DataInputOutput ba = new DataInputOutput();
serialize(ba, obj, compress);
return ba.toByteArray();
}
private void serialize(final DataOutput out, final Object obj)
throws IOException {
serialize(out, obj, false);
}
private void serialize(final DataOutput out, final Object obj, boolean compress)
throws IOException {
final Class clazz = obj != null ? obj.getClass() : null;
if (obj == null) {
out.write(NULL);
} else if (clazz == Boolean.class) {
if (((Boolean) obj).booleanValue()) {
out.write(BOOLEAN_TRUE);
} else {
out.write(BOOLEAN_FALSE);
}
} else if (clazz == Integer.class) {
serializeInt(out, (Integer) obj);
} else if (clazz == Double.class) {
serializeDouble(out, (Double) obj);
} else if (clazz == Float.class) {
serializeFloat(out, (Float) obj);
} else if (clazz == Long.class) {
serializeLong(out, (Long) obj);
} else if (clazz == BigInteger.class) {
serializeBigInteger(out, (BigInteger) obj);
} else if (clazz == BigDecimal.class) {
serializeBigDecimal(out, (BigDecimal) obj);
} else if (clazz == Short.class) {
serializeShort(out, (Short) obj);
} else if (clazz == Byte.class) {
serializeByte(out, (Byte) obj);
} else if (clazz == Character.class) {
serializeChar(out, (Character) obj);
} else if (clazz == String.class) {
serializeString(out, (String) obj);
} else if (obj instanceof Class) {
serializeClass(out, (Class) obj);
} else if (obj instanceof int[]) {
serializeIntArray(out, (int[]) obj, compress);
} else if (obj instanceof long[]) {
serializeLongArray(out, (long[]) obj, compress);
} else if (obj instanceof short[]) {
serializeShortArray(out, (short[]) obj, compress);
} else if (obj instanceof boolean[]) {
serializeBooleanArray(out, (boolean[]) obj);
} else if (obj instanceof double[]) {
serializeDoubleArray(out, (double[]) obj, compress);
} else if (obj instanceof float[]) {
serializeFloatArray(out, (float[]) obj, compress);
} else if (obj instanceof char[]) {
serializeCharArray(out, (char[]) obj, compress);
} else if (obj instanceof byte[]) {
serializeByteArray(out, (byte[]) obj, compress);
} else if (obj instanceof String[]) {
serializeStringArray(out, (String[]) obj);
} else if (obj instanceof int[][]) {
serializeIntIntArray(out, (int[][]) obj, compress);
} else if (obj instanceof long[][]) {
serializeLongLongArray(out, (long[][]) obj, compress);
} else {
// Custom
Serializer serializer = serializers.getSerializer(obj.getClass());
if (serializer != null) {
int index = serializers.getIndex(obj.getClass());
out.write(CUSTOM + index);
serializer.write(out, obj);
} else if (obj instanceof Object[]) {
serializeObjectArray(out, (Object[]) obj);
} else {
throw new UnsupportedTypeException(obj);
}
}
}
private static void serializeInt(final DataOutput out, final int val)
throws IOException {
if (val == -1) {
out.write(INTEGER_MINUS_1);
} else if (val == 0) {
out.write(INTEGER_0);
} else if (val == 1) {
out.write(INTEGER_1);
} else if (val == 2) {
out.write(INTEGER_2);
} else if (val == 3) {
out.write(INTEGER_3);
} else if (val == 4) {
out.write(INTEGER_4);
} else if (val == 5) {
out.write(INTEGER_5);
} else if (val == 6) {
out.write(INTEGER_6);
} else if (val == 7) {
out.write(INTEGER_7);
} else if (val == 8) {
out.write(INTEGER_8);
} else if (val == Integer.MIN_VALUE) {
out.write(INTEGER_MINUS_MAX);
} else if (val > 0 && val < 255) {
out.write(INTEGER_255);
out.write(val);
} else if (val < 0) {
out.write(INTEGER_PACK_NEG);
LongPacker.packInt(out, -val);
} else {
out.write(INTEGER_PACK);
LongPacker.packInt(out, val);
}
}
private static void serializeDouble(final DataOutput out, final double val)
throws IOException {
if (val == -1d) {
out.write(DOUBLE_MINUS_1);
} else if (val == 0d) {
out.write(DOUBLE_0);
} else if (val == 1d) {
out.write(DOUBLE_1);
} else if (val >= 0 && val <= 255 && (int) val == val) {
out.write(DOUBLE_255);
out.write((int) val);
} else if (val >= Short.MIN_VALUE && val <= Short.MAX_VALUE && (short) val == val) {
out.write(DOUBLE_SHORT);
out.writeShort((int) val);
} else {
out.write(DOUBLE_FULL);
out.writeDouble(val);
}
}
private static void serializeFloat(final DataOutput out, final float val)
throws IOException {
if (val == -1f) {
out.write(FLOAT_MINUS_1);
} else if (val == 0f) {
out.write(FLOAT_0);
} else if (val == 1f) {
out.write(FLOAT_1);
} else if (val >= 0 && val <= 255 && (int) val == val) {
out.write(FLOAT_255);
out.write((int) val);
} else if (val >= Short.MIN_VALUE && val <= Short.MAX_VALUE && (short) val == val) {
out.write(FLOAT_SHORT);
out.writeShort((int) val);
} else {
out.write(FLOAT_FULL);
out.writeFloat(val);
}
}
private static void serializeShort(final DataOutput out, final short val)
throws IOException {
if (val == -1) {
out.write(SHORT_MINUS_1);
} else if (val == 0) {
out.write(SHORT_0);
} else if (val == 1) {
out.write(SHORT_1);
} else if (val > 0 && val < 255) {
out.write(SHORT_255);
out.write(val);
} else {
out.write(SHORT_FULL);
out.writeShort(val);
}
}
private static void serializeByte(final DataOutput out, final byte val)
throws IOException {
if (val == -1) {
out.write(BYTE_MINUS_1);
} else if (val == 0) {
out.write(BYTE_0);
} else if (val == 1) {
out.write(BYTE_1);
} else {
out.write(BYTE_FULL);
out.writeByte(val);
}
}
private static void serializeLong(final DataOutput out, final long val)
throws IOException {
if (val == -1) {
out.write(LONG_MINUS_1);
} else if (val == 0) {
out.write(LONG_0);
} else if (val == 1) {
out.write(LONG_1);
} else if (val == 2) {
out.write(LONG_2);
} else if (val == 3) {
out.write(LONG_3);
} else if (val == 4) {
out.write(LONG_4);
} else if (val == 5) {
out.write(LONG_5);
} else if (val == 6) {
out.write(LONG_6);
} else if (val == 7) {
out.write(LONG_7);
} else if (val == 8) {
out.write(LONG_8);
} else if (val == Long.MIN_VALUE) {
out.write(LONG_MINUS_MAX);
} else if (val > 0 && val < 255) {
out.write(LONG_255);
out.write((int) val);
} else if (val < 0) {
out.write(LONG_PACK_NEG);
LongPacker.packLong(out, -val);
} else {
out.write(LONG_PACK);
LongPacker.packLong(out, val);
}
}
private static void serializeChar(final DataOutput out, final char val)
throws IOException {
out.write(CHAR);
out.writeChar(val);
}
private static void serializeString(final DataOutput out, final String val)
throws IOException {
if (val.length() == 0) {
out.write(STRING_EMPTY);
} else {
out.write(STRING);
final int len = val.length();
LongPacker.packInt(out, len);
for (int i = 0; i < len; i++) {
int c = (int) val.charAt(i); //TODO investigate if c could be negative here
LongPacker.packInt(out, c);
}
}
}
private static void serializeBigInteger(final DataOutput out, final BigInteger val)
throws IOException {
out.write(BIGINTEGER);
byte[] buf = val.toByteArray();
serializeByteArray(out, buf, false);
}
private static void serializeBigDecimal(final DataOutput out, final BigDecimal val)
throws IOException {
out.write(BIGDECIMAL);
serializeByteArray(out, val.unscaledValue().toByteArray(), false);
LongPacker.packInt(out, val.scale());
}
private static void serializeClass(final DataOutput out, final Class val)
throws IOException {
out.write(CLASS);
serializeString(out, val.getName());
}
private static void serializeBooleanArray(final DataOutput out, final boolean[] val)
throws IOException {
out.write(BOOLEAN_ARRAY);
LongPacker.packInt(out, val.length);
for (boolean s : val) {
out.writeBoolean(s);
}
}
private static void serializeShortArray(final DataOutput out, final short[] val, boolean compress)
throws IOException {
if (compress && val.length > 250) {
out.write(SHORT_ARRAY_C);
byte[] b = Snappy.compress(val);
LongPacker.packInt(out, b.length);
out.write(b);
} else {
out.write(SHORT_ARRAY);
LongPacker.packInt(out, val.length);
for (short s : val) {
out.writeShort(s);
}
}
}
private static void serializeDoubleArray(final DataOutput out, final double[] val, boolean compress)
throws IOException {
if (compress && val.length > 250) {
out.write(DOUBLE_ARRAY_C);
byte[] b = Snappy.compress(val);
LongPacker.packInt(out, b.length);
out.write(b);
} else {
out.write(DOUBLE_ARRAY);
LongPacker.packInt(out, val.length);
for (double s : val) {
out.writeDouble(s);
}
}
}
private static void serializeFloatArray(final DataOutput out, final float[] val, boolean compress)
throws IOException {
if (compress && val.length > 250) {
out.write(FLOAT_ARRAY_C);
byte[] b = Snappy.compress(val);
LongPacker.packInt(out, b.length);
out.write(b);
} else {
out.write(FLOAT_ARRAY);
LongPacker.packInt(out, val.length);
for (float s : val) {
out.writeFloat(s);
}
}
}
private static void serializeCharArray(final DataOutput out, final char[] val, boolean compress)
throws IOException {
if (compress && val.length > 250) {
out.write(CHAR_ARRAY_C);
byte[] b = Snappy.compress(val);
LongPacker.packInt(out, b.length);
out.write(b);
} else {
out.write(CHAR_ARRAY);
LongPacker.packInt(out, val.length);
for (char s : val) {
out.writeChar(s);
}
}
}
private static void serializeIntArray(final DataOutput out, final int[] val, boolean compress)
throws IOException {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i : val) {
max = Math.max(max, i);
min = Math.min(min, i);
}
if (0 <= min && max <= 255) {
out.write(ARRAY_INT_B);
LongPacker.packInt(out, val.length);
for (int i : val) {
out.write(i);
}
} else if (min >= Short.MIN_VALUE && max <= Short.MAX_VALUE) {
out.write(ARRAY_INT_S);
LongPacker.packInt(out, val.length);
for (int i : val) {
out.writeShort(i);
}
} else if (compress && val.length > 250) {
out.write(ARRAY_INT_C);
byte[] b = Snappy.compress(val);
LongPacker.packInt(out, b.length);
out.write(b);
} else if (min >= 0) {
out.write(ARRAY_INT_PACKED);
LongPacker.packInt(out, val.length);
for (int l : val) {
LongPacker.packInt(out, l);
}
} else {
out.write(ARRAY_INT_I);
LongPacker.packInt(out, val.length);
for (int i : val) {
out.writeInt(i);
}
}
}
private static void serializeIntIntArray(final DataOutput out, final int[][] val, boolean compress)
throws IOException {
out.write(INT_INT_ARRAY);
LongPacker.packInt(out, val.length);
for (int[] v : val) {
serializeIntArray(out, v, compress);
}
}
private static void serializeLongArray(final DataOutput out, final long[] val, boolean compress)
throws IOException {
long max = Long.MIN_VALUE;
long min = Long.MAX_VALUE;
for (long i : val) {
max = Math.max(max, i);
min = Math.min(min, i);
}
if (0 <= min && max <= 255) {
out.write(ARRAY_LONG_B);
LongPacker.packInt(out, val.length);
for (long l : val) {
out.write((int) l);
}
} else if (min >= Short.MIN_VALUE && max <= Short.MAX_VALUE) {
out.write(ARRAY_LONG_S);
LongPacker.packInt(out, val.length);
for (long l : val) {
out.writeShort((short) l);
}
} else if (compress && val.length > 250) {
out.write(ARRAY_LONG_C);
byte[] b = Snappy.compress(val);
LongPacker.packInt(out, b.length);
out.write(b);
} else if (0 <= min && max <= Long.MAX_VALUE) {
out.write(ARRAY_LONG_PACKED);
LongPacker.packInt(out, val.length);
for (long l : val) {
LongPacker.packLong(out, l);
}
} else if (Integer.MIN_VALUE <= min && max <= Integer.MAX_VALUE) {
out.write(ARRAY_LONG_I);
LongPacker.packInt(out, val.length);
for (long l : val) {
out.writeInt((int) l);
}
} else {
out.write(ARRAY_LONG_L);
LongPacker.packInt(out, val.length);
for (long l : val) {
out.writeLong(l);
}
}
}
private static void serializeLongLongArray(final DataOutput out, final long[][] val, boolean compress)
throws IOException {
out.write(LONG_LONG_ARRAY);
LongPacker.packInt(out, val.length);
for (long[] v : val) {
serializeLongArray(out, v, compress);
}
}
private static void serializeByteArray(final DataOutput out, final byte[] val, boolean compress)
throws IOException {
if (compress && val.length > 250) {
out.write(BYTE_ARRAY_C);
byte[] b = Snappy.compress(val);
LongPacker.packInt(out, b.length);
out.write(b);
} else {
out.write(BYTE_ARRAY);
LongPacker.packInt(out, val.length);
out.write(val);
}
}
private static void serializeStringArray(final DataOutput out, final String[] val)
throws IOException {
out.write(STRING_ARRAY);
LongPacker.packInt(out, val.length);
for (String s : val) {
serializeString(out, s);
}
}
private void serializeObjectArray(final DataOutput out, final Object[] val)
throws IOException {
out.write(ARRAY_OBJECT);
LongPacker.packInt(out, val.length);
for (Object o : val) {
serialize(out, o);
}
}
public Object deserialize(byte[] buf)
throws ClassNotFoundException, IOException {
DataInputOutput bs = new DataInputOutput(buf);
Object ret = deserialize(bs);
if (bs.available() != 0) {
throw new RuntimeException("bytes left: " + bs.available());
}
return ret;
}
public Object deserialize(DataInput is)
throws IOException, ClassNotFoundException {
Object ret = null;
final int head = is.readUnsignedByte();
if (head >= CUSTOM) {
Serializer serializer = serializers.getSerializer(head - CUSTOM);
ret = serializer.read(is);
} else {
switch (head) {
case NULL:
break;
case BOOLEAN_TRUE:
ret = Boolean.TRUE;
break;
case BOOLEAN_FALSE:
ret = Boolean.FALSE;
break;
case INTEGER_MINUS_1:
ret = Integer.valueOf(-1);
break;
case INTEGER_0:
ret = Integer.valueOf(0);
break;
case INTEGER_1:
ret = Integer.valueOf(1);
break;
case INTEGER_2:
ret = Integer.valueOf(2);
break;
case INTEGER_3:
ret = Integer.valueOf(3);
break;
case INTEGER_4:
ret = Integer.valueOf(4);
break;
case INTEGER_5:
ret = Integer.valueOf(5);
break;
case INTEGER_6:
ret = Integer.valueOf(6);
break;
case INTEGER_7:
ret = Integer.valueOf(7);
break;
case INTEGER_8:
ret = Integer.valueOf(8);
break;
case INTEGER_MINUS_MAX:
ret = Integer.valueOf(Integer.MIN_VALUE);
break;
case INTEGER_255:
ret = Integer.valueOf(is.readUnsignedByte());
break;
case INTEGER_PACK_NEG:
ret = Integer.valueOf(-LongPacker.unpackInt(is));
break;
case INTEGER_PACK:
ret = Integer.valueOf(LongPacker.unpackInt(is));
break;
case LONG_MINUS_1:
ret = Long.valueOf(-1);
break;
case LONG_0:
ret = Long.valueOf(0);
break;
case LONG_1:
ret = Long.valueOf(1);
break;
case LONG_2:
ret = Long.valueOf(2);
break;
case LONG_3:
ret = Long.valueOf(3);
break;
case LONG_4:
ret = Long.valueOf(4);
break;
case LONG_5:
ret = Long.valueOf(5);
break;
case LONG_6:
ret = Long.valueOf(6);
break;
case LONG_7:
ret = Long.valueOf(7);
break;
case LONG_8:
ret = Long.valueOf(8);
break;
case LONG_255:
ret = Long.valueOf(is.readUnsignedByte());
break;
case LONG_PACK_NEG:
ret = Long.valueOf(-LongPacker.unpackLong(is));
break;
case LONG_PACK:
ret = Long.valueOf(LongPacker.unpackLong(is));
break;
case LONG_MINUS_MAX:
ret = Long.valueOf(Long.MIN_VALUE);
break;
case SHORT_MINUS_1:
ret = Short.valueOf((short) -1);
break;
case SHORT_0:
ret = Short.valueOf((short) 0);
break;
case SHORT_1:
ret = Short.valueOf((short) 1);
break;
case SHORT_255:
ret = Short.valueOf((short) is.readUnsignedByte());
break;
case SHORT_FULL:
ret = Short.valueOf(is.readShort());
break;
case BYTE_MINUS_1:
ret = Byte.valueOf((byte) -1);
break;
case BYTE_0:
ret = Byte.valueOf((byte) 0);
break;
case BYTE_1:
ret = Byte.valueOf((byte) 1);
break;
case BYTE_FULL:
ret = Byte.valueOf(is.readByte());
break;
case SHORT_ARRAY:
ret = deserializeShortArray(is);
break;
case BOOLEAN_ARRAY:
ret = deserializeBooleanArray(is);
break;
case DOUBLE_ARRAY:
ret = deserializeDoubleArray(is);
break;
case FLOAT_ARRAY:
ret = deserializeFloatArray(is);
break;
case CHAR_ARRAY:
ret = deserializeCharArray(is);
break;
case SHORT_ARRAY_C:
ret = deserializeShortCompressedArray(is);
break;
case DOUBLE_ARRAY_C:
ret = deserializeDoubleCompressedArray(is);
break;
case FLOAT_ARRAY_C:
ret = deserializeFloatCompressedArray(is);
break;
case CHAR_ARRAY_C:
ret = deserializeCharCompressedArray(is);
break;
case CHAR:
ret = Character.valueOf(is.readChar());
break;
case FLOAT_MINUS_1:
ret = Float.valueOf(-1);
break;
case FLOAT_0:
ret = Float.valueOf(0);
break;
case FLOAT_1:
ret = Float.valueOf(1);
break;
case FLOAT_255:
ret = Float.valueOf(is.readUnsignedByte());