pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / tasks / SearchTravRetLoadFieldSelectorTask.java
1 package org.apache.lucene.benchmark.byTask.tasks;
2 /**
3  * Copyright 2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18
19 import org.apache.lucene.benchmark.byTask.PerfRunData;
20 import org.apache.lucene.document.FieldSelector;
21 import org.apache.lucene.document.SetBasedFieldSelector;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.index.IndexReader;
24
25 import java.util.StringTokenizer;
26 import java.util.Set;
27 import java.util.HashSet;
28 import java.util.Collections;
29 import java.io.IOException;
30
31 /**
32  * Search and Traverse and Retrieve docs task using a SetBasedFieldSelector.
33  *
34  * <p>Note: This task reuses the reader if it is already open.
35  * Otherwise a reader is opened at start and closed at the end.
36  *
37  * <p>Takes optional param: comma separated list of Fields to load.</p>
38  * 
39  * <p>Other side effects: counts additional 1 (record) for each traversed hit, 
40  * and 1 more for each retrieved (non null) document.</p>
41  */
42 public class SearchTravRetLoadFieldSelectorTask extends SearchTravTask {
43
44   protected FieldSelector fieldSelector;
45   public SearchTravRetLoadFieldSelectorTask(PerfRunData runData) {
46     super(runData);
47     
48   }
49
50   @Override
51   public boolean withRetrieve() {
52     return true;
53   }
54
55
56   @Override
57   protected Document retrieveDoc(IndexReader ir, int id) throws IOException {
58     return ir.document(id, fieldSelector);
59   }
60
61   @Override
62   public void setParams(String params) {
63     this.params = params; // cannot just call super.setParams(), b/c it's params differ.
64     Set<String> fieldsToLoad = new HashSet<String>();
65     for (StringTokenizer tokenizer = new StringTokenizer(params, ","); tokenizer.hasMoreTokens();) {
66       String s = tokenizer.nextToken();
67       fieldsToLoad.add(s);
68     }
69     fieldSelector = new SetBasedFieldSelector(fieldsToLoad, Collections.<String> emptySet());
70   }
71
72
73   /* (non-Javadoc)
74   * @see org.apache.lucene.benchmark.byTask.tasks.PerfTask#supportsParams()
75   */
76   @Override
77   public boolean supportsParams() {
78     return true;
79   }
80 }