Skip to content

Commit 366c404

Browse files
committed
Expose entry priority in poll/peek in IndexedPriorityQueue
1 parent a0e649e commit 366c404

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

core/trino-main/src/main/java/io/trino/execution/resourcegroups/IndexedPriorityQueue.java

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ public boolean remove(E element)
8181

8282
@Override
8383
public E poll()
84+
{
85+
Entry<E> entry = pollEntry();
86+
if (entry == null) {
87+
return null;
88+
}
89+
return entry.getValue();
90+
}
91+
92+
public Prioritized<E> pollPrioritized()
93+
{
94+
Entry<E> entry = pollEntry();
95+
if (entry == null) {
96+
return null;
97+
}
98+
return new Prioritized<>(entry.getValue(), entry.getPriority());
99+
}
100+
101+
private Entry<E> pollEntry()
84102
{
85103
Iterator<Entry<E>> iterator = queue.iterator();
86104
if (!iterator.hasNext()) {
@@ -89,18 +107,35 @@ public E poll()
89107
Entry<E> entry = iterator.next();
90108
iterator.remove();
91109
checkState(index.remove(entry.getValue()) != null, "Failed to remove entry from index");
92-
return entry.getValue();
110+
return entry;
93111
}
94112

95113
@Override
96114
public E peek()
115+
{
116+
Entry<E> entry = peekEntry();
117+
if (entry == null) {
118+
return null;
119+
}
120+
return entry.getValue();
121+
}
122+
123+
public Prioritized<E> peekPrioritized()
124+
{
125+
Entry<E> entry = peekEntry();
126+
if (entry == null) {
127+
return null;
128+
}
129+
return new Prioritized<>(entry.getValue(), entry.getPriority());
130+
}
131+
132+
public Entry<E> peekEntry()
97133
{
98134
Iterator<Entry<E>> iterator = queue.iterator();
99135
if (!iterator.hasNext()) {
100136
return null;
101137
}
102-
Entry<E> entry = iterator.next();
103-
return entry.getValue();
138+
return iterator.next();
104139
}
105140

106141
@Override
@@ -149,4 +184,26 @@ public long getGeneration()
149184
return generation;
150185
}
151186
}
187+
188+
public static class Prioritized<V>
189+
{
190+
private final V value;
191+
private final long priority;
192+
193+
public Prioritized(V value, long priority)
194+
{
195+
this.value = requireNonNull(value, "value is null");
196+
this.priority = priority;
197+
}
198+
199+
public V getValue()
200+
{
201+
return value;
202+
}
203+
204+
public long getPriority()
205+
{
206+
return priority;
207+
}
208+
}
152209
}

core/trino-main/src/test/java/io/trino/execution/resourcegroups/TestUpdateablePriorityQueue.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020

21+
import static org.assertj.core.api.Assertions.assertThat;
2122
import static org.testng.Assert.assertEquals;
2223
import static org.testng.Assert.assertTrue;
2324

@@ -35,6 +36,39 @@ public void testIndexedPriorityQueue()
3536
assertEquals(populateAndExtract(new IndexedPriorityQueue<>()), ImmutableList.of(3, 2, 1));
3637
}
3738

39+
@Test
40+
public void testPrioritizedPeekPollIndexedPriorityQueue()
41+
{
42+
IndexedPriorityQueue<Object> queue = new IndexedPriorityQueue<>();
43+
queue.addOrUpdate("a", 1);
44+
queue.addOrUpdate("b", 3);
45+
queue.addOrUpdate("c", 2);
46+
47+
IndexedPriorityQueue.Prioritized<Object> peek1 = queue.peekPrioritized();
48+
assertThat(peek1.getValue()).isEqualTo("b");
49+
assertThat(peek1.getPriority()).isEqualTo(3);
50+
IndexedPriorityQueue.Prioritized<Object> poll1 = queue.pollPrioritized();
51+
assertThat(poll1.getValue()).isEqualTo("b");
52+
assertThat(poll1.getPriority()).isEqualTo(3);
53+
54+
IndexedPriorityQueue.Prioritized<Object> peek2 = queue.peekPrioritized();
55+
assertThat(peek2.getValue()).isEqualTo("c");
56+
assertThat(peek2.getPriority()).isEqualTo(2);
57+
IndexedPriorityQueue.Prioritized<Object> poll2 = queue.pollPrioritized();
58+
assertThat(poll2.getValue()).isEqualTo("c");
59+
assertThat(poll2.getPriority()).isEqualTo(2);
60+
61+
IndexedPriorityQueue.Prioritized<Object> peek3 = queue.peekPrioritized();
62+
assertThat(peek3.getValue()).isEqualTo("a");
63+
assertThat(peek3.getPriority()).isEqualTo(1);
64+
IndexedPriorityQueue.Prioritized<Object> poll3 = queue.pollPrioritized();
65+
assertThat(poll3.getValue()).isEqualTo("a");
66+
assertThat(poll3.getPriority()).isEqualTo(1);
67+
68+
assertThat(queue.peekPrioritized()).isNull();
69+
assertThat(queue.pollPrioritized()).isNull();
70+
}
71+
3872
@Test
3973
public void testStochasticPriorityQueue()
4074
{

0 commit comments

Comments
 (0)