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

« back to all changes in this revision

Viewing changes to lucene/src/java/org/apache/lucene/index/IndexUpgrader.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.index;
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 org.apache.lucene.store.Directory;
21
 
import org.apache.lucene.store.FSDirectory;
22
 
import org.apache.lucene.util.Constants;
23
 
import org.apache.lucene.util.Version;
24
 
 
25
 
import java.io.File;
26
 
import java.io.IOException;
27
 
import java.io.PrintStream;
28
 
import java.util.Collection;
29
 
 
30
 
/**
31
 
  * This is an easy-to-use tool that upgrades all segments of an index from previous Lucene versions
32
 
  * to the current segment file format. It can be used from command line:
33
 
  * <pre>
34
 
  *  java -cp lucene-core.jar org.apache.lucene.index.IndexUpgrader [-delete-prior-commits] [-verbose] indexDir
35
 
  * </pre>
36
 
  * Alternatively this class can be instantiated and {@link #upgrade} invoked. It uses {@link UpgradeIndexMergePolicy}
37
 
  * and triggers the upgrade via an forceMerge request to {@link IndexWriter}.
38
 
  * <p>This tool keeps only the last commit in an index; for this
39
 
  * reason, if the incoming index has more than one commit, the tool
40
 
  * refuses to run by default. Specify {@code -delete-prior-commits}
41
 
  * to override this, allowing the tool to delete all but the last commit.
42
 
  * From Java code this can be enabled by passing {@code true} to
43
 
  * {@link #IndexUpgrader(Directory,Version,PrintStream,boolean)}.
44
 
  * <p><b>Warning:</b> This tool may reorder documents if the index was partially
45
 
  * upgraded before execution (e.g., documents were added). If your application relies
46
 
  * on &quot;monotonicity&quot; of doc IDs (which means that the order in which the documents
47
 
  * were added to the index is preserved), do a full forceMerge instead.
48
 
  * The {@link MergePolicy} set by {@link IndexWriterConfig} may also reorder
49
 
  * documents.
50
 
  */
51
 
public final class IndexUpgrader {
52
 
 
53
 
  private static void printUsage() {
54
 
    System.err.println("Upgrades an index so all segments created with a previous Lucene version are rewritten.");
55
 
    System.err.println("Usage:");
56
 
    System.err.println("  java " + IndexUpgrader.class.getName() + " [-delete-prior-commits] [-verbose] indexDir");
57
 
    System.err.println("This tool keeps only the last commit in an index; for this");
58
 
    System.err.println("reason, if the incoming index has more than one commit, the tool");
59
 
    System.err.println("refuses to run by default. Specify -delete-prior-commits to override");
60
 
    System.err.println("this, allowing the tool to delete all but the last commit.");
61
 
    System.err.println("WARNING: This tool may reorder document IDs!");
62
 
    System.exit(1);
63
 
  }
64
 
 
65
 
  @SuppressWarnings("deprecation")
66
 
  public static void main(String[] args) throws IOException {
67
 
    String dir = null;
68
 
    boolean deletePriorCommits = false;
69
 
    PrintStream out = null;
70
 
    for (String arg : args) {
71
 
      if ("-delete-prior-commits".equals(arg)) {
72
 
        deletePriorCommits = true;
73
 
      } else if ("-verbose".equals(arg)) {
74
 
        out = System.out;
75
 
      } else if (dir == null) {
76
 
        dir = arg;
77
 
      } else {
78
 
        printUsage();
79
 
      }
80
 
    }
81
 
    if (dir == null) {
82
 
      printUsage();
83
 
    }
84
 
    
85
 
    new IndexUpgrader(FSDirectory.open(new File(dir)), Version.LUCENE_CURRENT, out, deletePriorCommits).upgrade();
86
 
  }
87
 
  
88
 
  private final Directory dir;
89
 
  private final PrintStream infoStream;
90
 
  private final IndexWriterConfig iwc;
91
 
  private final boolean deletePriorCommits;
92
 
  
93
 
  /** Creates index upgrader on the given directory, using an {@link IndexWriter} using the given
94
 
   * {@code matchVersion}. The tool refuses to upgrade indexes with multiple commit points. */
95
 
  public IndexUpgrader(Directory dir, Version matchVersion) {
96
 
    this(dir, new IndexWriterConfig(matchVersion, null), null, false);
97
 
  }
98
 
  
99
 
  /** Creates index upgrader on the given directory, using an {@link IndexWriter} using the given
100
 
   * {@code matchVersion}. You have the possibility to upgrade indexes with multiple commit points by removing
101
 
   * all older ones. If {@code infoStream} is not {@code null}, all logging output will be sent to this stream. */
102
 
  public IndexUpgrader(Directory dir, Version matchVersion, PrintStream infoStream, boolean deletePriorCommits) {
103
 
    this(dir, new IndexWriterConfig(matchVersion, null), infoStream, deletePriorCommits);
104
 
  }
105
 
  
106
 
  /** Creates index upgrader on the given directory, using an {@link IndexWriter} using the given
107
 
   * config. You have the possibility to upgrade indexes with multiple commit points by removing
108
 
   * all older ones. If {@code infoStream} is not {@code null}, all logging output will be sent to this stream. */
109
 
  public IndexUpgrader(Directory dir, IndexWriterConfig iwc, PrintStream infoStream, boolean deletePriorCommits) {
110
 
    this.dir = dir;
111
 
    this.iwc = iwc;
112
 
    this.infoStream = infoStream;
113
 
    this.deletePriorCommits = deletePriorCommits;
114
 
  }
115
 
  
116
 
  public void upgrade() throws IOException {
117
 
    if (!IndexReader.indexExists(dir)) {
118
 
      throw new IndexNotFoundException(dir.toString());
119
 
    }
120
 
  
121
 
    if (!deletePriorCommits) {
122
 
      final Collection<IndexCommit> commits = IndexReader.listCommits(dir);
123
 
      if (commits.size() > 1) {
124
 
        throw new IllegalArgumentException("This tool was invoked to not delete prior commit points, but the following commits were found: " + commits);
125
 
      }
126
 
    }
127
 
    
128
 
    final IndexWriterConfig c = (IndexWriterConfig) iwc.clone();
129
 
    c.setMergePolicy(new UpgradeIndexMergePolicy(c.getMergePolicy()));
130
 
    c.setIndexDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
131
 
    
132
 
    final IndexWriter w = new IndexWriter(dir, c);
133
 
    try {
134
 
      w.setInfoStream(infoStream);
135
 
      w.message("Upgrading all pre-" + Constants.LUCENE_MAIN_VERSION + " segments of index directory '" + dir + "' to version " + Constants.LUCENE_MAIN_VERSION + "...");
136
 
      w.forceMerge(1);
137
 
      w.message("All segments upgraded to version " + Constants.LUCENE_MAIN_VERSION);
138
 
    } finally {
139
 
      w.close();
140
 
    }
141
 
  }
142
 
  
143
 
}