Search in sources :

Example 6 with Compressor

use of org.elasticsearch.common.compress.Compressor in project elasticsearch by elastic.

the class XContentHelper method convertToMap.

/**
     * Converts the given bytes into a map that is optionally ordered. The provided {@link XContentType} must be non-null.
     */
public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReference bytes, boolean ordered, XContentType xContentType) throws ElasticsearchParseException {
    try {
        final XContentType contentType;
        InputStream input;
        Compressor compressor = CompressorFactory.compressor(bytes);
        if (compressor != null) {
            InputStream compressedStreamInput = compressor.streamInput(bytes.streamInput());
            if (compressedStreamInput.markSupported() == false) {
                compressedStreamInput = new BufferedInputStream(compressedStreamInput);
            }
            input = compressedStreamInput;
        } else {
            input = bytes.streamInput();
        }
        contentType = xContentType != null ? xContentType : XContentFactory.xContentType(input);
        return new Tuple<>(Objects.requireNonNull(contentType), convertToMap(XContentFactory.xContent(contentType), input, ordered));
    } catch (IOException e) {
        throw new ElasticsearchParseException("Failed to parse content to map", e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) Compressor(org.elasticsearch.common.compress.Compressor) IOException(java.io.IOException) Tuple(org.elasticsearch.common.collect.Tuple)

Example 7 with Compressor

use of org.elasticsearch.common.compress.Compressor in project elasticsearch by elastic.

the class XContentHelper method writeRawField.

/**
     * Writes a "raw" (bytes) field, handling cases where the bytes are compressed, and tries to optimize writing using
     * {@link XContentBuilder#rawField(String, org.elasticsearch.common.bytes.BytesReference)}.
     * @deprecated use {@link #writeRawField(String, BytesReference, XContentType, XContentBuilder, Params)} to avoid content type
     * auto-detection
     */
@Deprecated
public static void writeRawField(String field, BytesReference source, XContentBuilder builder, ToXContent.Params params) throws IOException {
    Compressor compressor = CompressorFactory.compressor(source);
    if (compressor != null) {
        InputStream compressedStreamInput = compressor.streamInput(source.streamInput());
        builder.rawField(field, compressedStreamInput);
    } else {
        builder.rawField(field, source);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Compressor(org.elasticsearch.common.compress.Compressor)

Aggregations

Compressor (org.elasticsearch.common.compress.Compressor)7 BufferedInputStream (java.io.BufferedInputStream)5 InputStream (java.io.InputStream)5 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)2 StreamInput (org.elasticsearch.common.io.stream.StreamInput)2 IOException (java.io.IOException)1 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)1 Version (org.elasticsearch.Version)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 Diff (org.elasticsearch.cluster.Diff)1 IncompatibleClusterStateVersionException (org.elasticsearch.cluster.IncompatibleClusterStateVersionException)1 Tuple (org.elasticsearch.common.collect.Tuple)1 NotCompressedException (org.elasticsearch.common.compress.NotCompressedException)1 ThreadContext (org.elasticsearch.common.util.concurrent.ThreadContext)1