Class VersionedSerializer.Direct<TargetType>

  • Type Parameters:
    TargetType - Type of the object to serialize from and deserialize into.
    Enclosing class:
    VersionedSerializer<T>

    public abstract static class VersionedSerializer.Direct<TargetType>
    extends VersionedSerializer<TargetType>
    A Single-Type VersionedDeserializer that serializes and deserializes into the same object. This should be used in those cases when we already have an instance of the target object available during deserialization and we only need to update its state. Example: *
     
     class Segment { ... }
    
     class SegmentSerializer extends VersionedSerializer.Direct<Segment> {
        // This is the version we'll be serializing now. We have already introduced read support for Version 1, but
        // we cannot write into Version 1 until we know that all deployed code knows how to read it. In order to guarantee
        // a successful upgrade when changing Versions, all existing code needs to know how to read the new version.
        @Override
        protected byte getWriteVersion() { return 0; }
    
        @Override
        protected void declareVersions() {
          // We define all supported versions and their revisions here.
          version(0).revision(0, this::write00, this::read00)
                    .revision(1, this::write01, this::read01);
          version(1).revision(0, this::write10, this::read10);
        }
    
        // Serializes V0.0; this is the first method to be invoked when serializing V0.
        private void write00(Segment source, RevisionDataOutput output) throws IOException { ... }
    
        // Deserializes V0.0 into target; this is the first method to be invoked when deserializing V0.
        private void read00(RevisionDataInput input, Segment target) throws IOException { ... }
    
        // Serializes V0.1; this will be invoked after write00() and is an incremental update (only what's new in V0.1).
        private void write01(Segment source, RevisionDataOutput output) throws IOException { ... }
    
        // Deserializes V0.1 into target; will be invoked after read00().
        private void read01(RevisionDataInput input, Segment target) throws IOException { ... }
    
        // Serializes V1.0; this will be invoked if we ever serialize V1. This will not be mixed with V0.
        private void write10(Segment source, RevisionDataOutput output) throws IOException { ... }
    
        // Deserializes V1.0 into target; this will only be invoked for V1.
        private void read10(RevisionDataInput input, Segment target) throws IOException { ... }
     }
     }
     
    • Constructor Detail

      • Direct

        public Direct()