List of usage examples for org.apache.lucene.store IndexOutput getChecksum
public abstract long getChecksum() throws IOException;
From source file:org.apache.solr.codecs.onsql.ONSQLUtil.java
License:Apache License
public static void writeChecksum(IndexOutput out, BytesRefBuilder scratch) throws IOException { // Pad with zeros so different checksum values use the // same number of bytes // (BaseIndexFileFormatTestCase.testMergeStability cares): String checksum = String.format(Locale.ROOT, "%020d", out.getChecksum()); ONSQLUtil.write(out, CHECKSUM);/* ww w. j a v a 2 s . c om*/ ONSQLUtil.write(out, checksum, scratch); ONSQLUtil.writeNewline(out); }
From source file:org.elasticsearch.index.store.StoreTests.java
License:Apache License
public void testChecksumCorrupted() throws IOException { Directory dir = newDirectory();// ww w. j a v a2 s . c o m IndexOutput output = dir.createOutput("foo.bar", IOContext.DEFAULT); int iters = scaledRandomIntBetween(10, 100); for (int i = 0; i < iters; i++) { BytesRef bytesRef = new BytesRef(TestUtil.randomRealisticUnicodeString(random(), 10, 1024)); output.writeBytes(bytesRef.bytes, bytesRef.offset, bytesRef.length); } output.writeInt(CodecUtil.FOOTER_MAGIC); output.writeInt(0); String checksum = Store.digestToString(output.getChecksum()); output.writeLong(output.getChecksum() + 1); // write a wrong checksum to the file output.close(); IndexInput indexInput = dir.openInput("foo.bar", IOContext.DEFAULT); indexInput.seek(0); BytesRef ref = new BytesRef(scaledRandomIntBetween(1, 1024)); long length = indexInput.length(); IndexOutput verifyingOutput = new Store.LuceneVerifyingIndexOutput( new StoreFileMetaData("foo1.bar", length, checksum), dir.createOutput("foo1.bar", IOContext.DEFAULT)); length -= 8; // we write the checksum in the try / catch block below while (length > 0) { if (random().nextInt(10) == 0) { verifyingOutput.writeByte(indexInput.readByte()); length--; } else { int min = (int) Math.min(length, ref.bytes.length); indexInput.readBytes(ref.bytes, ref.offset, min); verifyingOutput.writeBytes(ref.bytes, ref.offset, min); length -= min; } } try { BytesRef checksumBytes = new BytesRef(8); checksumBytes.length = 8; indexInput.readBytes(checksumBytes.bytes, checksumBytes.offset, checksumBytes.length); if (randomBoolean()) { verifyingOutput.writeBytes(checksumBytes.bytes, checksumBytes.offset, checksumBytes.length); } else { for (int i = 0; i < checksumBytes.length; i++) { verifyingOutput.writeByte(checksumBytes.bytes[i]); } } fail("should be a corrupted index"); } catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) { // ok } IOUtils.close(indexInput, verifyingOutput, dir); }