001: package org.apache.lucene.benchmark.byTask.tasks;
002:
003: import org.apache.lucene.benchmark.byTask.PerfRunData;
004:
005: /**
006: * Licensed to the Apache Software Foundation (ASF) under one or more
007: * contributor license agreements. See the NOTICE file distributed with
008: * this work for additional information regarding copyright ownership.
009: * The ASF licenses this file to You under the Apache License, Version 2.0
010: * (the "License"); you may not use this file except in compliance with
011: * the License. You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: /**
023: * Delete a document by docid.
024: * <br>Other side effects: none.
025: * <br>Relevant properties: <code>doc.delete.log.step , doc.delete.step</code>.
026: * <br>If no docid param is supplied, deletes doc with <code>id = last-deleted-doc + doc.delete.step</code>.
027: * <br>Takes optional param: document id.
028: */
029: public class DeleteDocTask extends PerfTask {
030:
031: /**
032: * Gap between ids of deleted docs, applies when no docid param is provided.
033: */
034: public static final int DEFAULT_DOC_DELETE_STEP = 8;
035:
036: /**
037: * Default value for property <code>doc.delete.log.step<code> - indicating how often
038: * an "deleted N docs" message should be logged.
039: */
040: public static final int DEFAULT_DELETE_DOC_LOG_STEP = 500;
041:
042: public DeleteDocTask(PerfRunData runData) {
043: super (runData);
044: }
045:
046: private int logStep = -1;
047: private int deleteStep = -1;
048: private static int numDeleted = 0;
049: private static int lastDeleted = -1;
050:
051: private int docid = -1;
052: private boolean byStep = true;
053:
054: public int doLogic() throws Exception {
055: getRunData().getIndexReader().deleteDocument(docid);
056: lastDeleted = docid;
057: return 1; // one work item done here
058: }
059:
060: /* (non-Javadoc)
061: * @see org.apache.lucene.benchmark.byTask.tasks.PerfTask#setup()
062: */
063: public void setup() throws Exception {
064: super .setup();
065: // one time static initializations
066: if (logStep < 0) {
067: logStep = getRunData().getConfig().get(
068: "doc.delete.log.step", DEFAULT_DELETE_DOC_LOG_STEP);
069: }
070: if (deleteStep < 0) {
071: deleteStep = getRunData().getConfig().get(
072: "doc.delete.step", DEFAULT_DOC_DELETE_STEP);
073: }
074: // set the docid to be deleted
075: docid = (byStep ? lastDeleted + deleteStep : docid);
076: }
077:
078: /* (non-Javadoc)
079: * @see PerfTask#tearDown()
080: */
081: public void tearDown() throws Exception {
082: log(++numDeleted);
083: super .tearDown();
084: }
085:
086: private void log(int count) {
087: if (logStep > 0 && (count % logStep) == 0) {
088: System.out.println("--> processed (delete) " + count
089: + " docs, last deleted: " + lastDeleted);
090: }
091: }
092:
093: /**
094: * Set the params (docid only)
095: * @param params docid to delete, or -1 for deleting by delete gap settings.
096: */
097: public void setParams(String params) {
098: super .setParams(params);
099: docid = (int) Float.parseFloat(params);
100: byStep = (docid < 0);
101: }
102:
103: /* (non-Javadoc)
104: * @see org.apache.lucene.benchmark.byTask.tasks.PerfTask#supportsParams()
105: */
106: public boolean supportsParams() {
107: return true;
108: }
109:
110: }
|