pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test-framework / java / 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   @Override
58   public MergeSpecification findForcedMerges(
59        SegmentInfos segmentInfos, int maxSegmentCount, Map<SegmentInfo,Boolean> segmentsToMerge)
60     throws CorruptIndexException, IOException {
61
62     final List<SegmentInfo> eligibleSegments = new ArrayList<SegmentInfo>();
63     for(SegmentInfo info : segmentInfos) {
64       if (segmentsToMerge.containsKey(info)) {
65         eligibleSegments.add(info);
66       }
67     }
68
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);
76       int upto = 0;
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)));
81         upto += inc;
82       }
83     }
84
85     if (mergeSpec != null) {
86       for(OneMerge merge : mergeSpec.merges) {
87         for(SegmentInfo info : merge.segments) {
88           assert segmentsToMerge.containsKey(info);
89         }
90       }
91     }
92     return mergeSpec;
93   }
94
95   @Override
96   public MergeSpecification findForcedDeletesMerges(
97       SegmentInfos segmentInfos)
98     throws CorruptIndexException, IOException {
99     return findMerges(segmentInfos);
100   }
101
102   @Override
103   public void close() {
104   }
105
106   @Override
107   public boolean useCompoundFile(SegmentInfos infos, SegmentInfo mergedInfo) throws IOException {
108     // 80% of the time we create CFS:
109     return random.nextInt(5) != 1;
110   }
111 }