LUCENE-6246: simplify flags for PostingsEnum

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1661822 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan Ernst 2015-02-24 00:09:38 +00:00
parent acbba205a7
commit 992abbeff1
15 changed files with 37 additions and 43 deletions

View File

@ -119,10 +119,10 @@ API Changes
* LUCENE-6218, LUCENE-6220: Add Collector.needsScores() and needsScores
parameter to Query.createWeight(). (Robert Muir, Adrien Grand)
* LUCENE-4524: Merge DocsEnum and DocsAndPositionsEnum into a single
PostingsEnum iterator. TermsEnum.docs() and TermsEnum.docsAndPositions()
are replaced by TermsEnum.postings(). (Alan Woodward, Simon Willnauer,
Robert Muir)
* LUCENE-4524, LUCENE-6246, LUCENE-6256: Merge DocsEnum and DocsAndPositionsEnum
into a single PostingsEnum iterator. TermsEnum.docs() and TermsEnum.docsAndPositions()
are replaced by TermsEnum.postings().
(Alan Woodward, Simon Willnauer, Robert Muir, Ryan Ernst)
* LUCENE-6222: Removed TermFilter, use a QueryWrapperFilter(TermQuery)
instead. This will be as efficient now that queries can opt out from
@ -143,10 +143,6 @@ API Changes
* LUCENE-6245: Force Filter subclasses to implement toString API from Query.
(Ryan Ernst)
* LUCENE-6256: Change PostingsEnum.nextPosition() to consistently return -1 when
positions are not available.
(Ryan Ernst)
* LUCENE-6268: Replace FieldValueFilter and DocValuesRangeFilter with equivalent
queries that support approximations. (Adrien Grand)

View File

