init CFS output on writer init and release output lock if stream creation fails.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1144841 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2011-07-10 12:02:38 +00:00
parent 7885258d13
commit c5636fac7d
2 changed files with 19 additions and 20 deletions

View File

@ -74,7 +74,7 @@ public abstract class CompoundFileDirectory extends Directory {
this.openForWrite = false; this.openForWrite = false;
} }
protected final void initForWrite() { protected final void initForWrite() throws IOException {
assert !(directory instanceof CompoundFileDirectory) : "compound file inside of compound file: " + fileName; assert !(directory instanceof CompoundFileDirectory) : "compound file inside of compound file: " + fileName;
this.entries = SENTINEL; this.entries = SENTINEL;
this.openForWrite = true; this.openForWrite = true;

View File

@ -100,7 +100,7 @@ final class CompoundFileWriter implements Closeable{
* @throws NullPointerException * @throws NullPointerException
* if <code>dir</code> or <code>name</code> is null * if <code>dir</code> or <code>name</code> is null
*/ */
CompoundFileWriter(Directory dir, String name) { CompoundFileWriter(Directory dir, String name) throws IOException {
if (dir == null) if (dir == null)
throw new NullPointerException("directory cannot be null"); throw new NullPointerException("directory cannot be null");
if (name == null) if (name == null)
@ -110,6 +110,16 @@ final class CompoundFileWriter implements Closeable{
IndexFileNames.stripExtension(name), "", IndexFileNames.stripExtension(name), "",
IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION); IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION);
dataFileName = name; dataFileName = name;
boolean success = false;
try {
dataOut = directory.createOutput(dataFileName, IOContext.DEFAULT);
dataOut.writeVInt(FORMAT_CURRENT);
success = true;
} finally {
if (!success) {
IOUtils.closeSafely(true, dataOut);
}
}
} }
/** Returns the directory of the compound file. */ /** Returns the directory of the compound file. */
@ -136,7 +146,6 @@ final class CompoundFileWriter implements Closeable{
IOException priorException = null; IOException priorException = null;
IndexOutput entryTableOut = null; IndexOutput entryTableOut = null;
try { try {
initDataOut(IOContext.DEFAULT);
if (!pendingEntries.isEmpty() || outputTaken.get()) { if (!pendingEntries.isEmpty() || outputTaken.get()) {
throw new IllegalStateException("CFS has pending open files"); throw new IllegalStateException("CFS has pending open files");
} }
@ -215,6 +224,7 @@ final class CompoundFileWriter implements Closeable{
IndexOutput createOutput(String name, IOContext context) throws IOException { IndexOutput createOutput(String name, IOContext context) throws IOException {
ensureOpen(); ensureOpen();
boolean success = false; boolean success = false;
boolean outputLocked = false;
try { try {
assert name != null : "name must not be null"; assert name != null : "name must not be null";
if (entries.containsKey(name)) { if (entries.containsKey(name)) {
@ -225,9 +235,9 @@ final class CompoundFileWriter implements Closeable{
entries.put(name, entry); entries.put(name, entry);
final DirectCFSIndexOutput out; final DirectCFSIndexOutput out;
if (outputTaken.compareAndSet(false, true)) { if (outputTaken.compareAndSet(false, true)) {
initDataOut(context);
success = true;
out = new DirectCFSIndexOutput(dataOut, entry, false); out = new DirectCFSIndexOutput(dataOut, entry, false);
outputLocked = true;
success = true;
} else { } else {
entry.dir = this.directory; entry.dir = this.directory;
if (directory.fileExists(name)) { if (directory.fileExists(name)) {
@ -241,6 +251,10 @@ final class CompoundFileWriter implements Closeable{
} finally { } finally {
if (!success) { if (!success) {
entries.remove(name); entries.remove(name);
if (outputLocked) { // release the output lock if not successful
assert outputTaken.get();
releaseOutputLock();
}
} }
} }
} }
@ -249,21 +263,6 @@ final class CompoundFileWriter implements Closeable{
outputTaken.compareAndSet(true, false); outputTaken.compareAndSet(true, false);
} }
private synchronized final void initDataOut(IOContext context) throws IOException {
if (dataOut == null) {
boolean success = false;
try {
dataOut = directory.createOutput(dataFileName, context);
dataOut.writeVInt(FORMAT_CURRENT);
success = true;
} finally {
if (!success) {
IOUtils.closeSafely(true, dataOut);
}
}
}
}
private final void prunePendingEntries() throws IOException { private final void prunePendingEntries() throws IOException {
// claim the output and copy all pending files in // claim the output and copy all pending files in
if (outputTaken.compareAndSet(false, true)) { if (outputTaken.compareAndSet(false, true)) {