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.io.IOException;
21 import java.util.Collection;
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;
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)
36 public class OpenReaderTask extends PerfTask {
37 public static final String USER_DATA = "userData";
38 private boolean readOnly = true;
39 private String commitUserData = null;
41 public OpenReaderTask(PerfRunData runData) {
46 public int doLogic() throws IOException {
47 Directory dir = getRunData().getDirectory();
48 Config config = getRunData().getConfig();
50 final IndexDeletionPolicy deletionPolicy;
52 deletionPolicy = null;
54 deletionPolicy = CreateIndexTask.getIndexDeletionPolicy(config);
56 if (commitUserData != null) {
57 r = IndexReader.open(OpenReaderTask.findIndexCommit(dir, commitUserData),
61 r = IndexReader.open(dir,
65 getRunData().setIndexReader(r);
66 // We transfer reference to the run data
72 public void setParams(String params) {
73 super.setParams(params);
75 String[] split = params.split(",");
76 if (split.length > 0) {
77 readOnly = Boolean.valueOf(split[0]).booleanValue();
79 if (split.length > 1) {
80 commitUserData = split[1];
86 public boolean supportsParams() {
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();
96 ud = map.get(USER_DATA);
98 if (ud != null && ud.equals(userData)) {
103 throw new IOException("index does not contain commit with userData: " + userData);