|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client.ml.inference; |
| 21 | + |
| 22 | +import org.elasticsearch.common.CheckedFunction; |
| 23 | +import org.elasticsearch.common.bytes.BytesArray; |
| 24 | +import org.elasticsearch.common.bytes.BytesReference; |
| 25 | +import org.elasticsearch.common.io.Streams; |
| 26 | +import org.elasticsearch.common.io.stream.BytesStreamOutput; |
| 27 | +import org.elasticsearch.common.xcontent.DeprecationHandler; |
| 28 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 29 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 30 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 31 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 32 | +import org.elasticsearch.common.xcontent.XContentType; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.InputStream; |
| 36 | +import java.io.OutputStream; |
| 37 | +import java.nio.charset.StandardCharsets; |
| 38 | +import java.util.Base64; |
| 39 | +import java.util.zip.GZIPInputStream; |
| 40 | +import java.util.zip.GZIPOutputStream; |
| 41 | + |
| 42 | +/** |
| 43 | + * Collection of helper methods. Similar to CompressedXContent, but this utilizes GZIP. |
| 44 | + */ |
| 45 | +public final class InferenceToXContentCompressor { |
| 46 | + private static final int BUFFER_SIZE = 4096; |
| 47 | + private static final long MAX_INFLATED_BYTES = 1_000_000_000; // 1 gb maximum |
| 48 | + |
| 49 | + private InferenceToXContentCompressor() {} |
| 50 | + |
| 51 | + public static <T extends ToXContentObject> String deflate(T objectToCompress) throws IOException { |
| 52 | + BytesReference reference = XContentHelper.toXContent(objectToCompress, XContentType.JSON, false); |
| 53 | + return deflate(reference); |
| 54 | + } |
| 55 | + |
| 56 | + public static <T> T inflate(String compressedString, |
| 57 | + CheckedFunction<XContentParser, T, IOException> parserFunction, |
| 58 | + NamedXContentRegistry xContentRegistry) throws IOException { |
| 59 | + try(XContentParser parser = XContentHelper.createParser(xContentRegistry, |
| 60 | + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, |
| 61 | + inflate(compressedString, MAX_INFLATED_BYTES), |
| 62 | + XContentType.JSON)) { |
| 63 | + return parserFunction.apply(parser); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + static BytesReference inflate(String compressedString, long streamSize) throws IOException { |
| 68 | + byte[] compressedBytes = Base64.getDecoder().decode(compressedString.getBytes(StandardCharsets.UTF_8)); |
| 69 | + InputStream gzipStream = new GZIPInputStream(new BytesArray(compressedBytes).streamInput(), BUFFER_SIZE); |
| 70 | + InputStream inflateStream = new SimpleBoundedInputStream(gzipStream, streamSize); |
| 71 | + return Streams.readFully(inflateStream); |
| 72 | + } |
| 73 | + |
| 74 | + private static String deflate(BytesReference reference) throws IOException { |
| 75 | + BytesStreamOutput out = new BytesStreamOutput(); |
| 76 | + try (OutputStream compressedOutput = new GZIPOutputStream(out, BUFFER_SIZE)) { |
| 77 | + reference.writeTo(compressedOutput); |
| 78 | + } |
| 79 | + return new String(Base64.getEncoder().encode(BytesReference.toBytes(out.bytes())), StandardCharsets.UTF_8); |
| 80 | + } |
| 81 | +} |
0 commit comments