You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pulsar.apache.org by gi...@git.apache.org on 2017/07/12 18:48:54 UTC

[GitHub] rdhabalia commented on a change in pull request #425: Add GrowablePriorityLongPairQueue and tests

rdhabalia commented on a change in pull request #425: Add GrowablePriorityLongPairQueue and tests
URL: https://github.com/apache/incubator-pulsar/pull/425#discussion_r127039856
 
 

 ##########
 File path: pulsar-common/src/main/java/com/yahoo/pulsar/common/util/collections/GrowablePriorityLongPairQueue.java
 ##########
 @@ -0,0 +1,404 @@
+/**
+ * Copyright 2016 Yahoo Inc.
+ *
+ * Licensed 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 com.yahoo.pulsar.common.util.collections;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import java.util.concurrent.locks.ReentrantLock;
+
+import io.netty.util.internal.MathUtil;
+
+/**
+ * An unbounded priority queue based on a min heap where values are composed of pairs of longs.
+ *
+ * When the capacity is reached, data will be moved to a bigger array.
+ * 
+ * <b>It also act as a set and doesn't store duplicate values if {@link #allowedDuplicate} flag is passed false</b>
+ * 
+ ** <p>
+ * (long,long)
+ * <p>
+ *
+ */
+public class GrowablePriorityLongPairQueue {
+
+    private final ReentrantLock lock = new ReentrantLock();
+
+    private long[] data;
+    private int capacity;
+    private static final AtomicIntegerFieldUpdater<GrowablePriorityLongPairQueue> SIZE_UPDATER = AtomicIntegerFieldUpdater
+            .newUpdater(GrowablePriorityLongPairQueue.class, "size");
+    private volatile int size = 0;
+    private static final long EmptyItem = -1L;
+
+    public GrowablePriorityLongPairQueue() {
+        this(64);
+    }
+
+    public GrowablePriorityLongPairQueue(int initialCapacity) {
+        checkArgument(initialCapacity > 0);
+        this.capacity = MathUtil.findNextPositivePowerOfTwo(initialCapacity);
+        data = new long[2 * capacity];
+        fillEmptyValue(data, 0, data.length);
+    }
+
+    public interface LongPairPredicate {
+        boolean test(long v1, long v2);
+    }
+
+    public static interface LongPairConsumer {
+        void accept(long v1, long v2);
+    }
+
+    public boolean add(long item1, long item2) {
+        lock.lock();
+
+        try {
+
+            if (SIZE_UPDATER.get(this) >= this.capacity) {
+                expandArray();
+            }
+
+            int lastIndex = SIZE_UPDATER.get(this) << 1;
+            data[lastIndex] = item1;
+            data[lastIndex + 1] = item2;
+
+            int loc = lastIndex;
+
+            // Swap with parent until parent not larger
+            while (loc > 0 && compare(loc, parent(loc)) < 0) {
+                swap(loc, parent(loc));
+                loc = parent(loc);
+            }
+
+            SIZE_UPDATER.incrementAndGet(this);
+        } finally {
+            lock.unlock();
+        }
+
+        return true;
+    }
+
+    public void forEach(LongPairConsumer processor) {
+        lock.lock();
+        try {
+            int size = SIZE_UPDATER.get(this);
+
+            int index = 0;
+            for (int i = 0; i < size; i++) {
+                processor.accept(data[index], data[index + 1]);
+                index = index + 2;
+            }
+
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    /**
+     * @return a new list of all keys (makes a copy)
+     */
+    public Set<LongPair> items() {
+        Set<LongPair> items = new HashSet<>();
+        forEach((item1, item2) -> items.add(new LongPair(item1, item2)));
+        return items;
+    }
+
+    /**
+     * @return a new list of keys with max provided numberOfItems (makes a copy)
+     */
+    public Set<LongPair> items(int numberOfItems) {
 
 Review comment:
   > Or, for what we need, wouldn't the removeIf() method already be enough?
   
   `removeIf()` may not be enough in usecase where user just wants to read values without removing. 
   
   > Instead of exposing this method that forces a copy and creating a lot of LongPair objects, we could have a forEach() with a limit.
   
   Actually, `forEach()` with limit can be implemented by user with simple addition, so may not need, but sometimes user may want to get actual list of copied `LongPair` Objects same way we have in [OpenConcurrentHashMap.values()](https://github.com/apache/incubator-pulsar/blob/master/pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/ConcurrentOpenHashMap.java#L173) or [ConcurrentLongPairSet.items(int numberOfItems)](https://github.com/apache/incubator-pulsar/blob/master/pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/ConcurrentLongPairSet.java#L193)
   ```
   forEach((item1, item2) -> {
       if (count.get() > x) {
           process(item1,item2);
           count.getAndIncrement();
       }
   });
   ```
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services