LUCENE-1255: if position is negative, silently change it to 0

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@644400 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-04-03 16:42:06 +00:00
parent 378442872f
commit 5cfd9c8244
4 changed files with 55 additions and 4 deletions

View File

@ -223,7 +223,7 @@ public class CheckIndex {
final int pos = termPositions.nextPosition(); final int pos = termPositions.nextPosition();
if (pos < 0) if (pos < 0)
throw new RuntimeException("term " + term + ": doc " + doc + ": pos " + pos + " is out of bounds"); throw new RuntimeException("term " + term + ": doc " + doc + ": pos " + pos + " is out of bounds");
if (pos <= lastPos) if (pos < lastPos)
throw new RuntimeException("term " + term + ": doc " + doc + ": pos " + pos + " < lastPos " + lastPos); throw new RuntimeException("term " + term + ": doc " + doc + ": pos " + pos + " < lastPos " + lastPos);
} }
} }

View File

@ -239,11 +239,13 @@ final class DocumentsWriterFieldData implements Comparable {
try { try {
offsetEnd = offset-1; offsetEnd = offset-1;
Token token;
for(;;) { for(;;) {
token = stream.next(localToken); Token token = stream.next(localToken);
if (token == null) break; if (token == null) break;
position += (token.getPositionIncrement() - 1); position += (token.getPositionIncrement() - 1);
// LUCENE-1255: don't allow negative positon
if (position < 0)
position = 0;
addPosition(token); addPosition(token);
if (++length >= maxFieldLength) { if (++length >= maxFieldLength) {
if (threadState.docWriter.infoStream != null) if (threadState.docWriter.infoStream != null)

View File

@ -1692,8 +1692,11 @@ public class IndexWriter {
throw oom; throw oom;
} finally { } finally {
synchronized(this) { synchronized(this) {
if (!closed) if (!closed) {
closing = false; closing = false;
if (infoStream != null)
message("hit exception while closing");
}
notifyAll(); notifyAll();
} }
} }

View File

@ -30,6 +30,7 @@ import org.apache.lucene.util.UnicodeUtil;
import org.apache.lucene.analysis.WhitespaceAnalyzer; import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.WhitespaceTokenizer; import org.apache.lucene.analysis.WhitespaceTokenizer;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SinkTokenizer;
import org.apache.lucene.analysis.TokenFilter; import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer;
@ -40,6 +41,9 @@ import org.apache.lucene.document.Field;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Hits; import org.apache.lucene.search.Hits;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.store.RAMDirectory;
@ -3549,4 +3553,46 @@ public class TestIndexWriter extends LuceneTestCase
assertEquals(expected[i], utf16a.result[i]); assertEquals(expected[i], utf16a.result[i]);
} }
} }
// LUCENE-1255
public void testNegativePositions() throws Throwable {
SinkTokenizer tokens = new SinkTokenizer();
Token t = new Token();
t.setTermText("a");
t.setPositionIncrement(0);
tokens.add(t);
t.setTermText("b");
t.setPositionIncrement(1);
tokens.add(t);
t.setTermText("c");
tokens.add(t);
MockRAMDirectory dir = new MockRAMDirectory();
IndexWriter w = new IndexWriter(dir, false, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
Document doc = new Document();
doc.add(new Field("field", tokens));
w.addDocument(doc);
w.commit();
IndexSearcher s = new IndexSearcher(dir);
PhraseQuery pq = new PhraseQuery();
pq.add(new Term("field", "a"));
pq.add(new Term("field", "b"));
pq.add(new Term("field", "c"));
Hits hits = s.search(pq);
assertEquals(1, hits.length());
Query q = new SpanTermQuery(new Term("field", "a"));
hits = s.search(q);
assertEquals(1, hits.length());
TermPositions tps = s.getIndexReader().termPositions(new Term("field", "a"));
assertTrue(tps.next());
assertEquals(1, tps.freq());
assertEquals(0, tps.nextPosition());
w.close();
assertTrue(_TestUtil.checkIndex(dir));
s.close();
dir.close();
}
} }