Skip to content

Apache Lucene open-source search software

LUCENE-5487: merge trunk

Michael McCandless2d88332e9dc6
diff --cc lucene/core/src/java/org/apache/lucene/search/FakeScorer.java
index 8884a546b43,00000000000..89b92a5bb22
mode 100644,000000..100644
--- a/lucene/core/src/java/org/apache/lucene/search/FakeScorer.java
+++ b/lucene/core/src/java/org/apache/lucene/search/FakeScorer.java
@@@ -1,72 -1,0 +1,72 @@@
+package org.apache.lucene.search;
+
+/*
+ * 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.
+ */
+
+import java.util.Collection;
+
- /** Used by {@link BulkScorers} that need to pass a {@link
++/** Used by {@link BulkScorer}s that need to pass a {@link
+ * Scorer} to {@link Collector#setScorer}. */
+final class FakeScorer extends Scorer {
+ float score;
+ int doc = -1;
+ int freq = 1;
+
+ public FakeScorer() {
+ super(null);
+ }
+
+ @Override
+ public int advance(int target) {
+ throw new UnsupportedOperationException("FakeScorer doesn't support advance(int)");
+ }
+
+ @Override
+ public int docID() {
+ return doc;
+ }
+
+ @Override
+ public int freq() {
+ return freq;
+ }
+
+ @Override
+ public int nextDoc() {
+ throw new UnsupportedOperationException("FakeScorer doesn't support nextDoc()");
+ }
+
+ @Override
+ public float score() {
+ return score;
+ }
+
+ @Override
+ public long cost() {
+ return 1;
+ }
+
+ @Override
+ public Weight getWeight() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Collection<ChildScorer> getChildren() {
+ throw new UnsupportedOperationException();
+ }
+}
diff --cc lucene/test-framework/src/java/org/apache/lucene/search/AssertingBulkOutOfOrderScorer.java
index 00000000000,00000000000..0b2fa34b044
new file mode 100644
--- /dev/null
+++ b/lucene/test-framework/src/java/org/apache/lucene/search/AssertingBulkOutOfOrderScorer.java
@@@ -1,0 -1,0 +1,110 @@@
++package org.apache.lucene.search;
++
++/*
++ * 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.
++ */
++
++import java.io.IOException;
++import java.lang.ref.WeakReference;
++import java.util.Collection;
++import java.util.Collections;
++import java.util.Map;
++import java.util.Random;
++import java.util.WeakHashMap;
++
++import org.apache.lucene.index.DocsEnum;
++import org.apache.lucene.util.VirtualMethod;
++
++/** A crazy {@link BulkScorer} that wraps a {@link Scorer}
++ * but shuffles the order of the collected documents. */
++public class AssertingBulkOutOfOrderScorer extends BulkScorer {
++
++ final Random random;
++ final Scorer scorer;
++
++ public AssertingBulkOutOfOrderScorer(Random random, Scorer scorer) {
++ this.random = random;
++ this.scorer = scorer;
++ }
++
++ private void shuffle(int[] docIDs, float[] scores, int[] freqs, int size) {
++ for (int i = size - 1; i > 0; --i) {
++ final int other = random.nextInt(i + 1);
++
++ final int tmpDoc = docIDs[i];
++ docIDs[i] = docIDs[other];
++ docIDs[other] = tmpDoc;
++
++ final float tmpScore = scores[i];
++ scores[i] = scores[other];
++ scores[other] = tmpScore;
++
++ final int tmpFreq = freqs[i];
++ freqs[i] = freqs[other];
++ freqs[other] = tmpFreq;
++ }
++ }
++
++ private static void flush(int[] docIDs, float[] scores, int[] freqs, int size,
++ FakeScorer scorer, Collector collector) throws IOException {
++ for (int i = 0; i < size; ++i) {
++ scorer.doc = docIDs[i];
++ scorer.freq = freqs[i];
++ scorer.score = scores[i];
++ collector.collect(scorer.doc);
++ }
++ }
++
++ @Override
++ public boolean score(Collector collector, int max) throws IOException {
++ if (scorer.docID() == -1) {
++ scorer.nextDoc();
++ }
++
++ FakeScorer fake = new FakeScorer();
++ collector.setScorer(fake);
++
++ final int bufferSize = 1 + random.nextInt(100);
++ final int[] docIDs = new int[bufferSize];
++ final float[] scores = new float[bufferSize];
++ final int[] freqs = new int[bufferSize];
++
++ int buffered = 0;
++ int doc = scorer.docID();
++ while (doc < max) {
++ docIDs[buffered] = doc;
++ scores[buffered] = scorer.score();
++ freqs[buffered] = scorer.freq();
++
++ if (++buffered == bufferSize) {
++ shuffle(docIDs, scores, freqs, buffered);
++ flush(docIDs, scores, freqs, buffered, fake, collector);
++ buffered = 0;
++ }
++ doc = scorer.nextDoc();
++ }
++
++ shuffle(docIDs, scores, freqs, buffered);
++ flush(docIDs, scores, freqs, buffered, fake, collector);
++
++ return doc != Scorer.NO_MORE_DOCS;
++ }
++
++ @Override
++ public String toString() {
++ return "AssertingBulkOutOfOrderScorer(" + scorer + ")";
++ }
++}
diff --cc lucene/test-framework/src/java/org/apache/lucene/search/AssertingBulkScorer.java
index 050dfa477c5,00000000000..995f49aee1d
mode 100644,000000..100644
--- a/lucene/test-framework/src/java/org/apache/lucene/search/AssertingBulkScorer.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/search/AssertingBulkScorer.java
@@@ -1,90 -1,0 +1,84 @@@
+package org.apache.lucene.search;
+
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.lang.ref.WeakReference;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Random;
+import java.util.WeakHashMap;
+
+import org.apache.lucene.index.DocsEnum;
+import org.apache.lucene.util.VirtualMethod;
+
+/** Wraps a Scorer with additional checks */
+public class AssertingBulkScorer extends BulkScorer {
+
+ private static final VirtualMethod<BulkScorer> SCORE_COLLECTOR = new VirtualMethod<BulkScorer>(BulkScorer.class, "score", Collector.class);
+ private static final VirtualMethod<BulkScorer> SCORE_COLLECTOR_RANGE = new VirtualMethod<BulkScorer>(BulkScorer.class, "score", Collector.class, int.class);
+
- // we need to track scorers using a weak hash map because otherwise we
- // could loose references because of eg.
- // AssertingScorer.score(Collector) which needs to delegate to work correctly
- private static Map<BulkScorer, WeakReference<AssertingBulkScorer>> ASSERTING_INSTANCES = Collections.synchronizedMap(new WeakHashMap<BulkScorer, WeakReference<AssertingBulkScorer>>());
-
+ public static BulkScorer wrap(Random random, BulkScorer other) {
+ if (other == null || other instanceof AssertingBulkScorer) {
+ return other;
+ }
- final AssertingBulkScorer assertScorer = new AssertingBulkScorer(random, other);
- ASSERTING_INSTANCES.put(other, new WeakReference<AssertingBulkScorer>(assertScorer));
- return assertScorer;
++ return new AssertingBulkScorer(random, other);
+ }
+
+ public static boolean shouldWrap(BulkScorer inScorer) {
+ return SCORE_COLLECTOR.isOverriddenAsOf(inScorer.getClass()) || SCORE_COLLECTOR_RANGE.isOverriddenAsOf(inScorer.getClass());
+ }
+
+ final Random random;
+ final BulkScorer in;
+
+ private AssertingBulkScorer(Random random, BulkScorer in) {
+ this.random = random;
+ this.in = in;
+ }
+
+ public BulkScorer getIn() {
+ return in;
+ }
+
+ @Override
+ public void score(Collector collector) throws IOException {
+ if (random.nextBoolean()) {
+ try {
+ final boolean remaining = in.score(collector, DocsEnum.NO_MORE_DOCS);
+ assert !remaining;
+ } catch (UnsupportedOperationException e) {
+ in.score(collector);
+ }
+ } else {
+ in.score(collector);
+ }
+ }
+
+ @Override
+ public boolean score(Collector collector, int max) throws IOException {
+ return in.score(collector, max);
+ }
+
+ @Override
+ public String toString() {
+ return "AssertingBulkScorer(" + in + ")";
+ }
++
+}
diff --cc lucene/test-framework/src/java/org/apache/lucene/search/AssertingWeight.java
index e00dc75ad2b,c712b59fc29..793b396ab7c
--- a/lucene/test-framework/src/java/org/apache/lucene/search/AssertingWeight.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/search/AssertingWeight.java
@@@ -58,34 -60,19 +60,46 @@@ class AssertingWeight extends Weight
}
@Override
- public Scorer scorer(AtomicReaderContext context, boolean scoreDocsInOrder,
- boolean topScorer, Bits acceptDocs) throws IOException {
+ public Scorer scorer(AtomicReaderContext context, Bits acceptDocs) throws IOException {
// if the caller asks for in-order scoring or if the weight does not support
// out-of order scoring then collection will have to happen in-order.
- final boolean inOrder = scoreDocsInOrder || !scoresDocsOutOfOrder();
- final Scorer inScorer = in.scorer(context, scoreDocsInOrder, topScorer, acceptDocs);
- return AssertingScorer.wrap(new Random(random.nextLong()), inScorer, topScorer, inOrder);
+ final Scorer inScorer = in.scorer(context, acceptDocs);
+ return AssertingScorer.wrap(new Random(random.nextLong()), inScorer);
+ }
+
+ @Override
+ public BulkScorer bulkScorer(AtomicReaderContext context, boolean scoreDocsInOrder, Bits acceptDocs) throws IOException {
+ // if the caller asks for in-order scoring or if the weight does not support
+ // out-of order scoring then collection will have to happen in-order.
+ BulkScorer inScorer = in.bulkScorer(context, scoreDocsInOrder, acceptDocs);
+ if (inScorer == null) {
+ return null;
+ }
++
+ if (AssertingBulkScorer.shouldWrap(inScorer)) {
++ // The incoming scorer already has a specialized
++ // implementation for BulkScorer, so we should use it:
+ return AssertingBulkScorer.wrap(new Random(random.nextLong()), inScorer);
++ } else if (scoreDocsInOrder == false && random.nextBoolean()) {
++ // The caller claims it can handle out-of-order
++ // docs; let's confirm that by pulling docs and
++ // randomly shuffling them before collection:
++ //Scorer scorer = in.scorer(context, acceptDocs);
++ Scorer scorer = scorer(context, acceptDocs);
++
++ // Scorer should not be null if bulkScorer wasn't:
++ assert scorer != null;
++ return new AssertingBulkOutOfOrderScorer(new Random(random.nextLong()), scorer);
+ } else {
+ // Let super wrap this.scorer instead, so we use
+ // AssertingScorer:
+ return super.bulkScorer(context, scoreDocsInOrder, acceptDocs);
+ }
}
@Override
public boolean scoresDocsOutOfOrder() {
- return in.scoresDocsOutOfOrder();
+ return scoresDocsOutOfOrder;
}
--
}