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)));
58 public MergeSpecification findForcedMerges(
59 SegmentInfos segmentInfos, int maxSegmentCount, Map<SegmentInfo,Boolean> segmentsToMerge)
60 throws CorruptIndexException, IOException {
62 final List<SegmentInfo> eligibleSegments = new ArrayList<SegmentInfo>();
63 for(SegmentInfo info : segmentInfos) {
64 if (segmentsToMerge.containsKey(info)) {
65 eligibleSegments.add(info);
69 //System.out.println("MRMP: findMerges sis=" + segmentInfos + " eligible=" + eligibleSegments);
70 MergeSpecification mergeSpec = null;
71 if (eligibleSegments.size() > 1 || (eligibleSegments.size() == 1 && eligibleSegments.get(0).hasDeletions())) {
72 mergeSpec = new MergeSpecification();
73 // Already shuffled having come out of a set but
74 // shuffle again for good measure:
75 Collections.shuffle(eligibleSegments, random);
77 while(upto < eligibleSegments.size()) {
78 int max = Math.min(10, eligibleSegments.size()-upto);
79 int inc = max <= 2 ? max : _TestUtil.nextInt(random, 2, max);
80 mergeSpec.add(new OneMerge(eligibleSegments.subList(upto, upto+inc)));
85 if (mergeSpec != null) {
86 for(OneMerge merge : mergeSpec.merges) {
87 for(SegmentInfo info : merge.segments) {
88 assert segmentsToMerge.containsKey(info);
96 public MergeSpecification findForcedDeletesMerges(
97 SegmentInfos segmentInfos)
98 throws CorruptIndexException, IOException {
99 return findMerges(segmentInfos);
103 public void close() {
107 public boolean useCompoundFile(SegmentInfos infos, SegmentInfo mergedInfo) throws IOException {
108 // 80% of the time we create CFS:
109 return random.nextInt(5) != 1;