Skip to content

Apache Lucene open-source search software

LUCENE-2645: fix false assert trip

Michael McCandless17d62eeaefb1
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index bb247f22793..a95e04a4f7b 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -433,6 +433,10 @@ Bug fixes
with more document deletions is requested before a reader with fewer
deletions, provided they share some segments. (yonik)
+* LUCENE-2645: Fix false assertion error when same token was added one
+ after another with 0 posIncr. (Kurosaka Teruhiko via Mike
+ McCandless)
+
======================= Lucene 3.x (not yet released) ================
Changes in backwards compatibility policy
diff --git a/lucene/src/java/org/apache/lucene/index/codecs/sep/SepPostingsWriterImpl.java b/lucene/src/java/org/apache/lucene/index/codecs/sep/SepPostingsWriterImpl.java
index e4c5a484865..ac72d5a0125 100644
--- a/lucene/src/java/org/apache/lucene/index/codecs/sep/SepPostingsWriterImpl.java
+++ b/lucene/src/java/org/apache/lucene/index/codecs/sep/SepPostingsWriterImpl.java
@@ -230,7 +230,7 @@ public final class SepPostingsWriterImpl extends PostingsWriterBase {
assert !omitTF;
final int delta = position - lastPosition;
- assert delta > 0 || position == 0: "position=" + position + " lastPosition=" + lastPosition; // not quite right (if pos=0 is repeated twice we don't catch it)
+ assert delta >= 0: "position=" + position + " lastPosition=" + lastPosition; // not quite right (if pos=0 is repeated twice we don't catch it)
lastPosition = position;
if (storePayloads) {
diff --git a/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsWriter.java b/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsWriter.java
index 457e3c24821..474485be200 100644
--- a/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsWriter.java
+++ b/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsWriter.java
@@ -193,8 +193,8 @@ public final class StandardPostingsWriter extends PostingsWriterBase {
assert proxOut != null;
final int delta = position - lastPosition;
-
- assert delta > 0 || position == 0: "position=" + position + " lastPosition=" + lastPosition; // not quite right (if pos=0 is repeated twice we don't catch it)
+
+ assert delta >= 0: "position=" + position + " lastPosition=" + lastPosition;
lastPosition = position;
diff --git a/lucene/src/test/org/apache/lucene/index/TestSameTokenSamePosition.java b/lucene/src/test/org/apache/lucene/index/TestSameTokenSamePosition.java
new file mode 100644
index 00000000000..b46c37d965d
--- /dev/null
+++ b/lucene/src/test/org/apache/lucene/index/TestSameTokenSamePosition.java
@@ -0,0 +1,82 @@
+package org.apache.lucene.index;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+import java.io.Reader;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
+import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.LuceneTestCase;
+
+public class TestSameTokenSamePosition extends LuceneTestCase {
+
+ /**
+ * Attempt to reproduce an assertion error that happens
+ * only with the trunk version around April 2011.
+ * @param args
+ */
+ public void test() throws Exception {
+ Directory dir = newDirectory();
+ RandomIndexWriter riw = new RandomIndexWriter(random, dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new BugReproAnalyzer()));
+ Document doc = new Document();
+ doc.add(new Field("eng", "Six drunken" /*This shouldn't matter. */,
+ Field.Store.YES, Field.Index.ANALYZED));
+ riw.addDocument(doc);
+ riw.close();
+ dir.close();
+ }
+}
+
+final class BugReproAnalyzer extends Analyzer{
+ @Override
+ public TokenStream tokenStream(String arg0, Reader arg1) {
+ return new BugReproAnalyzerTokenizer();
+ }
+}
+
+final class BugReproAnalyzerTokenizer extends TokenStream {
+ private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
+ private final OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
+ private final PositionIncrementAttribute posIncAtt = addAttribute(PositionIncrementAttribute.class);
+ int tokenCount = 4;
+ int nextTokenIndex = 0;
+ String terms[] = new String[]{"six", "six", "drunken", "drunken"};
+ int starts[] = new int[]{0, 0, 4, 4};
+ int ends[] = new int[]{3, 3, 11, 11};
+ int incs[] = new int[]{1, 0, 1, 0};
+
+ @Override
+ public boolean incrementToken() throws IOException {
+ if (nextTokenIndex < tokenCount) {
+ termAtt.setEmpty().append(terms[nextTokenIndex]);
+ offsetAtt.setOffset(starts[nextTokenIndex], ends[nextTokenIndex]);
+ posIncAtt.setPositionIncrement(incs[nextTokenIndex]);
+ nextTokenIndex++;
+ return true;
+ } else {
+ return false;
+ }
+ }
+}