Java Code Examples for org.apache.lucene.store.DataOutput#writeString()

The following examples show how to use org.apache.lucene.store.DataOutput#writeString() . 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: JaspellLookup.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void writeRecursively(DataOutput out, TSTNode node) throws IOException {
  if (node == null) {
    return;
  }
  out.writeString(new String(new char[] {node.splitchar}, 0, 1));
  byte mask = 0;
  if (node.relatives[TSTNode.LOKID] != null) mask |= LO_KID;
  if (node.relatives[TSTNode.EQKID] != null) mask |= EQ_KID;
  if (node.relatives[TSTNode.HIKID] != null) mask |= HI_KID;
  if (node.data != null) mask |= HAS_VALUE;
  out.writeByte(mask);
  if (node.data != null) {
    out.writeLong(((Number)node.data).longValue());
  }
  writeRecursively(out, node.relatives[TSTNode.LOKID]);
  writeRecursively(out, node.relatives[TSTNode.EQKID]);
  writeRecursively(out, node.relatives[TSTNode.HIKID]);
}
 
Example 2
Source File: TSTLookup.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void writeRecursively(DataOutput out, TernaryTreeNode node) throws IOException {
  // write out the current node
  out.writeString(new String(new char[] {node.splitchar}, 0, 1));
  // prepare a mask of kids
  byte mask = 0;
  if (node.eqKid != null) mask |= EQ_KID;
  if (node.loKid != null) mask |= LO_KID;
  if (node.hiKid != null) mask |= HI_KID;
  if (node.token != null) mask |= HAS_TOKEN;
  if (node.val != null) mask |= HAS_VALUE;
  out.writeByte(mask);
  if (node.token != null) out.writeString(node.token);
  if (node.val != null) out.writeLong(((Number)node.val).longValue());
  // recurse and write kids
  if (node.loKid != null) {
    writeRecursively(out, node.loKid);
  }
  if (node.eqKid != null) {
    writeRecursively(out, node.eqKid);
  }
  if (node.hiKid != null) {
    writeRecursively(out, node.hiKid);
  }
}
 
Example 3
Source File: BinaryDictionaryWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void writePosDict(Path path) throws IOException {
  Files.createDirectories(path.getParent());
  try (OutputStream os = Files.newOutputStream(path);
       OutputStream bos = new BufferedOutputStream(os)) {
    final DataOutput out = new OutputStreamDataOutput(bos);
    CodecUtil.writeHeader(out, BinaryDictionary.POSDICT_HEADER, BinaryDictionary.VERSION);
    out.writeVInt(posDict.size());
    for (String s : posDict) {
      if (s == null) {
        out.writeByte((byte)0);
        out.writeByte((byte)0);
        out.writeByte((byte)0);
      } else {
        String[] data = CSVUtil.parse(s);
        if (data.length != 3) {
          throw new IllegalArgumentException("Malformed pos/inflection: " + s + "; expected 3 characters");
        }
        out.writeString(data[0]);
        out.writeString(data[1]);
        out.writeString(data[2]);
      }
    }
  }
}
 
Example 4
Source File: SimpleServer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
static void writeFilesMetaData(DataOutput out, Map<String,FileMetaData> files) throws IOException {
  out.writeVInt(files.size());
  for(Map.Entry<String,FileMetaData> ent : files.entrySet()) {
    out.writeString(ent.getKey());

    FileMetaData fmd = ent.getValue();
    out.writeVLong(fmd.length);
    out.writeVLong(fmd.checksum);
    out.writeVInt(fmd.header.length);
    out.writeBytes(fmd.header, 0, fmd.header.length);
    out.writeVInt(fmd.footer.length);
    out.writeBytes(fmd.footer, 0, fmd.footer.length);
  }
}
 
