Skip to content

Apache Lucene open-source search software

LUCENE-5941: IndexWriter.forceMerge documentation improvements

Shai Erera340a9dc03caa
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
index 4638322ce06..8479391f67f 100644
--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
@@ -1554,13 +1554,15 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable {
* should only call this if the index is static (will no
* longer be changed).</p>
*
- * <p>Note that this requires up to 2X the index size free
- * space in your Directory (3X if you're using compound
- * file format). For example, if your index size is 10 MB
- * then you need up to 20 MB free for this to complete (30
- * MB if you're using compound file format). Also,
- * it's best to call {@link #commit()} afterwards,
- * to allow IndexWriter to free up disk space.</p>
+ * <p>Note that this requires free space that is proportional
+ * to the size of the index in your Directory (2X if you're
+ * using compound file format). For example, if your index
+ * size is 10 MB then you need an additional 10 MB free for
+ * this to complete (20 MB if you're using compound file
+ * format). This is also affected by the {@link Codec} that
+ * is used to execute the merge, and may result in even a
+ * bigger index. Also, it's best to call {@link #commit()}
+ * afterwards, to allow IndexWriter to free up disk space.</p>
*
* <p>If some but not all readers re-open while merging
* is underway, this will cause > 2X temporary
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterForceMerge.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterForceMerge.java
index c141cc00b78..30997fe40de 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterForceMerge.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterForceMerge.java
@@ -121,10 +121,12 @@ public class TestIndexWriterForceMerge extends LuceneTestCase {
*/
public void testForceMergeTempSpaceUsage() throws IOException {
- MockDirectoryWrapper dir = newMockDirectory();
+ final MockDirectoryWrapper dir = newMockDirectory();
+ dir.setEnableVirusScanner(false);
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random()))
.setMaxBufferedDocs(10)
.setMergePolicy(newLogMergePolicy()));
+
if (VERBOSE) {
System.out.println("TEST: config1=" + writer.getConfig());
}
@@ -138,17 +140,16 @@ public class TestIndexWriterForceMerge extends LuceneTestCase {
TestIndexWriter.addDocWithIndex(writer, 500);
writer.close();
- if (VERBOSE) {
- System.out.println("TEST: start disk usage");
- }
long startDiskUsage = 0;
- String[] files = dir.listAll();
- for(int i=0;i<files.length;i++) {
- startDiskUsage += dir.fileLength(files[i]);
+ for (String f : dir.listAll()) {
+ startDiskUsage += dir.fileLength(f);
if (VERBOSE) {
- System.out.println(files[i] + ": " + dir.fileLength(files[i]));
+ System.out.println(f + ": " + dir.fileLength(f));
}
}
+ if (VERBOSE) {
+ System.out.println("TEST: start disk usage = " + startDiskUsage);
+ }
dir.resetMaxUsedSizeInBytes();
dir.setTrackDiskUsage(true);
@@ -156,11 +157,36 @@ public class TestIndexWriterForceMerge extends LuceneTestCase {
writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random()))
.setOpenMode(OpenMode.APPEND)
.setMergePolicy(newLogMergePolicy()));
+
+ if (VERBOSE) {
+ System.out.println("TEST: config2=" + writer.getConfig());
+ }
+
writer.forceMerge(1);
writer.close();
+
+ long finalDiskUsage = 0;
+ for (String f : dir.listAll()) {
+ finalDiskUsage += dir.fileLength(f);
+ if (VERBOSE) {
+ System.out.println(f + ": " + dir.fileLength(f));
+ }
+ }
+ if (VERBOSE) {
+ System.out.println("TEST: final disk usage = " + finalDiskUsage);
+ }
+
+ // The result of the merged index is often smaller, but sometimes it could
+ // be bigger (compression slightly changes, Codec changes etc.). Therefore
+ // we compare the temp space used to the max of the initial and final index
+ // size
+ long maxStartFinalDiskUsage = Math.max(startDiskUsage, finalDiskUsage);
long maxDiskUsage = dir.getMaxUsedSizeInBytes();
- assertTrue("forceMerge used too much temporary space: starting usage was " + startDiskUsage + " bytes; max temp usage was " + maxDiskUsage + " but should have been " + (4*startDiskUsage) + " (= 4X starting usage)",
- maxDiskUsage <= 4*startDiskUsage);
+ assertTrue("forceMerge used too much temporary space: starting usage was "
+ + startDiskUsage + " bytes; final usage was " + finalDiskUsage
+ + " bytes; max temp usage was " + maxDiskUsage
+ + " but should have been " + (3 * maxStartFinalDiskUsage)
+ + " (= 3X starting usage)", maxDiskUsage <= 3 * maxStartFinalDiskUsage);
dir.close();
}