1 package org.apache.lucene.benchmark.byTask.tasks;
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.util.Random;
22 import org.apache.lucene.benchmark.byTask.PerfRunData;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.TermDocs;
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
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.
38 * <li> delete.percent.rand.seed - defines the seed to
39 * initialize Random (default 1717)
42 public class DeleteByPercentTask extends PerfTask {
47 public DeleteByPercentTask(PerfRunData runData) {
49 random = new Random(runData.getConfig().get("delete.percent.rand.seed", 1717));
53 public void setup() throws Exception {
58 public void setParams(String params) {
59 super.setParams(params);
60 percent = Double.parseDouble(params)/100;
64 public boolean supportsParams() {
69 public int doLogic() throws Exception {
70 IndexReader r = getRunData().getIndexReader();
71 int maxDoc = r.maxDoc();
73 // percent is an absolute target:
74 int numToDelete = ((int) (maxDoc * percent)) - r.numDeletedDocs();
75 if (numToDelete < 0) {
77 numToDelete = (int) (maxDoc * percent);
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());
90 System.out.println("--> processed (delete) " + numDeleted + " docs");