You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/09/23 03:36:01 UTC

[GitHub] [pulsar] codelipenghui commented on a diff in pull request #17804: [improve][broker] Make MessageRedeliveryController work more efficiently

codelipenghui commented on code in PR #17804:
URL: https://github.com/apache/pulsar/pull/17804#discussion_r978242302


##########
pulsar-broker/src/main/java/org/apache/pulsar/utils/BitmapSortedLongPairSet.java:
##########
@@ -0,0 +1,143 @@
+/**
+ * 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.pulsar.utils;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NavigableMap;
+import java.util.NavigableSet;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.pulsar.common.util.collections.LongPairSet;
+import org.roaringbitmap.RoaringBitmap;
+
+public class BitmapSortedLongPairSet {
+
+    private final NavigableMap<Long, RoaringBitmap> map = new TreeMap<>();
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();
+
+    public void add(long item1, long item2) {
+        lock.writeLock().lock();
+        try {
+            RoaringBitmap bitSet = map.computeIfAbsent(item1, k -> new RoaringBitmap());
+            bitSet.add(item2, item2 + 1);
+        } finally {
+            lock.writeLock().unlock();
+        }
+    }
+
+    public void remove(long item1, long item2) {
+        lock.writeLock().lock();
+        try {
+            RoaringBitmap bitSet = map.get(item1);
+            if (bitSet != null) {
+                bitSet.remove(item2, item2 + 1);
+                if (bitSet.isEmpty()) {
+                    map.remove(item1, bitSet);
+                }
+            }
+        } finally {
+            lock.writeLock().unlock();
+        }
+    }
+
+    public boolean contains(long item1, long item2) {
+        lock.readLock().lock();
+        try {
+            RoaringBitmap bitSet = map.get(item1);
+            return bitSet != null && bitSet.contains(item2, item2 + 1);
+        } finally {
+            lock.readLock().unlock();
+        }
+    }
+
+    public synchronized void removeUpTo(long item1, long item2) {
+        lock.writeLock().lock();
+        try {
+            Map.Entry<Long, RoaringBitmap> firstEntry = map.firstEntry();
+            while (firstEntry != null && firstEntry.getKey() <= item1) {
+                if (firstEntry.getKey() < item1) {
+                    map.remove(firstEntry.getKey(), firstEntry.getValue());

Review Comment:
   Sub map will have the low and upper boundaries. If we change to sub map here e.g. from 3 to 10, you will not able to add 2 or 11 again.



##########
pulsar-broker/src/main/java/org/apache/pulsar/utils/BitmapSortedLongPairSet.java:
##########
@@ -0,0 +1,143 @@
+/**
+ * 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.pulsar.utils;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NavigableMap;
+import java.util.NavigableSet;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.pulsar.common.util.collections.LongPairSet;
+import org.roaringbitmap.RoaringBitmap;
+
+public class BitmapSortedLongPairSet {
+
+    private final NavigableMap<Long, RoaringBitmap> map = new TreeMap<>();
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();
+
+    public void add(long item1, long item2) {
+        lock.writeLock().lock();
+        try {
+            RoaringBitmap bitSet = map.computeIfAbsent(item1, k -> new RoaringBitmap());
+            bitSet.add(item2, item2 + 1);
+        } finally {
+            lock.writeLock().unlock();
+        }
+    }
+
+    public void remove(long item1, long item2) {
+        lock.writeLock().lock();
+        try {
+            RoaringBitmap bitSet = map.get(item1);
+            if (bitSet != null) {
+                bitSet.remove(item2, item2 + 1);
+                if (bitSet.isEmpty()) {
+                    map.remove(item1, bitSet);
+                }
+            }
+        } finally {
+            lock.writeLock().unlock();
+        }
+    }
+
+    public boolean contains(long item1, long item2) {
+        lock.readLock().lock();
+        try {
+            RoaringBitmap bitSet = map.get(item1);
+            return bitSet != null && bitSet.contains(item2, item2 + 1);
+        } finally {
+            lock.readLock().unlock();
+        }
+    }
+
+    public synchronized void removeUpTo(long item1, long item2) {

Review Comment:
   😁, I copied the method signature from another class



-- 
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: commits-unsubscribe@pulsar.apache.org

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