Apache Lucene open-source search software
LUCENE-9204: Remove Spans references from DisiWrapper (#150)
Alan Woodward4b55ae5de42d
diff --git a/lucene/core/src/java/org/apache/lucene/search/DisiWrapper.java b/lucene/core/src/java/org/apache/lucene/search/DisiWrapper.javaindex 6adb650d465..f69e0fb2ff1 100644--- a/lucene/core/src/java/org/apache/lucene/search/DisiWrapper.java+++ b/lucene/core/src/java/org/apache/lucene/search/DisiWrapper.java@@ -16,8 +16,6 @@*/package org.apache.lucene.search;-import org.apache.lucene.search.spans.Spans;-/*** Wrapper used in {@link DisiPriorityQueue}.*@@ -41,14 +39,8 @@ public class DisiWrapper {// For WANDScorerlong maxScore;- // FOR SPANS- public final Spans spans;- public int lastApproxMatchDoc; // last doc of approximation that did match- public int lastApproxNonMatchDoc; // last doc of approximation that did not match-public DisiWrapper(Scorer scorer) {this.scorer = scorer;- this.spans = null;this.iterator = scorer.iterator();this.cost = iterator.cost();this.doc = -1;@@ -62,23 +54,4 @@ public class DisiWrapper {matchCost = 0f;}}-- public DisiWrapper(Spans spans) {- this.scorer = null;- this.spans = spans;- this.iterator = spans;- this.cost = iterator.cost();- this.doc = -1;- this.twoPhaseView = spans.asTwoPhaseIterator();-- if (twoPhaseView != null) {- approximation = twoPhaseView.approximation();- matchCost = twoPhaseView.matchCost();- } else {- approximation = iterator;- matchCost = 0f;- }- this.lastApproxNonMatchDoc = -2;- this.lastApproxMatchDoc = -2;- }}diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/SpanDisiPriorityQueue.java b/lucene/core/src/java/org/apache/lucene/search/spans/SpanDisiPriorityQueue.javanew file mode 100644index 00000000000..7221417ce29--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/search/spans/SpanDisiPriorityQueue.java@@ -0,0 +1,166 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You 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 org.apache.lucene.search.spans;++import java.util.Arrays;+import java.util.Iterator;+import org.apache.lucene.util.PriorityQueue;++/**+ * A priority queue of DocIdSetIterators that orders by current doc ID. This specialization is+ * needed over {@link PriorityQueue} because the pluggable comparison function makes the rebalancing+ * quite slow.+ *+ * @lucene.internal+ */+final class SpanDisiPriorityQueue implements Iterable<SpanDisiWrapper> {++ static int leftNode(int node) {+ return ((node + 1) << 1) - 1;+ }++ static int rightNode(int leftNode) {+ return leftNode + 1;+ }++ static int parentNode(int node) {+ return ((node + 1) >>> 1) - 1;+ }++ private final SpanDisiWrapper[] heap;+ private int size;++ public SpanDisiPriorityQueue(int maxSize) {+ heap = new SpanDisiWrapper[maxSize];+ size = 0;+ }++ public int size() {+ return size;+ }++ public SpanDisiWrapper top() {+ return heap[0];+ }++ /** Get the list of scorers which are on the current doc. */+ public SpanDisiWrapper topList() {+ final SpanDisiWrapper[] heap = this.heap;+ final int size = this.size;+ SpanDisiWrapper list = heap[0];+ list.next = null;+ if (size >= 3) {+ list = topList(list, heap, size, 1);+ list = topList(list, heap, size, 2);+ } else if (size == 2 && heap[1].doc == list.doc) {+ list = prepend(heap[1], list);+ }+ return list;+ }++ // prepend w1 (iterator) to w2 (list)+ private SpanDisiWrapper prepend(SpanDisiWrapper w1, SpanDisiWrapper w2) {+ w1.next = w2;+ return w1;+ }++ private SpanDisiWrapper topList(SpanDisiWrapper list, SpanDisiWrapper[] heap, int size, int i) {+ final SpanDisiWrapper w = heap[i];+ if (w.doc == list.doc) {+ list = prepend(w, list);+ final int left = leftNode(i);+ final int right = left + 1;+ if (right < size) {+ list = topList(list, heap, size, left);+ list = topList(list, heap, size, right);+ } else if (left < size && heap[left].doc == list.doc) {+ list = prepend(heap[left], list);+ }+ }+ return list;+ }++ public SpanDisiWrapper add(SpanDisiWrapper entry) {+ final SpanDisiWrapper[] heap = this.heap;+ final int size = this.size;+ heap[size] = entry;+ upHeap(size);+ this.size = size + 1;+ return heap[0];+ }++ public SpanDisiWrapper pop() {+ final SpanDisiWrapper[] heap = this.heap;+ final SpanDisiWrapper result = heap[0];+ final int i = --size;+ heap[0] = heap[i];+ heap[i] = null;+ downHeap(i);+ return result;+ }++ public SpanDisiWrapper updateTop() {+ downHeap(size);+ return heap[0];+ }++ SpanDisiWrapper updateTop(SpanDisiWrapper topReplacement) {+ heap[0] = topReplacement;+ return updateTop();+ }++ void upHeap(int i) {+ final SpanDisiWrapper node = heap[i];+ final int nodeDoc = node.doc;+ int j = parentNode(i);+ while (j >= 0 && nodeDoc < heap[j].doc) {+ heap[i] = heap[j];+ i = j;+ j = parentNode(j);+ }+ heap[i] = node;+ }++ void downHeap(int size) {+ int i = 0;+ final SpanDisiWrapper node = heap[0];+ int j = leftNode(i);+ if (j < size) {+ int k = rightNode(j);+ if (k < size && heap[k].doc < heap[j].doc) {+ j = k;+ }+ if (heap[j].doc < node.doc) {+ do {+ heap[i] = heap[j];+ i = j;+ j = leftNode(i);+ k = rightNode(j);+ if (k < size && heap[k].doc < heap[j].doc) {+ j = k;+ }+ } while (j < size && heap[j].doc < node.doc);+ heap[i] = node;+ }+ }+ }++ @Override+ public Iterator<SpanDisiWrapper> iterator() {+ return Arrays.asList(heap).subList(0, size).iterator();+ }+}diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/SpanDisiWrapper.java b/lucene/core/src/java/org/apache/lucene/search/spans/SpanDisiWrapper.javanew file mode 100644index 00000000000..21ed543d803--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/search/spans/SpanDisiWrapper.java@@ -0,0 +1,62 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You 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 org.apache.lucene.search.spans;++import org.apache.lucene.search.DocIdSetIterator;+import org.apache.lucene.search.TwoPhaseIterator;++/**+ * Wrapper used in {@link SpanDisiPriorityQueue}.+ *+ * @lucene.internal+ */+public class SpanDisiWrapper {+ public final DocIdSetIterator iterator;+ public final long cost;+ public final float matchCost; // the match cost for two-phase iterators, 0 otherwise+ public int doc; // the current doc, used for comparison+ public SpanDisiWrapper next; // reference to a next element, see #topList++ // An approximation of the iterator, or the iterator itself if it does not+ // support two-phase iteration+ public final DocIdSetIterator approximation;+ // A two-phase view of the iterator, or null if the iterator does not support+ // two-phase iteration+ public final TwoPhaseIterator twoPhaseView;++ public final Spans spans;+ public int lastApproxMatchDoc; // last doc of approximation that did match+ public int lastApproxNonMatchDoc; // last doc of approximation that did not match++ public SpanDisiWrapper(Spans spans) {+ this.spans = spans;+ this.iterator = spans;+ this.cost = iterator.cost();+ this.doc = -1;+ this.twoPhaseView = spans.asTwoPhaseIterator();++ if (twoPhaseView != null) {+ approximation = twoPhaseView.approximation();+ matchCost = twoPhaseView.matchCost();+ } else {+ approximation = iterator;+ matchCost = 0f;+ }+ this.lastApproxNonMatchDoc = -2;+ this.lastApproxMatchDoc = -2;+ }+}diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/SpanDisjunctionDISIApproximation.java b/lucene/core/src/java/org/apache/lucene/search/spans/SpanDisjunctionDISIApproximation.javanew file mode 100644index 00000000000..2dc4766df8f--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/search/spans/SpanDisjunctionDISIApproximation.java@@ -0,0 +1,74 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You 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 org.apache.lucene.search.spans;++import java.io.IOException;+import org.apache.lucene.search.DocIdSetIterator;++/**+ * A {@link DocIdSetIterator} which is a disjunction of the approximations of the provided+ * iterators.+ *+ * @lucene.internal+ */+class SpanDisjunctionDISIApproximation extends DocIdSetIterator {++ final SpanDisiPriorityQueue subIterators;+ final long cost;++ public SpanDisjunctionDISIApproximation(SpanDisiPriorityQueue subIterators) {+ this.subIterators = subIterators;+ long cost = 0;+ for (SpanDisiWrapper w : subIterators) {+ cost += w.cost;+ }+ this.cost = cost;+ }++ @Override+ public long cost() {+ return cost;+ }++ @Override+ public int docID() {+ return subIterators.top().doc;+ }++ @Override+ public int nextDoc() throws IOException {+ SpanDisiWrapper top = subIterators.top();+ final int doc = top.doc;+ do {+ top.doc = top.approximation.nextDoc();+ top = subIterators.updateTop();+ } while (top.doc == doc);++ return top.doc;+ }++ @Override+ public int advance(int target) throws IOException {+ SpanDisiWrapper top = subIterators.top();+ do {+ top.doc = top.approximation.advance(target);+ top = subIterators.updateTop();+ } while (top.doc < target);++ return top.doc;+ }+}diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java b/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.javaindex 80719dbe910..78257fc0001 100644--- a/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java@@ -26,9 +26,6 @@ import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.index.Term;import org.apache.lucene.index.TermStates;import org.apache.lucene.search.BooleanClause;-import org.apache.lucene.search.DisiPriorityQueue;-import org.apache.lucene.search.DisiWrapper;-import org.apache.lucene.search.DisjunctionDISIApproximation;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.QueryVisitor;@@ -186,9 +183,9 @@ public final class SpanOrQuery extends SpanQuery {return subSpans.get(0);}- DisiPriorityQueue byDocQueue = new DisiPriorityQueue(subSpans.size());+ SpanDisiPriorityQueue byDocQueue = new SpanDisiPriorityQueue(subSpans.size());for (Spans spans : subSpans) {- byDocQueue.add(new DisiWrapper(spans));+ byDocQueue.add(new SpanDisiWrapper(spans));}SpanPositionQueue byPositionQueue =@@ -200,7 +197,7 @@ public final class SpanOrQuery extends SpanQuery {@Overridepublic int nextDoc() throws IOException {topPositionSpans = null;- DisiWrapper topDocSpans = byDocQueue.top();+ SpanDisiWrapper topDocSpans = byDocQueue.top();int currentDoc = topDocSpans.doc;do {topDocSpans.doc = topDocSpans.iterator.nextDoc();@@ -212,7 +209,7 @@ public final class SpanOrQuery extends SpanQuery {@Overridepublic int advance(int target) throws IOException {topPositionSpans = null;- DisiWrapper topDocSpans = byDocQueue.top();+ SpanDisiWrapper topDocSpans = byDocQueue.top();do {topDocSpans.doc = topDocSpans.iterator.advance(target);topDocSpans = byDocQueue.updateTop();@@ -222,7 +219,7 @@ public final class SpanOrQuery extends SpanQuery {@Overridepublic int docID() {- DisiWrapper topDocSpans = byDocQueue.top();+ SpanDisiWrapper topDocSpans = byDocQueue.top();return topDocSpans.doc;}@@ -231,7 +228,7 @@ public final class SpanOrQuery extends SpanQuery {float sumMatchCost = 0; // See also DisjunctionScorer.asTwoPhaseIterator()long sumApproxCost = 0;- for (DisiWrapper w : byDocQueue) {+ for (SpanDisiWrapper w : byDocQueue) {if (w.twoPhaseView != null) {long costWeight = (w.cost <= 1) ? 1 : w.cost;sumMatchCost += w.twoPhaseView.matchCost() * costWeight;@@ -246,7 +243,7 @@ public final class SpanOrQuery extends SpanQuery {final float matchCost = sumMatchCost / sumApproxCost;- return new TwoPhaseIterator(new DisjunctionDISIApproximation(byDocQueue)) {+ return new TwoPhaseIterator(new SpanDisjunctionDISIApproximation(byDocQueue)) {@Overridepublic boolean matches() throws IOException {return twoPhaseCurrentDocMatches();@@ -264,7 +261,7 @@ public final class SpanOrQuery extends SpanQuery {void computePositionsCost() {float sumPositionsCost = 0;long sumCost = 0;- for (DisiWrapper w : byDocQueue) {+ for (SpanDisiWrapper w : byDocQueue) {long costWeight = (w.cost <= 1) ? 1 : w.cost;sumPositionsCost += w.spans.positionsCost() * costWeight;sumCost += costWeight;@@ -283,7 +280,7 @@ public final class SpanOrQuery extends SpanQuery {int lastDocTwoPhaseMatched = -1;boolean twoPhaseCurrentDocMatches() throws IOException {- DisiWrapper listAtCurrentDoc = byDocQueue.topList();+ SpanDisiWrapper listAtCurrentDoc = byDocQueue.topList();// remove the head of the list as long as it does not matchfinal int currentDoc = listAtCurrentDoc.doc;while (listAtCurrentDoc.twoPhaseView != null) {@@ -307,7 +304,7 @@ public final class SpanOrQuery extends SpanQuery {void fillPositionQueue() throws IOException { // called at first nextStartPositionassert byPositionQueue.size() == 0;// add all matching Spans at current doc to byPositionQueue- DisiWrapper listAtCurrentDoc = byDocQueue.topList();+ SpanDisiWrapper listAtCurrentDoc = byDocQueue.topList();while (listAtCurrentDoc != null) {Spans spansAtDoc = listAtCurrentDoc.spans;if (lastDocTwoPhaseMatched