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;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Random;
27 import org.apache.lucene.util._TestUtil;
29 public class MockRandomMergePolicy extends MergePolicy {
30 private final Random random;
32 public MockRandomMergePolicy(Random random) {
33 // fork a private random, since we are called
34 // unpredictably from threads:
35 this.random = new Random(random.nextLong());
39 public MergeSpecification findMerges(SegmentInfos segmentInfos) {
40 MergeSpecification mergeSpec = null;
41 //System.out.println("MRMP: findMerges sis=" + segmentInfos);
43 if (segmentInfos.size() > 1 && random.nextInt(5) == 3) {
45 List<SegmentInfo> segments = new ArrayList<SegmentInfo>(segmentInfos.asList());
46 Collections.shuffle(segments, random);
48 // TODO: sometimes make more than 1 merge?
49 mergeSpec = new MergeSpecification();
50 final int segsToMerge = _TestUtil.nextInt(random, 1, segmentInfos.size());
51 mergeSpec.add(new OneMerge(segments.subList(0, segsToMerge)));
57 public MergeSpecification findForcedMerges(
58 SegmentInfos segmentInfos, int maxSegmentCount, Map<SegmentInfo,Boolean> segmentsToOptimize)
59 throws CorruptIndexException, IOException {
60 return findMergesForOptimize(segmentInfos, maxSegmentCount, segmentsToOptimize);
64 public MergeSpecification findMergesForOptimize(
65 SegmentInfos segmentInfos, int maxSegmentCount, Map<SegmentInfo,Boolean> segmentsToOptimize)
66 throws CorruptIndexException, IOException {
68 final List<SegmentInfo> eligibleSegments = new ArrayList<SegmentInfo>();
69 for(SegmentInfo info : segmentInfos) {
70 if (segmentsToOptimize.containsKey(info)) {
71 eligibleSegments.add(info);
75 //System.out.println("MRMP: findMergesForOptimize sis=" + segmentInfos + " eligible=" + eligibleSegments);
76 MergeSpecification mergeSpec = null;
77 if (eligibleSegments.size() > 1 || (eligibleSegments.size() == 1 && eligibleSegments.get(0).hasDeletions())) {
78 mergeSpec = new MergeSpecification();
79 // Already shuffled having come out of a set but
80 // shuffle again for good measure:
81 Collections.shuffle(eligibleSegments, random);
83 while(upto < eligibleSegments.size()) {
84 int max = Math.min(10, eligibleSegments.size()-upto);
85 int inc = max <= 2 ? max : _TestUtil.nextInt(random, 2, max);
86 mergeSpec.add(new OneMerge(eligibleSegments.subList(upto, upto+inc)));
91 if (mergeSpec != null) {
92 for(OneMerge merge : mergeSpec.merges) {
93 for(SegmentInfo info : merge.segments) {
94 assert segmentsToOptimize.containsKey(info);
101 public MergeSpecification findForcedDeletesMerges(
102 SegmentInfos segmentInfos, int maxSegmentCount, Map<SegmentInfo,Boolean> segmentsToOptimize)
103 throws CorruptIndexException, IOException {
104 return findMergesToExpungeDeletes(segmentInfos);
108 public MergeSpecification findMergesToExpungeDeletes(
109 SegmentInfos segmentInfos)
110 throws CorruptIndexException, IOException {
111 return findMerges(segmentInfos);
115 public void close() {
119 public boolean useCompoundFile(SegmentInfos infos, SegmentInfo mergedInfo) throws IOException {
120 // 80% of the time we create CFS:
121 return random.nextInt(5) != 1;