|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.lucene.index; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.ListIterator; |
| 22 | +import java.util.function.Predicate; |
| 23 | + |
| 24 | +/** |
| 25 | + * An approximate priority queue, which attempts to poll items by decreasing log of the weight, |
| 26 | + * though exact ordering is not guaranteed. This class doesn't support null elements. |
| 27 | + */ |
| 28 | +final class ApproximatePriorityQueue<T> { |
| 29 | + |
| 30 | + // Indexes between 0 and 63 are sparsely populated, and indexes that are |
| 31 | + // greater than or equal to 64 are densely populated |
| 32 | + // Items close to the beginning of this list are more likely to have a |
| 33 | + // higher weight. |
| 34 | + private final List<T> slots = new ArrayList<>(Long.SIZE); |
| 35 | + |
| 36 | + // A bitset where ones indicate that the corresponding index in `slots` is taken. |
| 37 | + private long usedSlots = 0L; |
| 38 | + |
| 39 | + ApproximatePriorityQueue() { |
| 40 | + for (int i = 0; i < Long.SIZE; ++i) { |
| 41 | + slots.add(null); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** Add an entry to this queue that has the provided weight. */ |
| 46 | + void add(T entry, long weight) { |
| 47 | + assert entry != null; |
| 48 | + |
| 49 | + // The expected slot of an item is the number of leading zeros of its weight, |
| 50 | + // ie. the larger the weight, the closer an item is to the start of the array. |
| 51 | + final int expectedSlot = Long.numberOfLeadingZeros(weight); |
| 52 | + |
| 53 | + // If the slot is already taken, we look for the next one that is free. |
| 54 | + // The above bitwise operation is equivalent to looping over slots until finding one that is |
| 55 | + // free. |
| 56 | + final long freeSlots = ~usedSlots; |
| 57 | + final int destinationSlot = |
| 58 | + expectedSlot + Long.numberOfTrailingZeros(freeSlots >>> expectedSlot); |
| 59 | + assert destinationSlot >= expectedSlot; |
| 60 | + if (destinationSlot < Long.SIZE) { |
| 61 | + usedSlots |= 1L << destinationSlot; |
| 62 | + T previous = slots.set(destinationSlot, entry); |
| 63 | + assert previous == null; |
| 64 | + } else { |
| 65 | + slots.add(entry); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Return an entry matching the predicate. This will usually be one of the available entries that |
| 71 | + * have the highest weight, though this is not guaranteed. This method returns {@code null} if no |
| 72 | + * free entries are available. |
| 73 | + */ |
| 74 | + T poll(Predicate<T> predicate) { |
| 75 | + // Look at indexes 0..63 first, which are sparsely populated. |
| 76 | + int nextSlot = 0; |
| 77 | + do { |
| 78 | + final int nextUsedSlot = nextSlot + Long.numberOfTrailingZeros(usedSlots >>> nextSlot); |
| 79 | + if (nextUsedSlot >= Long.SIZE) { |
| 80 | + break; |
| 81 | + } |
| 82 | + final T entry = slots.get(nextUsedSlot); |
| 83 | + if (predicate.test(entry)) { |
| 84 | + usedSlots &= ~(1L << nextUsedSlot); |
| 85 | + slots.set(nextUsedSlot, null); |
| 86 | + return entry; |
| 87 | + } else { |
| 88 | + nextSlot = nextUsedSlot + 1; |
| 89 | + } |
| 90 | + } while (nextSlot < Long.SIZE); |
| 91 | + |
| 92 | + // Then look at indexes 64.. which are densely populated. |
| 93 | + // Poll in descending order so that if the number of indexing threads |
| 94 | + // decreases, we keep using the same entry over and over again. |
| 95 | + // Resizing operations are also less costly on lists when items are closer |
| 96 | + // to the end of the list. |
| 97 | + for (ListIterator<T> lit = slots.listIterator(slots.size()); |
| 98 | + lit.previousIndex() >= Long.SIZE; ) { |
| 99 | + final T entry = lit.previous(); |
| 100 | + if (predicate.test(entry)) { |
| 101 | + lit.remove(); |
| 102 | + return entry; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // No entry matching the predicate was found. |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + // Only used for assertions |
| 111 | + boolean contains(Object o) { |
| 112 | + if (o == null) { |
| 113 | + throw new NullPointerException(); |
| 114 | + } |
| 115 | + return slots.contains(o); |
| 116 | + } |
| 117 | + |
| 118 | + boolean isEmpty() { |
| 119 | + return usedSlots == 0 && slots.size() == Long.SIZE; |
| 120 | + } |
| 121 | + |
| 122 | + boolean remove(Object o) { |
| 123 | + if (o == null) { |
| 124 | + throw new NullPointerException(); |
| 125 | + } |
| 126 | + int index = slots.indexOf(o); |
| 127 | + if (index == -1) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + if (index >= Long.SIZE) { |
| 131 | + slots.remove(index); |
| 132 | + } else { |
| 133 | + usedSlots &= ~(1L << index); |
| 134 | + slots.set(index, null); |
| 135 | + } |
| 136 | + return true; |
| 137 | + } |
| 138 | +} |
0 commit comments