Java Code Examples for org.apache.lucene.store.IndexInput#readInt()

The following examples show how to use org.apache.lucene.store.IndexInput#readInt() . 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: CompressedIndexInput.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public CompressedIndexInput(IndexInput in) throws IOException {
    super("compressed(" + in.toString() + ")");
    this.in = in;
    readHeader(in);
    this.version = in.readInt();
    long metaDataPosition = in.readLong();
    long headerLength = in.getFilePointer();
    in.seek(metaDataPosition);
    this.totalUncompressedLength = in.readVLong();
    int size = in.readVInt();
    offsets = BigArrays.NON_RECYCLING_INSTANCE.newLongArray(size);
    for (int i = 0; i < size; i++) {
        offsets.set(i, in.readVLong());
    }
    this.currentOffsetIdx = -1;
    this.currentUncompressedChunkPointer = 0;
    in.seek(headerLength);
}
 
Example 2
Source File: Lucene80NormsProducer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void readFields(IndexInput meta, FieldInfos infos) throws IOException {
  for (int fieldNumber = meta.readInt(); fieldNumber != -1; fieldNumber = meta.readInt()) {
    FieldInfo info = infos.fieldInfo(fieldNumber);
    if (info == null) {
      throw new CorruptIndexException("Invalid field number: " + fieldNumber, meta);
    } else if (!info.hasNorms()) {
      throw new CorruptIndexException("Invalid field: " + info.name, meta);
    }
    NormsEntry entry = new NormsEntry();
    entry.docsWithFieldOffset = meta.readLong();
    entry.docsWithFieldLength = meta.readLong();
    entry.jumpTableEntryCount = meta.readShort();
    entry.denseRankPower = meta.readByte();
    entry.numDocsWithField = meta.readInt();
    entry.bytesPerNorm = meta.readByte();
    switch (entry.bytesPerNorm) {
      case 0: case 1: case 2: case 4: case 8:
        break;
      default:
        throw new CorruptIndexException("Invalid bytesPerValue: " + entry.bytesPerNorm + ", field: " + info.name, meta);
    }
    entry.normsOffset = meta.readLong();
    norms.put(info.number, entry);
  }
}
 
Example 3
Source File: CodecUtil.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Retrieves the full index header from the provided {@link IndexInput}.
 *  This throws {@link CorruptIndexException} if this file does
 * not appear to be an index file. */
public static byte[] readIndexHeader(IndexInput in) throws IOException {
  in.seek(0);
  final int actualHeader = in.readInt();
  if (actualHeader != CODEC_MAGIC) {
    throw new CorruptIndexException("codec header mismatch: actual header=" + actualHeader + " vs expected header=" + CODEC_MAGIC, in);
  }
  String codec = in.readString();
  in.readInt();
  in.seek(in.getFilePointer() + StringHelper.ID_LENGTH);
  int suffixLength = in.readByte() & 0xFF;
  byte[] bytes = new byte[headerLength(codec) + StringHelper.ID_LENGTH + 1 + suffixLength];
  in.seek(0);
  in.readBytes(bytes, 0, bytes.length);
  return bytes;
}
 
Example 4
Source File: CodecUtil.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static void validateFooter(IndexInput in) throws IOException {
  long remaining = in.length() - in.getFilePointer();
  long expected = footerLength();
  if (remaining < expected) {
    throw new CorruptIndexException("misplaced codec footer (file truncated?): remaining=" + remaining + ", expected=" + expected + ", fp=" + in.getFilePointer(), in);
  } else if (remaining > expected) {
    throw new CorruptIndexException("misplaced codec footer (file extended?): remaining=" + remaining + ", expected=" + expected + ", fp=" + in.getFilePointer(), in);
  }
  
  final int magic = in.readInt();
  if (magic != FOOTER_MAGIC) {
    throw new CorruptIndexException("codec footer mismatch (file truncated?): actual footer=" + magic + " vs expected footer=" + FOOTER_MAGIC, in);
  }
  
  final int algorithmID = in.readInt();
  if (algorithmID != 0) {
    throw new CorruptIndexException("codec footer mismatch: unknown algorithmID: " + algorithmID, in);
  }
}
 
Example 5
Source File: PermutationVector.java    From semanticvectors with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
/**
 * Reads a (dense) version of a vector from a Lucene input stream. 
 */
public void readFromLuceneStream(IndexInput inputStream) {
 	  
  for (int i = 0; i < dimension; ++i) {
    try {
      coordinates[i] = inputStream.readInt();
    } catch (IOException e) {
      logger.severe("Failed to parse vector from Lucene stream.  This signifies a "
          + "programming or runtime error, e.g., a dimension mismatch.");
      e.printStackTrace();
    }
  }
}
 
