pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test-framework / org / apache / lucene / index / MockRandomMergePolicy.java
1 package org.apache.lucene.index;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
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;
25 import java.util.Map;
26
27 import org.apache.lucene.util._TestUtil;
28
29 public class MockRandomMergePolicy extends MergePolicy {
30   private final Random random;
31
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());
36   }
37
38   @Override
39   public MergeSpecification findMerges(SegmentInfos segmentInfos) {
40     MergeSpecification mergeSpec = null;
41     //System.out.println("MRMP: findMerges sis=" + segmentInfos);
42
43     if (segmentInfos.size() > 1 && random.nextInt(5) == 3) {
44       
45       List<SegmentInfo> segments = new ArrayList<SegmentInfo>(segmentInfos.asList());
46       Collections.shuffle(segments, random);
47
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)));
52     }
53
54     return mergeSpec;
55   }
56
57   public MergeSpecification findForcedMerges(
58        SegmentInfos segmentInfos, int maxSegmentCount, Map<SegmentInfo,Boolean> segmentsToOptimize)
59     throws CorruptIndexException, IOException {
60     return findMergesForOptimize(segmentInfos, maxSegmentCount, segmentsToOptimize);
61   }
62     
63   @Override
64   public MergeSpecification findMergesForOptimize(
65        SegmentInfos segmentInfos, int maxSegmentCount, Map<SegmentInfo,Boolean> segmentsToOptimize)
66     throws CorruptIndexException, IOException {
67
68     final List<SegmentInfo> eligibleSegments = new ArrayList<SegmentInfo>();
69     for(SegmentInfo info : segmentInfos) {
70       if (segmentsToOptimize.containsKey(info)) {
71         eligibleSegments.add(info);
72       }
73     }
74
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);
82       int upto = 0;
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)));
87         upto += inc;
88       }
89     }
90
91     if (mergeSpec != null) {
92       for(OneMerge merge : mergeSpec.merges) {
93         for(SegmentInfo info : merge.segments) {
94           assert segmentsToOptimize.containsKey(info);
95         }
96       }
97     }
98     return mergeSpec;
99   }
100
101   public MergeSpecification findForcedDeletesMerges(
102        SegmentInfos segmentInfos, int maxSegmentCount, Map<SegmentInfo,Boolean> segmentsToOptimize)
103     throws CorruptIndexException, IOException {
104     return findMergesToExpungeDeletes(segmentInfos);
105   }
106
107   @Override
108   public MergeSpecification findMergesToExpungeDeletes(
109       SegmentInfos segmentInfos)
110     throws CorruptIndexException, IOException {
111     return findMerges(segmentInfos);
112   }
113
114   @Override
115   public void close() {
116   }
117
118   @Override
119   public boolean useCompoundFile(SegmentInfos infos, SegmentInfo mergedInfo) throws IOException {
120     // 80% of the time we create CFS:
121     return random.nextInt(5) != 1;
122   }
123 }