Java Code Examples for org.apache.lucene.util.StringHelper#idToString()

The following examples show how to use org.apache.lucene.util.StringHelper#idToString() . 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: SegmentCommitInfo.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Returns a description of this segment. */
public String toString(int pendingDelCount) {
  String s = info.toString(delCount + pendingDelCount);
  if (delGen != -1) {
    s += ":delGen=" + delGen;
  }
  if (fieldInfosGen != -1) {
    s += ":fieldInfosGen=" + fieldInfosGen;
  }
  if (docValuesGen != -1) {
    s += ":dvGen=" + docValuesGen;
  }
  if (softDelCount > 0) {
    s += " :softDel=" + softDelCount;
  }
  if (this.id != null) {
    s += " :id=" + StringHelper.idToString(id);
  }

  return s;
}
 
Example 2
Source File: CodecUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Expert: just reads and verifies the object ID of an index header */
public static byte[] checkIndexHeaderID(DataInput in, byte[] expectedID) throws IOException {
  byte id[] = new byte[StringHelper.ID_LENGTH];
  in.readBytes(id, 0, id.length);
  if (!Arrays.equals(id, expectedID)) {
    throw new CorruptIndexException("file mismatch, expected id=" + StringHelper.idToString(expectedID) 
                                                       + ", got=" + StringHelper.idToString(id), in);
  }
  return id;
}
 
Example 3
Source File: TestIndexWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Make sure we see ids per segment and per commit. */
public void testIds() throws Exception {
  Directory d = newDirectory();
  IndexWriter w = new IndexWriter(d, newIndexWriterConfig(new MockAnalyzer(random())));
  w.addDocument(new Document());
  w.close();

  SegmentInfos sis = SegmentInfos.readLatestCommit(d);
  byte[] id1 = sis.getId();
  assertNotNull(id1);
  assertEquals(StringHelper.ID_LENGTH, id1.length);

  byte[] id2 = sis.info(0).info.getId();
  byte[] sciId2 = sis.info(0).getId();
  assertNotNull(id2);
  assertNotNull(sciId2);
  assertEquals(StringHelper.ID_LENGTH, id2.length);
  assertEquals(StringHelper.ID_LENGTH, sciId2.length);

  // Make sure CheckIndex includes id output:
  ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
  CheckIndex checker = new CheckIndex(d);
  checker.setDoSlowChecks(false);
  checker.setInfoStream(new PrintStream(bos, false, IOUtils.UTF_8), false);
  CheckIndex.Status indexStatus = checker.checkIndex(null);
  String s = bos.toString(IOUtils.UTF_8);
  checker.close();
  // Make sure CheckIndex didn't fail
  assertTrue(s, indexStatus != null && indexStatus.clean);

  // Commit id is always stored:
  assertTrue("missing id=" + StringHelper.idToString(id1) + " in:\n" + s, s.contains("id=" + StringHelper.idToString(id1)));

  assertTrue("missing id=" + StringHelper.idToString(id1) + " in:\n" + s, s.contains("id=" + StringHelper.idToString(id1)));
  d.close();

  Set<String> ids = new HashSet<>();
  for(int i=0;i<100000;i++) {
    String id = StringHelper.idToString(StringHelper.randomId());
    assertFalse("id=" + id + " i=" + i, ids.contains(id));
    ids.add(id);
  }
}
 
Example 4
Source File: IdUtils.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Generate a short random id (see {@link StringHelper#randomId()}).
 */
public static final String randomId() {
  return StringHelper.idToString(StringHelper.randomId());
}
 
Example 5
Source File: CodecUtil.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Writes a codec header for an index file, which records both a string to
 * identify the format of the file, a version number, and data to identify
 * the file instance (ID and auxiliary suffix such as generation).
 * <p>
 * This header can be parsed and validated with 
 * {@link #checkIndexHeader(DataInput, String, int, int, byte[], String) checkIndexHeader()}.
 * <p>
 * IndexHeader --&gt; CodecHeader,ObjectID,ObjectSuffix
 * <ul>
 *    <li>CodecHeader   --&gt; {@link #writeHeader}
 *    <li>ObjectID     --&gt; {@link DataOutput#writeByte byte}<sup>16</sup>
 *    <li>ObjectSuffix --&gt; SuffixLength,SuffixBytes
 *    <li>SuffixLength  --&gt; {@link DataOutput#writeByte byte}
 *    <li>SuffixBytes   --&gt; {@link DataOutput#writeByte byte}<sup>SuffixLength</sup>
 * </ul>
 * <p>
 * Note that the length of an index header depends only upon the
 * name of the codec and suffix, so this length can be computed at any time
 * with {@link #indexHeaderLength(String,String)}.
 * 
 * @param out Output stream
 * @param codec String to identify the format of this file. It should be simple ASCII, 
 *              less than 128 characters in length.
 * @param id Unique identifier for this particular file instance.
 * @param suffix auxiliary suffix information for the file. It should be simple ASCII,
 *              less than 256 characters in length.
 * @param version Version number
 * @throws IOException If there is an I/O error writing to the underlying medium.
 * @throws IllegalArgumentException If the codec name is not simple ASCII, or 
 *         is more than 127 characters in length, or if id is invalid,
 *         or if the suffix is not simple ASCII, or more than 255 characters
 *         in length.
 */
public static void writeIndexHeader(DataOutput out, String codec, int version, byte[] id, String suffix) throws IOException {
  if (id.length != StringHelper.ID_LENGTH) {
    throw new IllegalArgumentException("Invalid id: " + StringHelper.idToString(id));
  }
  writeHeader(out, codec, version);
  out.writeBytes(id, 0, id.length);
  BytesRef suffixBytes = new BytesRef(suffix);
  if (suffixBytes.length != suffix.length() || suffixBytes.length >= 256) {
    throw new IllegalArgumentException("suffix must be simple ASCII, less than 256 characters in length [got " + suffix + "]");
  }
  out.writeByte((byte) suffixBytes.length);
  out.writeBytes(suffixBytes.bytes, suffixBytes.offset, suffixBytes.length);
}