-
-
Notifications
You must be signed in to change notification settings - Fork 842
Expand file tree
/
Copy pathCompatibleFieldSerializer.java
More file actions
329 lines (291 loc) · 13.1 KB
/
Copy pathCompatibleFieldSerializer.java
File metadata and controls
329 lines (291 loc) · 13.1 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
/* Copyright (c) 2008-2020, Nathan Sweet
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
package com.esotericsoftware.kryo.serializers;
import static com.esotericsoftware.kryo.util.Util.*;
import static com.esotericsoftware.minlog.Log.*;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.Registration;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.InputChunked;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.io.OutputChunked;
import com.esotericsoftware.kryo.util.ObjectMap;
import com.esotericsoftware.kryo.util.Util;
import java.lang.reflect.Field;
import java.util.HashSet;
/** Serializes objects using direct field assignment, providing both forward and backward compatibility. This means fields can be
* added or removed without invalidating previously serialized bytes. Renaming or changing the type of a field is not supported.
* Like {@link FieldSerializer}, it can serialize most classes without needing annotations.
* <p>
* The forward and backward compatibility and serialization performance depend on
* {@link CompatibleFieldSerializerConfig#setReadUnknownFieldData(boolean)} and
* {@link CompatibleFieldSerializerConfig#setChunkedEncoding(boolean)}. Additionally, the first time the class is encountered in
* the serialized bytes, a simple schema is written containing the field name strings.
* <p>
* Note that the field data is identified by name. If a super class has a field with the same name as a subclass,
* {@link CompatibleFieldSerializerConfig#setExtendedFieldNames(boolean)} must be true.
* @author Nathan Sweet */
public class CompatibleFieldSerializer<T> extends FieldSerializer<T> {
private static final int binarySearchThreshold = 32;
private final CompatibleFieldSerializerConfig config;
public CompatibleFieldSerializer (Kryo kryo, Class type) {
this(kryo, type, new CompatibleFieldSerializerConfig());
}
public CompatibleFieldSerializer (Kryo kryo, Class type, CompatibleFieldSerializerConfig config) {
super(kryo, type, config);
this.config = config;
}
@Override
protected void initializeCachedFields() {
if (config != null && !config.extendedFieldNames) {
final HashSet hashSet = new HashSet();
CachedField[] fields = cachedFields.fields;
for (int i = 0, n = fields.length; i < n; i++) {
final Field field = fields[i].field;
if (!hashSet.add(field.getName())) {
if (WARN)
warn("Detected duplicate field " + field.getName() + " in class hierarchy "
+ field.getDeclaringClass()
+ ". Consider enabling FieldSerializerConfig.extendedFieldNames");
break;
}
}
}
}
@Override
public void write (Kryo kryo, Output output, T object) {
int pop = pushTypeVariables();
CachedField[] fields = cachedFields.fields;
ObjectMap context = kryo.getGraphContext();
if (!context.containsKey(this)) {
if (TRACE) trace("kryo", "Write fields for class: " + type.getName());
context.put(this, null);
output.writeVarInt(fields.length, true);
for (int i = 0, n = fields.length; i < n; i++) {
if (TRACE) trace("kryo", "Write field name: " + fields[i].name + pos(output.position()));
output.writeString(fields[i].name);
}
}
boolean chunked = config.chunked, readUnknownTagData = config.readUnknownFieldData;
Output fieldOutput;
OutputChunked outputChunked = null;
if (chunked)
fieldOutput = outputChunked = new OutputChunked(output, config.chunkSize);
else
fieldOutput = output;
for (int i = 0, n = fields.length; i < n; i++) {
CachedField cachedField = fields[i];
if (TRACE) log("Write", cachedField, output.position());
// Write the value class so the field data can be read even if the field is removed.
if (readUnknownTagData) {
Class valueClass = null;
try {
if (object != null) {
Object value = cachedField.field.get(object);
if (value != null) valueClass = value.getClass();
}
} catch (IllegalAccessException ex) {
}
kryo.writeClass(fieldOutput, valueClass);
if (valueClass == null) {
if (chunked) outputChunked.endChunk();
continue;
}
cachedField.setCanBeNull(false);
cachedField.setValueClass(valueClass);
cachedField.setReuseSerializer(false);
}
cachedField.write(fieldOutput, object);
if (chunked) outputChunked.endChunk();
}
popTypeVariables(pop);
}
@Override
public T read (Kryo kryo, Input input, Class<? extends T> type) {
int pop = pushTypeVariables();
T object = create(kryo, input, type);
kryo.reference(object);
CachedField[] fields = (CachedField[])kryo.getGraphContext().get(this);
if (fields == null) fields = readFields(kryo, input);
boolean chunked = config.chunked, readUnknownTagData = config.readUnknownFieldData;
Input fieldInput;
InputChunked inputChunked = null;
if (chunked)
fieldInput = inputChunked = new InputChunked(input, config.chunkSize);
else
fieldInput = input;
for (int i = 0, n = fields.length; i < n; i++) {
CachedField cachedField = fields[i];
if (readUnknownTagData) {
Registration registration;
try {
registration = kryo.readClass(fieldInput);
} catch (KryoException ex) {
String message = "Unable to read unknown data (unknown type). (" + getType().getName() + "#" + cachedField + ")";
if (!chunked) throw new KryoException(message, ex);
if (DEBUG) debug("kryo", message, ex);
inputChunked.nextChunk();
continue;
}
if (registration == null) {
if (chunked) inputChunked.nextChunk();
continue;
}
Class valueClass = registration.getType();
if (cachedField == null) {
// Read unknown data in case it is a reference.
if (TRACE) trace("kryo", "Read unknown data, type: " + className(valueClass) + pos(input.position()));
try {
kryo.readObject(fieldInput, valueClass);
} catch (KryoException ex) {
String message = "Unable to read unknown data, type: " + className(valueClass) + " (" + getType().getName()
+ "#" + cachedField + ")";
if (!chunked) throw new KryoException(message, ex);
if (DEBUG) debug("kryo", message, ex);
}
if (chunked) inputChunked.nextChunk();
continue;
}
// Ensure the type in the data is compatible with the field type.
if (cachedField.valueClass != null && !Util.isAssignableTo(valueClass, cachedField.field.getType())) {
String message = "Read type is incompatible with the field type: " + className(valueClass) + " -> "
+ className(cachedField.valueClass) + " (" + getType().getName() + "#" + cachedField + ")";
if (!chunked) throw new KryoException(message);
if (DEBUG) debug("kryo", message);
inputChunked.nextChunk();
continue;
}
cachedField.setCanBeNull(false);
cachedField.setValueClass(valueClass);
cachedField.setReuseSerializer(false);
} else if (cachedField == null) {
if (!chunked) throw new KryoException("Unknown field. (" + getType().getName() + ")");
if (TRACE) trace("kryo", "Skip unknown field.");
inputChunked.nextChunk();
continue;
}
if (TRACE) log("Read", cachedField, input.position());
cachedField.read(fieldInput, object);
if (chunked) inputChunked.nextChunk();
}
popTypeVariables(pop);
return object;
}
private CachedField[] readFields (Kryo kryo, Input input) {
if (TRACE) trace("kryo", "Read fields for class: " + type.getName());
int length = input.readVarInt(true);
String[] names = new String[length];
for (int i = 0; i < length; i++) {
names[i] = input.readString();
if (TRACE) trace("kryo", "Read field name: " + names[i]);
}
CachedField[] fields = new CachedField[length];
CachedField[] allFields = cachedFields.fields;
if (length < binarySearchThreshold) {
outer:
for (int i = 0; i < length; i++) {
String schemaName = names[i];
for (int ii = 0, nn = allFields.length; ii < nn; ii++) {
if (allFields[ii].name.equals(schemaName)) {
fields[i] = allFields[ii];
continue outer;
}
}
if (TRACE) trace("kryo", "Unknown field will be skipped: " + schemaName);
}
} else {
int low, mid, high, compare;
int lastFieldIndex = allFields.length;
outer:
for (int i = 0; i < length; i++) {
String schemaName = names[i];
low = 0;
high = lastFieldIndex;
while (low <= high) {
mid = (low + high) >>> 1;
compare = schemaName.compareTo(allFields[mid].name);
if (compare < 0)
high = mid - 1;
else if (compare > 0)
low = mid + 1;
else {
fields[i] = allFields[mid];
continue outer;
}
}
if (TRACE) trace("kryo", "Unknown field will be skipped: " + schemaName);
}
}
kryo.getGraphContext().put(this, fields);
return fields;
}
public CompatibleFieldSerializerConfig getCompatibleFieldSerializerConfig () {
return config;
}
/** Configuration for CompatibleFieldSerializer instances. */
public static class CompatibleFieldSerializerConfig extends FieldSerializerConfig {
boolean readUnknownFieldData = true, chunked;
int chunkSize = 1024;
@Override
public CompatibleFieldSerializerConfig clone () {
return (CompatibleFieldSerializerConfig)super.clone(); // Clone is ok as we have only primitive fields.
}
/** When false and encountering an unknown field, an exception is thrown or, if {@link #setChunkedEncoding(boolean) chunked
* encoding} is enabled, the data is skipped.
* <p>
* When true, the type of each field value is written before the value. When an unknown field is encountered, an attempt to
* read the data is made so if it is a reference then any other values in the object graph referencing that data can be
* deserialized. If reading the data fails (eg the class is unknown or has been removed) then an exception is thrown or, if
* {@link #setChunkedEncoding(boolean) chunked encoding} is enabled, the data is skipped.
* <p>
* In either case, if the data is skipped and {@link Kryo#setReferences(boolean) references} are enabled, then any
* references in the skipped data are not read and further deserialization receive the wrong references and fail.
* <p>
* Default is true. */
public void setReadUnknownFieldData (boolean readUnknownTagData) {
this.readUnknownFieldData = readUnknownTagData;
}
public boolean getReadUnknownTagData () {
return readUnknownFieldData;
}
/** When true, fields are written with chunked encoding to allow unknown field data to be skipped. Default is false.
* @see #setReadUnknownFieldData(boolean) */
public void setChunkedEncoding (boolean chunked) {
this.chunked = chunked;
if (TRACE) trace("kryo", "CompatibleFieldSerializerConfig setChunked: " + chunked);
}
public boolean getChunkedEncoding () {
return chunked;
}
@Override
public void setExtendedFieldNames(boolean extendedFieldNames) {
this.extendedFieldNames = extendedFieldNames;
if (TRACE) trace("kryo", "CompatibleFieldSerializerConfig extendedFieldNames: " + extendedFieldNames);
}
/** The maximum size of each chunk for chunked encoding. Default is 1024. */
public void setChunkSize (int chunkSize) {
this.chunkSize = chunkSize;
if (TRACE) trace("kryo", "CompatibleFieldSerializerConfig setChunkSize: " + chunkSize);
}
public int getChunkSize () {
return chunkSize;
}
}
}