Index: src/java/org/apache/lucene/store/FSDirectory.java =================================================================== --- src/java/org/apache/lucene/store/FSDirectory.java (revision 780453) +++ src/java/org/apache/lucene/store/FSDirectory.java (working copy) @@ -147,16 +147,17 @@ try { String name = System.getProperty("org.apache.lucene.FSDirectory.class", - FSDirectory.class.getName()); - IMPL = Class.forName(name); + SimpleFSDirectory.class.getName()); + if (FSDirectory.class.getName().equals(name)) { + // FSDirectory will be abstract, so we replace it by the coreect class + IMPL = SimpleFSDirectory.class; + } else { + 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); - } + IMPL = SimpleFSDirectory.class; } } @@ -698,144 +699,37 @@ return this.getClass().getName() + "@" + directory; } + /** @deprecated Use SimpleFSDirectory.SimpleFSIndexInput instead */ - protected static class FSIndexInput extends BufferedIndexInput { + protected static class FSIndexInput extends SimpleFSDirectory.SimpleFSIndexInput { - protected static class Descriptor extends RandomAccessFile { - // remember if the file is open, so that we don't try to close it - // more than once - protected volatile boolean isOpen; - long position; - final long length; - + /** @deprecated */ + protected static class Descriptor extends SimpleFSDirectory.SimpleFSIndexInput.Descriptor { + /** @deprecated */ public Descriptor(File file, String mode) throws IOException { super(file, mode); - isOpen=true; - length=length(); } - - public void close() throws IOException { - if (isOpen) { - isOpen=false; - super.close(); - } - } - - protected void finalize() throws Throwable { - try { - close(); - } finally { - super.finalize(); - } - } } - protected final Descriptor file; - boolean isClone; - + /** @deprecated */ public FSIndexInput(File path) throws IOException { - this(path, BufferedIndexInput.BUFFER_SIZE); + super(path); } + /** @deprecated */ public FSIndexInput(File path, int bufferSize) throws IOException { - super(bufferSize); - file = new Descriptor(path, "r"); + super(path, bufferSize); } - /** IndexInput methods */ - protected void readInternal(byte[] b, int offset, int len) - throws IOException { - synchronized (file) { - long position = getFilePointer(); - if (position != file.position) { - file.seek(position); - file.position = position; - } - int total = 0; - do { - int i = file.read(b, offset+total, len-total); - if (i == -1) - throw new IOException("read past EOF"); - file.position += i; - total += i; - } while (total < len); - } - } - - public void close() throws IOException { - // only close the file if this is not a clone - if (!isClone) file.close(); - } - - protected void seekInternal(long position) { - } - - public long length() { - return file.length; - } - - public Object clone() { - FSIndexInput clone = (FSIndexInput)super.clone(); - clone.isClone = true; - return clone; - } - - /** Method used for testing. Returns true if the underlying - * file descriptor is valid. - */ - boolean isFDValid() throws IOException { - return file.getFD().valid(); - } } /** @deprecated Use SimpleFSDirectory.SimpleFSIndexOutput instead */ - protected static class FSIndexOutput extends BufferedIndexOutput { - RandomAccessFile file = null; - - // remember if the file is open, so that we don't try to close it - // more than once - private volatile boolean isOpen; + protected static class FSIndexOutput extends SimpleFSDirectory.SimpleFSIndexOutput { + /** @deprecated */ public FSIndexOutput(File path) throws IOException { - file = new RandomAccessFile(path, "rw"); - isOpen = true; + super(path); } - - /** output methods: */ - public void flushBuffer(byte[] b, int offset, int size) throws IOException { - file.write(b, offset, size); - } - public void close() throws IOException { - // only close the file if it has not been closed yet - if (isOpen) { - boolean success = false; - try { - super.close(); - success = true; - } finally { - isOpen = false; - if (!success) { - try { - file.close(); - } catch (Throwable t) { - // Suppress so we don't mask original exception - } - } else - file.close(); - } - } - } - - /** Random-access methods */ - public void seek(long pos) throws IOException { - super.seek(pos); - file.seek(pos); - } - public long length() throws IOException { - return file.length(); - } - public void setLength(long length) throws IOException { - file.setLength(length); - } + } } Index: src/java/org/apache/lucene/store/MMapDirectory.java =================================================================== --- src/java/org/apache/lucene/store/MMapDirectory.java (revision 780453) +++ src/java/org/apache/lucene/store/MMapDirectory.java (working copy) @@ -125,7 +125,7 @@ + raf.toString()); int nrBuffers = (int) (length / maxBufSize); - if ((nrBuffers * maxBufSize) < length) nrBuffers++; + if (((long) nrBuffers * maxBufSize) < length) nrBuffers++; this.buffers = new ByteBuffer[nrBuffers]; this.bufSizes = new int[nrBuffers]; @@ -171,13 +171,13 @@ } public long getFilePointer() { - return (curBufIndex * (long) maxBufSize) + curBuf.position(); + return ((long) curBufIndex * (long) maxBufSize) + curBuf.position(); } public void seek(long pos) throws IOException { curBufIndex = (int) (pos / maxBufSize); curBuf = buffers[curBufIndex]; - int bufOffset = (int) (pos - (curBufIndex * maxBufSize)); + int bufOffset = (int) (pos - ((long) curBufIndex * maxBufSize)); curBuf.position(bufOffset); curAvail = bufSizes[curBufIndex] - bufOffset; } Index: src/java/org/apache/lucene/store/NIOFSDirectory.java =================================================================== --- src/java/org/apache/lucene/store/NIOFSDirectory.java (revision 780453) +++ src/java/org/apache/lucene/store/NIOFSDirectory.java (working copy) @@ -66,7 +66,7 @@ return new SimpleFSDirectory.SimpleFSIndexOutput(new File(directory, name)); } - private static class NIOFSIndexInput extends FSDirectory.FSIndexInput { + private static class NIOFSIndexInput extends SimpleFSDirectory.SimpleFSIndexInput { private ByteBuffer byteBuf; // wraps the buffer for NIO Index: src/java/org/apache/lucene/store/SimpleFSDirectory.java =================================================================== --- src/java/org/apache/lucene/store/SimpleFSDirectory.java (revision 780453) +++ src/java/org/apache/lucene/store/SimpleFSDirectory.java (working copy) @@ -38,6 +38,8 @@ public SimpleFSDirectory(File path, LockFactory lockFactory) throws IOException { super(path, lockFactory); } + + SimpleFSDirectory() throws IOException {} // Inherit javadoc public IndexOutput createOutput(String name) throws IOException { Index: src/test/org/apache/lucene/index/TestCompoundFile.java =================================================================== --- src/test/org/apache/lucene/index/TestCompoundFile.java (revision 780453) +++ src/test/org/apache/lucene/index/TestCompoundFile.java (working copy) @@ -26,7 +26,7 @@ import org.apache.lucene.store.IndexOutput; import org.apache.lucene.store.Directory; import org.apache.lucene.store.IndexInput; -import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.SimpleFSDirectory; import org.apache.lucene.store._TestHelper; import org.apache.lucene.util._TestUtil; @@ -62,9 +62,14 @@ super.setUp(); File file = new File(System.getProperty("tempDir"), "testIndex"); _TestUtil.rmDir(file); - dir = FSDirectory.open(file); + // use a simple FSDir here, to be sure to have SimpleFSInputs + dir = new SimpleFSDirectory(file,null); } + public void tearDown() throws Exception { + dir.close(); + super.tearDown(); + } /** Creates a file of the specified size with random data. */ private void createRandomFile(Directory dir, String name, int size) @@ -310,10 +315,10 @@ public void testReadAfterClose() throws IOException { - demo_FSIndexInputBug((FSDirectory) dir, "test"); + demo_FSIndexInputBug(dir, "test"); } - private void demo_FSIndexInputBug(FSDirectory fsdir, String file) + private void demo_FSIndexInputBug(Directory fsdir, String file) throws IOException { // Setup the test file - we need more than 1024 bytes @@ -358,7 +363,7 @@ CompoundFileReader.CSIndexInput cis = (CompoundFileReader.CSIndexInput) is; - return _TestHelper.isFSIndexInputOpen(cis.base); + return _TestHelper.isSimpleFSIndexInputOpen(cis.base); } else { return false; } @@ -373,50 +378,48 @@ IndexInput expected = dir.openInput("f11"); // this test only works for FSIndexInput - if (_TestHelper.isFSIndexInput(expected)) { + assertTrue(_TestHelper.isSimpleFSIndexInput(expected)); + assertTrue(_TestHelper.isSimpleFSIndexInputOpen(expected)); - assertTrue(_TestHelper.isFSIndexInputOpen(expected)); + IndexInput one = cr.openInput("f11"); + assertTrue(isCSIndexInputOpen(one)); - IndexInput one = cr.openInput("f11"); - assertTrue(isCSIndexInputOpen(one)); + IndexInput two = (IndexInput) one.clone(); + assertTrue(isCSIndexInputOpen(two)); - IndexInput two = (IndexInput) one.clone(); - assertTrue(isCSIndexInputOpen(two)); + assertSameStreams("basic clone one", expected, one); + expected.seek(0); + assertSameStreams("basic clone two", expected, two); - assertSameStreams("basic clone one", expected, one); - expected.seek(0); - assertSameStreams("basic clone two", expected, two); + // Now close the first stream + one.close(); + assertTrue("Only close when cr is closed", isCSIndexInputOpen(one)); - // Now close the first stream - one.close(); - assertTrue("Only close when cr is closed", isCSIndexInputOpen(one)); + // The following should really fail since we couldn't expect to + // access a file once close has been called on it (regardless of + // buffering and/or clone magic) + expected.seek(0); + two.seek(0); + assertSameStreams("basic clone two/2", expected, two); - // The following should really fail since we couldn't expect to - // access a file once close has been called on it (regardless of - // buffering and/or clone magic) - expected.seek(0); - two.seek(0); - assertSameStreams("basic clone two/2", expected, two); + // Now close the compound reader + cr.close(); + assertFalse("Now closed one", isCSIndexInputOpen(one)); + assertFalse("Now closed two", isCSIndexInputOpen(two)); - // Now close the compound reader - cr.close(); - assertFalse("Now closed one", isCSIndexInputOpen(one)); - assertFalse("Now closed two", isCSIndexInputOpen(two)); + // The following may also fail since the compound stream is closed + expected.seek(0); + two.seek(0); + //assertSameStreams("basic clone two/3", expected, two); - // The following may also fail since the compound stream is closed - expected.seek(0); - two.seek(0); - //assertSameStreams("basic clone two/3", expected, two); + // Now close the second clone + two.close(); + expected.seek(0); + two.seek(0); + //assertSameStreams("basic clone two/4", expected, two); - // Now close the second clone - two.close(); - expected.seek(0); - two.seek(0); - //assertSameStreams("basic clone two/4", expected, two); - } - expected.close(); } Index: src/test/org/apache/lucene/index/TestDoc.java =================================================================== --- src/test/org/apache/lucene/index/TestDoc.java (revision 780453) +++ src/test/org/apache/lucene/index/TestDoc.java (working copy) @@ -59,7 +59,7 @@ indexDir = new File(workDir, "testIndex"); indexDir.mkdirs(); - Directory directory = FSDirectory.getDirectory(indexDir, true); + Directory directory = FSDirectory.open(indexDir); directory.close(); files = new LinkedList(); Index: src/test/org/apache/lucene/index/TestIndexModifier.java =================================================================== --- src/test/org/apache/lucene/index/TestIndexModifier.java (revision 780453) +++ src/test/org/apache/lucene/index/TestIndexModifier.java (working copy) @@ -144,7 +144,7 @@ if (tempDir == null) throw new IOException("java.io.tmpdir undefined, cannot run test"); File indexDir = new File(tempDir, "lucenetestindex"); - Directory rd = FSDirectory.getDirectory(indexDir); + Directory rd = FSDirectory.open(indexDir); IndexThread.id = 0; IndexThread.idStack.clear(); IndexModifier index = new IndexModifier(rd, new StandardAnalyzer(), create); Index: src/test/org/apache/lucene/index/TestIndexWriter.java =================================================================== --- src/test/org/apache/lucene/index/TestIndexWriter.java (revision 780453) +++ src/test/org/apache/lucene/index/TestIndexWriter.java (working copy) @@ -4207,7 +4207,7 @@ public void testOtherFiles() throws Throwable { File indexDir = new File(System.getProperty("tempDir"), "otherfiles"); - Directory dir = new FSDirectory(indexDir, null); + Directory dir = FSDirectory.open(indexDir); try { // Create my own random file: Index: src/test/org/apache/lucene/index/TestStressIndexing2.java =================================================================== --- src/test/org/apache/lucene/index/TestStressIndexing2.java (revision 780453) +++ src/test/org/apache/lucene/index/TestStressIndexing2.java (working copy) @@ -69,7 +69,7 @@ public void testRandom() throws Throwable { r = newRandom(); Directory dir1 = new MockRAMDirectory(); - // dir1 = FSDirectory.getDirectory("foofoofoo"); + // dir1 = FSDirectory.open("foofoofoo"); Directory dir2 = new MockRAMDirectory(); // mergeFactor=2; maxBufferedDocs=2; Map docs = indexRandom(1, 3, 2, dir1); Map docs = indexRandom(10, 100, 100, dir1); Index: src/test/org/apache/lucene/store/_TestHelper.java =================================================================== --- src/test/org/apache/lucene/store/_TestHelper.java (revision 780453) +++ src/test/org/apache/lucene/store/_TestHelper.java (working copy) @@ -19,7 +19,7 @@ import java.io.IOException; -import org.apache.lucene.store.FSDirectory.FSIndexInput; +import org.apache.lucene.store.SimpleFSDirectory.SimpleFSIndexInput; /** This class provides access to package-level features defined in the * store package. It is used for testing only. @@ -27,35 +27,35 @@ public class _TestHelper { /** Returns true if the instance of the provided input stream is actually - * an FSIndexInput. + * an SimpleFSIndexInput. */ - public static boolean isFSIndexInput(IndexInput is) { - return is instanceof FSIndexInput; + public static boolean isSimpleFSIndexInput(IndexInput is) { + return is instanceof SimpleFSIndexInput; } - /** Returns true if the provided input stream is an FSIndexInput and + /** Returns true if the provided input stream is an SimpleFSIndexInput and * is a clone, that is it does not own its underlying file descriptor. */ - public static boolean isFSIndexInputClone(IndexInput is) { - if (isFSIndexInput(is)) { - return ((FSIndexInput) is).isClone; + public static boolean isSimpleFSIndexInputClone(IndexInput is) { + if (isSimpleFSIndexInput(is)) { + return ((SimpleFSIndexInput) is).isClone; } else { return false; } } - /** Given an instance of FSDirectory.FSIndexInput, this method returns + /** Given an instance of SimpleFSDirectory.SimpleFSIndexInput, this method returns * true if the underlying file descriptor is valid, and false otherwise. * This can be used to determine if the OS file has been closed. * The descriptor becomes invalid when the non-clone instance of the - * FSIndexInput that owns this descriptor is closed. However, the + * SimpleFSIndexInput that owns this descriptor is closed. However, the * descriptor may possibly become invalid in other ways as well. */ - public static boolean isFSIndexInputOpen(IndexInput is) + public static boolean isSimpleFSIndexInputOpen(IndexInput is) throws IOException { - if (isFSIndexInput(is)) { - FSIndexInput fis = (FSIndexInput) is; + if (isSimpleFSIndexInput(is)) { + SimpleFSIndexInput fis = (SimpleFSIndexInput) is; return fis.isFDValid(); } else { return false; Index: src/test/org/apache/lucene/store/TestDirectory.java =================================================================== --- src/test/org/apache/lucene/store/TestDirectory.java (revision 780453) +++ src/test/org/apache/lucene/store/TestDirectory.java (working copy) @@ -53,7 +53,7 @@ int sz = 3; Directory[] dirs = new Directory[sz]; - dirs[0] = new FSDirectory(path, null); + dirs[0] = new SimpleFSDirectory(path, null); dirs[1] = new NIOFSDirectory(path, null); dirs[2] = new MMapDirectory(path, null); @@ -123,7 +123,7 @@ File path = new File(System.getProperty("tempDir"), "doesnotexist"); try { assertTrue(!path.exists()); - Directory dir = new FSDirectory(path, null); + Directory dir = new SimpleFSDirectory(path, null); assertTrue(!path.exists()); dir.close(); } finally { @@ -159,7 +159,7 @@ try { path.mkdirs(); new File(path, "subdir").mkdirs(); - Directory fsDir = new FSDirectory(path, null); + Directory fsDir = new SimpleFSDirectory(path, null); assertEquals(0, new RAMDirectory(fsDir).listAll().length); } finally { _TestUtil.rmDir(path); @@ -169,13 +169,13 @@ // LUCENE-1468 public void testNotDirectory() throws Throwable { File path = new File(System.getProperty("tempDir"), "testnotdir"); - Directory fsDir = new FSDirectory(path, null); + Directory fsDir = new SimpleFSDirectory(path, null); try { IndexOutput out = fsDir.createOutput("afile"); out.close(); assertTrue(fsDir.fileExists("afile")); try { - new FSDirectory(new File(path, "afile"), null); + new SimpleFSDirectory(new File(path, "afile"), null); fail("did not hit expected exception"); } catch (NoSuchDirectoryException nsde) { // Expected