| Code with Finding: |
class FieldCacheRangeFilter.FieldCacheDocIdSet {
public DocIdSetIterator iterator() throws IOException {
// Synchronization needed because deleted docs BitVector
// can change after call to hasDeletions until TermDocs creation.
// We only use an iterator with termDocs, when this was requested (e.g. range contains 0)
// and the index has deletions
final TermDocs termDocs;
synchronized(reader) {
termDocs = isCacheable() ? null : reader.termDocs(null);
}
if (termDocs != null) {
// a DocIdSetIterator using TermDocs to iterate valid docIds
return new DocIdSetIterator() {
private int doc = -1;
/** @deprecated use {@link #nextDoc()} instead. */
public boolean next() throws IOException {
return nextDoc() != NO_MORE_DOCS;
}
/** @deprecated use {@link #advance(int)} instead. */
public boolean skipTo(int target) throws IOException {
return advance(target) != NO_MORE_DOCS;
}
/** @deprecated use {@link #docID()} instead. */
public int doc() {
return termDocs.doc();
}
public int docID() {
return doc;
}
public int nextDoc() throws IOException {
do {
if (!termDocs.next())
return doc = NO_MORE_DOCS;
} while (!matchDoc(doc = termDocs.doc()));
return doc;
}
public int advance(int target) throws IOException {
if (!termDocs.skipTo(target))
return doc = NO_MORE_DOCS;
while (!matchDoc(doc = termDocs.doc())) {
if (!termDocs.next())
return doc = NO_MORE_DOCS;
}
return doc;
}
};
} else {
// a DocIdSetIterator generating docIds by incrementing a variable -
// this one can be used if there are no deletions are on the index
return new DocIdSetIterator() {
private int doc = -1;
/** @deprecated use {@link #nextDoc()} instead. */
public boolean next() throws IOException {
return nextDoc() != NO_MORE_DOCS;
}
/** @deprecated use {@link #advance(int)} instead. */
public boolean skipTo(int target) throws IOException {
return advance(target) != NO_MORE_DOCS;
}
/** @deprecated use {@link #docID()} instead. */
public int doc() {
return doc;
}
public int docID() {
return doc;
}
public int nextDoc() {
try {
do {
doc++;
} while (!matchDoc(doc));
return doc;
} catch (ArrayIndexOutOfBoundsException e) {
return doc = NO_MORE_DOCS;
}
}
public int advance(int target) {
try {
doc = target;
while (!matchDoc(doc)) {
doc++;
}
return doc;
} catch (ArrayIndexOutOfBoundsException e) {
return doc = NO_MORE_DOCS;
}
}
};
}
}
}
|