From 0979fdc423adeea90e5314c90d5cf8f624727f50 Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Fri, 14 Nov 2008 19:22:22 +0000 Subject: [PATCH] LUCENE-1451: Add public constructors to FSDirectory and subclasses git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@714110 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 4 + .../org/apache/lucene/store/FSDirectory.java | 50 ++++++++++-- .../apache/lucene/store/MMapDirectory.java | 13 ++++ .../apache/lucene/store/NIOFSDirectory.java | 14 ++++ .../apache/lucene/store/TestDirectory.java | 77 +++++++++++++++++++ 5 files changed, 150 insertions(+), 8 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 832b4e464ea..d7ec63eb3a7 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -21,6 +21,10 @@ API Changes 2. LUCENE-1427: DocIdSet.iterator() is now allowed to throw IOException. (Paul Elschot, Mike McCandless) +3. LUCENE-1451: Add public constructors to FSDirectory and subclasses, + and deprecate FSDirectory.getDirectory(). FSDirectory instances + are not required to be singletons per path. (yonik) + Bug fixes 1. LUCENE-1415: MultiPhraseQuery has incorrect hashCode() and equals() diff --git a/src/java/org/apache/lucene/store/FSDirectory.java b/src/java/org/apache/lucene/store/FSDirectory.java index 5f2f5d588bd..3d8c4ddfae2 100644 --- a/src/java/org/apache/lucene/store/FSDirectory.java +++ b/src/java/org/apache/lucene/store/FSDirectory.java @@ -131,7 +131,11 @@ public class FSDirectory extends Directory { /** A buffer optionally used in renameTo method */ private byte[] buffer = null; + /** Returns the directory instance for the named location. + * + * @deprecated Use {@link #FSDirectory(File, LockFactory)} + * * @param path the path to the directory. * @return the FSDirectory for the named file. */ public static FSDirectory getDirectory(String path) @@ -140,6 +144,9 @@ public class FSDirectory extends Directory { } /** Returns the directory instance for the named location. + * + * @deprecated Use {@link #FSDirectory(File, LockFactory)} + * * @param path the path to the directory. * @param lockFactory instance of {@link LockFactory} providing the * locking implementation. @@ -150,6 +157,9 @@ public class FSDirectory extends Directory { } /** Returns the directory instance for the named location. + * + * @deprecated Use {@link #FSDirectory(File, LockFactory)} + * * @param file the path to the directory. * @return the FSDirectory for the named file. */ public static FSDirectory getDirectory(File file) @@ -158,6 +168,9 @@ public class FSDirectory extends Directory { } /** Returns the directory instance for the named location. + * + * @deprecated Use {@link #FSDirectory(File, LockFactory)} + * * @param file the path to the directory. * @param lockFactory instance of {@link LockFactory} providing the * locking implementation. @@ -165,14 +178,7 @@ public class FSDirectory extends Directory { public static FSDirectory getDirectory(File file, LockFactory lockFactory) throws IOException { - file = new File(file.getCanonicalPath()); - - if (file.exists() && !file.isDirectory()) - throw new IOException(file + " not a directory"); - - if (!file.exists()) - if (!file.mkdirs()) - throw new IOException("Cannot create directory: " + file); + file = createCanonicalDir(file); FSDirectory dir; synchronized (DIRECTORIES) { @@ -249,11 +255,39 @@ public class FSDirectory extends Directory { lockFactory.clearLock(IndexWriter.WRITE_LOCK_NAME); } + // returns the canonical version of the directory, creating it if it doesn't exist. + private static File createCanonicalDir(File file) throws IOException { + file = new File(file.getCanonicalPath()); + + if (file.exists() && !file.isDirectory()) + throw new IOException(file + " not a directory"); + + if (!file.exists()) + if (!file.mkdirs()) + throw new IOException("Cannot create directory: " + file); + + return file; + } + private File directory = null; private int refCount; protected FSDirectory() {}; // permit subclassing + /** Create a new FSDirectory for the named location. + * + * @param path the path of the directory + * @param lockFactory the lock factory to use, or null for the default. + * @throws IOException + * + * Use {@link #getDirectory(String)} if singletons per path are needed. + */ + public FSDirectory(File path, LockFactory lockFactory) throws IOException { + path = createCanonicalDir(path); + init(path, lockFactory); + refCount = 1; + } + private void init(File path, LockFactory lockFactory) throws IOException { // Set up lockFactory with cascaded defaults: if an instance was passed in, diff --git a/src/java/org/apache/lucene/store/MMapDirectory.java b/src/java/org/apache/lucene/store/MMapDirectory.java index ec7b1dc3e54..78e43b033f1 100644 --- a/src/java/org/apache/lucene/store/MMapDirectory.java +++ b/src/java/org/apache/lucene/store/MMapDirectory.java @@ -33,6 +33,19 @@ import java.nio.channels.FileChannel.MapMode; */ public class MMapDirectory extends FSDirectory { + /** Create a new MMapDirectory for the named location. + * @param path the path of the directory + * @param lockFactory the lock factory to use, or null for the default. + * @throws IOException + */ + public MMapDirectory(File path, LockFactory lockFactory) throws IOException { + super(path, lockFactory); + } + + // back compatibility so FSDirectory can instantiate via reflection + protected MMapDirectory() throws IOException { + } + private static class MMapIndexInput extends IndexInput { private ByteBuffer buffer; diff --git a/src/java/org/apache/lucene/store/NIOFSDirectory.java b/src/java/org/apache/lucene/store/NIOFSDirectory.java index ca47e914d53..b8eef46b7fb 100644 --- a/src/java/org/apache/lucene/store/NIOFSDirectory.java +++ b/src/java/org/apache/lucene/store/NIOFSDirectory.java @@ -43,6 +43,20 @@ import java.nio.channels.FileChannel; public class NIOFSDirectory extends FSDirectory { + /** Create a new NIOFSDirectory for the named location. + * + * @param path the path of the directory + * @param lockFactory the lock factory to use, or null for the default. + * @throws IOException + */ + public NIOFSDirectory(File path, LockFactory lockFactory) throws IOException { + super(path, lockFactory); + } + + // back compatibility so FSDirectory can instantiate via reflection + protected NIOFSDirectory() throws IOException { + } + // Inherit javadoc public IndexInput openInput(String name, int bufferSize) throws IOException { ensureOpen(); diff --git a/src/test/org/apache/lucene/store/TestDirectory.java b/src/test/org/apache/lucene/store/TestDirectory.java index 6472c52b902..71a180e68bc 100644 --- a/src/test/org/apache/lucene/store/TestDirectory.java +++ b/src/test/org/apache/lucene/store/TestDirectory.java @@ -19,6 +19,8 @@ package org.apache.lucene.store; import org.apache.lucene.util.LuceneTestCase; +import java.io.File; + public class TestDirectory extends LuceneTestCase { public void testDetectClose() throws Throwable { @@ -38,5 +40,80 @@ public class TestDirectory extends LuceneTestCase { } catch (AlreadyClosedException ace) { } } + + + // Test that different instances of FSDirectory can coexist on the same + // path, can read, write, and lock files. + public void testDirectInstantiation() throws Exception { + File path = new File(System.getProperty("tempDir")); + + int sz = 3; + Directory[] dirs = new Directory[sz]; + + dirs[0] = new FSDirectory(path, null); + dirs[1] = new NIOFSDirectory(path, null); + dirs[2] = new MMapDirectory(path, null); + + for (int i=0; i