Index: C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/LuceneConfig.java =================================================================== --- C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/LuceneConfig.java (revision 0) +++ C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/LuceneConfig.java (revision 0) @@ -0,0 +1,132 @@ +package org.apache.lucene; + +import java.io.File; +import org.apache.lucene.store.FSDirectory; + +/** A Lucene configuration allows multiple instances of Lucene with + * different settings to be run in the same JVM. Just pass the configuration + * with the appropriate settings as an extra parameter when obtaining Lucene + * objects. + *

+ * Use of this class is not required. If you do not use a configuration Lucene + * will fall back to the default behavior of reading the settings from system + * properties. + * + * @see org.apache.lucene.index.IndexReader#open(LuceneConfig, String) + * @see org.apache.lucene.index.IndexReader#open(LuceneConfig, File) + * @see org.apache.lucene.index.IndexReader#open(LuceneConfig, Directory) + * @see org.apache.lucene.index.SegmentReader#get(LuceneConfig, Directory, SegmentInfo, SegmentInfos, boolean, boolean) + * @see org.apache.lucene.index.SegmentReader#get(LuceneConfig, SegmentInfos, SegmentInfo, boolean) + * @see org.apache.lucene.search.IndexSearcher#IndexSearcher(LuceneConfig, String) + * @see org.apache.lucene.search.IndexSearcher#IndexSearcher(LuceneConfig, Directory) + * @see org.apache.lucene.store.FSDirectory#getDirectory(LuceneConfig, File, boolean) + */ +public class LuceneConfig { + public static final LuceneConfig DEFAULT_INSTANCE = new LuceneConfig(); + + private static final String DEFAULT_SEGMENT_READER_CLASS_NAME = "org.apache.lucene.index.SegmentReader"; + + private Class segmentReaderClass; + + private Class fsDirectoryClass; + + private String lockDir; + + /** + * Construct a Lucene configuration using the default behavior that gets the + * settings from system properties. + */ + public LuceneConfig() { + super(); + + loadGlobalSegmentReaderClass(); + + loadGlobalFSDirectoryClass(); + + getGlobalLockDir(); + } + + /** + * Construct a custom Lucene configuration. If a value for a setting is not + * given, the default behavior of getting it from a system property is used. + * + * @param src the SegmentReader implementation class. + * @param fsdc the FSDirectory implementation class. + * @param ld the path of the directory to use for lock files. + */ + public LuceneConfig(Class src, Class fsdc, String ld) { + super(); + + if (src == null) { + loadGlobalSegmentReaderClass(); + } else { + segmentReaderClass = src; + } + + if (fsdc == null) { + loadGlobalFSDirectoryClass(); + } else { + fsDirectoryClass = fsdc; + } + + if (ld == null) { + getGlobalLockDir(); + } else { + lockDir = ld; + } + } + + public Class getSegmentReaderClass() { + return segmentReaderClass; + } + + public Class getFSDirectoryClass() { + return fsDirectoryClass; + } + + public String getLockDir() { + return lockDir; + } + + private void loadGlobalSegmentReaderClass() + { + try { + String name = + System.getProperty("org.apache.lucene.SegmentReader.class", + DEFAULT_SEGMENT_READER_CLASS_NAME); + segmentReaderClass = Class.forName(name); + } catch (ClassNotFoundException e) { + throw new RuntimeException("cannot load SegmentReader class: " + e, e); + } catch (SecurityException se) { + try { + segmentReaderClass = Class.forName(DEFAULT_SEGMENT_READER_CLASS_NAME); + } catch (ClassNotFoundException e) { + throw new RuntimeException("cannot load default SegmentReader class: " + e, e); + } + } + } + + private void loadGlobalFSDirectoryClass() + { + try { + String name = + System.getProperty("org.apache.lucene.FSDirectory.class", + FSDirectory.class.getName()); + fsDirectoryClass = Class.forName(name); + } catch (ClassNotFoundException e) { + throw new RuntimeException("cannot load FSDirectory class: " + e.toString(), e); + } catch (SecurityException se) { + try { + fsDirectoryClass = Class.forName(FSDirectory.class.getName()); + } catch (ClassNotFoundException e) { + throw new RuntimeException("cannot load default FSDirectory class: " + e.toString(), e); + } + } + } + + private void getGlobalLockDir() + { + lockDir = System.getProperty("org.apache.lucene.lockDir", + System.getProperty("java.io.tmpdir")); + } +} Index: C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/index/IndexReader.java =================================================================== --- C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/index/IndexReader.java (revision 425666) +++ C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/index/IndexReader.java (working copy) @@ -16,6 +16,7 @@ * limitations under the License. */ +import org.apache.lucene.LuceneConfig; import org.apache.lucene.document.Document; import org.apache.lucene.document.FieldSelector; import org.apache.lucene.search.Similarity; @@ -122,13 +123,25 @@ /** Returns an IndexReader reading the index in an FSDirectory in the named path. */ public static IndexReader open(String path) throws IOException { - return open(FSDirectory.getDirectory(path, false), true); + return open(LuceneConfig.DEFAULT_INSTANCE, path); } /** Returns an IndexReader reading the index in an FSDirectory in the named + path. */ + public static IndexReader open(LuceneConfig lc, String path) throws IOException { + return open(lc, FSDirectory.getDirectory(path, false), true); + } + + /** Returns an IndexReader reading the index in an FSDirectory in the named path. */ public static IndexReader open(File path) throws IOException { - return open(FSDirectory.getDirectory(path, false), true); + return open(LuceneConfig.DEFAULT_INSTANCE, path); + } + + /** Returns an IndexReader reading the index in an FSDirectory in the named + path. */ + public static IndexReader open(LuceneConfig lc, File path) throws IOException { + return open(lc, FSDirectory.getDirectory(path, false), true); } /** Returns an IndexReader reading the index in the given Directory. */ @@ -133,11 +146,16 @@ /** Returns an IndexReader reading the index in the given Directory. */ public static IndexReader open(final Directory directory) throws IOException { - return open(directory, false); + return open(LuceneConfig.DEFAULT_INSTANCE, directory); + } + + /** Returns an IndexReader reading the index in the given Directory. */ + public static IndexReader open(LuceneConfig lc, final Directory directory) throws IOException { + return open(lc, directory, false); } - private static IndexReader open(final Directory directory, final boolean closeDirectory) throws IOException { - synchronized (directory) { // in- & inter-process sync + private static IndexReader open(final LuceneConfig lc, final Directory directory, final boolean closeDirectory) throws IOException { + synchronized (directory) { // in- & inter-process sync return (IndexReader)new Lock.With( directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), IndexWriter.COMMIT_LOCK_TIMEOUT) { @@ -144,8 +162,8 @@ public Object doBody() throws IOException { SegmentInfos infos = new SegmentInfos(); infos.read(directory); - if (infos.size() == 1) { // index is optimized - return SegmentReader.get(infos, infos.info(0), closeDirectory); + if (infos.size() == 1) { // index is optimized + return SegmentReader.get(lc, infos, infos.info(0), closeDirectory); } IndexReader[] readers = new IndexReader[infos.size()]; for (int i = 0; i < infos.size(); i++) Index: C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/index/SegmentReader.java =================================================================== --- C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/index/SegmentReader.java (revision 425666) +++ C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/index/SegmentReader.java (working copy) @@ -16,6 +16,7 @@ * limitations under the License. */ +import org.apache.lucene.LuceneConfig; import org.apache.lucene.document.Document; import org.apache.lucene.document.FieldSelector; import org.apache.lucene.search.DefaultSimilarity; @@ -30,7 +31,7 @@ /** * @version $Id$ */ -class SegmentReader extends IndexReader { +public class SegmentReader extends IndexReader { private String segment; FieldInfos fieldInfos; @@ -85,25 +86,6 @@ private Hashtable norms = new Hashtable(); - /** The class which implements SegmentReader. */ - private static Class IMPL; - static { - try { - String name = - System.getProperty("org.apache.lucene.SegmentReader.class", - SegmentReader.class.getName()); - IMPL = Class.forName(name); - } catch (ClassNotFoundException e) { - throw new RuntimeException("cannot load SegmentReader class: " + e, e); - } catch (SecurityException se) { - try { - IMPL = Class.forName(SegmentReader.class.getName()); - } catch (ClassNotFoundException e) { - throw new RuntimeException("cannot load default SegmentReader class: " + e, e); - } - } - } - protected SegmentReader() { super(null); } public static SegmentReader get(SegmentInfo si) throws IOException { @@ -115,6 +97,11 @@ return get(si.dir, si, sis, closeDir, true); } + public static SegmentReader get(LuceneConfig lc, SegmentInfos sis, SegmentInfo si, + boolean closeDir) throws IOException { + return get(lc, si.dir, si, sis, closeDir, true); + } + public static SegmentReader get(Directory dir, SegmentInfo si, SegmentInfos sis, boolean closeDir, boolean ownDir) @@ -119,9 +106,16 @@ SegmentInfos sis, boolean closeDir, boolean ownDir) throws IOException { + return get(LuceneConfig.DEFAULT_INSTANCE, dir, si, sis, closeDir, ownDir); + } + + public static SegmentReader get(LuceneConfig lc, Directory dir, SegmentInfo si, + SegmentInfos sis, + boolean closeDir, boolean ownDir) + throws IOException { SegmentReader instance; try { - instance = (SegmentReader)IMPL.newInstance(); + instance = (SegmentReader)lc.getSegmentReaderClass().newInstance(); } catch (Exception e) { throw new RuntimeException("cannot load SegmentReader class: " + e, e); } Index: C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/index/SegmentTermDocs.java =================================================================== --- C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/index/SegmentTermDocs.java (revision 425666) +++ C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/index/SegmentTermDocs.java (working copy) @@ -20,7 +20,7 @@ import org.apache.lucene.util.BitVector; import org.apache.lucene.store.IndexInput; -class SegmentTermDocs implements TermDocs { +public class SegmentTermDocs implements TermDocs { protected SegmentReader parent; protected IndexInput freqStream; protected int count; @@ -26,8 +26,8 @@ protected int count; protected int df; protected BitVector deletedDocs; - int doc = 0; - int freq; + protected int doc = 0; + protected int freq; private int skipInterval; private int numSkips; Index: C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/search/IndexSearcher.java =================================================================== --- C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/search/IndexSearcher.java (revision 425666) +++ C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/search/IndexSearcher.java (working copy) @@ -19,6 +19,7 @@ import java.io.IOException; import java.util.BitSet; +import org.apache.lucene.LuceneConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexReader; @@ -42,6 +43,11 @@ this(IndexReader.open(path), true); } + /** Creates a searcher searching the index in the named directory. */ + public IndexSearcher(LuceneConfig lc, String path) throws IOException { + this(IndexReader.open(lc, path), true); + } + /** Creates a searcher searching the index in the provided directory. */ public IndexSearcher(Directory directory) throws IOException { this(IndexReader.open(directory), true); @@ -47,6 +53,11 @@ this(IndexReader.open(directory), true); } + /** Creates a searcher searching the index in the provided directory. */ + public IndexSearcher(LuceneConfig lc, Directory directory) throws IOException { + this(IndexReader.open(lc, directory), true); + } + /** Creates a searcher searching the provided index. */ public IndexSearcher(IndexReader r) { this(r, false); Index: C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/store/FSDirectory.java =================================================================== --- C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/store/FSDirectory.java (revision 425666) +++ C:/Home/jstuyts/dev/wsp/hippo/Lucene/src/java/org/apache/lucene/store/FSDirectory.java (working copy) @@ -25,6 +25,7 @@ import java.security.NoSuchAlgorithmException; import java.util.Hashtable; +import org.apache.lucene.LuceneConfig; import org.apache.lucene.index.IndexFileNameFilter; /** @@ -63,33 +64,6 @@ return FSDirectory.disableLocks; } - /** - * Directory specified by org.apache.lucene.lockDir - * or java.io.tmpdir system property - */ - public static final String LOCK_DIR = - System.getProperty("org.apache.lucene.lockDir", - System.getProperty("java.io.tmpdir")); - - /** The default class which implements filesystem-based directories. */ - private static Class IMPL; - static { - try { - String name = - System.getProperty("org.apache.lucene.FSDirectory.class", - FSDirectory.class.getName()); - IMPL = Class.forName(name); - } catch (ClassNotFoundException e) { - throw new RuntimeException("cannot load FSDirectory class: " + e.toString(), e); - } catch (SecurityException se) { - try { - IMPL = Class.forName(FSDirectory.class.getName()); - } catch (ClassNotFoundException e) { - throw new RuntimeException("cannot load default FSDirectory class: " + e.toString(), e); - } - } - } - private static MessageDigest DIGESTER; static { @@ -117,6 +91,11 @@ return getDirectory(new File(path), create); } + public static FSDirectory getDirectory(File file, boolean create) + throws IOException { + return getDirectory(LuceneConfig.DEFAULT_INSTANCE, file, create); + } + /** Returns the directory instance for the named location. * *

Directories are cached, so that, for a given canonical path, the same @@ -123,10 +102,11 @@ * FSDirectory instance will always be returned. This permits * synchronization on directories. * + * @param l the Lucene instance containing configuration information. * @param file the path to the directory. * @param create if true, create, or erase any existing contents. * @return the FSDirectory for the named file. */ - public static FSDirectory getDirectory(File file, boolean create) + public static FSDirectory getDirectory(LuceneConfig lc, File file, boolean create) throws IOException { file = new File(file.getCanonicalPath()); FSDirectory dir; @@ -134,11 +114,11 @@ dir = (FSDirectory)DIRECTORIES.get(file); if (dir == null) { try { - dir = (FSDirectory)IMPL.newInstance(); + dir = (FSDirectory)lc.getFSDirectoryClass().newInstance(); } catch (Exception e) { throw new RuntimeException("cannot load FSDirectory class: " + e.toString(), e); } - dir.init(file, create); + dir.init(lc, file, create); DIRECTORIES.put(file, dir); } else if (create) { dir.create(); @@ -156,14 +136,14 @@ protected FSDirectory() {}; // permit subclassing - private void init(File path, boolean create) throws IOException { + private void init(LuceneConfig lc, File path, boolean create) throws IOException { directory = path; - if (LOCK_DIR == null) { + if (lc.getLockDir() == null) { lockDir = directory; } else { - lockDir = new File(LOCK_DIR); + lockDir = new File(lc.getLockDir()); } // Ensure that lockDir exists and is a directory. if (!lockDir.exists()) {