From 287fab46a1dae1c082a93db265b753c61e801a20 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Fri, 13 Feb 2015 13:42:11 +0100 Subject: [PATCH] [STORE] use Directory#fileLength() less during calculating checksums --- .../org/elasticsearch/index/store/Store.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/store/Store.java b/src/main/java/org/elasticsearch/index/store/Store.java index 58d47b3537a..ab97b95d872 100644 --- a/src/main/java/org/elasticsearch/index/store/Store.java +++ b/src/main/java/org/elasticsearch/index/store/Store.java @@ -713,7 +713,13 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref if (maxVersion.onOrAfter(FIRST_LUCENE_CHECKSUM_VERSION)) { checksumFromLuceneFile(directory, segmentsFile, builder, logger, maxVersion, true); } else { - builder.put(segmentsFile, new StoreFileMetaData(segmentsFile, directory.fileLength(segmentsFile), legacyChecksum, maxVersion, hashFile(directory, segmentsFile))); + final BytesRefBuilder fileHash = new BytesRefBuilder(); + final long length; + try (final IndexInput in = directory.openInput(segmentsFile, IOContext.READONCE)) { + length = in.length(); + hashFile(fileHash, new InputStreamIndexInput(in, length), length); + } + builder.put(segmentsFile, new StoreFileMetaData(segmentsFile, length, legacyChecksum, maxVersion, fileHash.get())); } } catch (CorruptIndexException | IndexNotFoundException | IndexFormatTooOldException | IndexFormatTooNewException ex) { // we either know the index is corrupted or it's just not there @@ -798,14 +804,16 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref final String checksum; final BytesRefBuilder fileHash = new BytesRefBuilder(); try (final IndexInput in = directory.openInput(file, IOContext.READONCE)) { + final long length; try { - if (in.length() < CodecUtil.footerLength()) { + length = in.length(); + if (length < CodecUtil.footerLength()) { // truncated files trigger IAE if we seek negative... these files are really corrupted though throw new CorruptIndexException("Can't retrieve checksum from file: " + file + " file length must be >= " + CodecUtil.footerLength() + " but was: " + in.length(), in); } if (readFileAsHash) { final VerifyingIndexInput verifyingIndexInput = new VerifyingIndexInput(in); // additional safety we checksum the entire file we read the hash for... - hashFile(fileHash, new InputStreamIndexInput(verifyingIndexInput, in.length()), in.length()); + hashFile(fileHash, new InputStreamIndexInput(verifyingIndexInput, length), length); checksum = digestToString(verifyingIndexInput.verify()); } else { checksum = digestToString(CodecUtil.retrieveChecksum(in)); @@ -815,7 +823,7 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref logger.debug("Can retrieve checksum from file [{}]", ex, file); throw ex; } - builder.put(file, new StoreFileMetaData(file, directory.fileLength(file), checksum, version, fileHash.get())); + builder.put(file, new StoreFileMetaData(file, length, checksum, version, fileHash.get())); } }