Example 5
Source File: SimplePrimaryNode.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Pushes CopyState on the wire */
private static void writeCopyState(CopyState state, DataOutput out) throws IOException {
  // TODO (opto): we could encode to byte[] once when we created the copyState, and then just send same byts to all replicas...
  out.writeVInt(state.infosBytes.length);
  out.writeBytes(state.infosBytes, 0, state.infosBytes.length);
  out.writeVLong(state.gen);
  out.writeVLong(state.version);
  SimpleServer.writeFilesMetaData(out, state.files);

  out.writeVInt(state.completedMergeFiles.size());
  for(String fileName : state.completedMergeFiles) {
    out.writeString(fileName);
  }
  out.writeVLong(state.primaryGen);
}
 
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: SortedSetSortField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void serialize(DataOutput out) throws IOException {
  out.writeString(getField());
  out.writeInt(reverse ? 1 : 0);
  out.writeInt(selector.ordinal());
  if (missingValue == SortField.STRING_FIRST) {
    out.writeInt(1);
  }
  else if (missingValue == SortField.STRING_LAST) {
    out.writeInt(2);
  }
  else {
    out.writeInt(0);
  }
}
 
Example 8
Source File: SortField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void serialize(DataOutput out) throws IOException {
  out.writeString(field);
  out.writeString(type.toString());
  out.writeInt(reverse ? 1 : 0);
  if (missingValue == null) {
    out.writeInt(0);
  }
  else {
    out.writeInt(1);
    switch (type) {
      case STRING:
        if (missingValue == STRING_LAST) {
          out.writeInt(0);
        }
        else if (missingValue == STRING_FIRST) {
          out.writeInt(1);
        }
        else {
          throw new IllegalArgumentException("Cannot serialize missing value of " + missingValue + " for type STRING");
        }
        break;
      case INT:
        out.writeInt((int)missingValue);
        break;
      case LONG:
        out.writeLong((long)missingValue);
        break;
      case FLOAT:
        out.writeInt(NumericUtils.floatToSortableInt((float)missingValue));
        break;
      case DOUBLE:
        out.writeLong(NumericUtils.doubleToSortableLong((double)missingValue));
        break;
      default:
        throw new IllegalArgumentException("Cannot serialize SortField of type " + type);
    }
  }
}
 
Example 9
Source File: SortedNumericSortField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void serialize(DataOutput out) throws IOException {
  out.writeString(getField());
  out.writeString(type.toString());
  out.writeInt(reverse ? 1 : 0);
  out.writeInt(selector.ordinal());
  if (missingValue == null) {
    out.writeInt(0);
  }
  else {
    out.writeInt(1);
    // oh for switch expressions...
    switch (type) {
      case INT:
        out.writeInt((int)missingValue);
        break;
      case LONG:
        out.writeLong((long)missingValue);
        break;
      case FLOAT:
        out.writeInt(NumericUtils.floatToSortableInt((float)missingValue));
        break;
      case DOUBLE:
        out.writeLong(NumericUtils.doubleToSortableLong((double)missingValue));
        break;
      default:
        throw new AssertionError();
    }
  }
}
 
Example 10
Source File: CodecUtil.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Writes a codec header, which records both a string to
 * identify the file and a version number. This header can
 * be parsed and validated with 
 * {@link #checkHeader(DataInput, String, int, int) checkHeader()}.
 * <p>
 * CodecHeader --&gt; Magic,CodecName,Version
 * <ul>
 *    <li>Magic --&gt; {@link DataOutput#writeInt Uint32}. This
 *        identifies the start of the header. It is always {@value #CODEC_MAGIC}.
 *    <li>CodecName --&gt; {@link DataOutput#writeString String}. This
 *        is a string to identify this file.
 *    <li>Version --&gt; {@link DataOutput#writeInt Uint32}. Records
 *        the version of the file.
 * </ul>
 * <p>
 * Note that the length of a codec header depends only upon the
 * name of the codec, so this length can be computed at any time
 * with {@link #headerLength(String)}.
 * 
 * @param out Output stream
 * @param codec String to identify this file. It should be simple ASCII, 
 *              less than 128 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
 */
public static void writeHeader(DataOutput out, String codec, int version) throws IOException {
  BytesRef bytes = new BytesRef(codec);
  if (bytes.length != codec.length() || bytes.length >= 128) {
    throw new IllegalArgumentException("codec must be simple ASCII, less than 128 characters in length [got " + codec + "]");
  }
  out.writeInt(CODEC_MAGIC);
  out.writeString(codec);
  out.writeInt(version);
}