LUCENE-3633: Remove more code duplication -> readerIndex is already available in ReaderUtil. Also fix bug in docId checking. Moved the check to implementation, so its also done if DocFieldVisitor is used.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1213006 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2011-12-11 14:44:32 +00:00
parent 1757f192ac
commit 77ee519468
2 changed files with 6 additions and 28 deletions

View File

@ -105,29 +105,11 @@ abstract class BaseMultiReader<R extends IndexReader> extends IndexReader implem
}
/** Helper method for subclasses to get the corresponding reader for a doc ID */
protected final int readerIndex(int n) { // find reader for doc n:
return readerIndex(n, this.starts, this.subReaders.length);
}
final static int readerIndex(int n, int[] starts, int numSubReaders) { // find reader for doc n:
int lo = 0; // search starts array
int hi = numSubReaders - 1; // for first element less
while (hi >= lo) {
int mid = (lo + hi) >>> 1;
int midValue = starts[mid];
if (n < midValue)
hi = mid - 1;
else if (n > midValue)
lo = mid + 1;
else { // found a match
while (mid+1 < numSubReaders && starts[mid+1] == midValue) {
mid++; // scan to last match
}
return mid;
}
protected final int readerIndex(int docID) {
if (docID < 0 || docID >= maxDoc) {
throw new IllegalArgumentException("docID must be >= 0 and < maxDoc=" + maxDoc + " (got docID=" + docID + ")");
}
return hi;
return ReaderUtil.subIndex(docID, this.starts);
}
@Override
@ -158,10 +140,9 @@ abstract class BaseMultiReader<R extends IndexReader> extends IndexReader implem
public Collection<String> getFieldNames (IndexReader.FieldOption fieldNames) {
ensureOpen();
// maintain a unique set of field names
Set<String> fieldSet = new HashSet<String>();
final Set<String> fieldSet = new HashSet<String>();
for (IndexReader reader : subReaders) {
Collection<String> names = reader.getFieldNames(fieldNames);
fieldSet.addAll(names);
fieldSet.addAll(reader.getFieldNames(fieldNames));
}
return fieldSet;
}

View File

@ -733,9 +733,6 @@ public abstract class IndexReader implements Cloneable,Closeable {
// IndexableField
public final Document document(int docID) throws CorruptIndexException, IOException {
ensureOpen();
if (docID < 0 || docID >= maxDoc()) {
throw new IllegalArgumentException("docID must be >= 0 and < maxDoc=" + maxDoc() + " (got docID=" + docID + ")");
}
final DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
document(docID, visitor);
return visitor.getDocument();