You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2011/12/13 18:51:24 UTC
svn commit: r1213806 - in /lucene/dev/branches/branch_3x: ./ lucene/
lucene/src/java/org/apache/lucene/index/
lucene/src/java/org/apache/lucene/util/
lucene/src/test-framework/java/org/apache/lucene/util/
Author: mikemccand
Date: Tue Dec 13 17:51:23 2011
New Revision: 1213806
URL: http://svn.apache.org/viewvc?rev=1213806&view=rev
Log:
LUCENE-3586: allow specifying -dir-impl FSDirectory subclass to CheckIndex, IndexUpgrader
Added:
lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/util/CommandLineUtil.java
- copied, changed from r1213800, lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/CommandLineUtil.java
Modified:
lucene/dev/branches/branch_3x/ (props changed)
lucene/dev/branches/branch_3x/lucene/ (props changed)
lucene/dev/branches/branch_3x/lucene/CHANGES.txt
lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/CheckIndex.java
lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexReader.java
lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexUpgrader.java
lucene/dev/branches/branch_3x/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java
Modified: lucene/dev/branches/branch_3x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/CHANGES.txt?rev=1213806&r1=1213805&r2=1213806&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_3x/lucene/CHANGES.txt Tue Dec 13 17:51:23 2011
@@ -54,6 +54,10 @@ New Features
* LUCENE-3593: Added a FieldValueFilter that accepts all documents that either
have at least one or no value at all in a specific field. (Simon Willnauer,
Uwe Schindler, Robert Muir)
+
+* LUCENE-3586: CheckIndex and IndexUpgrader allow you to specify the
+ specific FSDirectory implementation to use (with the new -dir-impl
+ command-line option). (Luca Cavanna via Mike McCandless)
Bug fixes
Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/CheckIndex.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/CheckIndex.java?rev=1213806&r1=1213805&r2=1213806&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/CheckIndex.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/CheckIndex.java Tue Dec 13 17:51:23 2011
@@ -17,26 +17,26 @@ package org.apache.lucene.index;
* limitations under the License.
*/
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.store.FSDirectory;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.store.IndexInput;
-import org.apache.lucene.util.StringHelper;
-import org.apache.lucene.document.AbstractField; // for javadocs
-import org.apache.lucene.document.Document;
-
-import java.text.NumberFormat;
-import java.io.PrintStream;
-import java.io.IOException;
import java.io.File;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.text.NumberFormat;
+import java.util.ArrayList;
import java.util.Collection;
-
import java.util.Comparator;
import java.util.List;
-import java.util.ArrayList;
import java.util.Map;
+import org.apache.lucene.document.AbstractField; // for javadocs
+import org.apache.lucene.document.Document;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.util.CommandLineUtil;
+import org.apache.lucene.util.StringHelper;
+
/**
* Basic tool and API to check the health of an index and
* write a new segments file that removes reference to
@@ -934,9 +934,11 @@ public class CheckIndex {
boolean doFix = false;
List<String> onlySegments = new ArrayList<String>();
String indexPath = null;
+ String dirImpl = null;
int i = 0;
while(i < args.length) {
- if (args[i].equals("-fix")) {
+ String arg = args[i];
+ if ("-fix".equals(arg)) {
doFix = true;
i++;
} else if (args[i].equals("-segment")) {
@@ -944,27 +946,35 @@ public class CheckIndex {
System.out.println("ERROR: missing name for -segment option");
System.exit(1);
}
- onlySegments.add(args[i+1]);
- i += 2;
+ i++;
+ onlySegments.add(args[i]);
+ } else if ("-dir-impl".equals(arg)) {
+ if (i == args.length - 1) {
+ System.out.println("ERROR: missing value for -dir-impl option");
+ System.exit(1);
+ }
+ i++;
+ dirImpl = args[i];
} else {
if (indexPath != null) {
System.out.println("ERROR: unexpected extra argument '" + args[i] + "'");
System.exit(1);
}
indexPath = args[i];
- i++;
}
+ i++;
}
if (indexPath == null) {
System.out.println("\nERROR: index path not specified");
- System.out.println("\nUsage: java org.apache.lucene.index.CheckIndex pathToIndex [-fix] [-segment X] [-segment Y]\n" +
+ System.out.println("\nUsage: java org.apache.lucene.index.CheckIndex pathToIndex [-fix] [-segment X] [-segment Y] [-dir-impl X]\n" +
"\n" +
" -fix: actually write a new segments_N file, removing any problematic segments\n" +
" -segment X: only check the specified segments. This can be specified multiple\n" +
" times, to check more than one segment, eg '-segment _2 -segment _a'.\n" +
" You can't use this with the -fix option\n" +
- "\n" +
+ " -dir-impl X: use a specific " + FSDirectory.class.getSimpleName() + " implementation. " +
+ "If no package is specified the " + FSDirectory.class.getPackage().getName() + " package will be used.\n" +
"**WARNING**: -fix should only be used on an emergency basis as it will cause\n" +
"documents (perhaps many) to be permanently removed from the index. Always make\n" +
"a backup copy of your index before running this! Do not run this tool on an index\n" +
@@ -994,7 +1004,11 @@ public class CheckIndex {
System.out.println("\nOpening index @ " + indexPath + "\n");
Directory dir = null;
try {
- dir = FSDirectory.open(new File(indexPath));
+ if (dirImpl == null) {
+ dir = FSDirectory.open(new File(indexPath));
+ } else {
+ dir = CommandLineUtil.newFSDirectory(dirImpl, new File(indexPath));
+ }
} catch (Throwable t) {
System.out.println("ERROR: could not open directory \"" + indexPath + "\"; exiting");
t.printStackTrace(System.out);
Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexReader.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexReader.java?rev=1213806&r1=1213805&r2=1213806&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexReader.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexReader.java Tue Dec 13 17:51:23 2011
@@ -28,10 +28,11 @@ import java.util.concurrent.atomic.Atomi
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.search.FieldCache; // javadocs
-import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.SearcherManager; // javadocs
+import org.apache.lucene.search.Similarity;
import org.apache.lucene.store.*;
import org.apache.lucene.util.ArrayUtil;
+import org.apache.lucene.util.CommandLineUtil;
import org.apache.lucene.util.VirtualMethod;
/** IndexReader is an abstract class, providing an interface for accessing an
@@ -1625,17 +1626,28 @@ public abstract class IndexReader implem
public static void main(String [] args) {
String filename = null;
boolean extract = false;
+ String dirImpl = null;
- for (int i = 0; i < args.length; ++i) {
- if (args[i].equals("-extract")) {
+ int j = 0;
+ while(j < args.length) {
+ String arg = args[j];
+ if ("-extract".equals(arg)) {
extract = true;
+ } else if ("-dir-impl".equals(arg)) {
+ if (j == args.length - 1) {
+ System.out.println("ERROR: missing value for -dir-impl option");
+ System.exit(1);
+ }
+ j++;
+ dirImpl = args[j];
} else if (filename == null) {
- filename = args[i];
+ filename = arg;
}
+ j++;
}
if (filename == null) {
- System.out.println("Usage: org.apache.lucene.index.IndexReader [-extract] <cfsfile>");
+ System.out.println("Usage: org.apache.lucene.index.IndexReader [-extract] [-dir-impl X] <cfsfile>");
return;
}
@@ -1646,7 +1658,12 @@ public abstract class IndexReader implem
File file = new File(filename);
String dirname = file.getAbsoluteFile().getParent();
filename = file.getName();
- dir = FSDirectory.open(new File(dirname));
+ if (dirImpl == null) {
+ dir = FSDirectory.open(new File(dirname));
+ } else {
+ dir = CommandLineUtil.newFSDirectory(dirImpl, new File(dirname));
+ }
+
cfr = new CompoundFileReader(dir, filename);
String [] files = cfr.listAll();
Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexUpgrader.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexUpgrader.java?rev=1213806&r1=1213805&r2=1213806&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexUpgrader.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexUpgrader.java Tue Dec 13 17:51:23 2011
@@ -19,6 +19,7 @@ package org.apache.lucene.index;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.util.CommandLineUtil;
import org.apache.lucene.util.Constants;
import org.apache.lucene.util.Version;
@@ -53,36 +54,56 @@ public final class IndexUpgrader {
private static void printUsage() {
System.err.println("Upgrades an index so all segments created with a previous Lucene version are rewritten.");
System.err.println("Usage:");
- System.err.println(" java " + IndexUpgrader.class.getName() + " [-delete-prior-commits] [-verbose] indexDir");
+ System.err.println(" java " + IndexUpgrader.class.getName() + " [-delete-prior-commits] [-verbose] [-dir-impl X] indexDir");
System.err.println("This tool keeps only the last commit in an index; for this");
System.err.println("reason, if the incoming index has more than one commit, the tool");
System.err.println("refuses to run by default. Specify -delete-prior-commits to override");
System.err.println("this, allowing the tool to delete all but the last commit.");
+ System.err.println("Specify a " + FSDirectory.class.getSimpleName() +
+ " implementation through the -dir-impl option to force its use. If no package is specified the "
+ + FSDirectory.class.getPackage().getName() + " package will be used.");
System.err.println("WARNING: This tool may reorder document IDs!");
System.exit(1);
}
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
- String dir = null;
+ String path = null;
boolean deletePriorCommits = false;
PrintStream out = null;
- for (String arg : args) {
+ String dirImpl = null;
+ int i = 0;
+ while (i<args.length) {
+ String arg = args[i];
if ("-delete-prior-commits".equals(arg)) {
deletePriorCommits = true;
} else if ("-verbose".equals(arg)) {
out = System.out;
- } else if (dir == null) {
- dir = arg;
- } else {
+ } else if (path == null) {
+ path = arg;
+ } else if ("-dir-impl".equals(arg)) {
+ if (i == args.length - 1) {
+ System.out.println("ERROR: missing value for -dir-impl option");
+ System.exit(1);
+ }
+ i++;
+ dirImpl = args[i];
+ }else {
printUsage();
}
+ i++;
}
- if (dir == null) {
+ if (path == null) {
printUsage();
}
- new IndexUpgrader(FSDirectory.open(new File(dir)), Version.LUCENE_CURRENT, out, deletePriorCommits).upgrade();
+ Directory dir = null;
+ if (dirImpl == null) {
+ dir = FSDirectory.open(new File(path));
+ } else {
+ dir = CommandLineUtil.newFSDirectory(dirImpl, new File(path));
+ }
+ new IndexUpgrader(dir, Version.LUCENE_CURRENT, out, deletePriorCommits).upgrade();
}
private final Directory dir;
Copied: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/util/CommandLineUtil.java (from r1213800, lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/CommandLineUtil.java)
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/util/CommandLineUtil.java?p2=lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/util/CommandLineUtil.java&p1=lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/CommandLineUtil.java&r1=1213800&r2=1213806&rev=1213806&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/CommandLineUtil.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/util/CommandLineUtil.java Tue Dec 13 17:51:23 2011
@@ -36,7 +36,7 @@ public final class CommandLineUtil {
/**
* Creates a specific FSDirectory instance starting from its class name
- * @param className The name of the FSDirectory class to load
+ * @param clazzName The name of the FSDirectory class to load
* @param file The file to be used as parameter constructor
* @return the new FSDirectory instance
*/
@@ -60,7 +60,7 @@ public final class CommandLineUtil {
/**
* Loads a specific Directory implementation
- * @param className The name of the Directory class to load
+ * @param clazzName The name of the Directory class to load
* @return The Directory class loaded
* @throws ClassNotFoundException
*/
@@ -71,7 +71,7 @@ public final class CommandLineUtil {
/**
* Loads a specific FSDirectory implementation
- * @param className The name of the FSDirectory class to load
+ * @param clazzName The name of the FSDirectory class to load
* @return The FSDirectory class loaded
* @throws ClassNotFoundException
*/
Modified: lucene/dev/branches/branch_3x/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java?rev=1213806&r1=1213805&r2=1213806&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java Tue Dec 13 17:51:23 2011
@@ -24,7 +24,6 @@ import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
-import java.lang.reflect.Constructor;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
@@ -971,24 +970,16 @@ public abstract class LuceneTestCase ext
fsdirClass = FS_DIRECTORIES[random.nextInt(FS_DIRECTORIES.length)];
}
- if (fsdirClass.indexOf(".") == -1) {// if not fully qualified, assume .store
- fsdirClass = "org.apache.lucene.store." + fsdirClass;
- }
-
Class<? extends FSDirectory> clazz;
try {
try {
- clazz = Class.forName(fsdirClass).asSubclass(FSDirectory.class);
+ clazz = CommandLineUtil.loadFSDirectoryClass(fsdirClass);
} catch (ClassCastException e) {
// TEST_DIRECTORY is not a sub-class of FSDirectory, so draw one at random
fsdirClass = FS_DIRECTORIES[random.nextInt(FS_DIRECTORIES.length)];
-
- if (fsdirClass.indexOf(".") == -1) {// if not fully qualified, assume .store
- fsdirClass = "org.apache.lucene.store." + fsdirClass;
- }
-
- clazz = Class.forName(fsdirClass).asSubclass(FSDirectory.class);
+ clazz = CommandLineUtil.loadFSDirectoryClass(fsdirClass);
}
+
MockDirectoryWrapper dir = new MockDirectoryWrapper(random, newFSDirectoryImpl(clazz, f));
if (lf != null) {
dir.setLockFactory(lf);
@@ -1135,10 +1126,7 @@ public abstract class LuceneTestCase ext
throws IOException {
FSDirectory d = null;
try {
- // Assuming every FSDirectory has a ctor(File), but not all may take a
- // LockFactory too, so setting it afterwards.
- Constructor<? extends FSDirectory> ctor = clazz.getConstructor(File.class);
- d = ctor.newInstance(file);
+ d = CommandLineUtil.newFSDirectory(clazz, file);
} catch (Exception e) {
d = FSDirectory.open(file);
}
@@ -1156,12 +1144,12 @@ public abstract class LuceneTestCase ext
}
static Directory newDirectoryImpl(Random random, String clazzName) {
- if (clazzName.equals("random"))
+ if (clazzName.equals("random")) {
clazzName = randomDirectory(random);
- if (clazzName.indexOf(".") == -1) // if not fully qualified, assume .store
- clazzName = "org.apache.lucene.store." + clazzName;
+ }
+
try {
- final Class<? extends Directory> clazz = Class.forName(clazzName).asSubclass(Directory.class);
+ final Class<? extends Directory> clazz = CommandLineUtil.loadDirectoryClass(clazzName);
// If it is a FSDirectory type, try its ctor(File)
if (FSDirectory.class.isAssignableFrom(clazz)) {
final File dir = _TestUtil.getTempDir("index");