diff --git a/src/java/org/apache/lucene/index/IndexWriter.java b/src/java/org/apache/lucene/index/IndexWriter.java index e1f40698f01..57f30abfac4 100644 --- a/src/java/org/apache/lucene/index/IndexWriter.java +++ b/src/java/org/apache/lucene/index/IndexWriter.java @@ -734,15 +734,14 @@ public class IndexWriter { /** Merges all RAM-resident segments, then may merge segments. */ private final void flushRamSegments() throws IOException { if (ramSegmentInfos.size() > 0) { - if (mergeSegments(ramSegmentInfos, 0, ramSegmentInfos.size()) > 0) { - maybeMergeSegments(); - } + mergeSegments(ramSegmentInfos, 0, ramSegmentInfos.size()); + maybeMergeSegments(); } } /** Incremental segment merger. */ private final void maybeMergeSegments() throws IOException { - long lowerBound = 0; + long lowerBound = -1; long upperBound = minMergeDocs; while (upperBound * mergeFactor <= maxMergeDocs) { @@ -782,7 +781,7 @@ public class IndexWriter { // continue to merge the rest of the worthy segments on this level minSegment++; exceedsUpperLimit = true; - } else if (docCount > 0) { + } else { // if the merged segment does not exceed upperBound, consider // this segment for further merges on this same level numSegments++; @@ -810,7 +809,6 @@ public class IndexWriter { if (infoStream != null) infoStream.print("merging segments"); SegmentMerger merger = new SegmentMerger(this, mergedName); - boolean fromRAM = false; final Vector segmentsToDelete = new Vector(); for (int i = minSegment; i < end; i++) { SegmentInfo si = sourceSegments.info(i); @@ -821,9 +819,6 @@ public class IndexWriter { if ((reader.directory() == this.directory) || // if we own the directory (reader.directory() == this.ramDirectory)) segmentsToDelete.addElement(reader); // queue segment for deletion - if (!fromRAM && (reader.directory() == this.ramDirectory)) { - fromRAM = true; - } } int mergedDocCount = merger.merge(); @@ -834,17 +829,13 @@ public class IndexWriter { SegmentInfo newSegment = new SegmentInfo(mergedName, mergedDocCount, directory); - if (fromRAM) { + if (sourceSegments == ramSegmentInfos) { sourceSegments.removeAllElements(); - if (mergedDocCount > 0) - segmentInfos.addElement(newSegment); + segmentInfos.addElement(newSegment); } else { for (int i = end-1; i > minSegment; i--) // remove old infos & add new sourceSegments.remove(i); - if (mergedDocCount > 0) - segmentInfos.set(minSegment, newSegment); - else - sourceSegments.remove(minSegment); + segmentInfos.set(minSegment, newSegment); } // close readers before we attempt to delete now-obsolete segments diff --git a/src/test/org/apache/lucene/index/TestIndexWriterMergePolicy.java b/src/test/org/apache/lucene/index/TestIndexWriterMergePolicy.java index 5ff719e8f42..5d73a16d8ed 100755 --- a/src/test/org/apache/lucene/index/TestIndexWriterMergePolicy.java +++ b/src/test/org/apache/lucene/index/TestIndexWriterMergePolicy.java @@ -157,9 +157,9 @@ public class TestIndexWriterMergePolicy extends TestCase { writer.setMaxBufferedDocs(10); writer.setMergeFactor(5); + // merge factor is changed, so check invariants after all adds for (int i = 0; i < 10; i++) { addDoc(writer); - checkInvariants(writer); } checkInvariants(writer); assertEquals(10, writer.docCount()); @@ -173,7 +173,7 @@ public class TestIndexWriterMergePolicy extends TestCase { writer.addDocument(doc); } - private void checkInvariants(IndexWriter writer) { + private void checkInvariants(IndexWriter writer) throws IOException { int maxBufferedDocs = writer.getMaxBufferedDocs(); int mergeFactor = writer.getMergeFactor(); int maxMergeDocs = writer.getMaxMergeDocs(); @@ -188,10 +188,10 @@ public class TestIndexWriterMergePolicy extends TestCase { int segmentCount = writer.getSegmentCount(); for (int i = segmentCount - 1; i >= 0; i--) { int docCount = writer.getDocCount(i); - assertTrue(docCount > lowerBound); + assertTrue(docCount > lowerBound || docCount == 0); if (docCount <= upperBound) { - segmentCount++; + numSegments++; } else { if (upperBound * mergeFactor <= maxMergeDocs) { assertTrue(numSegments < mergeFactor); @@ -199,12 +199,21 @@ public class TestIndexWriterMergePolicy extends TestCase { lowerBound = upperBound; upperBound *= mergeFactor; - segmentCount = 1; + numSegments = 1; } } if (upperBound * mergeFactor <= maxMergeDocs) { assertTrue(numSegments < mergeFactor); } + + String[] files = writer.getDirectory().list(); + int segmentCfsCount = 0; + for (int i = 0; i < files.length; i++) { + if (files[i].endsWith(".cfs")) { + segmentCfsCount++; + } + } + assertEquals(segmentCount, segmentCfsCount); } private void printSegmentDocCounts(IndexWriter writer) {