Java Code Examples for org.elasticsearch.common.bytes.BytesReference#writeTo()

The following examples show how to use org.elasticsearch.common.bytes.BytesReference#writeTo() . 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: LocalTranslog.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public Location writeToLocal(BytesReference data) throws IOException {
    final long position;
    final long generation;
    try (ReleasableLock lock = writeLock.acquire()) {
        ensureOpen();
        if (writtenOffset > TRANSLOG_ROLLING_SIZE_BYTES) {
            IOUtils.close(writeChannel);
            tmpTranslogGeneration.incrementAndGet();
            writeChannel = FileChannel.open(this.translogPath.resolve(getFileNameFromId(tmpTranslogGeneration.get())), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
            writtenOffset = 0;
        }
        generation = tmpTranslogGeneration.get();
        position = writtenOffset;
        try {
            data.writeTo(writeChannel);
        } catch (Throwable e) {
            throw e;
        }
        writtenOffset = writtenOffset + data.length();
    }
    return new Translog.Location(generation, position, data.length());
}
 
Example 2
Source File: ChecksumBlobStoreFormat.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Writes blob in atomic manner without resolving the blobName using using {@link #blobName} method.
 * <p>
 * The blob will be compressed and checksum will be written if required.
 *
 * @param obj           object to be serialized
 * @param blobContainer blob container
 * @param blobName          blob name
 */
protected void writeBlob(T obj, BlobContainer blobContainer, String blobName) throws IOException {
    BytesReference bytes = write(obj);
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        final String resourceDesc = "ChecksumBlobStoreFormat.writeBlob(blob=\"" + blobName + "\")";
        try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput(resourceDesc, byteArrayOutputStream, BUFFER_SIZE)) {
            CodecUtil.writeHeader(indexOutput, codec, VERSION);
            try (OutputStream indexOutputOutputStream = new IndexOutputOutputStream(indexOutput) {
                @Override
                public void close() throws IOException {
                    // this is important since some of the XContentBuilders write bytes on close.
                    // in order to write the footer we need to prevent closing the actual index input.
                } }) {
                bytes.writeTo(indexOutputOutputStream);
            }
            CodecUtil.writeFooter(indexOutput);
        }
        blobContainer.writeBlob(blobName, new BytesArray(byteArrayOutputStream.toByteArray()));
    }
}
 
Example 3
Source File: TranslogWriter.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * add the given bytes to the translog and return the location they were written at
 */
public Translog.Location add(BytesReference data) throws IOException {
    final long position;
    try (ReleasableLock lock = writeLock.acquire()) {
        ensureOpen();
        position = writtenOffset;
        try {
            data.writeTo(channel);
        } catch (Throwable e) {
            closeWithTragicEvent(e);
            throw e;
        }
        writtenOffset = writtenOffset + data.length();
        operationCounter++;;
    }
    return new Translog.Location(generation, position, data.length());
}
 
Example 4
Source File: CompressedXContent.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Create a {@link CompressedXContent} out of a serialized {@link ToXContent}
 * that may already be compressed.
 */
public CompressedXContent(BytesReference data) throws IOException {
    Compressor compressor = CompressorFactory.compressor(data);
    if (compressor != null) {
        // already compressed...
        this.bytes = data.toBytes();
        this.crc32 = crc32(new BytesArray(uncompressed()));
    } else {
        BytesStreamOutput out = new BytesStreamOutput();
        try (OutputStream compressedOutput = CompressorFactory.defaultCompressor().streamOutput(out)) {
            data.writeTo(compressedOutput);
        }
        this.bytes = out.bytes().toBytes();
        this.crc32 = crc32(data);
    }
    assertConsistent();
}
 
Example 5
Source File: JsonXContentGenerator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public final void writeRawField(String fieldName, BytesReference content) throws IOException {
    XContentType contentType = XContentFactory.xContentType(content);
    if (contentType == null) {
        throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed");
    }
    if (mayWriteRawData(contentType) == false) {
        writeFieldName(fieldName);
        copyRawValue(content, contentType.xContent());
    } else {
        writeStartRaw(fieldName);
        flush();
        content.writeTo(os);
        writeEndRaw();
    }
}
 
Example 6
Source File: CompressedXContent.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Create a {@link CompressedXContent} out of a serialized {@link ToXContent}
 * that may already be compressed.
 */
public CompressedXContent(BytesReference data) throws IOException {
    Compressor compressor = CompressorFactory.compressor(data);
    if (compressor != null) {
        // already compressed...
        this.bytes = BytesReference.toBytes(data);
        this.crc32 = crc32(new BytesArray(uncompressed()));
    } else {
        BytesStreamOutput out = new BytesStreamOutput();
        try (OutputStream compressedOutput = CompressorFactory.COMPRESSOR.streamOutput(out)) {
            data.writeTo(compressedOutput);
        }
        this.bytes = BytesReference.toBytes(out.bytes());
        this.crc32 = crc32(data);
    }
    assertConsistent();
}
 
