You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by "Yifan Cai (Jira)" <ji...@apache.org> on 2020/10/20 20:17:00 UTC

[jira] [Updated] (CASSANDRA-16218) Simplify the almost duplicated code for calculating serialization size and serialization

     [ https://issues.apache.org/jira/browse/CASSANDRA-16218?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Yifan Cai updated CASSANDRA-16218:
----------------------------------
    Change Category: Code Clarity
         Complexity: Normal
      Fix Version/s: 4.0-beta

> Simplify the almost duplicated code for calculating serialization size and serialization
> ----------------------------------------------------------------------------------------
>
>                 Key: CASSANDRA-16218
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-16218
>             Project: Cassandra
>          Issue Type: Improvement
>          Components: Messaging/Client, Messaging/Internode
>            Reporter: Yifan Cai
>            Assignee: Yifan Cai
>            Priority: Normal
>             Fix For: 4.0-beta
>
>
> 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}}).
> {code:java}
> // 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;
>  }
>  ...
> }
> {code}
> Therefore, in the size calculation, we can supply the fake data output and call serialize to get the size.
> {code:java}
> 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);
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org