make sure we close all in/output in the case of an error

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/docvalues@1129640 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2011-05-31 12:22:18 +00:00
parent c51bbaeda0
commit 7190c9ed30
3 changed files with 56 additions and 24 deletions

View File

@ -35,6 +35,7 @@ import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.ByteBlockPool; import org.apache.lucene.util.ByteBlockPool;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.CodecUtil; import org.apache.lucene.util.CodecUtil;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.PagedBytes; import org.apache.lucene.util.PagedBytes;
/** /**
@ -353,16 +354,23 @@ public final class Bytes {
super(bytesUsed); super(bytesUsed);
this.id = id; this.id = id;
this.pool = pool; this.pool = pool;
datOut = dir.createOutput(IndexFileNames.segmentFileName(id, "", datOut = dir.createOutput(IndexFileNames.segmentFileName(id, "",
DATA_EXTENSION)); DATA_EXTENSION));
boolean success = false;
try {
CodecUtil.writeHeader(datOut, codecName, version); CodecUtil.writeHeader(datOut, codecName, version);
if (initIndex) {
if (initIndex) { idxOut = dir.createOutput(IndexFileNames.segmentFileName(id, "",
idxOut = dir.createOutput(IndexFileNames.segmentFileName(id, "", INDEX_EXTENSION));
INDEX_EXTENSION)); CodecUtil.writeHeader(idxOut, codecName, version);
CodecUtil.writeHeader(idxOut, codecName, version); } else {
} else { idxOut = null;
idxOut = null; }
success = true;
} finally {
if (!success) {
IOUtils.closeSafely(true, datOut, idxOut);
}
} }
} }
@ -376,14 +384,10 @@ public final class Bytes {
@Override @Override
public void finish(int docCount) throws IOException { public void finish(int docCount) throws IOException {
try { try {
datOut.close(); IOUtils.closeSafely(false, datOut, idxOut);
} finally { } finally {
try { if (pool != null) {
if (idxOut != null) pool.reset();
idxOut.close();
} finally {
if (pool != null)
pool.reset();
} }
} }
} }

View File

@ -29,6 +29,7 @@ import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.AttributeSource; import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.CodecUtil; import org.apache.lucene.util.CodecUtil;
import org.apache.lucene.util.FloatsRef; import org.apache.lucene.util.FloatsRef;
import org.apache.lucene.util.IOUtils;
/** /**
* Exposes {@link Writer} and reader ({@link Source}) for 32 bit and 64 bit * Exposes {@link Writer} and reader ({@link Source}) for 32 bit and 64 bit
@ -82,9 +83,17 @@ public class Floats {
this.precision = (byte) precision; this.precision = (byte) precision;
datOut = dir.createOutput(IndexFileNames.segmentFileName(id, "", datOut = dir.createOutput(IndexFileNames.segmentFileName(id, "",
Writer.DATA_EXTENSION)); Writer.DATA_EXTENSION));
CodecUtil.writeHeader(datOut, CODEC_NAME, VERSION_CURRENT); boolean success = false;
assert datOut.getFilePointer() == CodecUtil.headerLength(CODEC_NAME); try {
datOut.writeByte(this.precision); CodecUtil.writeHeader(datOut, CODEC_NAME, VERSION_CURRENT);
assert datOut.getFilePointer() == CodecUtil.headerLength(CODEC_NAME);
datOut.writeByte(this.precision);
success = true;
} finally {
if (!success) {
IOUtils.closeSafely(true, datOut);
}
}
} }

View File

@ -27,6 +27,7 @@ import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.AttributeSource; import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.CodecUtil; import org.apache.lucene.util.CodecUtil;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LongsRef; import org.apache.lucene.util.LongsRef;
import org.apache.lucene.util.OpenBitSet; import org.apache.lucene.util.OpenBitSet;
import org.apache.lucene.util.RamUsageEstimator; import org.apache.lucene.util.RamUsageEstimator;
@ -62,11 +63,21 @@ class PackedIntsImpl {
super(bytesUsed); super(bytesUsed);
datOut = dir.createOutput(IndexFileNames.segmentFileName(id, "", datOut = dir.createOutput(IndexFileNames.segmentFileName(id, "",
DATA_EXTENSION)); DATA_EXTENSION));
CodecUtil.writeHeader(datOut, CODEC_NAME, VERSION_CURRENT); boolean success = false;
this.id = id; try {
docToValue = new long[1]; CodecUtil.writeHeader(datOut, CODEC_NAME, VERSION_CURRENT);
bytesUsed.addAndGet(RamUsageEstimator.NUM_BYTES_LONG); // TODO the bitset this.id = id;
// needs memory too docToValue = new long[1];
bytesUsed.addAndGet(RamUsageEstimator.NUM_BYTES_LONG); // TODO the
// bitset
// needs memory
// too
success = true;
} finally {
if (!success) {
datOut.close();
}
}
} }
@Override @Override
@ -168,7 +179,15 @@ class PackedIntsImpl {
protected IntsReader(Directory dir, String id) throws IOException { protected IntsReader(Directory dir, String id) throws IOException {
datIn = dir.openInput(IndexFileNames.segmentFileName(id, "", datIn = dir.openInput(IndexFileNames.segmentFileName(id, "",
Writer.DATA_EXTENSION)); Writer.DATA_EXTENSION));
CodecUtil.checkHeader(datIn, CODEC_NAME, VERSION_START, VERSION_START); boolean success = false;
try {
CodecUtil.checkHeader(datIn, CODEC_NAME, VERSION_START, VERSION_START);
success = true;
} finally {
if (!success) {
IOUtils.closeSafely(true, datIn);
}
}
} }
/** /**