add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / tasks / OpenReaderTask.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.io.IOException;
21 import java.util.Collection;
22 import java.util.Map;
23
24 import org.apache.lucene.benchmark.byTask.PerfRunData;
25 import org.apache.lucene.benchmark.byTask.utils.Config;
26 import org.apache.lucene.index.IndexCommit;
27 import org.apache.lucene.index.IndexDeletionPolicy;
28 import org.apache.lucene.index.IndexReader;
29 import org.apache.lucene.store.Directory;
30
31 /**
32  * Open an index reader.
33  * <br>Other side effects: index reader object in perfRunData is set.
34  * <br> Optional params readOnly,commitUserData eg. OpenReader(false,commit1)
35  */
36 public class OpenReaderTask extends PerfTask {
37   public static final String USER_DATA = "userData";
38   private boolean readOnly = true;
39   private String commitUserData = null;
40
41   public OpenReaderTask(PerfRunData runData) {
42     super(runData);
43   }
44
45   @Override
46   public int doLogic() throws IOException {
47     Directory dir = getRunData().getDirectory();
48     Config config = getRunData().getConfig();
49     IndexReader r = null;
50     final IndexDeletionPolicy deletionPolicy;
51     if (readOnly) {
52       deletionPolicy = null;
53     } else {
54       deletionPolicy = CreateIndexTask.getIndexDeletionPolicy(config);
55     }
56     if (commitUserData != null) {
57       r = IndexReader.open(OpenReaderTask.findIndexCommit(dir, commitUserData),
58                            deletionPolicy,
59                            readOnly); 
60     } else {
61       r = IndexReader.open(dir,
62                            deletionPolicy,
63                            readOnly); 
64     }
65     getRunData().setIndexReader(r);
66     // We transfer reference to the run data
67     r.decRef();
68     return 1;
69   }
70  
71   @Override
72   public void setParams(String params) {
73     super.setParams(params);
74     if (params != null) {
75       String[] split = params.split(",");
76       if (split.length > 0) {
77         readOnly = Boolean.valueOf(split[0]).booleanValue();
78       }
79       if (split.length > 1) {
80         commitUserData = split[1];
81       }
82     }
83   }
84
85   @Override
86   public boolean supportsParams() {
87     return true;
88   }
89
90   public static IndexCommit findIndexCommit(Directory dir, String userData) throws IOException {
91     Collection<IndexCommit> commits = IndexReader.listCommits(dir);
92     for (final IndexCommit ic : commits) {
93       Map<String,String> map = ic.getUserData();
94       String ud = null;
95       if (map != null) {
96         ud = map.get(USER_DATA);
97       }
98       if (ud != null && ud.equals(userData)) {
99         return ic;
100       }
101     }
102
103     throw new IOException("index does not contain commit with userData: " + userData);
104   }
105 }