Skip to content

Apache Lucene open-source search software

LUCENE-1423 InstantiatedTermEnum#skipTo(Term) throws ArrayIndexOutOfBoundsException on an empty index.

Karl-Johan Wettin456b10fdf91e
diff --git a/contrib/CHANGES.txt b/contrib/CHANGES.txt
index 76ca6c47252..edc02d88bbe 100644
--- a/contrib/CHANGES.txt
+++ b/contrib/CHANGES.txt
@@ -2,6 +2,35 @@ Lucene contrib change Log
======================= Trunk (not yet released) =======================
+Changes in runtime behavior
+
+ (None)
+
+API Changes
+
+ (None)
+
+Bug fixes
+
+ 1. LUCENE-1423: InstantiatedTermEnum#skipTo(Term) throws ArrayIndexOutOfBounds on empty index.
+ (Karl Wettin)
+
+New features
+
+ (None)
+
+Documentation
+
+ (None)
+
+Build
+
+ (None)
+
+Test Cases
+
+ (None)
+
======================= Release 2.4.0 2008-10-06 =======================
Changes in runtime behavior
diff --git a/contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedTermEnum.java b/contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedTermEnum.java
index 4306466a363..b21d8f6f173 100644
--- a/contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedTermEnum.java
+++ b/contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedTermEnum.java
@@ -84,6 +84,10 @@ public class InstantiatedTermEnum
// in lucene for many years now, so there is
// very to gain by optimizing this method more,
+ if (reader.getIndex().getOrderedTerms().length == 0) {
+ return false;
+ }
+
InstantiatedTerm term = reader.getIndex().findTerm(target);
if (term != null) {
this.term = term;
diff --git a/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestEmptyIndex.java b/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestEmptyIndex.java
new file mode 100644
index 00000000000..3f24c081d54
--- /dev/null
+++ b/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestEmptyIndex.java
@@ -0,0 +1,77 @@
+/**
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+package org.apache.lucene.store.instantiated;
+
+import junit.framework.TestCase;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.search.TopDocCollector;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermEnum;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.RAMDirectory;
+
+public class TestEmptyIndex extends TestCase {
+
+ public void testSearch() throws Exception {
+
+ InstantiatedIndex ii = new InstantiatedIndex();
+
+ IndexReader r = new InstantiatedIndexReader(ii);
+ IndexSearcher s = new IndexSearcher(r);
+
+ TopDocCollector c = new TopDocCollector(1);
+ s.search(new TermQuery(new Term("foo", "bar")), c);
+
+ assertEquals(0, c.getTotalHits());
+
+ s.close();
+ r.close();
+ ii.close();
+
+ }
+
+ public void testTermEnum() throws Exception {
+
+ InstantiatedIndex ii = new InstantiatedIndex();
+ IndexReader r = new InstantiatedIndexReader(ii);
+ termEnumTest(r);
+ r.close();
+ ii.close();
+
+ // make sure a Directory acts the same
+
+ Directory d = new RAMDirectory();
+ new IndexWriter(d, null, true, IndexWriter.MaxFieldLength.UNLIMITED).close();
+ r = IndexReader.open(d);
+ termEnumTest(r);
+ r.close();
+ d.close();
+ }
+
+ public void termEnumTest(IndexReader r) throws Exception {
+ TermEnum terms = r.terms();
+
+ assertNull(terms.term());
+ assertFalse(terms.next());
+ assertFalse(terms.skipTo(new Term("foo", "bar")));
+
+ }
+
+}
diff --git a/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestRealTime.java b/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestRealTime.java
new file mode 100644
index 00000000000..dd244acffbd
--- /dev/null
+++ b/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestRealTime.java
@@ -0,0 +1,66 @@
+/**
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+package org.apache.lucene.store.instantiated;
+
+import junit.framework.TestCase;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.TopDocCollector;
+import org.apache.lucene.search.HitCollector;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.Term;
+
+public class TestRealTime extends TestCase {
+
+ public void test() throws Exception {
+
+ InstantiatedIndex index = new InstantiatedIndex();
+ InstantiatedIndexReader reader = new InstantiatedIndexReader(index);
+ IndexSearcher searcher = new IndexSearcher(reader);
+ InstantiatedIndexWriter writer = new InstantiatedIndexWriter(index);
+
+ Document doc;
+ Collector collector;
+
+ doc = new Document();
+ doc.add(new Field("f", "a", Field.Store.NO, Field.Index.NOT_ANALYZED));
+ writer.addDocument(doc);
+ writer.commit();
+
+ collector = new Collector();
+ searcher.search(new TermQuery(new Term("f", "a")), collector);
+ assertEquals(1, collector.hits);
+
+ doc = new Document();
+ doc.add(new Field("f", "a", Field.Store.NO, Field.Index.NOT_ANALYZED));
+ writer.addDocument(doc);
+ writer.commit();
+
+ collector = new Collector();
+ searcher.search(new TermQuery(new Term("f", "a")), collector);
+ assertEquals(2, collector.hits);
+
+ }
+
+ public static class Collector extends HitCollector {
+ private int hits = 0;
+ public void collect(int doc, float score) {
+ hits++;
+ }
+ }
+
+}