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


##########
src/java/org/apache/cassandra/utils/SimpleBitSetSerializer.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.utils;
+
+import java.io.IOException;
+
+import accord.utils.LargeBitSet;
+import accord.utils.SimpleBitSet;
+import accord.utils.SmallBitSet;
+import org.apache.cassandra.io.UnversionedSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+
+public class SimpleBitSetSerializer implements 
UnversionedSerializer<SimpleBitSet>
+{
+    public static final SimpleBitSetSerializer instance = new 
SimpleBitSetSerializer();
+
+    private enum Kind
+    {
+        small((byte) 0x1, SmallBitSetSerializer.instance),
+        large((byte) 0x2, LargeBitSetSerializer.instance);
+
+        private final byte value;
+        private final UnversionedSerializer<SimpleBitSet> serializer;
+
+        @SuppressWarnings("unchecked")
+        Kind(byte value, UnversionedSerializer<? extends SimpleBitSet> 
serializer)
+        {
+            this.value = value;
+            this.serializer = (UnversionedSerializer<SimpleBitSet>) serializer;
+        }
+
+        public static Kind fromByte(byte value)
+        {
+            switch (value)
+            {
+                case 1: return small;
+                case 2: return large;
+                default: throw new UnsupportedOperationException("Unknown 
bitset byte type: " + value);
+            }
+        }
+    }
+
+    private static Kind inferKind(SimpleBitSet t)
+    {
+        if (t.getClass() == LargeBitSet.class) return Kind.large;
+        if (t.getClass() == SmallBitSet.class) return Kind.small;
+        throw new UnsupportedOperationException("Unknown bitset type: " + 
t.getClass());
+    }
+
+    @Override
+    public void serialize(SimpleBitSet t, DataOutputPlus out) throws 
IOException
+    {
+        Kind kind = inferKind(t);

Review Comment:
   why not standardise(ish) the two serializers? They're basically the same, we 
just have an implicit capacity for the small bitset. Then for this serializer 
we can avoid writing a kind and instead write the capacity, which implies if it 
can be created as a SmallBitSet or must be a LargeBitSet.
   
   The large serializer can avoid writing out wordsInUse if <= 1, and always 
serialize at least one long (with value 0 if empty), and then the two are 
serialied identically, and we would just need the large serializer to expose 
methods that accept/assume the capacity so we can invoke that directly for this 
use case.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to