merlimat commented on a change in pull request #425: Add GrowablePriorityLongPairQueue and tests URL: https://github.com/apache/incubator-pulsar/pull/425#discussion_r127040854
########## 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: Well, still you have to iterate through potentially a lot of items, while holding the mutex and you need and AtomicInteger for the count. Accepting a count in `forEach()` (or `forEachLimit()` will make it more efficient and easy to use. Is `forEach()` going to be used from the dispatcher? ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
