~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/NewAnalyzerTask.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
import org.apache.lucene.analysis.Analyzer;
19
 
import org.apache.lucene.benchmark.byTask.PerfRunData;
20
 
import org.apache.lucene.util.Version;
21
 
 
22
 
import java.io.IOException;
23
 
import java.util.ArrayList;
24
 
import java.util.List;
25
 
import java.util.StringTokenizer;
26
 
import java.lang.reflect.Constructor;
27
 
 
28
 
/**
29
 
 * Create a new {@link org.apache.lucene.analysis.Analyzer} and set it it in the getRunData() for use by all future tasks.
30
 
 *
31
 
 */
32
 
public class NewAnalyzerTask extends PerfTask {
33
 
  private List<String> analyzerClassNames;
34
 
  private int current;
35
 
 
36
 
  public NewAnalyzerTask(PerfRunData runData) {
37
 
    super(runData);
38
 
    analyzerClassNames = new ArrayList<String>();
39
 
  }
40
 
  
41
 
  public static final Analyzer createAnalyzer(String className) throws Exception{
42
 
    final Class<? extends Analyzer> clazz = Class.forName(className).asSubclass(Analyzer.class);
43
 
    try {
44
 
      // first try to use a ctor with version parameter (needed for many new Analyzers that have no default one anymore
45
 
      Constructor<? extends Analyzer> cnstr = clazz.getConstructor(Version.class);
46
 
      return cnstr.newInstance(Version.LUCENE_CURRENT);
47
 
    } catch (NoSuchMethodException nsme) {
48
 
      // otherwise use default ctor
49
 
      return clazz.newInstance();
50
 
    }
51
 
  }
52
 
 
53
 
  @Override
54
 
  public int doLogic() throws IOException {
55
 
    String className = null;
56
 
    try {
57
 
      if (current >= analyzerClassNames.size())
58
 
      {
59
 
        current = 0;
60
 
      }
61
 
      className = analyzerClassNames.get(current++);
62
 
      if (className == null || className.equals(""))
63
 
      {
64
 
        className = "org.apache.lucene.analysis.standard.StandardAnalyzer"; 
65
 
      }
66
 
      if (className.indexOf(".") == -1  || className.startsWith("standard."))//there is no package name, assume o.a.l.analysis
67
 
      {
68
 
        className = "org.apache.lucene.analysis." + className;
69
 
      }
70
 
      getRunData().setAnalyzer(createAnalyzer(className));
71
 
      System.out.println("Changed Analyzer to: " + className);
72
 
    } catch (Exception e) {
73
 
      throw new RuntimeException("Error creating Analyzer: " + className, e);
74
 
    }
75
 
    return 1;
76
 
  }
77
 
 
78
 
  /**
79
 
   * Set the params (analyzerClassName only),  Comma-separate list of Analyzer class names.  If the Analyzer lives in
80
 
   * org.apache.lucene.analysis, the name can be shortened by dropping the o.a.l.a part of the Fully Qualified Class Name.
81
 
   * <p/>
82
 
   * Example Declaration: {"NewAnalyzer" NewAnalyzer(WhitespaceAnalyzer, SimpleAnalyzer, StopAnalyzer, standard.StandardAnalyzer) >
83
 
   * @param params analyzerClassName, or empty for the StandardAnalyzer
84
 
   */
85
 
  @Override
86
 
  public void setParams(String params) {
87
 
    super.setParams(params);
88
 
    for (StringTokenizer tokenizer = new StringTokenizer(params, ","); tokenizer.hasMoreTokens();) {
89
 
      String s = tokenizer.nextToken();
90
 
      analyzerClassNames.add(s.trim());
91
 
    }
92
 
  }
93
 
 
94
 
  /* (non-Javadoc)
95
 
   * @see org.apache.lucene.benchmark.byTask.tasks.PerfTask#supportsParams()
96
 
   */
97
 
  @Override
98
 
  public boolean supportsParams() {
99
 
    return true;
100
 
  }
101
 
}