@ -653,7 +653,7 @@ public class BlockTermsReader extends FieldsProducer {
@Override
public PostingsEnum postings(Bits liveDocs, PostingsEnum reuse, int flags) throws IOException {
if (PostingsEnum.requiresPositions(flags)) {
if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) {
if (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) {
// Positions were not indexed:
return null;

View File

@ -204,7 +204,7 @@ final class OrdsIntersectTermsEnum extends TermsEnum {
@Override
public PostingsEnum postings(Bits skipDocs, PostingsEnum reuse, int flags) throws IOException {
if (PostingsEnum.requiresPositions(flags)) {
if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) {
if (fr.fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) {
// Positions were not indexed:
return null;

View File

@ -925,7 +925,7 @@ public final class OrdsSegmentTermsEnum extends TermsEnum {
@Override
public PostingsEnum postings(Bits skipDocs, PostingsEnum reuse, int flags) throws IOException {
if (PostingsEnum.requiresPositions(flags)) {
if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) {
if (fr.fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) {
// Positions were not indexed:
return null;

View File

@ -209,7 +209,7 @@ class SimpleTextFieldsReader extends FieldsProducer {
@Override
public PostingsEnum postings(Bits liveDocs, PostingsEnum reuse, int flags) throws IOException {
if (PostingsEnum.requiresPositions(flags)) {
if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) {
if (indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) {
// Positions were not indexed
return null;

View File

@ -389,7 +389,7 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader {
@Override
public PostingsEnum postings(Bits liveDocs, PostingsEnum reuse, int flags) throws IOException {
if (PostingsEnum.requiresPositions(flags)) {
if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) {
SimpleTVPostings postings = current.getValue();
if (postings.positions == null && postings.startOffsets == null) {
return null;
@ -402,7 +402,7 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader {
// TODO: reuse
SimpleTVDocsEnum e = new SimpleTVDocsEnum();
e.reset(liveDocs, (flags & PostingsEnum.FREQS) == 0 ? 1 : current.getValue().freq);
e.reset(liveDocs, PostingsEnum.featureRequested(flags, PostingsEnum.FREQS) == false ? 1 : current.getValue().freq);
return e;
}

View File

@ -214,8 +214,8 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {
if (!indexHasPositions)
return null;
if ((!indexHasOffsets || (flags & PostingsEnum.OFFSETS) == 0) &&
(!indexHasPayloads || (flags & PostingsEnum.PAYLOADS) == 0)) {
if ((!indexHasOffsets || PostingsEnum.featureRequested(flags, PostingsEnum.OFFSETS) == false) &&
(!indexHasPayloads || PostingsEnum.featureRequested(flags, PostingsEnum.PAYLOADS) == false)) {
BlockPostingsEnum docsAndPositionsEnum;
if (reuse instanceof BlockPostingsEnum) {
docsAndPositionsEnum = (BlockPostingsEnum) reuse;
@ -317,7 +317,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {
}
doc = -1;
this.needsFreq = (flags & PostingsEnum.FREQS) != 0;
this.needsFreq = PostingsEnum.featureRequested(flags, PostingsEnum.FREQS);
if (indexHasFreq == false || needsFreq == false) {
Arrays.fill(freqBuffer, 1);
}
@ -981,8 +981,8 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {
lastPosBlockFP = posTermStartFP + termState.lastPosBlockOffset;
}
this.needsOffsets = (flags & PostingsEnum.OFFSETS) != 0;
this.needsPayloads = (flags & PostingsEnum.PAYLOADS) != 0;
this.needsOffsets = PostingsEnum.featureRequested(flags, PostingsEnum.OFFSETS);
this.needsPayloads = PostingsEnum.featureRequested(flags, PostingsEnum.PAYLOADS);
doc = -1;
accum = 0;

View File

@ -244,7 +244,7 @@ class FreqProxFields extends Fields {
throw new IllegalArgumentException("did not index positions");
}
if (!terms.hasOffsets && (flags & PostingsEnum.OFFSETS) == PostingsEnum.OFFSETS) {
if (!terms.hasOffsets && PostingsEnum.featureRequested(flags, PostingsEnum.OFFSETS)) {
// Caller wants offsets but we didn't index them;
// don't lie:
throw new IllegalArgumentException("did not index offsets");
@ -264,7 +264,7 @@ class FreqProxFields extends Fields {
FreqProxDocsEnum docsEnum;
if (!terms.hasFreq && (flags & PostingsEnum.FREQS) != 0) {
if (!terms.hasFreq && PostingsEnum.featureRequested(flags, PostingsEnum.FREQS)) {
// Caller wants freqs but we didn't index them;
// don't lie:
throw new IllegalArgumentException("did not index freq");

View File

@ -151,7 +151,7 @@ public final class MultiFields extends Fields {
* term does not exist or positions were not indexed.
* @see #getTermPositionsEnum(IndexReader, Bits, String, BytesRef, int) */
public static PostingsEnum getTermPositionsEnum(IndexReader r, Bits liveDocs, String field, BytesRef term) throws IOException {
return getTermPositionsEnum(r, liveDocs, field, term, PostingsEnum.OFFSETS | PostingsEnum.PAYLOADS);
return getTermPositionsEnum(r, liveDocs, field, term, PostingsEnum.ALL);
}
/** Returns {@link PostingsEnum} for the specified

View File

@ -33,37 +33,35 @@ public abstract class PostingsEnum extends DocIdSetIterator {
* Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)} if you don't
* require per-document postings in the returned enum.
*/
public static final int NONE = 0x0;
public static final short NONE = 0;
/** Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)}
* if you require term frequencies in the returned enum. */
public static final int FREQS = 0x1;
public static final short FREQS = 1 << 3;
/** Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)}
* if you require term positions in the returned enum. */
public static final int POSITIONS = 0x3;
public static final short POSITIONS = FREQS | 1 << 4;
/** Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)}
* if you require offsets in the returned enum. */
public static final int OFFSETS = 0x7;
public static final short OFFSETS = POSITIONS | 1 << 5;
/** Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)}
* if you require payloads in the returned enum. */
public static final int PAYLOADS = 0xB;
public static final short PAYLOADS = POSITIONS | 1 << 6;
/**
* Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)}
* to get positions, payloads and offsets in the returned enum
*/
public static final int ALL = POSITIONS | PAYLOADS;
public static final short ALL = OFFSETS | PAYLOADS;
/**
* Returns true if the passed in flags require positions to be indexed
* @param flags the postings flags
* @return true if the passed in flags require positions to be indexed
* Returns true if the given feature is requested in the flags, false otherwise.
*/
public static boolean requiresPositions(int flags) {
return ((flags & POSITIONS) >= POSITIONS);
public static boolean featureRequested(int flags, short feature) {
return (flags & feature) == feature;
}
private AttributeSource atts = null;

View File

@ -609,7 +609,7 @@ public class TestPayloads extends LuceneTestCase {
writer.addDocument(doc);
DirectoryReader reader = writer.getReader();
LeafReader sr = SlowCompositeReaderWrapper.wrap(reader);
PostingsEnum de = sr.postings(new Term("field", "withPayload"), PostingsEnum.POSITIONS);
PostingsEnum de = sr.postings(new Term("field", "withPayload"), PostingsEnum.PAYLOADS);
de.nextDoc();
de.nextPosition();
assertEquals(new BytesRef("test"), de.getPayload());
@ -643,7 +643,7 @@ public class TestPayloads extends LuceneTestCase {
writer.addDocument(doc);
DirectoryReader reader = writer.getReader();
SegmentReader sr = getOnlySegmentReader(reader);
PostingsEnum de = sr.postings(new Term("field", "withPayload"), PostingsEnum.POSITIONS);
PostingsEnum de = sr.postings(new Term("field", "withPayload"), PostingsEnum.PAYLOADS);
de.nextDoc();
de.nextPosition();
assertEquals(new BytesRef("test"), de.getPayload());

View File

@ -132,7 +132,7 @@ public class SortingLeafReader extends FilterLeafReader {
@Override
public PostingsEnum postings(Bits liveDocs, PostingsEnum reuse, final int flags) throws IOException {
if (PostingsEnum.requiresPositions(flags)) {
if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) {
final PostingsEnum inReuse;
final SortingPostingsEnum wrapReuse;
if (reuse != null && reuse instanceof SortingPostingsEnum) {
@ -171,7 +171,7 @@ public class SortingLeafReader extends FilterLeafReader {
}
final PostingsEnum inDocs = in.postings(newToOld(liveDocs), inReuse, flags);
final boolean withFreqs = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >=0 && (flags & PostingsEnum.FREQS) != 0;
final boolean withFreqs = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >=0 && PostingsEnum.featureRequested(flags, PostingsEnum.FREQS);
return new SortingDocsEnum(docMap.size(), wrapReuse, inDocs, withFreqs, docMap);
}

View File

@ -66,7 +66,7 @@ final class IDVersionPostingsReader extends PostingsReaderBase {
public PostingsEnum postings(FieldInfo fieldInfo, BlockTermState termState, Bits liveDocs, PostingsEnum reuse, int flags) throws IOException {
SingleDocsEnum docsEnum;
if (PostingsEnum.requiresPositions(flags)) {
if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) {
SinglePostingsEnum posEnum;
if (reuse instanceof SinglePostingsEnum) {

View File

@ -643,18 +643,18 @@ public abstract class BasePostingsFormatTestCase extends BaseIndexFileFormatTest
if (liveDocs != null) {
throw new IllegalArgumentException("liveDocs must be null");
}
if (PostingsEnum.requiresPositions(flags)) {
if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) {
if (maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) {
return null;
}
if ((flags & PostingsEnum.OFFSETS) == PostingsEnum.OFFSETS && maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) < 0) {
if (PostingsEnum.featureRequested(flags, PostingsEnum.OFFSETS) && maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) < 0) {
return null;
}
if ((flags & PostingsEnum.PAYLOADS) == PostingsEnum.PAYLOADS && allowPayloads == false) {
if (PostingsEnum.featureRequested(flags, PostingsEnum.PAYLOADS) && allowPayloads == false) {
return null;
}
}
if ((flags & PostingsEnum.FREQS) != 0 && maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS) < 0) {
if (PostingsEnum.featureRequested(flags, PostingsEnum.FREQS) && maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS) < 0) {
return null;
}
return getSeedPostings(current.getKey().utf8ToString(), current.getValue().seed, false, maxAllowed, allowPayloads);

View File

@ -1017,7 +1017,7 @@ public final class TestUtil {
case 0: posFlags = PostingsEnum.POSITIONS; break;
case 1: posFlags = PostingsEnum.OFFSETS; break;
case 2: posFlags = PostingsEnum.PAYLOADS; break;
default: posFlags = PostingsEnum.OFFSETS | PostingsEnum.PAYLOADS; break;
default: posFlags = PostingsEnum.ALL; break;
}
PostingsEnum docsAndPositions = termsEnum.postings(liveDocs, null, posFlags);
if (docsAndPositions != null) {