Example 7
Source File: BufferingTranslogWriter.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Translog.Location add(BytesReference data) throws IOException {
    try (ReleasableLock lock = writeLock.acquire()) {
        ensureOpen();
        final long offset = totalOffset;
        if (data.length() >= buffer.length) {
            flush();
            // we use the channel to write, since on windows, writing to the RAF might not be reflected
            // when reading through the channel
            try {
                data.writeTo(channel);
            } catch (Throwable ex) {
                closeWithTragicEvent(ex);
                throw ex;
            }
            writtenOffset += data.length();
            totalOffset += data.length();
        } else {
            if (data.length() > buffer.length - bufferCount) {
                flush();
            }
            data.writeTo(bufferOs);
            totalOffset += data.length();
        }
        operationCounter++;
        return new Translog.Location(generation, offset, data.length());
    }
}
 
Example 8
Source File: FsBlobContainer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void writeBlob(String blobName, BytesReference data) throws IOException {
    final Path file = path.resolve(blobName);
    try (OutputStream outputStream = Files.newOutputStream(file)) {
        data.writeTo(outputStream);
    }
    IOUtils.fsync(file, false);
    IOUtils.fsync(path, true);
}
 
Example 9
Source File: JsonXContentGenerator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public final void writeRawValue(BytesReference content) throws IOException {
    XContentType contentType = XContentFactory.xContentType(content);
    if (contentType == null) {
        throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed");
    }
    if (mayWriteRawData(contentType) == false) {
        copyRawValue(content, contentType.xContent());
    } else {
        flush();
        content.writeTo(os);
        writeEndRaw();
    }
}
 
Example 10
Source File: StreamOutput.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the bytes reference, including a length header.
 */
public void writeBytesReference(@Nullable BytesReference bytes) throws IOException {
    if (bytes == null) {
        writeVInt(0);
        return;
    }
    writeVInt(bytes.length());
    bytes.writeTo(this);
}
 
Example 11
Source File: CsvXContentGenerator.java    From elasticsearch-rest-command with The Unlicense 5 votes vote down vote up
protected void writeObjectRaw(String fieldName, BytesReference content, OutputStream bos) throws IOException {
    generator.writeFieldName(fieldName);
    generator.writeRaw(':');
    flush();
    content.writeTo(bos);
    finishWriteRaw();
}
 
Example 12
Source File: ChecksumBlobStoreFormat.java    From crate with Apache License 2.0 5 votes vote down vote up
private void writeTo(final T obj, final String blobName, final CheckedConsumer<BytesArray, IOException> consumer) throws IOException {
    final BytesReference bytes;
    try (BytesStreamOutput bytesStreamOutput = new BytesStreamOutput()) {
        if (compress) {
            try (StreamOutput compressedStreamOutput = CompressorFactory.COMPRESSOR.streamOutput(bytesStreamOutput)) {
                write(obj, compressedStreamOutput);
            }
        } else {
            write(obj, bytesStreamOutput);
        }
        bytes = bytesStreamOutput.bytes();
    }
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        final String resourceDesc = "ChecksumBlobStoreFormat.writeBlob(blob=\"" + blobName + "\")";
        try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput(resourceDesc, blobName, outputStream, BUFFER_SIZE)) {
            CodecUtil.writeHeader(indexOutput, codec, VERSION);
            try (OutputStream indexOutputOutputStream = new IndexOutputOutputStream(indexOutput) {
                @Override
                public void close() {
                    // this is important since some of the XContentBuilders write bytes on close.
                    // in order to write the footer we need to prevent closing the actual index input.
                }
            }) {
                bytes.writeTo(indexOutputOutputStream);
            }
            CodecUtil.writeFooter(indexOutput);
        }
        consumer.accept(new BytesArray(outputStream.toByteArray()));
    }
}
 
Example 13
Source File: StreamOutput.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the bytes reference, including a length header.
 */
public void writeBytesReference(@Nullable BytesReference bytes) throws IOException {
    if (bytes == null) {
        writeVInt(0);
        return;
    }
    writeVInt(bytes.length());
    bytes.writeTo(this);
}
 
Example 14
Source File: StreamOutput.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Writes an optional bytes reference including a length header. Use this if you need to differentiate between null and empty bytes
 * references. Use {@link #writeBytesReference(BytesReference)} and {@link StreamInput#readBytesReference()} if you do not.
 */
public void writeOptionalBytesReference(@Nullable BytesReference bytes) throws IOException {
    if (bytes == null) {
        writeVInt(0);
        return;
    }
    writeVInt(bytes.length() + 1);
    bytes.writeTo(this);
}
 
Example 15
Source File: MockTcpTransport.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void sendMessage(BytesReference reference, ActionListener<Void> listener) {
    try {
        synchronized (this) {
            OutputStream outputStream = new BufferedOutputStream(activeChannel.getOutputStream());
            reference.writeTo(outputStream);
            outputStream.flush();
        }
        listener.onResponse(null);
    } catch (IOException e) {
        listener.onFailure(e);
        onException(this, e);
    }
}
 
Example 16
Source File: AbstractLegacyBlobContainer.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void writeBlob(String blobName, BytesReference data) throws IOException {
    try (OutputStream stream = createOutput(blobName)) {
        data.writeTo(stream);
    }
}