Skip to content

Apache Lucene open-source search software

Merging r1061975 through r1062931 into realtime branch

Michael Busch1588c321353a
diff --cc dev-tools/idea/.idea/modules.xml
index 66a3fcd51c7,ecd7c276c37..8b2d77f96e8
--- a/dev-tools/idea/.idea/modules.xml
+++ b/dev-tools/idea/.idea/modules.xml
@@@ -33,6 -20,19 +33,7 @@@
<module filepath="$PROJECT_DIR$/lucene/contrib/swing/swing.iml" />
<module filepath="$PROJECT_DIR$/lucene/contrib/wordnet/wordnet.iml" />
<module filepath="$PROJECT_DIR$/lucene/contrib/xml-query-parser/xml-query-parser.iml" />
- <module filepath="$PROJECT_DIR$/modules/analysis/common/common.iml" />
- <module filepath="$PROJECT_DIR$/modules/analysis/icu/icu.iml" />
- <module filepath="$PROJECT_DIR$/modules/analysis/phonetic/phonetic.iml" />
- <module filepath="$PROJECT_DIR$/modules/analysis/smartcn/smartcn.iml" />
- <module filepath="$PROJECT_DIR$/modules/analysis/stempel/stempel.iml" />
- <module filepath="$PROJECT_DIR$/modules/benchmark/benchmark.iml" />
- <module filepath="$PROJECT_DIR$/solr/solr.iml" />
- <module filepath="$PROJECT_DIR$/solr/contrib/analysis-extras/analysis-extras.iml" />
- <module filepath="$PROJECT_DIR$/solr/contrib/clustering/clustering.iml" />
- <module filepath="$PROJECT_DIR$/solr/contrib/dataimporthandler/dataimporthandler.iml" />
- <module filepath="$PROJECT_DIR$/solr/contrib/dataimporthandler/src/extras/extras.iml" />
- <module filepath="$PROJECT_DIR$/solr/contrib/extraction/extraction.iml" />
+ <module filepath="$PROJECT_DIR$/solr/contrib/uima/uima.iml" />
</modules>
</component>
</project>
diff --cc lucene/src/java/org/apache/lucene/index/DocumentsWriter.java
index da0a7283e10,25cd8cac737..ac781cd90c8
--- a/lucene/src/java/org/apache/lucene/index/DocumentsWriter.java
+++ b/lucene/src/java/org/apache/lucene/index/DocumentsWriter.java
@@@ -27,12 -29,17 +27,12 @@@ import java.util.concurrent.atomic.Atom
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
+import org.apache.lucene.index.DocumentsWriterPerThread.IndexingChain;
+import org.apache.lucene.index.DocumentsWriterPerThreadPool.ThreadState;
import org.apache.lucene.search.Query;
- import org.apache.lucene.search.Similarity;
+ import org.apache.lucene.search.SimilarityProvider;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.Directory;
-import org.apache.lucene.store.RAMFile;
-import org.apache.lucene.util.ArrayUtil;
-import org.apache.lucene.util.RecyclingByteBlockAllocator;
-import org.apache.lucene.util.ThreadInterruptedException;
-import org.apache.lucene.util.RamUsageEstimator;
-import static org.apache.lucene.util.ByteBlockPool.BYTE_BLOCK_MASK;
-import static org.apache.lucene.util.ByteBlockPool.BYTE_BLOCK_SIZE;
/**
* This class accepts multiple added documents and directly
@@@ -103,20 -110,156 +103,20 @@@
final class DocumentsWriter {
final AtomicLong bytesUsed = new AtomicLong(0);
- IndexWriter writer;
Directory directory;
- String segment; // Current segment we are working on
-
- private int nextDocID; // Next docID to be added
- private int numDocs; // # of docs added, but not yet flushed
-
- // Max # ThreadState instances; if there are more threads
- // than this they share ThreadStates
- private DocumentsWriterThreadState[] threadStates = new DocumentsWriterThreadState[0];
- private final HashMap<Thread,DocumentsWriterThreadState> threadBindings = new HashMap<Thread,DocumentsWriterThreadState>();
-
boolean bufferIsFull; // True when it's time to write segment
- private boolean aborting; // True if an abort is pending
+ private volatile boolean closed;
PrintStream infoStream;
- Similarity similarity;
+ SimilarityProvider similarityProvider;
- // max # simultaneous threads; if there are more than
- // this, they wait for others to finish first
- private final int maxThreadStates;
-
- // Deletes for our still-in-RAM (to be flushed next) segment
- private SegmentDeletes pendingDeletes = new SegmentDeletes();
-
- static class DocState {
- DocumentsWriter docWriter;
- Analyzer analyzer;
- PrintStream infoStream;
- SimilarityProvider similarityProvider;
- int docID;
- Document doc;
- String maxTermPrefix;
-
- // Only called by asserts
- public boolean testPoint(String name) {
- return docWriter.writer.testPoint(name);
- }
-
- public void clear() {
- // don't hold onto doc nor analyzer, in case it is
- // largish:
- doc = null;
- analyzer = null;
- }
- }
-
- /** Consumer returns this on each doc. This holds any
- * state that must be flushed synchronized "in docID
- * order". We gather these and flush them in order. */
- abstract static class DocWriter {
- DocWriter next;
- int docID;
- abstract void finish() throws IOException;
- abstract void abort();
- abstract long sizeInBytes();
-
- void setNext(DocWriter next) {
- this.next = next;
- }
- }
-
- /**
- * Create and return a new DocWriterBuffer.
- */
- PerDocBuffer newPerDocBuffer() {
- return new PerDocBuffer();
- }
+ List<String> newFiles;
- /**
- * RAMFile buffer for DocWriters.
- */
- @SuppressWarnings("serial")
- class PerDocBuffer extends RAMFile {
-
- /**
- * Allocate bytes used from shared pool.
- */
- @Override
- protected byte[] newBuffer(int size) {
- assert size == PER_DOC_BLOCK_SIZE;
- return perDocAllocator.getByteBlock();
- }
-
- /**
- * Recycle the bytes used.
- */
- synchronized void recycle() {
- if (buffers.size() > 0) {
- setLength(0);
-
- // Recycle the blocks
- perDocAllocator.recycleByteBlocks(buffers);
- buffers.clear();
- sizeInBytes = 0;
-
- assert numBuffers() == 0;
- }
- }
- }
-
- /**
- * The IndexingChain must define the {@link #getChain(DocumentsWriter)} method
- * which returns the DocConsumer that the DocumentsWriter calls to process the
- * documents.
- */
- abstract static class IndexingChain {
- abstract DocConsumer getChain(DocumentsWriter documentsWriter);
- }
-
- static final IndexingChain defaultIndexingChain = new IndexingChain() {
-
- @Override
- DocConsumer getChain(DocumentsWriter documentsWriter) {
- /*
- This is the current indexing chain:
-
- DocConsumer / DocConsumerPerThread
- --> code: DocFieldProcessor / DocFieldProcessorPerThread
- --> DocFieldConsumer / DocFieldConsumerPerThread / DocFieldConsumerPerField
- --> code: DocFieldConsumers / DocFieldConsumersPerThread / DocFieldConsumersPerField
- --> code: DocInverter / DocInverterPerThread / DocInverterPerField
- --> InvertedDocConsumer / InvertedDocConsumerPerThread / InvertedDocConsumerPerField
- --> code: TermsHash / TermsHashPerThread / TermsHashPerField
- --> TermsHashConsumer / TermsHashConsumerPerThread / TermsHashConsumerPerField
- --> code: FreqProxTermsWriter / FreqProxTermsWriterPerThread / FreqProxTermsWriterPerField
- --> code: TermVectorsTermsWriter / TermVectorsTermsWriterPerThread / TermVectorsTermsWriterPerField
- --> InvertedDocEndConsumer / InvertedDocConsumerPerThread / InvertedDocConsumerPerField
- --> code: NormsWriter / NormsWriterPerThread / NormsWriterPerField
- --> code: StoredFieldsWriter / StoredFieldsWriterPerThread / StoredFieldsWriterPerField
- */
-
- // Build up indexing chain:
-
- final TermsHashConsumer termVectorsWriter = new TermVectorsTermsWriter(documentsWriter);
- final TermsHashConsumer freqProxWriter = new FreqProxTermsWriter();
- /*
- * nesting TermsHash instances here to allow the secondary (TermVectors) share the interned postings
- * via a shared ByteBlockPool. See TermsHashPerField for details.
- */
- final TermsHash termVectorsTermHash = new TermsHash(documentsWriter, false, termVectorsWriter, null);
- final InvertedDocConsumer termsHash = new TermsHash(documentsWriter, true, freqProxWriter, termVectorsTermHash);
- final NormsWriter normsWriter = new NormsWriter();
- final DocInverter docInverter = new DocInverter(termsHash, normsWriter);
- return new DocFieldProcessor(documentsWriter, docInverter);
- }
- };
+ final IndexWriter indexWriter;
- final DocConsumer consumer;
+ private AtomicInteger numDocsInRAM = new AtomicInteger(0);
+ private AtomicLong ramUsed = new AtomicLong(0);
// How much RAM we can use before flushing. This is 0 if
// we are flushing by doc count instead.
@@@ -126,104 -275,73 +126,104 @@@
// non-zero we will flush by RAM usage instead.
private int maxBufferedDocs = IndexWriterConfig.DEFAULT_MAX_BUFFERED_DOCS;
- private boolean closed;
private final FieldInfos fieldInfos;
- private final BufferedDeletes bufferedDeletes;
- private final IndexWriter.FlushControl flushControl;
+ final BufferedDeletes bufferedDeletes;
+ SegmentDeletes pendingDeletes;
+ final IndexingChain chain;
+
+ final DocumentsWriterPerThreadPool perThreadPool;
- DocumentsWriter(Directory directory, IndexWriter writer, IndexingChain indexingChain, int maxThreadStates, FieldInfos fieldInfos, BufferedDeletes bufferedDeletes) throws IOException {
+ DocumentsWriter(Directory directory, IndexWriter writer, IndexingChain chain, DocumentsWriterPerThreadPool indexerThreadPool, FieldInfos fieldInfos, BufferedDeletes bufferedDeletes) throws IOException {
this.directory = directory;
- this.writer = writer;
+ this.indexWriter = writer;
- this.similarity = writer.getConfig().getSimilarity();
+ this.similarityProvider = writer.getConfig().getSimilarityProvider();
- this.maxThreadStates = maxThreadStates;
this.fieldInfos = fieldInfos;
this.bufferedDeletes = bufferedDeletes;
- flushControl = writer.flushControl;
-
- consumer = indexingChain.getChain(this);
+ this.perThreadPool = indexerThreadPool;
+ this.pendingDeletes = new SegmentDeletes();
+ this.chain = chain;
+ this.perThreadPool.initialize(this);
}
- // Buffer a specific docID for deletion. Currently only
- // used when we hit a exception when adding a document
- synchronized void deleteDocID(int docIDUpto) {
- pendingDeletes.addDocID(docIDUpto);
- // NOTE: we do not trigger flush here. This is
- // potentially a RAM leak, if you have an app that tries
- // to add docs but every single doc always hits a
- // non-aborting exception. Allowing a flush here gets
- // very messy because we are only invoked when handling
- // exceptions so to do this properly, while handling an
- // exception we'd have to go off and flush new deletes
- // which is risky (likely would hit some other
- // confounding exception).
- }
-
- boolean deleteQueries(Query... queries) {
- final boolean doFlush = flushControl.waitUpdate(0, queries.length);
- synchronized(this) {
- for (Query query : queries) {
- pendingDeletes.addQuery(query, numDocs);
+ boolean deleteQueries(final Query... queries) throws IOException {
+ Iterator<ThreadState> threadsIterator = perThreadPool.getActivePerThreadsIterator();
+
+ boolean deleted = false;
+ while (threadsIterator.hasNext()) {
+ ThreadState state = threadsIterator.next();
+ state.lock();
+ try {
+ state.perThread.deleteQueries(queries);
+ deleted = true;
+ } finally {
+ state.unlock();
}
}
- return doFlush;
- }
-
- boolean deleteQuery(Query query) {
- final boolean doFlush = flushControl.waitUpdate(0, 1);
- synchronized(this) {
- pendingDeletes.addQuery(query, numDocs);
+
+ if (!deleted) {
+ synchronized(this) {
+ for (Query query : queries) {
+ pendingDeletes.addQuery(query, SegmentDeletes.MAX_INT);
+ }
+ }
}
- return doFlush;
+
+ return false;
+ }
+
+ boolean deleteQuery(final Query query) throws IOException {
+ return deleteQueries(query);
}
-
- boolean deleteTerms(Term... terms) {
- final boolean doFlush = flushControl.waitUpdate(0, terms.length);
- synchronized(this) {
- for (Term term : terms) {
- pendingDeletes.addTerm(term, numDocs);
+
+ boolean deleteTerms(final Term... terms) throws IOException {
+ Iterator<ThreadState> threadsIterator = perThreadPool.getActivePerThreadsIterator();
+
+ boolean deleted = false;
+ while (threadsIterator.hasNext()) {
+ ThreadState state = threadsIterator.next();
+ deleted = true;
+ state.lock();
+ try {
+ state.perThread.deleteTerms(terms);
+ } finally {
+ state.unlock();
+ }
+ }
+
+ if (!deleted) {
+ synchronized(this) {
+ for (Term term : terms) {
+ pendingDeletes.addTerm(term, SegmentDeletes.MAX_INT);
+ }
}
}
- return doFlush;
+
+ return false;
+ }
+
+ boolean deleteTerm(final Term term) throws IOException {
+ return deleteTerms(term);
}
- boolean deleteTerm(Term term, boolean skipWait) {
- final boolean doFlush = flushControl.waitUpdate(0, 1, skipWait);
- synchronized(this) {
- pendingDeletes.addTerm(term, numDocs);
+ boolean deleteTerm(final Term term, ThreadState exclude) {
+ Iterator<ThreadState> threadsIterator = perThreadPool.getActivePerThreadsIterator();
+
+ boolean deleted = false;
+ while (threadsIterator.hasNext()) {
+ deleted = true;
+ ThreadState state = threadsIterator.next();
+ if (state != exclude) {
+ state.lock();
+ try {
+ state.perThread.deleteTerms(term);
+ } finally {
+ state.unlock();
+ }
+ }
}
- return doFlush;
+
+ return deleted;
}
public FieldInfos getFieldInfos() {
@@@ -240,20 -352,15 +240,20 @@@
* here. */
synchronized void setInfoStream(PrintStream infoStream) {
this.infoStream = infoStream;
- for(int i=0;i<threadStates.length;i++) {
- threadStates[i].docState.infoStream = infoStream;
- }
+ pushConfigChange();
+ }
+
- synchronized void setSimilarity(Similarity similarity) {
- this.similarity = similarity;
++ synchronized void setSimilarityProvider(SimilarityProvider similarityProvider) {
++ this.similarityProvider = similarityProvider;
+ pushConfigChange();
}
- synchronized void setSimilarityProvider(SimilarityProvider similarity) {
- this.similarityProvider = similarity;
- for(int i=0;i<threadStates.length;i++) {
- threadStates[i].docState.similarityProvider = similarity;
+ private final void pushConfigChange() {
+ Iterator<ThreadState> it = perThreadPool.getAllPerThreadsIterator();
+ while (it.hasNext()) {
+ DocumentsWriterPerThread perThread = it.next().perThread;
+ perThread.docState.infoStream = this.infoStream;
- perThread.docState.similarity = this.similarity;
++ perThread.docState.similarityProvider = this.similarityProvider;
}
}
diff --cc lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java
index 12fd2de097b,00000000000..95e03df0fc7
mode 100644,000000..100644
--- a/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java
+++ b/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java
@@@ -1,363 -1,0 +1,363 @@@
+package org.apache.lucene.index;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 static org.apache.lucene.util.ByteBlockPool.BYTE_BLOCK_MASK;
+import static org.apache.lucene.util.ByteBlockPool.BYTE_BLOCK_SIZE;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.text.NumberFormat;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.search.Query;
- import org.apache.lucene.search.Similarity;
++import org.apache.lucene.search.SimilarityProvider;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.ByteBlockPool.DirectAllocator;
+import org.apache.lucene.util.RamUsageEstimator;
+
+public class DocumentsWriterPerThread {
+
+ /**
+ * The IndexingChain must define the {@link #getChain(DocumentsWriter)} method
+ * which returns the DocConsumer that the DocumentsWriter calls to process the
+ * documents.
+ */
+ abstract static class IndexingChain {
+ abstract DocConsumer getChain(DocumentsWriterPerThread documentsWriterPerThread);
+ }
+
+
+ static final IndexingChain defaultIndexingChain = new IndexingChain() {
+
+ @Override
+ DocConsumer getChain(DocumentsWriterPerThread documentsWriterPerThread) {
+ /*
+ This is the current indexing chain:
+
+ DocConsumer / DocConsumerPerThread
+ --> code: DocFieldProcessor / DocFieldProcessorPerThread
+ --> DocFieldConsumer / DocFieldConsumerPerThread / DocFieldConsumerPerField
+ --> code: DocFieldConsumers / DocFieldConsumersPerThread / DocFieldConsumersPerField
+ --> code: DocInverter / DocInverterPerThread / DocInverterPerField
+ --> InvertedDocConsumer / InvertedDocConsumerPerThread / InvertedDocConsumerPerField
+ --> code: TermsHash / TermsHashPerThread / TermsHashPerField
+ --> TermsHashConsumer / TermsHashConsumerPerThread / TermsHashConsumerPerField
+ --> code: FreqProxTermsWriter / FreqProxTermsWriterPerThread / FreqProxTermsWriterPerField
+ --> code: TermVectorsTermsWriter / TermVectorsTermsWriterPerThread / TermVectorsTermsWriterPerField
+ --> InvertedDocEndConsumer / InvertedDocConsumerPerThread / InvertedDocConsumerPerField
+ --> code: NormsWriter / NormsWriterPerThread / NormsWriterPerField
+ --> code: StoredFieldsWriter / StoredFieldsWriterPerThread / StoredFieldsWriterPerField
+ */
+
+ // Build up indexing chain:
+
+ final TermsHashConsumer termVectorsWriter = new TermVectorsTermsWriter(documentsWriterPerThread);
+ final TermsHashConsumer freqProxWriter = new FreqProxTermsWriter();
+
+ final InvertedDocConsumer termsHash = new TermsHash(documentsWriterPerThread, freqProxWriter,
+ new TermsHash(documentsWriterPerThread, termVectorsWriter, null));
+ final NormsWriter normsWriter = new NormsWriter();
+ final DocInverter docInverter = new DocInverter(documentsWriterPerThread.docState, termsHash, normsWriter);
+ return new DocFieldProcessor(documentsWriterPerThread, docInverter);
+ }
+ };
+
+ // Deletes for our still-in-RAM (to be flushed next) segment
+ SegmentDeletes pendingDeletes = new SegmentDeletes();
+
+ static class DocState {
+ final DocumentsWriterPerThread docWriter;
+ Analyzer analyzer;
+ PrintStream infoStream;
- Similarity similarity;
++ SimilarityProvider similarityProvider;
+ int docID;
+ Document doc;
+ String maxTermPrefix;
+
+ DocState(DocumentsWriterPerThread docWriter) {
+ this.docWriter = docWriter;
+ }
+
+ // Only called by asserts
+ public boolean testPoint(String name) {
+ return docWriter.writer.testPoint(name);
+ }
+ }
+
+ /** Called if we hit an exception at a bad time (when
+ * updating the index files) and must discard all
+ * currently buffered docs. This resets our state,
+ * discarding any docs added since last flush. */
+ void abort() throws IOException {
+ aborting = true;
+ try {
+ if (infoStream != null) {
+ message("docWriter: now abort");
+ }
+ try {
+ consumer.abort();
+ } catch (Throwable t) {
+ }
+
+ pendingDeletes.clear();
+ // Reset all postings data
+ doAfterFlush();
+
+ } finally {
+ aborting = false;
+ if (infoStream != null) {
+ message("docWriter: done abort");
+ }
+ }
+ }
+
+ final DocumentsWriter parent;
+ final IndexWriter writer;
+
+ final Directory directory;
+ final DocState docState;
+ final DocConsumer consumer;
+ private DocFieldProcessor docFieldProcessor;
+
+ String segment; // Current segment we are working on
+ boolean aborting; // True if an abort is pending
+
+ private final PrintStream infoStream;
+ private int numDocsInRAM;
+ private int flushedDocCount;
+ SegmentWriteState flushState;
+
+ final AtomicLong bytesUsed = new AtomicLong(0);
+
+ private final FieldInfos fieldInfos;
+
+ public DocumentsWriterPerThread(Directory directory, DocumentsWriter parent, IndexingChain indexingChain) {
+ this.directory = directory;
+ this.parent = parent;
+ this.writer = parent.indexWriter;
+ this.fieldInfos = new FieldInfos();
+ this.infoStream = parent.indexWriter.getInfoStream();
+ this.docState = new DocState(this);
- this.docState.similarity = parent.indexWriter.getConfig().getSimilarity();
++ this.docState.similarityProvider = parent.indexWriter.getConfig().getSimilarityProvider();
+
+ consumer = indexingChain.getChain(this);
+ if (consumer instanceof DocFieldProcessor) {
+ docFieldProcessor = (DocFieldProcessor) consumer;
+ }
+ }
+
+ void setAborting() {
+ aborting = true;
+ }
+
+ public void updateDocument(Document doc, Analyzer analyzer, Term delTerm) throws IOException {
+ assert writer.testPoint("DocumentsWriterPerThread addDocument start");
+ docState.doc = doc;
+ docState.analyzer = analyzer;
+ docState.docID = numDocsInRAM;
+ if (delTerm != null) {
+ pendingDeletes.addTerm(delTerm, docState.docID);
+ }
+
+ if (segment == null) {
+ // this call is synchronized on IndexWriter.segmentInfos
+ segment = writer.newSegmentName();
+ assert numDocsInRAM == 0;
+ }
+
+ boolean success = false;
+ try {
+ consumer.processDocument();
+
+ success = true;
+ } finally {
+ if (!success) {
+ if (!aborting) {
+ // mark document as deleted
+ deleteDocID(docState.docID);
+ numDocsInRAM++;
+ } else {
+ abort();
+ }
+ }
+ }
+
+ success = false;
+ try {
+ numDocsInRAM++;
+ consumer.finishDocument();
+
+ success = true;
+ } finally {
+ if (!success) {
+ abort();
+ }
+ }
+ }
+
+ // Buffer a specific docID for deletion. Currently only
+ // used when we hit a exception when adding a document
+ void deleteDocID(int docIDUpto) {
+ pendingDeletes.addDocID(docIDUpto);
+ // NOTE: we do not trigger flush here. This is
+ // potentially a RAM leak, if you have an app that tries
+ // to add docs but every single doc always hits a
+ // non-aborting exception. Allowing a flush here gets
+ // very messy because we are only invoked when handling
+ // exceptions so to do this properly, while handling an
+ // exception we'd have to go off and flush new deletes
+ // which is risky (likely would hit some other
+ // confounding exception).
+ }
+
+ void deleteQueries(Query... queries) {
+ for (Query query : queries) {
+ pendingDeletes.addQuery(query, numDocsInRAM);
+ }
+ }
+
+ void deleteTerms(Term... terms) {
+ for (Term term : terms) {
+ pendingDeletes.addTerm(term, numDocsInRAM);
+ }
+ }
+
+ int getNumDocsInRAM() {
+ return numDocsInRAM;
+ }
+
+ /** Returns true if any of the fields in the current
+ * buffered docs have omitTermFreqAndPositions==false */
+ boolean hasProx() {
+ return (docFieldProcessor != null) ? docFieldProcessor.fieldInfos.hasProx()
+ : true;
+ }
+
+ SegmentCodecs getCodec() {
+ return flushState.segmentCodecs;
+ }
+
+ /** Reset after a flush */
+ private void doAfterFlush() throws IOException {
+ segment = null;
+ parent.substractFlushedNumDocs(numDocsInRAM);
+ numDocsInRAM = 0;
+ }
+
+ /** Flush all pending docs to a new segment */
+ SegmentInfo flush() throws IOException {
+ assert numDocsInRAM > 0;
+
+ flushState = new SegmentWriteState(infoStream, directory, segment, fieldInfos,
+ numDocsInRAM, writer.getConfig().getTermIndexInterval(),
+ SegmentCodecs.build(fieldInfos, writer.codecs));
+
+ if (infoStream != null) {
+ message("flush postings as segment " + flushState.segmentName + " numDocs=" + numDocsInRAM);
+ }
+
+ if (aborting) {
+ if (infoStream != null) {
+ message("flush: skip because aborting is set");
+ }
+ return null;
+ }
+
+ boolean success = false;
+
+ try {
+
+ SegmentInfo newSegment = new SegmentInfo(segment, flushState.numDocs, directory, false, fieldInfos.hasProx(), flushState.segmentCodecs, false);
+ consumer.flush(flushState);
+ newSegment.setHasVectors(flushState.hasVectors);
+
+ if (infoStream != null) {
+ message("new segment has " + (flushState.hasVectors ? "vectors" : "no vectors"));
+ message("flushedFiles=" + newSegment.files());
+ message("flushed codecs=" + newSegment.getSegmentCodecs());
+ }
+ flushedDocCount += flushState.numDocs;
+
+ doAfterFlush();
+
+ success = true;
+
+ return newSegment;
+ } finally {
+ if (!success) {
+ if (segment != null) {
+ synchronized(parent.indexWriter) {
+ parent.indexWriter.deleter.refresh(segment);
+ }
+ }
+
+ abort();
+ }
+ }
+ }
+
+ /** Get current segment name we are writing. */
+ String getSegment() {
+ return segment;
+ }
+
+ long bytesUsed() {
+ return bytesUsed.get();
+ }
+
+ FieldInfos getFieldInfos() {
+ return fieldInfos;
+ }
+
+ void message(String message) {
+ writer.message("DW: " + message);
+ }
+
+ NumberFormat nf = NumberFormat.getInstance();
+
+ /* Initial chunks size of the shared byte[] blocks used to
+ store postings data */
+ final static int BYTE_BLOCK_NOT_MASK = ~BYTE_BLOCK_MASK;
+
+ /* if you increase this, you must fix field cache impl for
+ * getTerms/getTermsIndex requires <= 32768 */
+ final static int MAX_TERM_LENGTH_UTF8 = BYTE_BLOCK_SIZE-2;
+
+ /* Initial chunks size of the shared int[] blocks used to
+ store postings data */
+ final static int INT_BLOCK_SHIFT = 13;
+ final static int INT_BLOCK_SIZE = 1 << INT_BLOCK_SHIFT;
+ final static int INT_BLOCK_MASK = INT_BLOCK_SIZE - 1;
+
+ /* Allocate another int[] from the shared pool */
+ int[] getIntBlock() {
+ int[] b = new int[INT_BLOCK_SIZE];
+ bytesUsed.addAndGet(INT_BLOCK_SIZE*RamUsageEstimator.NUM_BYTES_INT);
+ return b;
+ }
+
+ final DirectAllocator byteBlockAllocator = new DirectAllocator();
+
+ String toMB(long v) {
+ return nf.format(v/1024./1024.);
+ }
+
+}
diff --cc lucene/src/java/org/apache/lucene/index/IndexWriter.java
index 271dc2a83a1,e746427eec9..f9da4d58725
--- a/lucene/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/lucene/src/java/org/apache/lucene/index/IndexWriter.java
@@@ -604,12 -611,10 +611,10 @@@ public class IndexWriter implements Clo
return sr;
}
}
-
+
-
-
/**
* Obtain the number of deleted docs for a pooled reader.
- * If the reader isn't being pooled, the segmentInfo's
+ * If the reader isn't being pooled, the segmentInfo's
* delCount is returned.
*/
public int numDeletedDocs(SegmentInfo info) throws IOException {
@@@ -3143,7 -3100,8 +3153,8 @@@
message("merge segmentCodecs=" + merger.getSegmentCodecs());
message("merge store matchedCount=" + merger.getMatchedSubReaderCount() + " vs " + numSegments);
}
+ anyNonBulkMerges |= merger.getMatchedSubReaderCount() != numSegments;
-
+
assert mergedDocCount == totDocCount;
// Very important to do this before opening the reader
diff --cc lucene/src/java/org/apache/lucene/index/IndexWriterConfig.java
index 80ad08e5b3b,18daa12e06c..60962204161
--- a/lucene/src/java/org/apache/lucene/index/IndexWriterConfig.java
+++ b/lucene/src/java/org/apache/lucene/index/IndexWriterConfig.java
@@@ -18,10 -18,11 +18,11 @@@ package org.apache.lucene.index
*/
import org.apache.lucene.analysis.Analyzer;
-import org.apache.lucene.index.DocumentsWriter.IndexingChain;
+import org.apache.lucene.index.DocumentsWriterPerThread.IndexingChain;
import org.apache.lucene.index.IndexWriter.IndexReaderWarmer;
import org.apache.lucene.index.codecs.CodecProvider;
- import org.apache.lucene.search.Similarity;
+ import org.apache.lucene.search.IndexSearcher;
+ import org.apache.lucene.search.SimilarityProvider;
import org.apache.lucene.util.Version;
/**
@@@ -247,14 -246,13 +246,13 @@@ public final class IndexWriterConfig im
}
/**
- * Expert: returns the {@link Similarity} implementation used by this
- * IndexWriter. This defaults to the current value of
- * {@link Similarity#getDefault()}.
+ * Expert: returns the {@link SimilarityProvider} implementation used by this
+ * IndexWriter.
*/
- public Similarity getSimilarity() {
- return similarity;
+ public SimilarityProvider getSimilarityProvider() {
+ return similarityProvider;
}
-
+
/**
* Expert: set the interval between indexed terms. Large values cause less
* memory to be used by IndexReader, but slow random-access to terms. Small
diff --cc lucene/src/java/org/apache/lucene/index/NormsWriterPerField.java
index ec886ab196b,49a726e6a90..d91072da049
--- a/lucene/src/java/org/apache/lucene/index/NormsWriterPerField.java
+++ b/lucene/src/java/org/apache/lucene/index/NormsWriterPerField.java
@@@ -26,9 -27,11 +27,10 @@@ import org.apache.lucene.util.ArrayUtil
final class NormsWriterPerField extends InvertedDocEndConsumerPerField implements Comparable<NormsWriterPerField> {
- final NormsWriterPerThread perThread;
final FieldInfo fieldInfo;
- final DocumentsWriter.DocState docState;
+ final DocumentsWriterPerThread.DocState docState;
-
+ final Similarity similarity;
+
// Holds all docID/norm pairs we've seen
int[] docIDs = new int[1];
byte[] norms = new byte[1];
@@@ -43,10 -46,12 +45,11 @@@
upto = 0;
}
- public NormsWriterPerField(final DocInverterPerField docInverterPerField, final NormsWriterPerThread perThread, final FieldInfo fieldInfo) {
- this.perThread = perThread;
+ public NormsWriterPerField(final DocInverterPerField docInverterPerField, final FieldInfo fieldInfo) {
this.fieldInfo = fieldInfo;
- docState = perThread.docState;
+ docState = docInverterPerField.docState;
fieldState = docInverterPerField.fieldState;
+ similarity = docState.similarityProvider.get(fieldInfo.name);
}
@Override
diff --cc lucene/src/java/org/apache/lucene/index/SegmentInfo.java
index 007638614d5,a3dfaac25a7..785388059ab
--- a/lucene/src/java/org/apache/lucene/index/SegmentInfo.java
+++ b/lucene/src/java/org/apache/lucene/index/SegmentInfo.java
@@@ -583,10 -590,11 +601,11 @@@ public final class SegmentInfo
}
/** Used for debugging. Format may suddenly change.
- *
+ *
* <p>Current format looks like
- * <code>_a:c45/4->_1</code>, which means the segment's
- * name is <code>_a</code>; it's using compound file
+ * <code>_a(3.1):c45/4->_1</code>, which means the segment's
+ * name is <code>_a</code>; it was created with Lucene 3.1 (or
+ * '?' if it's unkown); it's using compound file
* format (would be <code>C</code> if not compound); it
* has 45 documents; it has 4 deletions (this part is
* left off when there are no deletions); it's using the
diff --cc lucene/src/java/org/apache/lucene/index/SegmentMerger.java
index b0837ee1819,bacfec8a599..1dd36f7a2be
--- a/lucene/src/java/org/apache/lucene/index/SegmentMerger.java
+++ b/lucene/src/java/org/apache/lucene/index/SegmentMerger.java
@@@ -56,10 -56,10 +56,10 @@@ final class SegmentMerger
private List<IndexReader> readers = new ArrayList<IndexReader>();
private final FieldInfos fieldInfos;
-
+
private int mergedDocs;
- private final CheckAbort checkAbort;
+ private final MergeState.CheckAbort checkAbort;
/** Maximum number of contiguous documents to bulk-copy
when merging stored fields */
diff --cc lucene/src/test/org/apache/lucene/index/TestIndexReader.java
index 2736b783db6,41fb07fbf73..9c39b8f1375
--- a/lucene/src/test/org/apache/lucene/index/TestIndexReader.java
+++ b/lucene/src/test/org/apache/lucene/index/TestIndexReader.java
@@@ -582,12 -587,12 +587,12 @@@ public class TestIndexReader extends Lu
// now open reader & set norm for doc 0 (writes to
// _0_1.s0)
reader = IndexReader.open(dir, false);
- reader.setNorm(0, "content", Similarity.getDefault().encodeNormValue(2.0f));
+ reader.setNorm(0, "content", sim.encodeNormValue(2.0f));
reader.close();
-
+
// now open reader again & set norm for doc 0 (writes to _0_2.s0)
reader = IndexReader.open(dir, false);
- reader.setNorm(0, "content", Similarity.getDefault().encodeNormValue(2.0f));
+ reader.setNorm(0, "content", sim.encodeNormValue(2.0f));
reader.close();
assertFalse("failed to remove first generation norms file on writing second generation",
dir.fileExists("_0_1.s0"));
diff --cc lucene/src/test/org/apache/lucene/index/TestIndexWriterConfig.java
index ee2e5497d5c,1e12d8531fd..a89c58a1cab
--- a/lucene/src/test/org/apache/lucene/index/TestIndexWriterConfig.java
+++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriterConfig.java
@@@ -24,10 -24,10 +24,10 @@@ import java.util.HashSet
import java.util.Set;
import org.apache.lucene.analysis.MockAnalyzer;
-import org.apache.lucene.index.DocumentsWriter.IndexingChain;
+import org.apache.lucene.index.DocumentsWriterPerThread.IndexingChain;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.DefaultSimilarity;
- import org.apache.lucene.search.Similarity;
+ import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.Test;
@@@ -175,15 -174,16 +176,16 @@@ public class TestIndexWriterConfig exte
conf.setMergeScheduler(null);
assertEquals(ConcurrentMergeScheduler.class, conf.getMergeScheduler().getClass());
- // Test Similarity
- assertTrue(Similarity.getDefault() == conf.getSimilarity());
- conf.setSimilarity(new MySimilarity());
- assertEquals(MySimilarity.class, conf.getSimilarity().getClass());
- conf.setSimilarity(null);
- assertTrue(Similarity.getDefault() == conf.getSimilarity());
+ // Test Similarity:
+ // we shouldnt assert what the default is, just that its not null.
+ assertTrue(IndexSearcher.getDefaultSimilarityProvider() == conf.getSimilarityProvider());
+ conf.setSimilarityProvider(new MySimilarity());
+ assertEquals(MySimilarity.class, conf.getSimilarityProvider().getClass());
+ conf.setSimilarityProvider(null);
+ assertTrue(IndexSearcher.getDefaultSimilarityProvider() == conf.getSimilarityProvider());
// Test IndexingChain
- assertTrue(DocumentsWriter.defaultIndexingChain == conf.getIndexingChain());
+ assertTrue(DocumentsWriterPerThread.defaultIndexingChain == conf.getIndexingChain());
conf.setIndexingChain(new MyIndexingChain());
assertEquals(MyIndexingChain.class, conf.getIndexingChain().getClass());
conf.setIndexingChain(null);