Skip to content

Apache Lucene open-source search software

merged with trunk

Simon Willnauer8428ecc7ee12
diff --cc dev-tools/idea/lucene/contrib/ant/ant.iml
index 715876178e0,8987c57a65f..6e678eb887b
--- a/dev-tools/idea/lucene/contrib/ant/ant.iml
+++ b/dev-tools/idea/lucene/contrib/ant/ant.iml
@@@ -6,12 -6,9 +6,8 @@@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/java" isTestSource="false" />
- <sourceFolder url="file://$MODULE_DIR$/src/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" />
</content>
- <orderEntry type="module" module-name="common" />
- <orderEntry type="module" module-name="lucene" />
- <orderEntry type="module" module-name="queries" />
- <orderEntry type="module" module-name="misc" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
diff --cc dev-tools/idea/lucene/contrib/queryparser/queryparser.iml
index 7095e409743,49139191784..a565d107ca7
--- a/dev-tools/idea/lucene/contrib/queryparser/queryparser.iml
+++ b/dev-tools/idea/lucene/contrib/queryparser/queryparser.iml
@@@ -6,11 -6,9 +6,8 @@@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/java" isTestSource="false" />
- <sourceFolder url="file://$MODULE_DIR$/src/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" />
</content>
- <orderEntry type="module" module-name="lucene" />
- <orderEntry type="module" module-name="queries" />
- <orderEntry type="module" module-name="misc" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="JUnit" level="project" />
diff --cc dev-tools/idea/modules/analysis/common/common.iml
index 28d4158822a,4882b813b62..739f4de8ba1
--- a/dev-tools/idea/modules/analysis/common/common.iml
+++ b/dev-tools/idea/modules/analysis/common/common.iml
@@@ -7,9 -7,9 +7,8 @@@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/resources" isTestSource="false" />
- <sourceFolder url="file://$MODULE_DIR$/src/java/tools" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" />
</content>
- <orderEntry type="module" module-name="lucene" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="JUnit" level="project" />
diff --cc lucene/src/test-framework/org/apache/lucene/index/RandomIndexWriter.java
index 8380785c32c,0712e4104c4..eb8bae75937
--- a/lucene/src/test-framework/org/apache/lucene/index/RandomIndexWriter.java
+++ b/lucene/src/test-framework/org/apache/lucene/index/RandomIndexWriter.java
@@@ -92,14 -93,26 +93,22 @@@ public class RandomIndexWriter implemen
}
}
- /**
- * Adds a Document.
- * @see IndexWriter#addDocument(Document)
- */
public void addDocument(Document doc) throws IOException {
w.addDocument(doc);
+ maybeCommit();
+ }
+
+ private void maybeCommit() throws IOException {
if (docCount++ == flushAt) {
if (LuceneTestCase.VERBOSE) {
- System.out.println("RIW.addDocument: now doing a commit");
+ System.out.println("RIW.add/updateDocument: now doing a commit at docCount=" + docCount);
}
w.commit();
- flushAt += _TestUtil.nextInt(r, 10, 1000);
+ flushAt += _TestUtil.nextInt(r, (int) (flushAtFactor * 10), (int) (flushAtFactor * 1000));
+ if (flushAtFactor < 2e6) {
+ // gradually but exponentially increase time b/w flushes
+ flushAtFactor *= 1.05;
+ }
}
}
diff --cc lucene/src/test/org/apache/lucene/index/TestAddIndexes.java
index fbbaef941da,1e289b2ebca..acc03927239
--- a/lucene/src/test/org/apache/lucene/index/TestAddIndexes.java
+++ b/lucene/src/test/org/apache/lucene/index/TestAddIndexes.java
@@@ -643,10 -643,10 +643,10 @@@ public class TestAddIndexes extends Luc
writer.close();
dir2 = newDirectory();
- writer2 = new IndexWriter(dir2, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
+ writer2 = new IndexWriter(dir2, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
writer2.setInfoStream(VERBOSE ? System.out : null);
writer2.commit();
-
+
readers = new IndexReader[NUM_COPY];
for(int i=0;i<NUM_COPY;i++)
@@@ -1069,11 -1069,11 +1069,11 @@@
w.addDocument(d);
w.close();
}
-
+
IndexReader[] readers = new IndexReader[] { IndexReader.open(dirs[0]), IndexReader.open(dirs[1]) };
-
+
Directory dir = new RAMDirectory();
- IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()).setMergePolicy(newLogMergePolicy());
+ IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy());
LogMergePolicy lmp = (LogMergePolicy) conf.getMergePolicy();
lmp.setUseCompoundFile(true);
lmp.setNoCFSRatio(1.0); // Force creation of CFS
diff --cc lucene/src/test/org/apache/lucene/index/TestDeletionPolicy.java
index 6eb806de87d,e6166355a22..550ff0968d2
--- a/lucene/src/test/org/apache/lucene/index/TestDeletionPolicy.java
+++ b/lucene/src/test/org/apache/lucene/index/TestDeletionPolicy.java
@@@ -469,9 -469,9 +469,9 @@@ public class TestDeletionPolicy extend
// Now open writer on the commit just before optimize,
// but this time keeping only the last commit:
- writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()).setIndexCommit(lastCommit));
+ writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setIndexCommit(lastCommit));
assertEquals(10, writer.numDocs());
-
+
// Reader still sees optimized index, because writer
// opened on the prior commit has not yet committed:
r = IndexReader.open(dir, true);
diff --cc lucene/src/test/org/apache/lucene/index/TestFlushByRamOrCountsPolicy.java
index 9d4286ea5ee,00000000000..4e803c17ff9
mode 100644,000000..100644
--- a/lucene/src/test/org/apache/lucene/index/TestFlushByRamOrCountsPolicy.java
+++ b/lucene/src/test/org/apache/lucene/index/TestFlushByRamOrCountsPolicy.java
@@@ -1,458 -1,0 +1,458 @@@
+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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.lucene.analysis.MockAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.index.DocumentsWriterPerThreadPool.ThreadState;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.LockObtainFailedException;
+import org.apache.lucene.store.MockDirectoryWrapper;
+import org.apache.lucene.util.LineFileDocs;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.ThrottledIndexOutput;
+import org.junit.Before;
+
+public class TestFlushByRamOrCountsPolicy extends LuceneTestCase {
+
+ private LineFileDocs lineDocFile;
+ private int numCPUs;
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ lineDocFile = new LineFileDocs(random);
+ numCPUs = Runtime.getRuntime().availableProcessors();
+ }
+
+ public void testFlushByRam() throws CorruptIndexException,
+ LockObtainFailedException, IOException, InterruptedException {
+ int[] numThreads = new int[] { numCPUs + random.nextInt(numCPUs + 1), 1 };
+ for (int i = 0; i < numThreads.length; i++) {
+ runFlushByRam(numThreads[i],
+ 1 + random.nextInt(10) + random.nextDouble(), false);
+ }
+
+ for (int i = 0; i < numThreads.length; i++) {
+ // with a 250 mb ram buffer we should never stall
+ runFlushByRam(numThreads[i], 250.d, true);
+ }
+ }
+
+ protected void runFlushByRam(int numThreads, double maxRam,
+ boolean ensureNotStalled) throws IOException, CorruptIndexException,
+ LockObtainFailedException, InterruptedException {
+ final int numDocumentsToIndex = 50 + random.nextInt(150);
+ AtomicInteger numDocs = new AtomicInteger(numDocumentsToIndex);
+ Directory dir = newDirectory();
+ MockDefaultFlushPolicy flushPolicy = new MockDefaultFlushPolicy();
+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT,
- new MockAnalyzer()).setFlushPolicy(flushPolicy);
++ new MockAnalyzer(random)).setFlushPolicy(flushPolicy);
+
+ final int numDWPT = 1 + random.nextInt(8);
+ DocumentsWriterPerThreadPool threadPool = new ThreadAffinityDocumentsWriterThreadPool(
+ numDWPT);
+ iwc.setIndexerThreadPool(threadPool);
+ iwc.setRAMBufferSizeMB(1 + random.nextInt(10) + random.nextDouble());
+ iwc.setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH);
+ iwc.setMaxBufferedDeleteTerms(IndexWriterConfig.DISABLE_AUTO_FLUSH);
+ IndexWriter writer = new IndexWriter(dir, iwc);
+ assertFalse(flushPolicy.flushOnDocCount());
+ assertFalse(flushPolicy.flushOnDeleteTerms());
+ assertTrue(flushPolicy.flushOnRAM());
+ DocumentsWriter docsWriter = writer.getDocsWriter();
+ assertNotNull(docsWriter);
+ DocumentsWriterFlushControl flushControl = docsWriter.flushControl;
+ assertEquals(" bytes must be 0 after init", 0, flushControl.flushBytes());
+
+ IndexThread[] threads = new IndexThread[numThreads];
+ for (int x = 0; x < threads.length; x++) {
+ threads[x] = new IndexThread(numDocs, numThreads, writer, lineDocFile,
+ false);
+ threads[x].start();
+ }
+
+ for (int x = 0; x < threads.length; x++) {
+ threads[x].join();
+ }
+ final long maxRAMBytes = (long) (iwc.getRAMBufferSizeMB() * 1024. * 1024.);
+ assertEquals(" all flushes must be due numThreads=" + numThreads, 0,
+ flushControl.flushBytes());
+ assertEquals(numDocumentsToIndex, writer.numDocs());
+ assertEquals(numDocumentsToIndex, writer.maxDoc());
+ assertTrue("peak bytes without flush exceeded watermark",
+ flushPolicy.peakBytesWithoutFlush <= maxRAMBytes);
+ assertActiveBytesAfter(flushControl);
+ if (flushPolicy.hasMarkedPending) {
+ assertTrue(maxRAMBytes < flushControl.peakActiveBytes);
+ }
+ if (ensureNotStalled) {
+ assertFalse(docsWriter.healthiness.wasStalled);
+ }
+ writer.close();
+ assertEquals(0, flushControl.activeBytes());
+ dir.close();
+ }
+
+ public void testFlushDocCount() throws CorruptIndexException,
+ LockObtainFailedException, IOException, InterruptedException {
+ int[] numThreads = new int[] { numCPUs + random.nextInt(numCPUs + 1), 1 };
+ for (int i = 0; i < numThreads.length; i++) {
+
+ final int numDocumentsToIndex = 50 + random.nextInt(150);
+ AtomicInteger numDocs = new AtomicInteger(numDocumentsToIndex);
+ Directory dir = newDirectory();
+ MockDefaultFlushPolicy flushPolicy = new MockDefaultFlushPolicy();
+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT,
- new MockAnalyzer()).setFlushPolicy(flushPolicy);
++ new MockAnalyzer(random)).setFlushPolicy(flushPolicy);
+
+ final int numDWPT = 1 + random.nextInt(8);
+ DocumentsWriterPerThreadPool threadPool = new ThreadAffinityDocumentsWriterThreadPool(
+ numDWPT);
+ iwc.setIndexerThreadPool(threadPool);
+ iwc.setMaxBufferedDocs(2 + random.nextInt(50));
+ iwc.setRAMBufferSizeMB(IndexWriterConfig.DISABLE_AUTO_FLUSH);
+ iwc.setMaxBufferedDeleteTerms(IndexWriterConfig.DISABLE_AUTO_FLUSH);
+ IndexWriter writer = new IndexWriter(dir, iwc);
+ assertTrue(flushPolicy.flushOnDocCount());
+ assertFalse(flushPolicy.flushOnDeleteTerms());
+ assertFalse(flushPolicy.flushOnRAM());
+ DocumentsWriter docsWriter = writer.getDocsWriter();
+ assertNotNull(docsWriter);
+ DocumentsWriterFlushControl flushControl = docsWriter.flushControl;
+ assertEquals(" bytes must be 0 after init", 0, flushControl.flushBytes());
+
+ IndexThread[] threads = new IndexThread[numThreads[i]];
+ for (int x = 0; x < threads.length; x++) {
+ threads[x] = new IndexThread(numDocs, numThreads[i], writer,
+ lineDocFile, false);
+ threads[x].start();
+ }
+
+ for (int x = 0; x < threads.length; x++) {
+ threads[x].join();
+ }
+
+ assertEquals(" all flushes must be due numThreads=" + numThreads[i], 0,
+ flushControl.flushBytes());
+ assertEquals(numDocumentsToIndex, writer.numDocs());
+ assertEquals(numDocumentsToIndex, writer.maxDoc());
+ assertTrue("peak bytes without flush exceeded watermark",
+ flushPolicy.peakDocCountWithoutFlush <= iwc.getMaxBufferedDocs());
+ assertActiveBytesAfter(flushControl);
+ writer.close();
+ assertEquals(0, flushControl.activeBytes());
+ dir.close();
+ }
+ }
+
+ public void testFlushPolicySetup() throws IOException {
+ Directory dir = newDirectory();
+ FlushByRamOrCountsPolicy flushPolicy = new FlushByRamOrCountsPolicy();
+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT,
- new MockAnalyzer()).setFlushPolicy(flushPolicy);
++ new MockAnalyzer(random)).setFlushPolicy(flushPolicy);
+
+ final int numDWPT = 1 + random.nextInt(10);
+ DocumentsWriterPerThreadPool threadPool = new ThreadAffinityDocumentsWriterThreadPool(
+ numDWPT);
+ iwc.setIndexerThreadPool(threadPool);
+ double maxMB = 1.0 + Math.ceil(random.nextDouble());
+ iwc.setRAMBufferSizeMB(maxMB);
+ iwc.setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH);
+
+ IndexWriter writer = new IndexWriter(dir, iwc);
+ assertEquals((long) (maxMB * 1024. * 1024. * 2.),
+ flushPolicy.getMaxNetBytes());
+
+ writer.close();
+ dir.close();
+ }
+
+ public void testRandom() throws IOException, InterruptedException {
+ final int numThreads = 1 + random.nextInt(8);
+ final int numDocumentsToIndex = 100 + random.nextInt(300);
+ AtomicInteger numDocs = new AtomicInteger(numDocumentsToIndex);
+ Directory dir = newDirectory();
+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT,
- new MockAnalyzer());
++ new MockAnalyzer(random));
+ MockDefaultFlushPolicy flushPolicy = new MockDefaultFlushPolicy();
+ iwc.setFlushPolicy(flushPolicy);
+
+ final int numDWPT = 1 + random.nextInt(8);
+ DocumentsWriterPerThreadPool threadPool = new ThreadAffinityDocumentsWriterThreadPool(
+ numDWPT);
+ iwc.setIndexerThreadPool(threadPool);
+
+ IndexWriter writer = new IndexWriter(dir, iwc);
+ DocumentsWriter docsWriter = writer.getDocsWriter();
+ assertNotNull(docsWriter);
+ DocumentsWriterFlushControl flushControl = docsWriter.flushControl;
+
+ assertEquals(" bytes must be 0 after init", 0, flushControl.flushBytes());
+
+ IndexThread[] threads = new IndexThread[numThreads];
+ for (int x = 0; x < threads.length; x++) {
+ threads[x] = new IndexThread(numDocs, numThreads, writer, lineDocFile,
+ true);
+ threads[x].start();
+ }
+
+ for (int x = 0; x < threads.length; x++) {
+ threads[x].join();
+ }
+
+ assertEquals(" all flushes must be due", 0, flushControl.flushBytes());
+ assertEquals(numDocumentsToIndex, writer.numDocs());
+ assertEquals(numDocumentsToIndex, writer.maxDoc());
+ if (flushPolicy.flushOnRAM() && !flushPolicy.flushOnDocCount()
+ && !flushPolicy.flushOnDeleteTerms()) {
+ final long maxRAMBytes = (long) (iwc.getRAMBufferSizeMB() * 1024. * 1024.);
+ assertTrue("peak bytes without flush exceeded watermark",
+ flushPolicy.peakBytesWithoutFlush <= maxRAMBytes);
+ if (flushPolicy.hasMarkedPending) {
+ assertTrue("max: " + maxRAMBytes + " " + flushControl.peakActiveBytes,
+ maxRAMBytes <= flushControl.peakActiveBytes);
+ }
+ }
+ assertActiveBytesAfter(flushControl);
+ writer.commit();
+ assertEquals(0, flushControl.activeBytes());
+ IndexReader r = IndexReader.open(dir);
+ assertEquals(numDocumentsToIndex, r.numDocs());
+ assertEquals(numDocumentsToIndex, r.maxDoc());
+ if (!flushPolicy.flushOnRAM()) {
+ assertFalse("never stall if we don't flush on RAM", docsWriter.healthiness.wasStalled);
+ assertFalse("never block if we don't flush on RAM", docsWriter.healthiness.hasBlocked());
+ }
+ r.close();
+ writer.close();
+ dir.close();
+ }
+
+ public void testHealthyness() throws InterruptedException,
+ CorruptIndexException, LockObtainFailedException, IOException {
+
+ int[] numThreads = new int[] { 3 + random.nextInt(8), 1 };
+ final int numDocumentsToIndex = 50 + random.nextInt(50);
+ for (int i = 0; i < numThreads.length; i++) {
+ AtomicInteger numDocs = new AtomicInteger(numDocumentsToIndex);
+ MockDirectoryWrapper dir = newDirectory();
+ // mock a very slow harddisk here so that flushing is very slow
+ dir.setThrottledIndexOutput(new ThrottledIndexOutput(ThrottledIndexOutput
+ .mBitsToBytes(50 + random.nextInt(10)), 5 + random.nextInt(5), null));
+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT,
- new MockAnalyzer());
++ new MockAnalyzer(random));
+ iwc.setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH);
+ iwc.setMaxBufferedDeleteTerms(IndexWriterConfig.DISABLE_AUTO_FLUSH);
+ FlushPolicy flushPolicy = new FlushByRamOrCountsPolicy();
+ iwc.setFlushPolicy(flushPolicy);
+
+ DocumentsWriterPerThreadPool threadPool = new ThreadAffinityDocumentsWriterThreadPool(
+ numThreads[i]== 1 ? 1 : 2);
+ iwc.setIndexerThreadPool(threadPool);
+ // with such a small ram buffer we should be stalled quiet quickly
+ iwc.setRAMBufferSizeMB(0.25);
+ IndexWriter writer = new IndexWriter(dir, iwc);
+ IndexThread[] threads = new IndexThread[numThreads[i]];
+ for (int x = 0; x < threads.length; x++) {
+ threads[x] = new IndexThread(numDocs, numThreads[i], writer,
+ lineDocFile, false);
+ threads[x].start();
+ }
+
+ for (int x = 0; x < threads.length; x++) {
+ threads[x].join();
+ }
+ DocumentsWriter docsWriter = writer.getDocsWriter();
+ assertNotNull(docsWriter);
+ DocumentsWriterFlushControl flushControl = docsWriter.flushControl;
+ assertEquals(" all flushes must be due", 0, flushControl.flushBytes());
+ assertEquals(numDocumentsToIndex, writer.numDocs());
+ assertEquals(numDocumentsToIndex, writer.maxDoc());
+ if (flushControl.peakNetBytes > (long)(iwc.getRAMBufferSizeMB() * 1024d * 1024d * 2d)) {
+ assertTrue("should be unhealthy here numThreads: " + numThreads[i],
+ docsWriter.healthiness.wasStalled);
+ }
+
+ if (numThreads[i] == 1) { // single thread could be unhealthy is a single
+ // doc is very large?!
+ assertFalse(
+ "single thread must not block numThreads: " + numThreads[i],
+ docsWriter.healthiness.hasBlocked());
+ } else {
+ if (docsWriter.healthiness.wasStalled) {
+ // TODO maybe this assumtion is too strickt
+ assertTrue(" we should have blocked here numThreads: "
+ + numThreads[i], docsWriter.healthiness.hasBlocked());
+ }
+ }
+ assertActiveBytesAfter(flushControl);
+ writer.close(true);
+ dir.close();
+ }
+ }
+
+ protected void assertActiveBytesAfter(DocumentsWriterFlushControl flushControl) {
+ Iterator<ThreadState> allActiveThreads = flushControl.allActiveThreads();
+ long bytesUsed = 0;
+ while (allActiveThreads.hasNext()) {
+ bytesUsed += allActiveThreads.next().perThread.bytesUsed();
+ }
+ assertEquals(bytesUsed, flushControl.activeBytes());
+ }
+
+ public class IndexThread extends Thread {
+ IndexWriter writer;
+ IndexWriterConfig iwc;
+ LineFileDocs docs;
+ private AtomicInteger pendingDocs;
+ private final boolean doRandomCommit;
+
+ public IndexThread(AtomicInteger pendingDocs, int numThreads,
+ IndexWriter writer, LineFileDocs docs, boolean doRandomCommit) {
+ this.pendingDocs = pendingDocs;
+ this.writer = writer;
+ iwc = writer.getConfig();
+ this.docs = docs;
+ this.doRandomCommit = doRandomCommit;
+ }
+
+ public void run() {
+ try {
+ long ramSize = 0;
+ while (pendingDocs.decrementAndGet() > -1) {
+ Document doc = docs.nextDoc();
+ writer.addDocument(doc);
+ long newRamSize = writer.ramSizeInBytes();
+ if (newRamSize != ramSize) {
+ ramSize = newRamSize;
+ }
+ if (doRandomCommit) {
+ int commit;
+ synchronized (random) {
+ commit = random.nextInt(20);
+ }
+ if (commit == 0) {
+ writer.commit();
+ }
+ }
+ }
+ } catch (Throwable ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+ }
+
+ private static class MockDefaultFlushPolicy extends FlushByRamOrCountsPolicy {
+ long peakBytesWithoutFlush = Integer.MIN_VALUE;
+ long peakDocCountWithoutFlush = Integer.MIN_VALUE;
+ boolean hasMarkedPending = false;
+
+ @Override
+ public void onDelete(DocumentsWriterFlushControl control, ThreadState state) {
+ final ArrayList<ThreadState> pending = new ArrayList<DocumentsWriterPerThreadPool.ThreadState>();
+ final ArrayList<ThreadState> notPending = new ArrayList<DocumentsWriterPerThreadPool.ThreadState>();
+ findPending(control, pending, notPending);
+ final boolean flushCurrent = state.flushPending;
+ final ThreadState toFlush;
+ if (state.flushPending) {
+ toFlush = state;
+ } else if (flushOnDeleteTerms()
+ && state.perThread.pendingDeletes.numTermDeletes.get() >= indexWriterConfig
+ .getMaxBufferedDeleteTerms()) {
+ toFlush = state;
+ } else {
+ toFlush = null;
+ }
+ super.onDelete(control, state);
+ if (toFlush != null) {
+ if (flushCurrent) {
+ assertTrue(pending.remove(toFlush));
+ } else {
+ assertTrue(notPending.remove(toFlush));
+ }
+ assertTrue(toFlush.flushPending);
+ hasMarkedPending = true;
+ }
+
+ for (ThreadState threadState : notPending) {
+ assertFalse(threadState.flushPending);
+ }
+ }
+
+ @Override
+ public void onInsert(DocumentsWriterFlushControl control, ThreadState state) {
+ final ArrayList<ThreadState> pending = new ArrayList<DocumentsWriterPerThreadPool.ThreadState>();
+ final ArrayList<ThreadState> notPending = new ArrayList<DocumentsWriterPerThreadPool.ThreadState>();
+ findPending(control, pending, notPending);
+ final boolean flushCurrent = state.flushPending;
+ long activeBytes = control.activeBytes();
+ final ThreadState toFlush;
+ if (state.flushPending) {
+ toFlush = state;
+ } else if (flushOnDocCount()
+ && state.perThread.getNumDocsInRAM() >= indexWriterConfig
+ .getMaxBufferedDocs()) {
+ toFlush = state;
+ } else if (flushOnRAM()
+ && activeBytes >= (long) (indexWriterConfig.getRAMBufferSizeMB() * 1024. * 1024.)) {
+ toFlush = findLargestNonPendingWriter(control, state);
+ assertFalse(toFlush.flushPending);
+ } else {
+ toFlush = null;
+ }
+ super.onInsert(control, state);
+ if (toFlush != null) {
+ if (flushCurrent) {
+ assertTrue(pending.remove(toFlush));
+ } else {
+ assertTrue(notPending.remove(toFlush));
+ }
+ assertTrue(toFlush.flushPending);
+ hasMarkedPending = true;
+ } else {
+ peakBytesWithoutFlush = Math.max(activeBytes, peakBytesWithoutFlush);
+ peakDocCountWithoutFlush = Math.max(state.perThread.getNumDocsInRAM(),
+ peakDocCountWithoutFlush);
+ }
+
+ for (ThreadState threadState : notPending) {
+ assertFalse(threadState.flushPending);
+ }
+ }
+ }
+
+ static void findPending(DocumentsWriterFlushControl flushControl,
+ ArrayList<ThreadState> pending, ArrayList<ThreadState> notPending) {
+ Iterator<ThreadState> allActiveThreads = flushControl.allActiveThreads();
+ while (allActiveThreads.hasNext()) {
+ ThreadState next = allActiveThreads.next();
+ if (next.flushPending) {
+ pending.add(next);
+ } else {
+ notPending.add(next);
+ }
+ }
+ }
+}
diff --cc lucene/src/test/org/apache/lucene/index/TestIndexReader.java
index e5a21dd922d,e0c700e6001..1c13de2b407
--- a/lucene/src/test/org/apache/lucene/index/TestIndexReader.java
+++ b/lucene/src/test/org/apache/lucene/index/TestIndexReader.java
@@@ -66,10 -66,10 +66,10 @@@ public class TestIndexReader extends Lu
Map<String,String> commitUserData = new HashMap<String,String>();
commitUserData.put("foo", "fighters");
-
+
// set up writer
IndexWriter writer = new IndexWriter(d, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer())
+ TEST_VERSION_CURRENT, new MockAnalyzer(random))
.setMaxBufferedDocs(2));
for(int i=0;i<27;i++)
addDocumentWithFields(writer);
@@@ -115,11 -115,11 +115,11 @@@
r3.close();
d.close();
}
-
+
public void testIsCurrent() throws Exception {
Directory d = newDirectory();
- IndexWriter writer = new IndexWriter(d, newIndexWriterConfig(
+ IndexWriter writer = new IndexWriter(d, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()));
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)));
addDocumentWithFields(writer);
writer.close();
// set up reader:
@@@ -370,9 -370,9 +370,9 @@@
public void testBinaryFields() throws IOException {
Directory dir = newDirectory();
byte[] bin = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
-
+
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()).setMergePolicy(newLogMergePolicy()));
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
-
+
for (int i = 0; i < 10; i++) {
addDoc(writer, "document number " + (i + 1));
addDocumentWithFields(writer);
@@@ -880,10 -880,10 +880,10 @@@
Term searchTerm = new Term("content", "aaa");
int START_COUNT = 157;
int END_COUNT = 144;
-
+
// First build up a starting index:
MockDirectoryWrapper startDir = newDirectory();
- IndexWriter writer = new IndexWriter(startDir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
+ IndexWriter writer = new IndexWriter(startDir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
if (VERBOSE) {
System.out.println("TEST: create initial index");
writer.setInfoStream(System.out);
@@@ -1066,8 -1066,8 +1066,8 @@@
}
public void testDocsOutOfOrderJIRA140() throws IOException {
- Directory dir = newDirectory();
+ Directory dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
for(int i=0;i<11;i++) {
addDoc(writer, "aaa");
}
@@@ -1106,8 -1106,8 +1106,8 @@@
public void testExceptionReleaseWriteLockJIRA768() throws IOException {
- Directory dir = newDirectory();
+ Directory dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
addDoc(writer, "aaa");
writer.close();
@@@ -1602,9 -1602,9 +1602,9 @@@
public void testNoDupCommitFileNames() throws Throwable {
Directory dir = newDirectory();
-
+
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer())
+ TEST_VERSION_CURRENT, new MockAnalyzer(random))
.setMaxBufferedDocs(2));
writer.addDocument(createDocument("a"));
writer.addDocument(createDocument("a"));
@@@ -1820,8 -1820,8 +1820,8 @@@
// LUCENE-2046
public void testPrepareCommitIsCurrent() throws Throwable {
Directory dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()));
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)));
writer.commit();
Document doc = new Document();
writer.addDocument(doc);
diff --cc lucene/src/test/org/apache/lucene/index/TestIndexWriter.java
index 9911c107ab0,ed6f862830d..970c2590f41
--- a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java
+++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java
@@@ -366,8 -367,8 +367,8 @@@ public class TestIndexWriter extends Lu
* these docs until writer is closed.
*/
public void testCommitOnClose() throws IOException {
- Directory dir = newDirectory();
+ Directory dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()));
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
for (int i = 0; i < 14; i++) {
addDoc(writer);
}
@@@ -414,8 -415,8 +415,8 @@@
* and add docs to it.
*/
public void testCommitOnCloseAbort() throws IOException {
- MockDirectoryWrapper dir = newDirectory();
+ MockDirectoryWrapper dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(10));
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(10));
for (int i = 0; i < 14; i++) {
addDoc(writer);
}
@@@ -449,10 -450,10 +450,10 @@@
hits = searcher.search(new TermQuery(searchTerm), null, 1000).scoreDocs;
assertEquals("saw changes after writer.abort", 14, hits.length);
searcher.close();
-
+
// Now make sure we can re-open the index, add docs,
// and all is good:
- writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer())
+ writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random))
.setOpenMode(OpenMode.APPEND).setMaxBufferedDocs(10));
// On abort, writer in fact may write to the same
@@@ -611,8 -634,8 +634,8 @@@
}
public void testIndexNoDocuments() throws IOException {
- MockDirectoryWrapper dir = newDirectory();
+ MockDirectoryWrapper dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()));
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
writer.commit();
writer.close();
@@@ -633,8 -656,8 +656,8 @@@
}
public void testManyFields() throws IOException {
- MockDirectoryWrapper dir = newDirectory();
+ MockDirectoryWrapper dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(10));
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(10));
for(int j=0;j<100;j++) {
Document doc = new Document();
doc.add(newField("a"+j, "aaa" + j, Field.Store.YES, Field.Index.ANALYZED));
@@@ -663,10 -686,10 +686,10 @@@
}
public void testSmallRAMBuffer() throws IOException {
- MockDirectoryWrapper dir = newDirectory();
+ MockDirectoryWrapper dir = newDirectory();
IndexWriter writer = new IndexWriter(
dir,
- newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()).
+ newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).
setRAMBufferSizeMB(0.000001).
setMergePolicy(newLogMergePolicy(10))
);
@@@ -803,8 -825,8 +826,8 @@@
}
public void testDiverseDocs() throws IOException {
- MockDirectoryWrapper dir = newDirectory();
+ MockDirectoryWrapper dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()).setRAMBufferSizeMB(0.5));
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setRAMBufferSizeMB(0.5));
for(int i=0;i<3;i++) {
// First, docs where every term is unique (heavy on
// Posting instances)
@@@ -850,8 -872,8 +873,8 @@@
}
public void testEnablingNorms() throws IOException {
- MockDirectoryWrapper dir = newDirectory();
+ MockDirectoryWrapper dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(10));
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(10));
// Enable norms for only 1 doc, pre flush
for(int j=0;j<10;j++) {
Document doc = new Document();
@@@ -896,9 -918,9 +919,9 @@@
}
public void testHighFreqTerm() throws IOException {
- MockDirectoryWrapper dir = newDirectory();
+ MockDirectoryWrapper dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()).setRAMBufferSizeMB(0.01));
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)).setRAMBufferSizeMB(0.01));
// Massive doc that has 128 K a's
StringBuilder b = new StringBuilder(1024*1024);
for(int i=0;i<4096;i++) {
@@@ -946,10 -968,10 +969,10 @@@
return myLockFactory.makeLock(name);
}
}
-
+
Directory dir = new MyRAMDirectory(new RAMDirectory());
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()));
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)));
for (int i = 0; i < 100; i++) {
addDoc(writer);
}
@@@ -1066,8 -1088,8 +1089,8 @@@
public void testBadSegment() throws IOException {
Directory dir = newDirectory();
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()));
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)));
-
+
Document document = new Document();
document.add(newField("tvtest", "", Store.NO, Index.ANALYZED, TermVector.YES));
iw.addDocument(document);
@@@ -1352,8 -1375,8 +1376,8 @@@
// are required
public void testExpungeDeletes() throws IOException {
Directory dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer())
+ TEST_VERSION_CURRENT, new MockAnalyzer(random))
.setMaxBufferedDocs(2).setRAMBufferSizeMB(
IndexWriterConfig.DISABLE_AUTO_FLUSH));
writer.setInfoStream(VERBOSE ? System.out : null);
@@@ -1993,8 -2016,8 +2017,8 @@@
// commit(Map) never called for this index
assertEquals(0, r.getCommitUserData().size());
r.close();
-
+
- w = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(2));
+ w = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(2));
for(int j=0;j<17;j++)
addDoc(w);
Map<String,String> data = new HashMap<String,String>();
@@@ -2021,9 -2044,9 +2045,9 @@@
// LUCENE-2529
public void testPositionIncrementGapEmptyField() throws Exception {
Directory dir = newDirectory();
- MockAnalyzer analyzer = new MockAnalyzer();
+ MockAnalyzer analyzer = new MockAnalyzer(random);
analyzer.setPositionIncrementGap( 100 );
- IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(
TEST_VERSION_CURRENT, analyzer));
Document doc = new Document();
Field f = newField("field", "", Field.Store.NO,
@@@ -2118,8 -2141,8 +2142,8 @@@
if (w != null) {
w.close();
}
- IndexWriterConfig conf = newIndexWriterConfig(
+ IndexWriterConfig conf = newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(2);
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(2);
w = new IndexWriter(dir, conf);
Document doc = new Document();
@@@ -2336,8 -2359,8 +2360,8 @@@
public void testNoDocsIndex() throws Throwable {
Directory dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()));
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)));
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
writer.setInfoStream(new PrintStream(bos));
writer.addDocument(new Document());
@@@ -2354,8 -2377,8 +2378,8 @@@
final int NUM_THREADS = 5;
final double RUN_SEC = 0.5;
final Directory dir = newDirectory();
- final RandomIndexWriter w = new RandomIndexWriter(random, dir, newIndexWriterConfig(
+ final RandomIndexWriter w = new RandomIndexWriter(random, dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()).setMergePolicy(newLogMergePolicy()));
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
_TestUtil.reduceOpenFiles(w.w);
w.commit();
final AtomicBoolean failed = new AtomicBoolean();
@@@ -2625,10 -2648,10 +2649,10 @@@
// in case a deletion policy which holds onto commits is used.
Directory dir = newDirectory();
SnapshotDeletionPolicy sdp = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer())
+ TEST_VERSION_CURRENT, new MockAnalyzer(random))
.setIndexDeletionPolicy(sdp));
-
+
// First commit
Document doc = new Document();
doc.add(newField("c", "val", Store.YES, Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS));
@@@ -2723,20 -2753,20 +2754,20 @@@
public void testEmptyFSDirWithNoLock() throws Exception {
// Tests that if FSDir is opened w/ a NoLockFactory (or SingleInstanceLF),
- // then IndexWriter ctor succeeds. Previously (LUCENE-2386) it failed
+ // then IndexWriter ctor succeeds. Previously (LUCENE-2386) it failed
// when listAll() was called in IndexFileDeleter.
Directory dir = newFSDirectory(new File(TEMP_DIR, "emptyFSDirNoLock"), NoLockFactory.getNoLockFactory());
- new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer())).close();
+ new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random))).close();
dir.close();
}
public void testEmptyDirRollback() throws Exception {
// Tests that if IW is created over an empty Directory, some documents are
- // indexed, flushed (but not committed) and then IW rolls back, then no
+ // indexed, flushed (but not committed) and then IW rolls back, then no
// files are left in the Directory.
Directory dir = newDirectory();
- IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer())
+ TEST_VERSION_CURRENT, new MockAnalyzer(random))
.setMaxBufferedDocs(2).setMergePolicy(newLogMergePolicy()));
String[] files = dir.listAll();
@@@ -2780,17 -2810,17 +2811,17 @@@
public void testNoSegmentFile() throws IOException {
Directory dir = newDirectory();
dir.setLockFactory(NoLockFactory.getNoLockFactory());
- IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(2));
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(2));
-
+
Document doc = new Document();
doc.add(newField("c", "val", Store.YES, Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS));
w.addDocument(doc);
w.addDocument(doc);
- IndexWriter w2 = new IndexWriter(dir, newIndexWriterConfig(
+ IndexWriter w2 = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(2)
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(2)
.setOpenMode(OpenMode.CREATE));
-
+
w2.close();
// If we don't do that, the test fails on Windows
w.rollback();
@@@ -2826,10 -2856,10 +2857,10 @@@
assertNotNull(commit);
- w = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()).setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE).setIndexCommit(commit));
+ w = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE).setIndexCommit(commit));
assertEquals(1, w.numDocs());
-
+
// commit IndexWriter to "third"
w.addDocument(doc);
commitData.put("tag", "third");
diff --cc lucene/src/test/org/apache/lucene/index/TestIndexWriterConfig.java
index b9096a87e47,26f800b0272..6dc45249d88
--- a/lucene/src/test/org/apache/lucene/index/TestIndexWriterConfig.java
+++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriterConfig.java
@@@ -135,10 -126,10 +135,10 @@@ public class TestIndexWriterConfig exte
assertEquals(8, IndexWriterConfig.DEFAULT_MAX_THREAD_STATES);
assertEquals(IndexReader.DEFAULT_TERMS_INDEX_DIVISOR, IndexWriterConfig.DEFAULT_READER_TERMS_INDEX_DIVISOR);
}
-
+
@Test
public void testToString() throws Exception {
- String str = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()).toString();
+ String str = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).toString();
for (Field f : IndexWriterConfig.class.getDeclaredFields()) {
int modifiers = f.getModifiers();
if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
@@@ -152,23 -143,23 +152,23 @@@
assertTrue(f.getName() + " not found in toString", str.indexOf(f.getName()) != -1);
}
}
-
+
@Test
public void testClone() throws Exception {
- IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer());
+ IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
IndexWriterConfig clone = (IndexWriterConfig) conf.clone();
-
+
// Clone is shallow since not all parameters are cloneable.
assertTrue(conf.getIndexDeletionPolicy() == clone.getIndexDeletionPolicy());
-
+
conf.setMergeScheduler(new SerialMergeScheduler());
assertEquals(ConcurrentMergeScheduler.class, clone.getMergeScheduler().getClass());
}
@Test
public void testInvalidValues() throws Exception {
- IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer());
+ IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
-
+
// Test IndexDeletionPolicy
assertEquals(KeepOnlyLastCommitDeletionPolicy.class, conf.getIndexDeletionPolicy().getClass());
conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(null));
diff --cc lucene/src/test/org/apache/lucene/index/TestIndexWriterDelete.java
index a71196ba883,cf51ffbf0c6..4de8d97789f
--- a/lucene/src/test/org/apache/lucene/index/TestIndexWriterDelete.java
+++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriterDelete.java
@@@ -295,12 -295,12 +295,12 @@@ public class TestIndexWriterDelete exte
public void testDeleteAllRollback() throws IOException {
Directory dir = newDirectory();
IndexWriter modifier = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer(MockTokenizer.WHITESPACE, false)).setMaxBufferedDocs(2)
+ TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false)).setMaxBufferedDocs(2)
.setMaxBufferedDeleteTerms(2));
-
+
int id = 0;
int value = 100;
-
+
for (int i = 0; i < 7; i++) {
addDoc(modifier, ++id, value);
}
@@@ -332,12 -332,12 +332,12 @@@
public void testDeleteAllNRT() throws IOException {
Directory dir = newDirectory();
IndexWriter modifier = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer(MockTokenizer.WHITESPACE, false)).setMaxBufferedDocs(2)
+ TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false)).setMaxBufferedDocs(2)
.setMaxBufferedDeleteTerms(2));
-
+
int id = 0;
int value = 100;
-
+
for (int i = 0; i < 7; i++) {
addDoc(modifier, ++id, value);
}
@@@ -536,10 -536,10 +536,12 @@@
fail(testName + " hit IOException after disk space was freed up");
}
}
--
++ final double randomIOExceptionRate = dir.getRandomIOExceptionRate();
++ dir.setRandomIOExceptionRate(0.0);
if (!success) {
// Must force the close else the writer can have
// open files which cause exc in MockRAMDir.close
++
modifier.rollback();
}
@@@ -549,6 -549,6 +551,8 @@@
_TestUtil.checkIndex(dir);
TestIndexWriter.assertNoUnreferencedFiles(dir, "after writer.close");
}
++ dir.setRandomIOExceptionRate(randomIOExceptionRate);
++
// Finally, verify index is not corrupt, and, if
// we succeeded, we see all docs changed, and if
diff --cc lucene/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
index b20a195446d,7ac0a23545e..4769319accf
--- a/lucene/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
+++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
@@@ -870,10 -869,10 +870,10 @@@ public class TestIndexWriterExceptions
dir.close();
}
}
-
+
public void testOptimizeExceptions() throws IOException {
Directory startDir = newDirectory();
- IndexWriterConfig conf = newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(2).setMergePolicy(newLogMergePolicy());
+ IndexWriterConfig conf = newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(2).setMergePolicy(newLogMergePolicy());
((LogMergePolicy) conf.getMergePolicy()).setMergeFactor(100);
IndexWriter w = new IndexWriter(startDir, conf);
for(int i=0;i<27;i++)
diff --cc lucene/src/test/org/apache/lucene/index/TestLazyBug.java
index 3b5a0ce67a3,e7cc9e6cf4c..da5100aaa3b
--- a/lucene/src/test/org/apache/lucene/index/TestLazyBug.java
+++ b/lucene/src/test/org/apache/lucene/index/TestLazyBug.java
@@@ -63,9 -63,10 +63,9 @@@ public class TestLazyBug extends Lucene
Directory dir = newDirectory();
try {
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()).setMergePolicy(newLogMergePolicy()));
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
LogMergePolicy lmp = (LogMergePolicy) writer.getConfig().getMergePolicy();
lmp.setUseCompoundFile(false);
-
for (int d = 1; d <= NUM_DOCS; d++) {
Document doc = new Document();
for (int f = 1; f <= NUM_FIELDS; f++ ) {
diff --cc lucene/src/test/org/apache/lucene/index/TestLazyProxSkipping.java
index 26880f3b73e,331430e435c..1bf6b37ec7c
--- a/lucene/src/test/org/apache/lucene/index/TestLazyProxSkipping.java
+++ b/lucene/src/test/org/apache/lucene/index/TestLazyProxSkipping.java
@@@ -61,16 -64,23 +64,23 @@@ public class TestLazyProxSkipping exten
}
return ii;
}
-
+
}
-
+
private void createIndex(int numHits) throws IOException {
int numDocs = 500;
-
+
+ final Analyzer analyzer = new Analyzer() {
+ @Override
+ public TokenStream tokenStream(String fieldName, Reader reader) {
+ return new MockTokenizer(reader, MockTokenizer.WHITESPACE, true);
+ }
+ };
Directory directory = new SeekCountingDirectory(new RAMDirectory());
+ // note: test explicitly disables payloads
IndexWriter writer = new IndexWriter(
directory,
- newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(MockTokenizer.WHITESPACE, true, false)).
+ newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer).
setMaxBufferedDocs(10).
setMergePolicy(newLogMergePolicy(false))
);
@@@ -130,10 -140,10 +140,10 @@@
performTest(10);
searcher.close();
}
-
+
public void testSeek() throws IOException {
Directory directory = newDirectory();
- IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()));
+ IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
for (int i = 0; i < 10; i++) {
Document doc = new Document();
doc.add(newField(this.field, "a b", Field.Store.YES, Field.Index.ANALYZED));
diff --cc lucene/src/test/org/apache/lucene/index/TestRollingUpdates.java
index 1cdceff95d0,e3624805754..af9a1dea57e
--- a/lucene/src/test/org/apache/lucene/index/TestRollingUpdates.java
+++ b/lucene/src/test/org/apache/lucene/index/TestRollingUpdates.java
@@@ -73,73 -72,4 +73,73 @@@ public class TestRollingUpdates extend
dir.close();
}
+
+
+ public void testUpdateSameDoc() throws Exception {
+ final Directory dir = newDirectory();
+
+ final LineFileDocs docs = new LineFileDocs(random);
+ for (int r = 0; r < 3; r++) {
+ final IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()).setMergePolicy(
++ TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(
+ newLogMergePolicy()).setMaxBufferedDocs(2));
+
+ final int SIZE = 200 * RANDOM_MULTIPLIER;
+ final int numUpdates = (int) (SIZE * (2 + random.nextDouble()));
+ int numThreads = 3 + random.nextInt(Runtime.getRuntime().availableProcessors());
+ IndexingThread[] threads = new IndexingThread[numThreads];
+ for (int i = 0; i < numThreads; i++) {
+ threads[i] = new IndexingThread(docs, w, numUpdates);
+ threads[i].start();
+ }
+
+ for (int i = 0; i < numThreads; i++) {
+ threads[i].join();
+ }
+
+ w.close();
+ }
+ IndexReader open = IndexReader.open(dir);
+ assertEquals(1, open.numDocs());
+ open.close();
+ docs.close();
+ dir.close();
+ }
+
+ static class IndexingThread extends Thread {
+ final LineFileDocs docs;
+ final IndexWriter writer;
+ final int num;
+
+ public IndexingThread(LineFileDocs docs, IndexWriter writer, int num) {
+ super();
+ this.docs = docs;
+ this.writer = writer;
+ this.num = num;
+ }
+
+ public void run() {
+ try {
+// IndexReader open = IndexReader.open(writer, true);
+ for (int i = 0; i < num; i++) {
+ Document doc = new Document();// docs.nextDoc();
+ doc.add(newField("id", "test", Index.NOT_ANALYZED));
+ writer.updateDocument(new Term("id", "test"), doc);
+// if (random.nextInt(10) == 0) {
+// IndexReader reader = open.reopen();
+// if (reader != open) {
+// open.close();
+// open = reader;
+// }
+// assertEquals("iter: " + i + " numDocs: "+ open.numDocs() + " del: " + open.numDeletedDocs() + " max: " + open.maxDoc(), 1, open.numDocs());
+//
+// }
+ }
+// open.close();
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ }
+ }
}
diff --cc lucene/src/test/org/apache/lucene/index/TestStressIndexing2.java
index 02d844d0654,0762d5a8e4f..a0fbe6d1f55
--- a/lucene/src/test/org/apache/lucene/index/TestStressIndexing2.java
+++ b/lucene/src/test/org/apache/lucene/index/TestStressIndexing2.java
@@@ -200,8 -200,8 +200,8 @@@ public class TestStressIndexing2 extend
boolean doReaderPooling) throws IOException, InterruptedException {
Map<String,Document> docs = new HashMap<String,Document>();
IndexWriter w = new MockIndexWriter(dir, newIndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()).setOpenMode(OpenMode.CREATE)
+ TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE)
- .setRAMBufferSizeMB(0.1).setMaxBufferedDocs(maxBufferedDocs).setMaxThreadStates(maxThreadStates)
+ .setRAMBufferSizeMB(0.1).setMaxBufferedDocs(maxBufferedDocs).setIndexerThreadPool(new ThreadAffinityDocumentsWriterThreadPool(maxThreadStates))
.setReaderPooling(doReaderPooling).setMergePolicy(newLogMergePolicy()));
w.setInfoStream(VERBOSE ? System.out : null);
LogMergePolicy lmp = (LogMergePolicy) w.getConfig().getMergePolicy();