Apache Lucene open-source search software
LUCENE-1634: add calibrateSizeByDeletes to LogMergePolicy
Michael McCandless571a0587673e
diff --git a/CHANGES.txt b/CHANGES.txtindex e73c1c93991..df1de6052d0 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -307,6 +307,10 @@ Bug fixes24. LUCENE-1494: Added FieldMaskingSpanQuery which can be used tocross-correlate Spans from different fields.(Paul Cowan and Chris Hostetter)++25. LUCENE-1634: Add calibrateSizeByDeletes to LogMergePolicy, to take+ deletions into account when considering merges. (Yasuhiro Matsuda+ via Mike McCandless)Optimizationsdiff --git a/src/java/org/apache/lucene/index/LogByteSizeMergePolicy.java b/src/java/org/apache/lucene/index/LogByteSizeMergePolicy.javaindex c2ba59d2f16..1322eafe2ce 100644--- a/src/java/org/apache/lucene/index/LogByteSizeMergePolicy.java+++ b/src/java/org/apache/lucene/index/LogByteSizeMergePolicy.java@@ -36,7 +36,7 @@ public class LogByteSizeMergePolicy extends LogMergePolicy {maxMergeSize = (long) (DEFAULT_MAX_MERGE_MB*1024*1024);}protected long size(SegmentInfo info) throws IOException {- return info.sizeInBytes();+ return sizeBytes(info);}/** <p>Determines the largest segment (measured by totaldiff --git a/src/java/org/apache/lucene/index/LogDocMergePolicy.java b/src/java/org/apache/lucene/index/LogDocMergePolicy.javaindex 3baef827754..7ea03d8d56f 100644--- a/src/java/org/apache/lucene/index/LogDocMergePolicy.java+++ b/src/java/org/apache/lucene/index/LogDocMergePolicy.java@@ -1,5 +1,7 @@package org.apache.lucene.index;+import java.io.IOException;+/*** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements. See the NOTICE file distributed with@@ -34,8 +36,8 @@ public class LogDocMergePolicy extends LogMergePolicy {// it to Long.MAX_VALUE to disable itmaxMergeSize = Long.MAX_VALUE;}- protected long size(SegmentInfo info) {- return info.docCount;+ protected long size(SegmentInfo info) throws IOException {+ return sizeDocs(info);}/** Sets the minimum size for the lowest level segments.diff --git a/src/java/org/apache/lucene/index/LogMergePolicy.java b/src/java/org/apache/lucene/index/LogMergePolicy.javaindex ca321f64191..d9de85ebcee 100644--- a/src/java/org/apache/lucene/index/LogMergePolicy.java+++ b/src/java/org/apache/lucene/index/LogMergePolicy.java@@ -20,8 +20,6 @@ package org.apache.lucene.index;import java.io.IOException;import java.util.Set;-import org.apache.lucene.store.Directory;-/** <p>This class implements a {@link MergePolicy} that tries* to merge segments into levels of exponentially* increasing size, where each level has < mergeFactor@@ -59,6 +57,9 @@ public abstract class LogMergePolicy extends MergePolicy {long maxMergeSize;int maxMergeDocs = DEFAULT_MAX_MERGE_DOCS;+ /* TODO 3.0: change this default to true */+ protected boolean calibrateSizeByDeletes = false;+private boolean useCompoundFile = true;private boolean useCompoundDocStore = true;private IndexWriter writer;@@ -132,10 +133,40 @@ public abstract class LogMergePolicy extends MergePolicy {return useCompoundDocStore;}+ /** Sets whether the segment size should be calibrated by+ * the number of deletes when choosing segments for merge. */+ public void setCalibrateSizeByDeletes(boolean calibrateSizeByDeletes) {+ this.calibrateSizeByDeletes = calibrateSizeByDeletes;+ }++ /** Returns true if the segment size should be calibrated+ * by the number of deletes when choosing segments for merge. */+ public boolean getCalibrateSizeByDeletes() {+ return calibrateSizeByDeletes;+ }+public void close() {}abstract protected long size(SegmentInfo info) throws IOException;+ protected long sizeDocs(SegmentInfo info) throws IOException {+ if (calibrateSizeByDeletes) {+ return (info.docCount - (long)info.getDelCount());+ } else {+ return info.docCount;+ }+ }++ protected long sizeBytes(SegmentInfo info) throws IOException {+ long byteSize = info.sizeInBytes();+ if (calibrateSizeByDeletes) {+ float delRatio = (info.docCount <= 0 ? 0.0f : ((float)info.getDelCount() / (float)info.docCount));+ return (info.docCount <= 0 ? byteSize : (long)((float)byteSize * (1.0f - delRatio)));+ } else {+ return byteSize;+ }+ }+private boolean isOptimized(SegmentInfos infos, IndexWriter writer, int maxNumSegments, Set segmentsToOptimize) throws IOException {final int numSegments = infos.size();int numToOptimize = 0;@@ -321,8 +352,6 @@ public abstract class LogMergePolicy extends MergePolicy {float[] levels = new float[numSegments];final float norm = (float) Math.log(mergeFactor);- final Directory directory = writer.getDirectory();-for(int i=0;i<numSegments;i++) {final SegmentInfo info = infos.info(i);long size = size(info);@@ -390,7 +419,7 @@ public abstract class LogMergePolicy extends MergePolicy {boolean anyTooLarge = false;for(int i=start;i<end;i++) {final SegmentInfo info = infos.info(i);- anyTooLarge |= (size(info) >= maxMergeSize || info.docCount >= maxMergeDocs);+ anyTooLarge |= (size(info) >= maxMergeSize || sizeDocs(info) >= maxMergeDocs);}if (!anyTooLarge) {