Uploaded image for project: 'Cassandra'
  1. Cassandra
  2. CASSANDRA-16218

Simplify the almost duplicated code for calculating serialization size and serialization

Agile BoardAttach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskConvert to sub-taskMoveLinkCloneLabelsUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

    • Change Category:
      Code Clarity
    • Complexity:
      Normal
    • Platform:
      All
    • Impacts:
      None

      Description

      The current pattern of counting the serialization size and the actual serialization in the codebase is error-prone and hard to maintain. Those 2 code paths have almost the same code repeated. 
       
      The pattern can be found in org.apache.cassandra.net.Message#Serializer and numerous locations that use org.apache.cassandra.transport.CBCodec
       
      I am proposing a new approach that lets both methods share the same code path. The code basically looks like the below (in the case of org.apache.cassandra.net.Message#Serializer).

      // A fake DataOutputPlus that simply increment the size when invoking write* methods
      public class SizeCountingDataOutput implements DataOutputPlus
      {
       private int size = 0;
      
       public int getSize()
       {
         return size;
       }
      
       public void write(int b)
       {
         size += 1;
       }
      
       public void write(byte[] b)
       {
         size += b.length;
       }
       ...
      }
      

      Therefore, in the size calculation, we can supply the fake data output and call serialize to get the size.

      private <T> int serializedSize(Message<T> message, int version)
      {
       SizeCountingDataOutput out = new SizeCountingDataOutput();
       try
       {
         serialize(message, out, version);
       }
       catch (IOException exception)
       {
         throw new AssertionError("It should not happen!", exception);
       }
       return out.getSize();
      // The original implementation
      // return version >= VERSION_40 ? serializedSizePost40(message, version) : serializedSizePre40(message, version);
      }
      

        Attachments

        Issue Links

          Activity

            People

            • Assignee:
              yifanc Yifan Cai Assign to me
              Reporter:
              yifanc Yifan Cai
              Authors:
              Yifan Cai

              Dates

              • Created:
                Updated:
                Resolved:

                Issue deployment