belliottsmith commented on code in PR #4202:
URL: https://github.com/apache/cassandra/pull/4202#discussion_r2193401089


##########
src/java/org/apache/cassandra/service/accord/serializers/TopologySerializers.java:
##########
@@ -159,4 +167,127 @@ public long serializedSize(Topology topology)
             return size;
         }
     };
+
+    public static final UnversionedSerializer<Topology> compactTopology = new 
UnversionedSerializer<>()
+    {
+        @Override
+        public void serialize(Topology topology, DataOutputPlus out) throws 
IOException
+        {
+            out.writeLong(topology.epoch());
+            CollectionSerializers.serializeList(topology.staleIds(), out, 
TopologySerializers.nodeId);
+
+            List<Shard> shards = topology.shards();
+
+            // need to loop twice; once to collect tables/ranges, and another 
to save shards
+            ImmutableUniqueList<TableId> tables;

Review Comment:
   There are some other variations on this, fwiw, if you want to serialise a 
size upfront:
   
   *Option 1*
   You can first build your lookup, then serialise in the correct order with a 
second loop
   ```
   for (value : values)
       indexLookup.putIfAbsent(value, indexLookup.size())
   
   int count = 0;
   for (value : values)
       if (indexLookup.get(value) == count)
       {
           count++;
           out.write(value);
       }
   ```
   
   *Option 2*
   You can first build your lookup without indexes, then assign them indexes in 
the order of iteration for the collection. The index is arbitrary, so this is 
fine, and better if there are many duplicates.
   ```
   for (value : values)
       indexLookup.putIfAbsent(value, -1)
   int count = 0;
   for (entry : indexLookup.entrySet())
       entry.setValue(count++)
   ```
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to