LUCENE-1578: Support for loading unoptimized readers to the constructor of InstantiatedIndex. (Karl Wettin)

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@784481 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Karl-Johan Wettin 2009-06-13 21:54:07 +00:00
parent 1511ec5e31
commit 196428ec39
5 changed files with 79 additions and 6 deletions

View File

@ -64,6 +64,9 @@ New features
6. LUCENE-1676: Added DelimitedPayloadTokenFilter class for automatically adding payloads "in-stream" (Grant Ingersoll)
7. LUCENE-1578: Support for loading unoptimized readers to the
constructor of InstantiatedIndex. (Karl Wettin)
Optimizations
1. LUCENE-1643: Re-use the collation key (RawCollationKey) for

View File

@ -110,7 +110,8 @@ public class InstantiatedIndex
public InstantiatedIndex(IndexReader sourceIndexReader, Set<String> 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 @@ public class InstantiatedIndex
}
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 @@ public class InstantiatedIndex
// load offsets to term-document informations
for (InstantiatedDocument document : getDocumentsByNumber()) {
if (document == null) {
continue; // deleted
}
for (Field field : (List<Field>) document.getDocument().getFields()) {
if (field.isTermVectorStored() && field.isStoreOffsetWithTermVector()) {
TermPositionVector termPositionVector = (TermPositionVector) sourceIndexReader.getTermFreqVector(document.getDocumentNumber(), field.name());

View File

@ -40,6 +40,10 @@ import org.apache.lucene.index.TermPositionVector;
import org.apache.lucene.index.TermPositions;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocCollector;
import org.apache.lucene.search.TopScoreDocCollector;
/**
* Asserts equality of content and behaviour of two index readers.

View File

@ -24,6 +24,12 @@ import org.apache.lucene.document.Document;
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 {

View File

@ -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);
}
}