bdeggleston commented on code in PR #4119:
URL: https://github.com/apache/cassandra/pull/4119#discussion_r2082508449


##########
src/java/org/apache/cassandra/replication/MultiOffsets.java:
##########
@@ -0,0 +1,255 @@
+/*
+ * 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.replication;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.function.Consumer;
+
+import com.google.common.base.Preconditions;
+
+import org.agrona.collections.Long2ObjectHashMap;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.io.IVersionedSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+
+public abstract class MultiOffsets<T extends Offsets>
+{
+    abstract Long2ObjectHashMap<T> offsetMap();
+
+    public int idCount()
+    {
+        int count = 0;
+        for (T offsets : offsetMap().values())
+            count += offsets.offsetCount();
+        return count;
+    }
+
+    public void forEachId(Consumer<ShortMutationId> consumer)
+    {
+        offsetMap().values().forEach(offsets -> offsets.forEachOffset(((logId, 
offset) -> {
+            consumer.accept(new ShortMutationId(logId, offset));
+        })));
+    }
+
+    public boolean isEmpty()
+    {
+        return idCount() == 0;
+    }
+
+    private static abstract class AbstractMutable<T extends 
Offsets.AbstractMutable<T>> extends MultiOffsets<T>
+    {
+        protected final Long2ObjectHashMap<T> offsetMap = new 
Long2ObjectHashMap<>();
+
+        @Override
+        Long2ObjectHashMap<T> offsetMap()
+        {
+            return offsetMap;
+        }
+
+        protected abstract T createOrNull(Offsets.RangeIterator iterator);
+        protected abstract T create(CoordinatorLogId logId);
+        protected abstract T copy(Offsets offsets);
+
+        protected T create(long logId)
+        {
+            return create(new CoordinatorLogId(logId));
+        }
+
+        public void add(ShortMutationId id)
+        {
+            T offsets = offsetMap.computeIfAbsent(id.logId(), this::create);
+            offsets.add(id.offset());
+        }
+
+        public void add(Offsets offsets)
+        {
+            if (offsets.isEmpty())
+                return;
+
+            T existing = offsetMap.get(offsets.logId().asLong());
+            if (existing == null)
+            {
+                offsetMap.put(offsets.logId().asLong(), copy(offsets));
+                return;
+            }
+
+            existing.addAll(offsets);
+        }
+
+        public void addAll(MultiOffsets<?> that)
+        {
+            for (Offsets offsets : that.offsetMap().values())
+                add(offsets);
+        }
+
+        public void remove(Offsets offsets)
+        {
+            T existing = offsetMap.get(offsets.logId().asLong());
+            if (existing == null)
+                return;
+
+            if (existing.isEmpty())
+            {
+                offsetMap.remove(offsets.logId().asLong());
+                return;
+            }
+
+            T next = createOrNull(Offsets.difference(existing.rangeIterator(), 
offsets.rangeIterator()));
+            if (next == null)
+                offsetMap.remove(offsets.logId().asLong());
+            else
+                offsetMap.put(offsets.logId().asLong(), next);
+        }
+
+        public void removeAll(MultiOffsets<?> that)
+        {
+            for (Offsets offsets : that.offsetMap().values())
+                remove(offsets);
+        }
+    }
+
+    public static class Mutable extends AbstractMutable<Offsets.Mutable>
+    {
+        @Override
+        protected Offsets.Mutable createOrNull(Offsets.RangeIterator iterator)
+        {
+            return Offsets.Mutable.createOrNull(iterator);
+        }
+
+        @Override
+        protected Offsets.Mutable create(CoordinatorLogId logId)
+        {
+            return new Offsets.Mutable(logId);
+        }
+
+        @Override
+        protected Offsets.Mutable copy(Offsets offsets)
+        {
+            return Offsets.Mutable.copy(offsets);
+        }
+    }
+
+    public static class Immutable extends MultiOffsets<Offsets.Immutable>
+    {
+        private final Long2ObjectHashMap<Offsets.Immutable> offsetMap;
+
+        private Immutable(Long2ObjectHashMap<Offsets.Immutable> offsetMap)
+        {
+            this.offsetMap = offsetMap;
+        }
+
+        @Override
+        Long2ObjectHashMap<Offsets.Immutable> offsetMap()
+        {
+            return offsetMap;
+        }
+
+        public static class Builder extends 
AbstractMutable<Offsets.Immutable.Builder>
+        {
+            @Override
+            protected Offsets.Immutable.Builder 
createOrNull(Offsets.RangeIterator iterator)
+            {
+                return Offsets.Immutable.Builder.createOrNull(iterator);
+            }
+
+            @Override
+            protected Offsets.Immutable.Builder create(CoordinatorLogId logId)
+            {
+                return new Offsets.Immutable.Builder(logId);
+            }
+
+            @Override
+            protected Offsets.Immutable.Builder copy(Offsets offsets)
+            {
+                return Offsets.Immutable.Builder.copy(offsets);
+            }
+
+            public MultiOffsets.Immutable build()
+            {
+                Long2ObjectHashMap<Offsets.Immutable> result = new 
Long2ObjectHashMap<>();
+                offsetMap.forEachLong((key, builder) -> result.put(key, 
builder.build()));
+                return new Immutable(result);
+            }
+        }
+
+        private static class KeySink implements Consumer<Long>
+        {
+            int idx = 0;
+            final long[] keys;
+
+            public KeySink(int size)
+            {
+                this.keys = new long[size];
+            }
+
+            @Override
+            public void accept(Long v)
+            {
+                keys[idx++] = v;
+            }
+
+            public void sort()
+            {
+                Arrays.sort(keys);
+            }
+        }
+
+        public static final IVersionedSerializer<MultiOffsets.Immutable> 
serializer = new IVersionedSerializer<MultiOffsets.Immutable>()
+        {
+            @Override
+            public void serialize(MultiOffsets.Immutable mo, DataOutputPlus 
out, int version) throws IOException
+            {
+                int size = mo.offsetMap.size();
+                KeySink keys = new KeySink(size);
+                mo.offsetMap.keySet().forEach(keys);
+                keys.sort();

Review Comment:
   yeah I can't remember why I did that ¯\_(ツ)_/¯ 



-- 
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