pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / tasks / DeleteByPercentTask.java
1 package org.apache.lucene.benchmark.byTask.tasks;
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.util.Random;
21
22 import org.apache.lucene.benchmark.byTask.PerfRunData;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.TermDocs;
25
26 /**
27  * Deletes a percentage of documents from an index randomly
28  * over the number of documents.  The parameter, X, is in
29  * percent.  EG 50 means 1/2 of all documents will be
30  * deleted.
31  *
32  * <p><b>NOTE</b>: the param is an absolute percentage of
33  * maxDoc().  This means if you delete 50%, and then delete
34  * 50% again, the 2nd delete will do nothing.
35  *
36  * <p> Parameters:
37  * <ul>
38  * <li> delete.percent.rand.seed - defines the seed to
39  * initialize Random (default 1717)
40  * </ul>
41  */
42 public class DeleteByPercentTask extends PerfTask {
43   double percent;
44   int numDeleted = 0;
45   final Random random;
46
47   public DeleteByPercentTask(PerfRunData runData) {
48     super(runData);
49     random = new Random(runData.getConfig().get("delete.percent.rand.seed", 1717));
50   }
51   
52   @Override
53   public void setup() throws Exception {
54     super.setup();
55   }
56
57   @Override
58   public void setParams(String params) {
59     super.setParams(params);
60     percent = Double.parseDouble(params)/100;
61   }
62
63   @Override
64   public boolean supportsParams() {
65     return true;
66   }
67
68   @Override
69   public int doLogic() throws Exception {
70     IndexReader r = getRunData().getIndexReader();
71     int maxDoc = r.maxDoc();
72     int numDeleted = 0;
73     // percent is an absolute target:
74     int numToDelete = ((int) (maxDoc * percent)) - r.numDeletedDocs();
75     if (numToDelete < 0) {
76       r.undeleteAll();
77       numToDelete = (int) (maxDoc * percent);
78     }
79     while (numDeleted < numToDelete) {
80       double delRate = ((double) (numToDelete-numDeleted))/r.numDocs();
81       TermDocs termDocs = r.termDocs(null);
82       while (termDocs.next() && numDeleted < numToDelete) {
83         if (random.nextDouble() <= delRate) {
84           r.deleteDocument(termDocs.doc());
85           numDeleted++;
86         }
87       }
88       termDocs.close();
89     }
90     System.out.println("--> processed (delete) " + numDeleted + " docs");
91     r.decRef();
92     return numDeleted;
93   }
94 }