Example 6
Source File: CodecUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Expert: verifies the incoming {@link IndexInput} has an index header
 * and that its segment ID matches the expected one, and then copies
 * that index header into the provided {@link DataOutput}.  This is
 * useful when building compound files.
 *
 * @param in Input stream, positioned at the point where the
 *        index header was previously written. Typically this is located
 *        at the beginning of the file.
 * @param out Output stream, where the header will be copied to.
 * @param expectedID Expected segment ID
 * @throws CorruptIndexException If the first four bytes are not
 *         {@link #CODEC_MAGIC}, or if the <code>expectedID</code>
 *         does not match.
 * @throws IOException If there is an I/O error reading from the underlying medium.
 *
 * @lucene.internal 
 */
public static void verifyAndCopyIndexHeader(IndexInput in, DataOutput out, byte[] expectedID) throws IOException {
  // make sure it's large enough to have a header and footer
  if (in.length() < footerLength() + headerLength("")) {
    throw new CorruptIndexException("compound sub-files must have a valid codec header and footer: file is too small (" + in.length() + " bytes)", in);
  }

  int actualHeader = in.readInt();
  if (actualHeader != CODEC_MAGIC) {
    throw new CorruptIndexException("compound sub-files must have a valid codec header and footer: codec header mismatch: actual header=" + actualHeader + " vs expected header=" + CodecUtil.CODEC_MAGIC, in);
  }

  // we can't verify these, so we pass-through:
  String codec = in.readString();
  int version = in.readInt();

  // verify id:
  checkIndexHeaderID(in, expectedID);

  // we can't verify extension either, so we pass-through:
  int suffixLength = in.readByte() & 0xFF;
  byte[] suffixBytes = new byte[suffixLength];
  in.readBytes(suffixBytes, 0, suffixLength);

  // now write the header we just verified
  out.writeInt(CodecUtil.CODEC_MAGIC);
  out.writeString(codec);
  out.writeInt(version);
  out.writeBytes(expectedID, 0, expectedID.length);
  out.writeByte((byte) suffixLength);
  out.writeBytes(suffixBytes, 0, suffixLength);
}
 
Example 7
Source File: Blur022SegmentInfoReader.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Override
public SegmentInfo read(Directory dir, String segment, IOContext context) throws IOException {
  final String fileName = IndexFileNames.segmentFileName(segment, "", Blur022SegmentInfoFormat.SI_EXTENSION);
  final IndexInput input = dir.openInput(fileName, context);
  boolean success = false;
  try {
    CodecUtil.checkHeader(input, Blur022SegmentInfoFormat.CODEC_NAME, Blur022SegmentInfoFormat.VERSION_START,
        Blur022SegmentInfoFormat.VERSION_CURRENT);
    final String version = input.readString();
    final int docCount = input.readInt();
    if (docCount < 0) {
      throw new CorruptIndexException("invalid docCount: " + docCount + " (resource=" + input + ")");
    }
    final boolean isCompoundFile = input.readByte() == SegmentInfo.YES;
    final Map<String, String> diagnostics = input.readStringStringMap();
    final Map<String, String> attributes = input.readStringStringMap();
    final Set<String> files = input.readStringSet();

    if (input.getFilePointer() != input.length()) {
      throw new CorruptIndexException("did not read all bytes from file \"" + fileName + "\": read "
          + input.getFilePointer() + " vs size " + input.length() + " (resource: " + input + ")");
    }

    final SegmentInfo si = new SegmentInfo(dir, version, segment, docCount, isCompoundFile, null, diagnostics,
        Collections.unmodifiableMap(attributes));
    si.setFiles(files);

    success = true;

    return si;

  } finally {
    if (!success) {
      IOUtils.closeWhileHandlingException(input);
    } else {
      input.close();
    }
  }
}
 
Example 8
Source File: CacheIndexInputTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
public static void readRandomDataInt(IndexInput baseInput, IndexInput testInput, Random random, int sampleSize)
    throws IOException {
  assertEquals(baseInput.length(), testInput.length());
  int fileLength = (int) baseInput.length();
  for (int i = 0; i < sampleSize; i++) {
    int position = random.nextInt(fileLength - 4);
    baseInput.seek(position);
    int i1 = baseInput.readInt();
    testInput.seek(position);
    int i2 = testInput.readInt();
    assertEquals("Read [" + i + "] The position is [" + position + "]", i1, i2);
  }
}
 
Example 9
Source File: right_IndexWriter_1.42.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
private final Vector readDeleteableFiles() throws IOException {
  Vector result = new Vector();
  if (!directory.fileExists("deletable"))
    return result;

  IndexInput input = directory.openInput("deletable");
  try {
    for (int i = input.readInt(); i > 0; i--)	  // read file names
      result.addElement(input.readString());
  } finally {
    input.close();
  }
  return result;
}
 
Example 10
Source File: left_IndexWriter_1.41.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
private final Vector readDeleteableFiles() throws IOException {
  Vector result = new Vector();
  if (!directory.fileExists("deletable"))
    return result;

  IndexInput input = directory.openInput("deletable");
  try {
    for (int i = input.readInt(); i > 0; i--)	  // read file names
      result.addElement(input.readString());
  } finally {
    input.close();
  }
  return result;
}
 
Example 11
Source File: BloomFilter.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static void skipBloom(IndexInput in) throws IOException {
    int version = in.readInt(); // we do nothing with this now..., defaults to 0
    final int numLongs = in.readInt();
    in.seek(in.getFilePointer() + (numLongs * 8) + 4 + 4); // filter + numberOfHashFunctions + hashType
}
 
Example 12
Source File: DocIdsWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
static <T> void readInts32(IndexInput in, int count, int[] docIDs) throws IOException {
  for (int i = 0; i < count; i++) {
    docIDs[i] = in.readInt();
  }
}