Repository navigation
Expand file tree
/
Copy pathSerializer.java
More file actions
580 lines (469 loc) · 16.6 KB
/
Copy pathSerializer.java
File metadata and controls
580 lines (469 loc) · 16.6 KB
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
/*
* Copyright (c) 2012 Jan Kotek
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapdb;
import java.io.*;
import java.nio.charset.Charset;
import java.util.UUID;
/**
* Provides serialization and deserialization
*
* @author Jan Kotek
*/
public interface Serializer<A> {
/**
* Serialize the content of an object into a ObjectOutput
*
* @param out ObjectOutput to save object into
* @param value Object to serialize
*/
public void serialize( DataOutput out, A value)
throws IOException;
/**
* Deserialize the content of an object from a DataInput.
*
* @param in to read serialized data from
* @param available how many bytes are available in DataInput for reading, may be -1 (in streams) or 0 (null).
* @return deserialized object
* @throws java.io.IOException
*/
public A deserialize( DataInput in, int available)
throws IOException;
/**
* Data could be serialized into record with variable size or fixed size.
* Some optimizations can be applied to serializers with fixed size
*
* @return fixed size or -1 for variable size
*/
public int fixedSize();
/**
* Serializes strings using UTF8 encoding.
* Stores string size so can be used as collection serializer.
* Does not handle null values
*/
Serializer<String> STRING = new Serializer<String>() {
@Override
public void serialize(DataOutput out, String value) throws IOException {
out.writeUTF(value);
}
@Override
public String deserialize(DataInput in, int available) throws IOException {
return in.readUTF();
}
@Override
public int fixedSize() {
return -1;
}
};
/**
* Serializes strings using UTF8 encoding.
* Deserialized String is interned {@link String#intern()},
* so it could save some memory.
*
* Stores string size so can be used as collection serializer.
* Does not handle null values
*/
Serializer<String> STRING_INTERN = new Serializer<String>() {
@Override
public void serialize(DataOutput out, String value) throws IOException {
out.writeUTF(value);
}
@Override
public String deserialize(DataInput in, int available) throws IOException {
return in.readUTF().intern();
}
@Override
public int fixedSize() {
return -1;
}
};
/**
* Serializes strings using ASCII encoding (8 bit character).
* Is faster compared to UTF8 encoding.
* Stores string size so can be used as collection serializer.
* Does not handle null values
*/
Serializer<String> STRING_ASCII = new Serializer<String>() {
@Override
public void serialize(DataOutput out, String value) throws IOException {
char[] cc = new char[value.length()];
//TODO does this really works? is not char 2 byte unsigned?
value.getChars(0,cc.length,cc,0);
DataOutput2.packInt(out,cc.length);
for(char c:cc){
out.write(c);
}
}
@Override
public String deserialize(DataInput in, int available) throws IOException {
int size = DataInput2.unpackInt(in);
char[] cc = new char[size];
for(int i=0;i<size;i++){
cc[i] = (char) in.readUnsignedByte();
}
return new String(cc);
}
@Override
public int fixedSize() {
return -1;
}
};
/**
* Serializes strings using UTF8 encoding.
* Used mainly for testing.
* Does not handle null values.
*/
Serializer<String> STRING_NOSIZE = new Serializer<String>() {
private final Charset UTF8_CHARSET = Charset.forName("UTF8");
@Override
public void serialize(DataOutput out, String value) throws IOException {
final byte[] bytes = value.getBytes(UTF8_CHARSET);
out.write(bytes);
}
@Override
public String deserialize(DataInput in, int available) throws IOException {
if(available==-1) throw new IllegalArgumentException("STRING_NOSIZE does not work with collections.");
byte[] bytes = new byte[available];
in.readFully(bytes);
return new String(bytes, UTF8_CHARSET);
}
@Override
public int fixedSize() {
return -1;
}
};
/** Serializes Long into 8 bytes, used mainly for testing.
* Does not handle null values.*/
Serializer<Long> LONG = new Serializer<Long>() {
@Override
public void serialize(DataOutput out, Long value) throws IOException {
if(value != null)
out.writeLong(value);
}
@Override
public Long deserialize(DataInput in, int available) throws IOException {
if(available==0) return null;
return in.readLong();
}
@Override
public int fixedSize() {
return 8;
}
};
/** Serializes Integer into 4 bytes.
* Does not handle null values.*/
Serializer<Integer> INTEGER = new Serializer<Integer>() {
@Override
public void serialize(DataOutput out, Integer value) throws IOException {
out.writeInt(value);
}
@Override
public Integer deserialize(DataInput in, int available) throws IOException {
return in.readInt();
}
@Override
public int fixedSize() {
return 4;
}
};
Serializer<Boolean> BOOLEAN = new Serializer<Boolean>() {
@Override
public void serialize(DataOutput out, Boolean value) throws IOException {
out.writeBoolean(value);
}
@Override
public Boolean deserialize(DataInput in, int available) throws IOException {
if(available==0) return null;
return in.readBoolean();
}
@Override
public int fixedSize() {
return 1;
}
};
/**
* Always throws {@link IllegalAccessError} when invoked. Useful for testing and assertions.
*/
Serializer<Object> ILLEGAL_ACCESS = new Serializer<Object>() {
@Override
public void serialize(DataOutput out, Object value) throws IOException {
throw new IllegalAccessError();
}
@Override
public Object deserialize(DataInput in, int available) throws IOException {
throw new IllegalAccessError();
}
@Override
public int fixedSize() {
return -1;
}
};
/**
* Basic serializer for most classes in 'java.lang' and 'java.util' packages.
* It does not handle custom POJO classes. It also does not handle classes which
* require access to `DB` itself.
*/
@SuppressWarnings("unchecked")
Serializer<Object> BASIC = new SerializerBase();
/**
* Serializes `byte[]` it adds header which contains size information
*/
Serializer<byte[] > BYTE_ARRAY = new Serializer<byte[]>() {
@Override
public void serialize(DataOutput out, byte[] value) throws IOException {
DataOutput2.packInt(out,value.length);
out.write(value);
}
@Override
public byte[] deserialize(DataInput in, int available) throws IOException {
int size = DataInput2.unpackInt(in);
byte[] ret = new byte[size];
in.readFully(ret);
return ret;
}
@Override
public int fixedSize() {
return -1;
}
} ;
/**
* Serializes `byte[]` directly into underlying store
* It does not store size, so it can not be used in Maps and other collections.
*/
Serializer<byte[] > BYTE_ARRAY_NOSIZE = new Serializer<byte[]>() {
@Override
public void serialize(DataOutput out, byte[] value) throws IOException {
if(value==null||value.length==0) return;
out.write(value);
}
@Override
public byte[] deserialize(DataInput in, int available) throws IOException {
if(available==-1) throw new IllegalArgumentException("BYTE_ARRAY_NOSIZE does not work with collections.");
if(available==0) return null;
byte[] ret = new byte[available];
in.readFully(ret);
return ret;
}
@Override
public int fixedSize() {
return -1;
}
} ;
/**
* Serializes `char[]` it adds header which contains size information
*/
Serializer<char[] > CHAR_ARRAY = new Serializer<char[]>() {
@Override
public void serialize(DataOutput out, char[] value) throws IOException {
DataOutput2.packInt(out,value.length);
for(char c:value){
out.writeChar(c);
}
}
@Override
public char[] deserialize(DataInput in, int available) throws IOException {
final int size = DataInput2.unpackInt(in);
char[] ret = new char[size];
for(int i=0;i<size;i++){
ret[i] = in.readChar();
}
return ret;
}
@Override
public int fixedSize() {
return -1;
}
};
/**
* Serializes `int[]` it adds header which contains size information
*/
Serializer<int[] > INT_ARRAY = new Serializer<int[]>() {
@Override
public void serialize(DataOutput out, int[] value) throws IOException {
DataOutput2.packInt(out,value.length);
for(int c:value){
out.writeInt(c);
}
}
@Override
public int[] deserialize(DataInput in, int available) throws IOException {
final int size = DataInput2.unpackInt(in);
int[] ret = new int[size];
for(int i=0;i<size;i++){
ret[i] = in.readInt();
}
return ret;
}
@Override
public int fixedSize() {
return -1;
}
};
/**
* Serializes `long[]` it adds header which contains size information
*/
Serializer<long[] > LONG_ARRAY = new Serializer<long[]>() {
@Override
public void serialize(DataOutput out, long[] value) throws IOException {
DataOutput2.packInt(out,value.length);
for(long c:value){
out.writeLong(c);
}
}
@Override
public long[] deserialize(DataInput in, int available) throws IOException {
final int size = DataInput2.unpackInt(in);
long[] ret = new long[size];
for(int i=0;i<size;i++){
ret[i] = in.readLong();
}
return ret;
}
@Override
public int fixedSize() {
return -1;
}
};
/**
* Serializes `double[]` it adds header which contains size information
*/
Serializer<double[] > DOUBLE_ARRAY = new Serializer<double[]>() {
@Override
public void serialize(DataOutput out, double[] value) throws IOException {
DataOutput2.packInt(out,value.length);
for(double c:value){
out.writeDouble(c);
}
}
@Override
public double[] deserialize(DataInput in, int available) throws IOException {
final int size = DataInput2.unpackInt(in);
double[] ret = new double[size];
for(int i=0;i<size;i++){
ret[i] = in.readDouble();
}
return ret;
}
@Override
public int fixedSize() {
return -1;
}
};
/** Serializer which uses standard Java Serialization with {@link java.io.ObjectInputStream} and {@link java.io.ObjectOutputStream} */
Serializer<Object> JAVA = new Serializer<Object>() {
@Override
public void serialize(DataOutput out, Object value) throws IOException {
ObjectOutputStream out2 = new ObjectOutputStream((OutputStream) out);
out2.writeObject(value);
out2.flush();
}
@Override
public Object deserialize(DataInput in, int available) throws IOException {
try {
ObjectInputStream in2 = new ObjectInputStream((InputStream) in);
return in2.readObject();
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
}
@Override
public int fixedSize() {
return -1;
}
};
/** Serializers {@link java.util.UUID} class */
Serializer<java.util.UUID> UUID = new Serializer<java.util.UUID>() {
@Override
public void serialize(DataOutput out, UUID value) throws IOException {
out.writeLong(value.getMostSignificantBits());
out.writeLong(value.getLeastSignificantBits());
}
@Override
public UUID deserialize(DataInput in, int available) throws IOException {
return new UUID(in.readLong(), in.readLong());
}
@Override
public int fixedSize() {
return 16;
}
};
/** wraps another serializer and (de)compresses its output/input*/
public final static class CompressionWrapper<E> implements Serializer<E>, Serializable {
private static final long serialVersionUID = 4440826457939614346L;
protected final Serializer<E> serializer;
protected final ThreadLocal<CompressLZF> LZF = new ThreadLocal<CompressLZF>() {
@Override protected CompressLZF initialValue() {
return new CompressLZF();
}
};
public CompressionWrapper(Serializer<E> serializer) {
this.serializer = serializer;
}
/** used for deserialization */
protected CompressionWrapper(SerializerBase serializerBase, DataInput is, SerializerBase.FastArrayList<Object> objectStack) throws IOException {
objectStack.add(this);
this.serializer = (Serializer<E>) serializerBase.deserialize(is,objectStack);
}
@Override
public void serialize(DataOutput out, E value) throws IOException {
DataOutput2 out2 = new DataOutput2();
serializer.serialize(out2,value);
byte[] tmp = new byte[out2.pos+41];
int newLen;
try{
newLen = LZF.get().compress(out2.buf,out2.pos,tmp,0);
}catch(IndexOutOfBoundsException e){
newLen=0; //larger after compression
}
if(newLen>=out2.pos){
//compression adds size, so do not compress
DataOutput2.packInt(out,0);
out.write(out2.buf,0,out2.pos);
return;
}
DataOutput2.packInt(out, out2.pos+1); //unpacked size, zero indicates no compression
out.write(tmp,0,newLen);
}
@Override
public E deserialize(DataInput in, int available) throws IOException {
final int unpackedSize = DataInput2.unpackInt(in)-1;
if(unpackedSize==-1){
//was not compressed
return serializer.deserialize(in, available>0?available-1:available);
}
byte[] unpacked = new byte[unpackedSize];
LZF.get().expand(in,unpacked,0,unpackedSize);
DataIO.DataInputByteArray in2 = new DataIO.DataInputByteArray(unpacked);
E ret = serializer.deserialize(in2,unpackedSize);
assert(in2.pos==unpackedSize): "data were not fully read";
return ret;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CompressionWrapper that = (CompressionWrapper) o;
return serializer.equals(that.serializer);
}
@Override
public int hashCode() {
return serializer.hashCode();
}
@Override
public int fixedSize() {
return -1;
}
}
}