Example usage for org.apache.lucene.codecs CodecUtil FOOTER_MAGIC

List of usage examples for org.apache.lucene.codecs CodecUtil FOOTER_MAGIC

Introduction

In this page you can find the example usage for org.apache.lucene.codecs CodecUtil FOOTER_MAGIC.

Prototype

int FOOTER_MAGIC

To view the source code for org.apache.lucene.codecs CodecUtil FOOTER_MAGIC.

Click Source Link

Document

Constant to identify the start of a codec footer.

Usage

From source file:org.elasticsearch.index.store.StoreTests.java

License:Apache License

public void testChecksumCorrupted() throws IOException {
    Directory dir = newDirectory();/*  w  ww.j  a  v  a 2 s . co  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);
}