don't throw scary EOFE if you pass oob docID to IR.document

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1164620 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2011-09-02 16:43:31 +00:00
parent d535c65c95
commit 46fe671e8c
3 changed files with 23 additions and 0 deletions

View File

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

View File

@ -455,6 +455,9 @@ public class SegmentReader extends IndexReader implements Cloneable {
public void document(int docID, StoredFieldVisitor visitor) throws CorruptIndexException, IOException { public void document(int docID, StoredFieldVisitor visitor) throws CorruptIndexException, IOException {
ensureOpen(); ensureOpen();
if (docID < 0 || docID >= maxDoc()) {
throw new IllegalArgumentException("docID must be >= 0 and < maxDoc=" + maxDoc() + " (got docID=" + docID + ")");
}
getFieldsReader().visitDocument(docID, visitor); getFieldsReader().visitDocument(docID, visitor);
} }

View File

@ -1388,4 +1388,21 @@ public class TestIndexReader extends LuceneTestCase
assertEquals(3, closeCount[0]); assertEquals(3, closeCount[0]);
dir.close(); dir.close();
} }
public void testOOBDocID() throws Exception {
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
writer.addDocument(new Document());
IndexReader r = writer.getReader();
writer.close();
r.document(0);
try {
r.document(1);
fail("did not hit exception");
} catch (IllegalArgumentException iae) {
// expected
}
r.close();
dir.close();
}
} }