Apache Solr open-source search software
data corruption reading lazy loaded fields: LUCENE-683
Yonik Seeleycb4f27f25f5e
diff --git a/CHANGES.txt b/CHANGES.txtindex e051850a39b..6c94733d87b 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -127,6 +127,8 @@ Bug fixeshas no value.(Oliver Hutchison via Chris Hostetter)+15. LUCENE-683: Fixed data corruption when reading lazy loaded fields.+ (Yonik Seeley)Optimizationsdiff --git a/src/java/org/apache/lucene/index/FieldsReader.java b/src/java/org/apache/lucene/index/FieldsReader.javaindex 1ec1d484c98..6eefeb19ac9 100644--- a/src/java/org/apache/lucene/index/FieldsReader.java+++ b/src/java/org/apache/lucene/index/FieldsReader.java@@ -39,12 +39,12 @@ import org.apache.lucene.store.IndexInput;* @version $Id$*/final class FieldsReader {- private FieldInfos fieldInfos;- private IndexInput fieldsStream;- private IndexInput indexStream;+ private final FieldInfos fieldInfos;+ private final IndexInput fieldsStream;+ private final IndexInput indexStream;private int size;- private static ThreadLocal fieldsStreamTL = new ThreadLocal();+ private ThreadLocal fieldsStreamTL = new ThreadLocal();FieldsReader(Directory d, String segment, FieldInfos fn) throws IOException {fieldInfos = fn;diff --git a/src/test/org/apache/lucene/index/TestLazyBug.java b/src/test/org/apache/lucene/index/TestLazyBug.javanew file mode 100755index 00000000000..66e153463b0--- /dev/null+++ b/src/test/org/apache/lucene/index/TestLazyBug.java@@ -0,0 +1,130 @@+package org.apache.lucene.index;++/**+ * 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.+ */++import junit.framework.TestCase;+import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.analysis.SimpleAnalyzer;+import org.apache.lucene.document.*;+import org.apache.lucene.store.Directory;+import org.apache.lucene.store.RAMDirectory;++import java.util.*;+import java.lang.reflect.Array;+++/**+ * Test demonstrating EOF bug on the last field of the last doc+ * if other docs have allready been accessed.+ */+public class TestLazyBug extends TestCase {++ public static int BASE_SEED = 13;++ public static int NUM_DOCS = 500;+ public static int NUM_FIELDS = 100;++ private static String[] data = new String[] {+ "now",+ "is the time",+ "for all good men",+ "to come to the aid",+ "of their country!",+ "this string contains big chars:{\u0111 \u0222 \u0333 \u1111 \u2222 \u3333}",+ "this string is a bigger string, mary had a little lamb, little lamb, little lamb!"+ };++ private static Set dataset = new HashSet(Arrays.asList(data));++ private static String MAGIC_FIELD = "f"+(NUM_FIELDS/3);++ private static FieldSelector SELECTOR = new FieldSelector() {+ public FieldSelectorResult accept(String f) {+ if (f.equals(MAGIC_FIELD)) {+ return FieldSelectorResult.LOAD;+ }+ return FieldSelectorResult.LAZY_LOAD;+ }+ };++ private static Directory makeIndex() throws RuntimeException {+ Directory dir = new RAMDirectory();+ try {+ Random r = new Random(BASE_SEED + 42) ;+ Analyzer analyzer = new SimpleAnalyzer();+ IndexWriter writer = new IndexWriter(dir, analyzer, true);++ writer.setUseCompoundFile(false);++ for (int d = 1; d <= NUM_DOCS; d++) {+ Document doc = new Document();+ for (int f = 1; f <= NUM_FIELDS; f++ ) {+ doc.add(new Field("f"+f,+ data[f % data.length]+ + '#' + data[r.nextInt(data.length)],+ Field.Store.YES,+ Field.Index.TOKENIZED));+ }+ writer.addDocument(doc);+ }+ writer.close();+ } catch (Exception e) {+ throw new RuntimeException(e);+ }+ return dir;+ }++ public static void doTest(int[] docs) throws Exception {+ Directory dir = makeIndex();+ IndexReader reader = IndexReader.open(dir);+ for (int i = 0; i < docs.length; i++) {+ Document d = reader.document(docs[i], SELECTOR);+ String trash = d.get(MAGIC_FIELD);++ List fields = d.getFields();+ for (Iterator fi = fields.iterator(); fi.hasNext(); ) {+ Fieldable f=null;+ try {+ f = (Fieldable) fi.next();+ String fname = f.name();+ String fval = f.stringValue();+ assertNotNull(docs[i]+" FIELD: "+fname, fval);+ String[] vals = fval.split("#");+ if (!dataset.contains(vals[0]) || !dataset.contains(vals[1])) {+ fail("FIELD:"+fname+",VAL:"+fval);+ }+ } catch (Exception e) {+ throw new Exception(docs[i]+" WTF: "+f.name(), e);+ }+ }+ }+ reader.close();+ }++ public void testLazyWorks() throws Exception {+ doTest(new int[] { 399 });+ }++ public void testLazyAlsoWorks() throws Exception {+ doTest(new int[] { 399, 150 });+ }++ public void testLazyBroken() throws Exception {+ doTest(new int[] { 150, 399 });+ }++}