LUCENE-508: make sure SegmentTermEnum.prev() is accurate (= last term) after next() returns false

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@609780 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-01-07 21:15:48 +00:00
parent eaba22c72a
commit 2677871bbb
3 changed files with 24 additions and 1 deletions

View File

@ -213,7 +213,11 @@ Bug fixes
28. LUCENE-749: ChainedFilter behavior fixed when logic of
first filter is ANDNOT. (Antonio Bruno via Doron Cohen)
29. LUCENE-508: Make sure SegmentTermEnum.prev() is accurate (= last
term) after next() returns false. (Steven Tamm via Mike
McCandless)
New features

View File

@ -114,6 +114,7 @@ final class SegmentTermEnum extends TermEnum implements Cloneable {
/** Increments the enumeration to the next element. True if one exists.*/
public final boolean next() throws IOException {
if (position++ >= size - 1) {
prevBuffer.set(termBuffer);
termBuffer.reset();
return false;
}

View File

@ -30,6 +30,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.MockRAMDirectory;
/**
* @author goller
@ -66,6 +67,23 @@ public class TestSegmentTermEnum extends LuceneTestCase
verifyDocFreq();
}
public void testPrevTermAtEnd() throws IOException
{
Directory dir = new MockRAMDirectory();
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
addDoc(writer, "aaa bbb");
writer.close();
IndexReader reader = IndexReader.open(dir);
SegmentTermEnum termEnum = (SegmentTermEnum) reader.terms();
assertTrue(termEnum.next());
assertEquals("aaa", termEnum.term().text());
assertTrue(termEnum.next());
assertEquals("aaa", termEnum.prev().text());
assertEquals("bbb", termEnum.term().text());
assertFalse(termEnum.next());
assertEquals("bbb", termEnum.prev().text());
}
private void verifyDocFreq()
throws IOException
{