Java Code Examples for org.apache.lucene.store.ChecksumIndexInput#length()

The following examples show how to use org.apache.lucene.store.ChecksumIndexInput#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleTextUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static void checkFooter(ChecksumIndexInput input) throws IOException {
  BytesRefBuilder scratch = new BytesRefBuilder();
  String expectedChecksum = String.format(Locale.ROOT, "%020d", input.getChecksum());
  readLine(input, scratch);
  if (StringHelper.startsWith(scratch.get(), CHECKSUM) == false) {
    throw new CorruptIndexException("SimpleText failure: expected checksum line but got " + scratch.get().utf8ToString(), input);
  }
  String actualChecksum = new BytesRef(scratch.bytes(), CHECKSUM.length, scratch.length() - CHECKSUM.length).utf8ToString();
  if (!expectedChecksum.equals(actualChecksum)) {
    throw new CorruptIndexException("SimpleText checksum failure: " + actualChecksum + " != " + expectedChecksum, input);
  }
  if (input.length() != input.getFilePointer()) {
    throw new CorruptIndexException("Unexpected stuff at the end of file, please be careful with your text editor!", input);
  }
}
 
Example 2
Source File: CodecUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** 
 * Validates the codec footer previously written by {@link #writeFooter}, optionally
 * passing an unexpected exception that has already occurred.
 * <p>
 * When a {@code priorException} is provided, this method will add a suppressed exception 
 * indicating whether the checksum for the stream passes, fails, or cannot be computed, and 
 * rethrow it. Otherwise it behaves the same as {@link #checkFooter(ChecksumIndexInput)}.
 * <p>
 * Example usage:
 * <pre class="prettyprint">
 * try (ChecksumIndexInput input = ...) {
 *   Throwable priorE = null;
 *   try {
 *     // ... read a bunch of stuff ... 
 *   } catch (Throwable exception) {
 *     priorE = exception;
 *   } finally {
 *     CodecUtil.checkFooter(input, priorE);
 *   }
 * }
 * </pre>
 */
public static void checkFooter(ChecksumIndexInput in, Throwable priorException) throws IOException {
  if (priorException == null) {
    checkFooter(in);
  } else {
    try {
      // If we have evidence of corruption then we return the corruption as the
      // main exception and the prior exception gets suppressed. Otherwise we
      // return the prior exception with a suppressed exception that notifies
      // the user that checksums matched.
      long remaining = in.length() - in.getFilePointer();
      if (remaining < footerLength()) {
        // corruption caused us to read into the checksum footer already: we can't proceed
        throw new CorruptIndexException("checksum status indeterminate: remaining=" + remaining +
                                        "; please run checkindex for more details", in);
      } else {
        // otherwise, skip any unread bytes.
        in.skipBytes(remaining - footerLength());
        
        // now check the footer
        long checksum = checkFooter(in);
        priorException.addSuppressed(new CorruptIndexException("checksum passed (" + Long.toHexString(checksum) +
                                                               "). possibly transient resource issue, or a Lucene or JVM bug", in));
      }
    } catch (CorruptIndexException corruptException) {
      corruptException.addSuppressed(priorException);
      throw corruptException;
    } catch (Throwable t) {
      // catch-all for things that shouldn't go wrong (e.g. OOM during readInt) but could...
      priorException.addSuppressed(new CorruptIndexException("checksum status indeterminate: unexpected exception", in, t));
    }
    throw IOUtils.rethrowAlways(priorException);
  }
}
 
Example 3
Source File: OfflineSorter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Constructs a ByteSequencesReader from the provided IndexInput */
public ByteSequencesReader(ChecksumIndexInput in, String name) {
  this.in = in;
  this.name = name;
  end = in.length() - CodecUtil.footerLength();
}