1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
22 /** This is a {@link LogMergePolicy} that measures size of a
23 * segment as the total byte size of the segment's files. */
24 public class LogByteSizeMergePolicy extends LogMergePolicy {
26 /** Default minimum segment size. @see setMinMergeMB */
27 public static final double DEFAULT_MIN_MERGE_MB = 1.6;
29 /** Default maximum segment size. A segment of this size
30 * or larger will never be merged. @see setMaxMergeMB */
31 public static final double DEFAULT_MAX_MERGE_MB = 2048;
33 /** Default maximum segment size. A segment of this size
34 * or larger will never be merged during forceMerge. @see setMaxMergeMBForForceMerge */
35 public static final double DEFAULT_MAX_MERGE_MB_FOR_FORCED_MERGE = Long.MAX_VALUE;
37 public LogByteSizeMergePolicy() {
38 minMergeSize = (long) (DEFAULT_MIN_MERGE_MB*1024*1024);
39 maxMergeSize = (long) (DEFAULT_MAX_MERGE_MB*1024*1024);
40 maxMergeSizeForForcedMerge = (long) (DEFAULT_MAX_MERGE_MB_FOR_FORCED_MERGE*1024*1024);
44 protected long size(SegmentInfo info) throws IOException {
45 return sizeBytes(info);
48 /** <p>Determines the largest segment (measured by total
49 * byte size of the segment's files, in MB) that may be
50 * merged with other segments. Small values (e.g., less
51 * than 50 MB) are best for interactive indexing, as this
52 * limits the length of pauses while indexing to a few
53 * seconds. Larger values are best for batched indexing
54 * and speedier searches.</p>
56 * <p>Note that {@link #setMaxMergeDocs} is also
57 * used to check whether a segment is too large for
58 * merging (it's either or).</p>*/
59 public void setMaxMergeMB(double mb) {
60 maxMergeSize = (long) (mb*1024*1024);
63 /** Returns the largest segment (measured by total byte
64 * size of the segment's files, in MB) that may be merged
65 * with other segments.
66 * @see #setMaxMergeMB */
67 public double getMaxMergeMB() {
68 return ((double) maxMergeSize)/1024/1024;
71 /** @deprecated Renamed to {@link
72 * #setMaxMergeMBForForcedMerge} */
74 public void setMaxMergeMBForOptimize(double mb) {
75 setMaxMergeMBForForcedMerge(mb);
78 /** <p>Determines the largest segment (measured by total
79 * byte size of the segment's files, in MB) that may be
80 * merged with other segments during forceMerge. Setting
81 * it low will leave the index with more than 1 segment,
82 * even if {@link IndexWriter#forceMerge} is called.*/
83 public void setMaxMergeMBForForcedMerge(double mb) {
84 maxMergeSizeForForcedMerge = (long) (mb*1024*1024);
87 /** @deprecated Renamed to {@link
88 * #getMaxMergeMBForForcedMerge} */
90 public double getMaxMergeMBForOptimize() {
91 return getMaxMergeMBForForcedMerge();
94 /** Returns the largest segment (measured by total byte
95 * size of the segment's files, in MB) that may be merged
96 * with other segments during forceMerge.
97 * @see #setMaxMergeMBForForcedMerge */
98 public double getMaxMergeMBForForcedMerge() {
99 return ((double) maxMergeSizeForForcedMerge)/1024/1024;
102 /** Sets the minimum size for the lowest level segments.
103 * Any segments below this size are considered to be on
104 * the same level (even if they vary drastically in size)
105 * and will be merged whenever there are mergeFactor of
106 * them. This effectively truncates the "long tail" of
107 * small segments that would otherwise be created into a
108 * single level. If you set this too large, it could
109 * greatly increase the merging cost during indexing (if
110 * you flush many small segments). */
111 public void setMinMergeMB(double mb) {
112 minMergeSize = (long) (mb*1024*1024);
115 /** Get the minimum size for a segment to remain
117 * @see #setMinMergeMB **/
118 public double getMinMergeMB() {
119 return ((double) minMergeSize)/1024/1024;