gaob13 / kryo

Automatically exported from code.google.com/p/kryo
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Forward and backward compatibility #12

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Current Kryo object graph serializers (eg, FieldSerializer) use the Java
class definition as a schema. This reduces serialized size because the data
for an object's fields is written in a known order; there is no need to
write field identifiers. The drawback is that any change to the class
definition invalidates serialized bytes made with the old class definition.

Both scenarios should be supported. Some uses of Kryo have short-lived
serialized bytes (eg, IPC or a networked game) and gladly sacrifice
forward/backward compatibility for smaller serialized size. Other uses keep
serialized bytes around for a long time (eg, storing them on disk or in a
database) and it would be unacceptable to invalidate all existing data when
a class is modified.

To support forward/backward compatibility, during serialization, the first
time an object type is encountered the field names would be written. This
defines the order the field values will appear in the serialized bytes for
that type. If a field no longer exists, it is ignored. Fields not in the
serialized bytes are left unchanged.

It isn't terribly efficient to write the field name as Strings. Possibly an
annotation could be provided that would allow fields to be given an
ordinal, allowing the field to be referenced with just 1 byte rather than 1
byte per character in its name.

Original issue reported on code.google.com by nathan.s...@gmail.com on 25 Mar 2010 at 1:11

GoogleCodeExporter commented 9 years ago
A first draft for this issue is checked in, r85. The class is currently named
CompatibleFieldSerializer. It supports adding and/or removing fields, which 
does not
invalidate previously serialized bytes. Note that it does not support changing 
the
type of a field.

The overhead compared to FieldSerializer is the header that is output the first 
time
an object of the type is serialized. The header consists of an int for the 
number of
fields, then a String for each field name. Also, in order to support skipping 
the
bytes for a field that no longer exists, for each field value I had to write an 
int
that is the length of the value in bytes.

Original comment by nathan.s...@gmail.com on 26 Mar 2010 at 4:12

GoogleCodeExporter commented 9 years ago
We've got tests and everything. Calling it good for now.

Original comment by nathan.s...@gmail.com on 12 Apr 2010 at 9:45

GoogleCodeExporter commented 9 years ago
Hi, any examples how to use this ?

Original comment by dimec...@gmail.com on 27 Oct 2011 at 5:22