Index: src/java/org/apache/lucene/store/FSDirectory.java =================================================================== --- src/java/org/apache/lucene/store/FSDirectory.java (revision 591752) +++ src/java/org/apache/lucene/store/FSDirectory.java (working copy) @@ -62,6 +62,8 @@ private static boolean disableLocks = false; + private boolean doSync = false; + // TODO: should this move up to the Directory base class? Also: should we // make a per-instance (in addition to the static "default") version? @@ -136,33 +138,67 @@ * @return the FSDirectory for the named file. */ public static FSDirectory getDirectory(String path) throws IOException { - return getDirectory(new File(path), null); + return getDirectory(new File(path), null, false); } /** Returns the directory instance for the named location. * @param path the path to the directory. * @param lockFactory instance of {@link LockFactory} providing the - * locking implementation. + * locking implementation. If null, the default + * {@link SimpleFSLockFactory} is used. * @return the FSDirectory for the named file. */ public static FSDirectory getDirectory(String path, LockFactory lockFactory) throws IOException { - return getDirectory(new File(path), lockFactory); + return getDirectory(new File(path), lockFactory, false); } /** Returns the directory instance for the named location. + * @param path the path to the directory. + * @param lockFactory instance of {@link LockFactory} providing the + * locking implementation. If null, the default + * {@link SimpleFSLockFactory} is used. + * @param doSync if true, sync() is called on all file + * descriptors before close(). This improves the + * likelihood that the index will remain consistent + * even if the JVM, OS or machine crashes. Note + * that this may slow down indexing. + * @return the FSDirectory for the named file. */ + public static FSDirectory getDirectory(String path, LockFactory lockFactory, boolean doSync) + throws IOException { + return getDirectory(new File(path), lockFactory, doSync); + } + + /** Returns the directory instance for the named location. * @param file the path to the directory. * @return the FSDirectory for the named file. */ public static FSDirectory getDirectory(File file) throws IOException { - return getDirectory(file, null); + return getDirectory(file, null, false); } /** Returns the directory instance for the named location. * @param file the path to the directory. * @param lockFactory instance of {@link LockFactory} providing the - * locking implementation. + * locking implementation. If null, the default + * {@link SimpleFSLockFactory} is used. * @return the FSDirectory for the named file. */ public static FSDirectory getDirectory(File file, LockFactory lockFactory) + throws IOException { + return getDirectory(file, lockFactory, false); + } + + /** Returns the directory instance for the named location. + * @param file the path to the directory. + * @param lockFactory instance of {@link LockFactory} providing the + * locking implementation. If null, the default + * {@link SimpleFSLockFactory} is used. + * @param doSync if true, sync() is called on all file + * descriptors before close(). This improves the + * likelihood that the index will remain consistent + * even if the JVM, OS or machine crashes. Note + * that this may slow down indexing. + * @return the FSDirectory for the named file. */ + public static FSDirectory getDirectory(File file, LockFactory lockFactory, boolean doSync) throws IOException { file = new File(file.getCanonicalPath()); @@ -183,7 +219,7 @@ } catch (Exception e) { throw new RuntimeException("cannot load FSDirectory class: " + e.toString(), e); } - dir.init(file, lockFactory); + dir.init(file, lockFactory, doSync); DIRECTORIES.put(file, dir); } else { // Catch the case where a Directory is pulled from the cache, but has a @@ -254,7 +290,7 @@ protected FSDirectory() {}; // permit subclassing - private void init(File path, LockFactory lockFactory) throws IOException { + private void init(File path, LockFactory lockFactory, boolean doSync) throws IOException { // Set up lockFactory with cascaded defaults: if an instance was passed in, // use that; else if locks are disabled, use NoLockFactory; else if the @@ -262,6 +298,7 @@ // instantiate that; else, use SimpleFSLockFactory: directory = path; + this.doSync = doSync; boolean doClearLockID = false; @@ -432,7 +469,7 @@ if (file.exists() && !file.delete()) // delete existing, if any throw new IOException("Cannot overwrite: " + file); - return new FSIndexOutput(file); + return new FSIndexOutput(file, doSync); } // Inherit javadoc @@ -588,10 +625,13 @@ // remember if the file is open, so that we don't try to close it // more than once private boolean isOpen; - - public FSIndexOutput(File path) throws IOException { + private boolean doSync; + + public FSIndexOutput(File path, boolean doSync) throws IOException { file = new RandomAccessFile(path, "rw"); + isOpen = true; + this.doSync = doSync; } /** output methods: */ @@ -602,6 +642,8 @@ // only close the file if it has not been closed yet if (isOpen) { super.close(); + if (doSync) + file.getFD().sync(); file.close(); isOpen = false; } Index: contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/PerfRunData.java =================================================================== --- contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/PerfRunData.java (revision 591752) +++ contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/PerfRunData.java (working copy) @@ -126,7 +126,8 @@ FileUtils.fullyDelete(indexDir); } indexDir.mkdirs(); - directory = FSDirectory.getDirectory(indexDir); + final boolean doSync = config.get("fsdirectory.dosync", false); + directory = FSDirectory.getDirectory(indexDir, null, doSync); } else { directory = new RAMDirectory(); } Index: contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/package.html =================================================================== --- contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/package.html (revision 591752) +++ contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/package.html (working copy) @@ -479,6 +479,12 @@