Apache Solr open-source search software
LUCENE-1510 InstantiatedIndexReader#norms methods throws NullPointerException on empty index.
Karl-Johan Wettinf991524da8cf
diff --git a/contrib/CHANGES.txt b/contrib/CHANGES.txtindex 57fa8f7de98..c4c334b3746 100644--- a/contrib/CHANGES.txt+++ b/contrib/CHANGES.txt@@ -19,6 +19,9 @@ Bug fixessame way IndexWriter does. Parts of InstantiatedIndex was not Serializable.(Karl Wettin)+ 3. LUCENE-1510: InstantiatedIndexReader#norms methods throws NullPointerException on empty index.+ (Karl Wettin, Robert Newson)+New features1. LUCENE-1470: Added TrieRangeQuery, a much faster implementation ofdiff --git a/contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndexReader.java b/contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndexReader.javaindex 8bcf57fbe9a..80ef5a51f7b 100644--- a/contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndexReader.java+++ b/contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndexReader.java@@ -280,6 +280,9 @@ public class InstantiatedIndexReader extends IndexReader {*/public byte[] norms(String field) throws IOException {byte[] norms = getIndex().getNormsByFieldNameAndDocumentNumber().get(field);+ if (norms == null) {+ return new byte[0]; // todo a static final zero length attribute?+ }if (updatedNormsByFieldNameAndDocumentNumber != null) {norms = norms.clone();List<NormUpdate> updated = updatedNormsByFieldNameAndDocumentNumber.get(field);@@ -294,6 +297,9 @@ public class InstantiatedIndexReader extends IndexReader {public void norms(String field, byte[] bytes, int offset) throws IOException {byte[] norms = getIndex().getNormsByFieldNameAndDocumentNumber().get(field);+ if (norms == null) {+ return;+ }System.arraycopy(norms, 0, bytes, offset, norms.length);}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.javaindex 3f24c081d54..9e3786dc8b9 100644--- a/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestEmptyIndex.java+++ b/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestEmptyIndex.java@@ -17,16 +17,19 @@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.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.index.TermEnum;-import org.apache.lucene.index.IndexWriter;+import org.apache.lucene.search.IndexSearcher;+import org.apache.lucene.search.TermQuery;+import org.apache.lucene.search.TopDocCollector;import org.apache.lucene.store.Directory;import org.apache.lucene.store.RAMDirectory;+import java.util.Arrays;+import java.io.IOException;+public class TestEmptyIndex extends TestCase {public void testSearch() throws Exception {@@ -47,6 +50,38 @@ public class TestEmptyIndex extends TestCase {}+ public void testNorms() throws Exception {++ InstantiatedIndex ii = new InstantiatedIndex();+ IndexReader r = new InstantiatedIndexReader(ii);+ testNorms(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);+ testNorms(r);+ r.close();+ d.close();++ }++ private void testNorms(IndexReader r) throws IOException {+ byte[] norms;+ norms = r.norms("foo");+ assertNotNull(norms);+ assertEquals(0, norms.length);+ norms = new byte[10];+ Arrays.fill(norms, (byte)10);+ r.norms("foo", norms, 10);+ for (byte b : norms) {+ assertEquals((byte)10, b);+ }+ }+public void testTermEnum() throws Exception {InstantiatedIndex ii = new InstantiatedIndex();