-
Notifications
You must be signed in to change notification settings - Fork 23
Implement Indexed Priority Queue to improve grouping performance (#251) #299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dilaxn
wants to merge
2
commits into
opensearch-project:main
Choose a base branch
from
Dilaxn:feature/indexed-priority-queue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
src/main/java/org/opensearch/plugin/insights/core/utils/IndexedPriorityQueue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.insights.core.utils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class IndexedPriorityQueue<K, V> { | ||
Dilaxn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private static final Logger logger = LogManager.getLogger(IndexedPriorityQueue.class); | ||
|
||
public static class Entry<K, V> { | ||
public final K key; | ||
public V value; | ||
|
||
Entry(K key, V value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
} | ||
|
||
private final List<Entry<K, V>> heap; | ||
private final Map<K, Integer> indexMap; | ||
private final Comparator<? super V> comparator; | ||
|
||
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); | ||
private final Lock read = lock.readLock(); | ||
private final Lock write = lock.writeLock(); | ||
|
||
public IndexedPriorityQueue(int initialCapacity, Comparator<? super V> comparator) { | ||
this.comparator = comparator; | ||
this.heap = new ArrayList<>(initialCapacity); | ||
this.indexMap = new HashMap<>(initialCapacity); | ||
} | ||
|
||
public boolean insert(K key, V value) { | ||
write.lock(); | ||
try { | ||
if (indexMap.containsKey(key)) { | ||
logger.debug("Key {} already exists in queue, skipping insert", key); | ||
return false; | ||
} | ||
heap.add(new Entry<>(key, value)); | ||
int idx = heap.size() - 1; | ||
indexMap.put(key, idx); | ||
siftUp(idx); | ||
logger.debug("Successfully inserted key {} at index {}", key, idx); | ||
return true; | ||
} finally { | ||
write.unlock(); | ||
} | ||
} | ||
|
||
public boolean remove(K key) { | ||
write.lock(); | ||
try { | ||
Integer idx = indexMap.remove(key); | ||
if (idx == null) { | ||
logger.debug("Key {} not found in queue, nothing to remove", key); | ||
return false; | ||
} | ||
|
||
int lastIdx = heap.size() - 1; | ||
if (idx != lastIdx) { | ||
Entry<K, V> lastItem = heap.get(lastIdx); | ||
heap.set(idx, lastItem); | ||
indexMap.put(lastItem.key, idx); | ||
|
||
boolean needSiftUp = false; | ||
boolean needSiftDown = false; | ||
|
||
if (idx > 0) { | ||
int parentIdx = (idx - 1) >>> 1; | ||
if (comparator.compare(lastItem.value, heap.get(parentIdx).value) < 0) { | ||
needSiftUp = true; | ||
} | ||
} | ||
|
||
if (!needSiftUp && idx < heap.size() >>> 1) { | ||
int left = (idx << 1) + 1; | ||
if (comparator.compare(lastItem.value, heap.get(left).value) > 0) { | ||
needSiftDown = true; | ||
} else if (left + 1 < heap.size() && comparator.compare(lastItem.value, heap.get(left + 1).value) > 0) { | ||
needSiftDown = true; | ||
} | ||
} | ||
|
||
if (needSiftUp) { | ||
siftUp(idx); | ||
} else if (needSiftDown) { | ||
siftDown(idx); | ||
} | ||
} | ||
heap.remove(lastIdx); | ||
logger.debug("Successfully removed key {} from index {}", key, idx); | ||
return true; | ||
} finally { | ||
write.unlock(); | ||
} | ||
} | ||
|
||
public V poll() { | ||
Entry<K, V> e = pollEntry(); | ||
return e == null ? null : e.value; | ||
} | ||
|
||
public Entry<K, V> pollEntry() { | ||
write.lock(); | ||
try { | ||
if (heap.isEmpty()) { | ||
return null; | ||
} | ||
Entry<K, V> head = heap.get(0); | ||
remove(head.key); | ||
return head; | ||
} finally { | ||
write.unlock(); | ||
} | ||
} | ||
|
||
public Entry<K, V> peek() { | ||
read.lock(); | ||
try { | ||
return heap.isEmpty() ? null : heap.get(0); | ||
} finally { | ||
read.unlock(); | ||
} | ||
} | ||
|
||
public int size() { | ||
read.lock(); | ||
try { | ||
return heap.size(); | ||
} finally { | ||
read.unlock(); | ||
} | ||
} | ||
|
||
public List<V> getAllValues() { | ||
read.lock(); | ||
try { | ||
List<V> values = new ArrayList<>(); | ||
for (Entry<K, V> entry : heap) { | ||
values.add(entry.value); | ||
} | ||
return values; | ||
} finally { | ||
read.unlock(); | ||
} | ||
} | ||
|
||
public void clear() { | ||
write.lock(); | ||
try { | ||
heap.clear(); | ||
indexMap.clear(); | ||
} finally { | ||
write.unlock(); | ||
} | ||
} | ||
|
||
private void siftUp(int idx) { | ||
Entry<K, V> item = heap.get(idx); | ||
while (idx > 0) { | ||
int parentIdx = (idx - 1) >>> 1; | ||
Entry<K, V> parent = heap.get(parentIdx); | ||
if (comparator.compare(item.value, parent.value) >= 0) break; | ||
heap.set(idx, parent); | ||
indexMap.put(parent.key, idx); | ||
idx = parentIdx; | ||
} | ||
heap.set(idx, item); | ||
indexMap.put(item.key, idx); | ||
} | ||
|
||
private void siftDown(int idx) { | ||
int half = heap.size() >>> 1; | ||
Entry<K, V> item = heap.get(idx); | ||
while (idx < half) { | ||
int left = (idx << 1) + 1; | ||
int right = left + 1; | ||
int smallest = left; | ||
|
||
if (right < heap.size() && comparator.compare(heap.get(right).value, heap.get(left).value) < 0) { | ||
smallest = right; | ||
} | ||
|
||
Entry<K, V> smallestItem = heap.get(smallest); | ||
if (comparator.compare(item.value, smallestItem.value) <= 0) break; | ||
|
||
heap.set(idx, smallestItem); | ||
indexMap.put(smallestItem.key, idx); | ||
idx = smallest; | ||
} | ||
heap.set(idx, item); | ||
indexMap.put(item.key, idx); | ||
} | ||
|
||
public boolean isEmpty() { | ||
Dilaxn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
read.lock(); | ||
try { | ||
return heap.isEmpty(); | ||
} finally { | ||
read.unlock(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.