Apache Lucene open-source search software
LUCENE-4834: Sorter API: Make TermsEnum.docs accept any source of liveDocs.
Adrien Grand5eb31627cd3d
diff --git a/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java b/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.javaindex 87a25a4fdcc..f9c7a3af775 100644--- a/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java+++ b/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java@@ -66,13 +66,11 @@ public class SortingAtomicReader extends FilterAtomicReader {private static class SortingFields extends FilterFields {private final Sorter.DocMap docMap;- private final Bits inLiveDocs;private final FieldInfos infos;- public SortingFields(final Fields in, final Bits inLiveDocs, FieldInfos infos, Sorter.DocMap docMap) {+ public SortingFields(final Fields in, FieldInfos infos, Sorter.DocMap docMap) {super(in);this.docMap = docMap;- this.inLiveDocs = inLiveDocs;this.infos = infos;}@@ -82,7 +80,7 @@ public class SortingAtomicReader extends FilterAtomicReader {if (terms == null) {return null;} else {- return new SortingTerms(terms, inLiveDocs, infos.fieldInfo(field).getIndexOptions(), docMap);+ return new SortingTerms(terms, infos.fieldInfo(field).getIndexOptions(), docMap);}}@@ -91,19 +89,17 @@ public class SortingAtomicReader extends FilterAtomicReader {private static class SortingTerms extends FilterTerms {private final Sorter.DocMap docMap;- private final Bits inLiveDocs;private final IndexOptions indexOptions;- public SortingTerms(final Terms in, final Bits inLiveDocs, IndexOptions indexOptions, final Sorter.DocMap docMap) {+ public SortingTerms(final Terms in, IndexOptions indexOptions, final Sorter.DocMap docMap) {super(in);this.docMap = docMap;- this.inLiveDocs = inLiveDocs;this.indexOptions = indexOptions;}@Overridepublic TermsEnum iterator(final TermsEnum reuse) throws IOException {- return new SortingTermsEnum(in.iterator(reuse), inLiveDocs, docMap, indexOptions);+ return new SortingTermsEnum(in.iterator(reuse), docMap, indexOptions);}}@@ -111,44 +107,53 @@ public class SortingAtomicReader extends FilterAtomicReader {private static class SortingTermsEnum extends FilterTermsEnum {private final Sorter.DocMap docMap;- private final Bits inLiveDocs;private final IndexOptions indexOptions;- public SortingTermsEnum(final TermsEnum in, final Bits inLiveDocs, Sorter.DocMap docMap, IndexOptions indexOptions) {+ public SortingTermsEnum(final TermsEnum in, Sorter.DocMap docMap, IndexOptions indexOptions) {super(in);this.docMap = docMap;- this.inLiveDocs = inLiveDocs;this.indexOptions = indexOptions;}+ Bits newToOld(final Bits liveDocs) {+ if (liveDocs == null) {+ return null;+ }+ return new Bits() {++ @Override+ public boolean get(int index) {+ return liveDocs.get(docMap.oldToNew(index));+ }++ @Override+ public int length() {+ return liveDocs.length();+ }++ };+ }+@Overridepublic DocsEnum docs(Bits liveDocs, DocsEnum reuse, final int flags) throws IOException {- if (liveDocs != null) {- liveDocs = inLiveDocs;- }-// if we're asked to reuse the given DocsEnum and it is Sorting, return// the wrapped one, since some Codecs expect it.if (reuse != null && reuse instanceof SortingDocsEnum) {reuse = ((SortingDocsEnum) reuse).getWrapped();}boolean withFreqs = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >=0 && (flags & DocsEnum.FLAG_FREQS) != 0;- return new SortingDocsEnum(in.docs(liveDocs, reuse, flags), withFreqs, docMap);+ return new SortingDocsEnum(in.docs(newToOld(liveDocs), reuse, flags), withFreqs, docMap);}@Overridepublic DocsAndPositionsEnum docsAndPositions(Bits liveDocs, DocsAndPositionsEnum reuse, final int flags) throws IOException {- if (liveDocs != null) {- liveDocs = inLiveDocs;- }-// if we're asked to reuse the given DocsAndPositionsEnum and it is// Sorting, return the wrapped one, since some Codecs expect it.if (reuse != null && reuse instanceof SortingDocsAndPositionsEnum) {reuse = ((SortingDocsAndPositionsEnum) reuse).getWrapped();}- final DocsAndPositionsEnum positions = in.docsAndPositions(liveDocs, reuse, flags);+ final DocsAndPositionsEnum positions = in.docsAndPositions(newToOld(liveDocs), reuse, flags);if (positions == null) {return null;} else {@@ -581,7 +586,7 @@ public class SortingAtomicReader extends FilterAtomicReader {if (fields == null) {return null;} else {- return new SortingFields(fields, in.getLiveDocs(), in.getFieldInfos(), docMap);+ return new SortingFields(fields, in.getFieldInfos(), docMap);}}diff --git a/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java b/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.javaindex 9eb7d2815a9..fe4ce13aa24 100644--- a/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java+++ b/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java@@ -1,5 +1,22 @@package org.apache.lucene.index.sorter;+/*+ * 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.util.ArrayList;import java.util.Arrays;@@ -38,6 +55,8 @@ import org.apache.lucene.index.SortedDocValues;import org.apache.lucene.index.SortedSetDocValues;import org.apache.lucene.index.Term;import org.apache.lucene.index.Terms;+import org.apache.lucene.index.TermsEnum;+import org.apache.lucene.index.TermsEnum.SeekStatus;import org.apache.lucene.search.CollectionStatistics;import org.apache.lucene.search.DocIdSetIterator;import org.apache.lucene.search.TermStatistics;@@ -45,29 +64,13 @@ import org.apache.lucene.search.similarities.Similarity;import org.apache.lucene.store.Directory;import org.apache.lucene.util.Bits;import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.FixedBitSet;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util._TestUtil;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;-/*- * 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.- */-public abstract class SorterTestBase extends LuceneTestCase {static final class NormsSimilarity extends Similarity {@@ -231,7 +234,7 @@ public abstract class SorterTestBase extends LuceneTestCase {int numDocs = atLeast(20);createIndex(dir, numDocs, random());- reader = new SlowCompositeReaderWrapper(DirectoryReader.open(dir));+ reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(dir));}@AfterClass@@ -286,34 +289,54 @@ public abstract class SorterTestBase extends LuceneTestCase {}}}-++ Bits randomLiveDocs(int maxDoc) {+ if (rarely()) {+ if (random().nextBoolean()) {+ return null;+ } else {+ return new Bits.MatchNoBits(maxDoc);+ }+ }+ final FixedBitSet bits = new FixedBitSet(maxDoc);+ final int bitsSet = _TestUtil.nextInt(random(), 1, maxDoc - 1);+ for (int i = 0; i < bitsSet; ++i) {+ while (true) {+ final int index = random().nextInt(maxDoc);+ if (!bits.get(index)) {+ bits.set(index);+ break;+ }+ }+ }+ return bits;+ }+@Testpublic void testDocsEnum() throws Exception {- Term term = new Term(DOCS_ENUM_FIELD, DOCS_ENUM_TERM);- DocsEnum docs = reader.termDocsEnum(term);- Bits mappedLiveDocs = reader.getLiveDocs();+ Bits mappedLiveDocs = randomLiveDocs(reader.maxDoc());+ TermsEnum termsEnum = reader.terms(DOCS_ENUM_FIELD).iterator(null);+ assertEquals(SeekStatus.FOUND, termsEnum.seekCeil(new BytesRef(DOCS_ENUM_TERM)));+ DocsEnum docs = termsEnum.docs(mappedLiveDocs, null);+int doc;int prev = -1;while ((doc = docs.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (mappedLiveDocs != null) {- assertTrue("document " + doc + " marked as deleted", mappedLiveDocs.get(doc));- }+ assertTrue("document " + doc + " marked as deleted", mappedLiveDocs == null || mappedLiveDocs.get(doc));assertEquals("incorrect value; doc " + doc, sortedValues[doc].intValue(), Integer.parseInt(reader.document(doc).get(ID_FIELD)));while (++prev < doc) {- assertFalse("document " + prev + " not marked as deleted", mappedLiveDocs.get(prev));+ assertFalse("document " + prev + " not marked as deleted", mappedLiveDocs == null || mappedLiveDocs.get(prev));}}- docs = reader.termDocsEnum(term);- doc = 0;+ docs = termsEnum.docs(mappedLiveDocs, docs);+ doc = -1;prev = -1;- while ((doc = docs.advance(doc)) != DocIdSetIterator.NO_MORE_DOCS) {- if (mappedLiveDocs != null) {- assertTrue("document " + doc + " marked as deleted", mappedLiveDocs.get(doc));- }+ while ((doc = docs.advance(doc + 1)) != DocIdSetIterator.NO_MORE_DOCS) {+ assertTrue("document " + doc + " marked as deleted", mappedLiveDocs == null || mappedLiveDocs.get(doc));assertEquals("incorrect value; doc " + doc, sortedValues[doc].intValue(), Integer.parseInt(reader.document(doc).get(ID_FIELD)));while (++prev < doc) {- assertFalse("document " + prev + " not marked as deleted", mappedLiveDocs.get(prev));+ assertFalse("document " + prev + " not marked as deleted", mappedLiveDocs == null || mappedLiveDocs.get(prev));}}}