Index: src/java/org/apache/lucene/index/CompoundFileWriter.java =================================================================== --- src/java/org/apache/lucene/index/CompoundFileWriter.java (revision 613824) +++ src/java/org/apache/lucene/index/CompoundFileWriter.java (working copy) @@ -156,13 +156,23 @@ // Remember the positions of directory entries so that we can // adjust the offsets later Iterator it = entries.iterator(); + long totalSize = 0; while(it.hasNext()) { FileEntry fe = (FileEntry) it.next(); fe.directoryOffset = os.getFilePointer(); os.writeLong(0); // for now os.writeString(fe.file); + totalSize += directory.fileLength(fe.file); } + // Pre-allocate size of file as optimization -- + // this can potentially help IO performance as + // we write the file and also later during + // searching. It also uncovers a disk-full + // situation earlier and hopefully without + // actually filling disk to 100%: + os.setLength(totalSize+os.getFilePointer()); + // Open the files and copy their data into the stream. // Remember the locations of each file's data section. byte buffer[] = new byte[16384]; Index: src/java/org/apache/lucene/store/IndexOutput.java =================================================================== --- src/java/org/apache/lucene/store/IndexOutput.java (revision 613824) +++ src/java/org/apache/lucene/store/IndexOutput.java (working copy) @@ -189,5 +189,15 @@ /** The number of bytes in the file. */ public abstract long length() throws IOException; - + /** Set the file length. By default, this method does + * nothing (it's optional for a Directory to implement + * it). But, certain Directory implementations (for + * example @see FSDirectory) can use this to inform the + * underlying IO system to pre-allocate the file to the + * specified size. If the length is longer than the + * current file length, the bytes added to the file are + * undefined. Otherwise the file is truncated. + * @param length file length + */ + public void setLength(long length) throws IOException {}; } Index: src/java/org/apache/lucene/store/FSDirectory.java =================================================================== --- src/java/org/apache/lucene/store/FSDirectory.java (revision 613824) +++ src/java/org/apache/lucene/store/FSDirectory.java (working copy) @@ -615,6 +615,8 @@ public long length() throws IOException { return file.length(); } - + public void setLength(long length) throws IOException { + file.setLength(length); + } } }