Index: src/test/org/apache/lucene/store/instantiated/TestRealTime.java =================================================================== --- src/test/org/apache/lucene/store/instantiated/TestRealTime.java (revision 745917) +++ src/test/org/apache/lucene/store/instantiated/TestRealTime.java (arbetskopia) @@ -24,6 +24,12 @@ import org.apache.lucene.document.Field; import org.apache.lucene.index.Term; +/** + * Assert that the content of an index + * is instantly available + * for all open searchers + * also after a commit. + */ public class TestRealTime extends TestCase { public void test() throws Exception { Index: src/test/org/apache/lucene/store/instantiated/TestUnoptimizedReaderOnConstructor.java =================================================================== --- src/test/org/apache/lucene/store/instantiated/TestUnoptimizedReaderOnConstructor.java (revision 0) +++ src/test/org/apache/lucene/store/instantiated/TestUnoptimizedReaderOnConstructor.java (revision 0) @@ -0,0 +1,53 @@ +package org.apache.lucene.store.instantiated; + +import junit.framework.TestCase; + +import java.io.IOException; + +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.store.Directory; +import org.apache.lucene.analysis.WhitespaceAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; + +/** + * @author kalle + * @since 2009-mar-30 13:15:49 + */ +public class TestUnoptimizedReaderOnConstructor extends TestCase { + + public void test() throws Exception { + Directory dir = new RAMDirectory(); + IndexWriter iw = new IndexWriter(dir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED); + addDocument(iw, "Hello, world!"); + addDocument(iw, "All work and no play makes jack a dull boy"); + iw.commit("a"); + iw.close(); + + iw = new IndexWriter(dir, new WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED); + addDocument(iw, "Hello, tellus!"); + addDocument(iw, "All work and no play makes danny a dull boy"); + iw.commit("b"); + iw.close(); + + iw = new IndexWriter(dir, new WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED); + addDocument(iw, "Hello, earth!"); + addDocument(iw, "All work and no play makes wendy a dull girl"); + iw.commit("c"); + iw.close(); + + IndexReader unoptimizedReader = IndexReader.open(dir); + unoptimizedReader.deleteDocument(2); + + InstantiatedIndex ii = new InstantiatedIndex(unoptimizedReader); + + } + + private void addDocument(IndexWriter iw, String text) throws IOException { + Document doc = new Document(); + doc.add(new Field("field", text, Field.Store.NO, Field.Index.ANALYZED)); + iw.addDocument(doc); + } +} Index: src/java/org/apache/lucene/store/instantiated/InstantiatedIndex.java =================================================================== --- src/java/org/apache/lucene/store/instantiated/InstantiatedIndex.java (revision 745917) +++ src/java/org/apache/lucene/store/instantiated/InstantiatedIndex.java (arbetskopia) @@ -110,7 +110,8 @@ public InstantiatedIndex(IndexReader sourceIndexReader, Set fields) throws IOException { if (!sourceIndexReader.isOptimized()) { - throw new IOException("Source index is not optimized."); + System.out.println(("Source index is not optimized.")); + //throw new IOException("Source index is not optimized."); } @@ -170,11 +171,14 @@ } - documentsByNumber = new InstantiatedDocument[sourceIndexReader.numDocs()]; + documentsByNumber = new InstantiatedDocument[sourceIndexReader.maxDoc()]; + // create documents - for (int i = 0; i < sourceIndexReader.numDocs(); i++) { - if (!sourceIndexReader.isDeleted(i)) { + for (int i = 0; i < sourceIndexReader.maxDoc(); i++) { + if (sourceIndexReader.isDeleted(i)) { + deletedDocuments.add(i); + } else { InstantiatedDocument document = new InstantiatedDocument(); // copy stored fields from source reader Document sourceDocument = sourceIndexReader.document(i); @@ -259,6 +263,9 @@ // load offsets to term-document informations for (InstantiatedDocument document : getDocumentsByNumber()) { + if (document == null) { + continue; // deleted + } for (Field field : (List) document.getDocument().getFields()) { if (field.isTermVectorStored() && field.isStoreOffsetWithTermVector()) { TermPositionVector termPositionVector = (TermPositionVector) sourceIndexReader.getTermFreqVector(document.getDocumentNumber(), field.name());