Java Code Examples for org.apache.lucene.store.OutputStreamDataOutput#writeBytes()

The following examples show how to use org.apache.lucene.store.OutputStreamDataOutput#writeBytes() . 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: Completion090PostingsFormat.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public BytesRef buildPayload(BytesRef surfaceForm, long weight, BytesRef payload) throws IOException {
    if (weight < -1 || weight > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("weight must be >= -1 && <= Integer.MAX_VALUE");
    }
    for (int i = 0; i < surfaceForm.length; i++) {
        if (surfaceForm.bytes[i] == UNIT_SEPARATOR) {
            throw new IllegalArgumentException(
                    "surface form cannot contain unit separator character U+001F; this character is reserved");
        }
    }
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    OutputStreamDataOutput output = new OutputStreamDataOutput(byteArrayOutputStream);
    output.writeVLong(weight + 1);
    output.writeVInt(surfaceForm.length);
    output.writeBytes(surfaceForm.bytes, surfaceForm.offset, surfaceForm.length);
    output.writeVInt(payload.length);
    output.writeBytes(payload.bytes, 0, payload.length);

    output.close();
    return new BytesRef(byteArrayOutputStream.toByteArray());
}
 
Example 2
Source File: TranslogWriter.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static TranslogWriter create(Type type, ShardId shardId, String translogUUID, long fileGeneration, Path file, Callback<ChannelReference> onClose, int bufferSize, ChannelFactory channelFactory) throws IOException {
    final BytesRef ref = new BytesRef(translogUUID);
    final int headerLength = getHeaderLength(ref.length);
    final FileChannel channel = channelFactory.open(file);
    try {
        // This OutputStreamDataOutput is intentionally not closed because
        // closing it will close the FileChannel
        final OutputStreamDataOutput out = new OutputStreamDataOutput(java.nio.channels.Channels.newOutputStream(channel));
        CodecUtil.writeHeader(out, TRANSLOG_CODEC, VERSION);
        out.writeInt(ref.length);
        out.writeBytes(ref.bytes, ref.offset, ref.length);
        channel.force(true);
        writeCheckpoint(headerLength, 0, file.getParent(), fileGeneration, StandardOpenOption.WRITE);
        final TranslogWriter writer = type.create(shardId, fileGeneration, new ChannelReference(file, fileGeneration, channel, onClose), bufferSize);
        return writer;
    } catch (Throwable throwable){
        // if we fail to bake the file-generation into the checkpoint we stick with the file and once we recover and that
        // file exists we remove it. We only apply this logic to the checkpoint.generation+1 any other file with a higher generation is an error condition
        IOUtils.closeWhileHandlingException(channel);
        throw throwable;
    }
}