|
19 | 19 |
|
20 | 20 | package org.elasticsearch.index.store; |
21 | 21 |
|
| 22 | +import org.apache.lucene.codecs.CodecUtil; |
22 | 23 | import org.apache.lucene.util.BytesRef; |
23 | 24 | import org.apache.lucene.util.Version; |
24 | 25 | import org.elasticsearch.common.io.stream.StreamInput; |
25 | 26 | import org.elasticsearch.common.io.stream.StreamOutput; |
26 | 27 | import org.elasticsearch.common.io.stream.Writeable; |
| 28 | +import org.elasticsearch.common.lucene.store.ByteArrayIndexInput; |
27 | 29 |
|
28 | 30 | import java.io.IOException; |
29 | 31 | import java.text.ParseException; |
@@ -100,6 +102,29 @@ public String checksum() { |
100 | 102 | return this.checksum; |
101 | 103 | } |
102 | 104 |
|
| 105 | + /** |
| 106 | + * Checks if the bytes returned by {@link #hash()} are the contents of the file that this instances refers to. |
| 107 | + * |
| 108 | + * @return {@code true} iff {@link #hash()} will return the actual file contents |
| 109 | + */ |
| 110 | + public boolean hashEqualsContents() { |
| 111 | + if (hash.length == length) { |
| 112 | + try { |
| 113 | + final boolean checksumsMatch = Store.digestToString(CodecUtil.retrieveChecksum( |
| 114 | + new ByteArrayIndexInput("store_file", hash.bytes, hash.offset, hash.length))).equals(checksum); |
| 115 | + assert checksumsMatch : "Checksums did not match for [" + this + "] which has a hash of [" + hash + "]"; |
| 116 | + return checksumsMatch; |
| 117 | + } catch (Exception e) { |
| 118 | + // Hash didn't contain any bytes that Lucene could extract a checksum from so we can't verify against the checksum of the |
| 119 | + // original file. We should never see an exception here because lucene files are assumed to always contain the checksum |
| 120 | + // footer. |
| 121 | + assert false : new AssertionError("Saw exception for hash [" + hash + "] but expected it to be Lucene file", e); |
| 122 | + return false; |
| 123 | + } |
| 124 | + } |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
103 | 128 | /** |
104 | 129 | * Returns <code>true</code> iff the length and the checksums are the same. otherwise <code>false</code> |
105 | 130 | */ |
|
0 commit comments