You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2013/03/15 10:59:04 UTC
svn commit: r1456851 - in /lucene/dev/branches/branch_4x: ./ lucene/
lucene/misc/
lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java
lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java
Author: jpountz
Date: Fri Mar 15 09:59:04 2013
New Revision: 1456851
URL: http://svn.apache.org/r1456851
Log:
LUCENE-4834: Sorter API: Make TermsEnum.docs accept any source of liveDocs (merged from r1456842).
Modified:
lucene/dev/branches/branch_4x/ (props changed)
lucene/dev/branches/branch_4x/lucene/ (props changed)
lucene/dev/branches/branch_4x/lucene/misc/ (props changed)
lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java
lucene/dev/branches/branch_4x/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java
Modified: lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java?rev=1456851&r1=1456850&r2=1456851&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java (original)
+++ lucene/dev/branches/branch_4x/lucene/misc/src/java/org/apache/lucene/index/sorter/SortingAtomicReader.java Fri Mar 15 09:59:04 2013
@@ -66,13 +66,11 @@ public class SortingAtomicReader extends
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
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
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;
}
@Override
public 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
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();
+ }
+
+ };
+ }
+
@Override
public 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);
}
@Override
public 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
if (fields == null) {
return null;
} else {
- return new SortingFields(fields, in.getLiveDocs(), in.getFieldInfos(), docMap);
+ return new SortingFields(fields, in.getFieldInfos(), docMap);
}
}
Modified: lucene/dev/branches/branch_4x/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java?rev=1456851&r1=1456850&r2=1456851&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java (original)
+++ lucene/dev/branches/branch_4x/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java Fri Mar 15 09:59:04 2013
@@ -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.SortedDoc
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,6 +64,7 @@ import org.apache.lucene.search.similari
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.LuceneTestCase.SuppressCodecs;
import org.apache.lucene.util._TestUtil;
@@ -52,23 +72,6 @@ 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.
- */
-
@SuppressCodecs("Lucene3x")
public abstract class SorterTestBase extends LuceneTestCase {
@@ -233,7 +236,7 @@ public abstract class SorterTestBase ext
int numDocs = atLeast(20);
createIndex(dir, numDocs, random());
- reader = new SlowCompositeReaderWrapper(DirectoryReader.open(dir));
+ reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(dir));
}
@AfterClass
@@ -288,34 +291,54 @@ public abstract class SorterTestBase ext
}
}
}
-
+
+ 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;
+ }
+
@Test
public 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));
}
}
}