|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.core.compress; |
| 10 | + |
| 11 | +import org.opensearch.common.Nullable; |
| 12 | +import org.opensearch.common.annotation.InternalApi; |
| 13 | +import org.opensearch.core.common.bytes.BytesReference; |
| 14 | +import org.opensearch.core.compress.spi.CompressorProvider; |
| 15 | +import org.opensearch.core.xcontent.MediaTypeRegistry; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.ServiceLoader; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +/** |
| 24 | + * A registry that wraps a static Map singleton which holds a mapping of unique String names (typically the |
| 25 | + * compressor header as a string) to registerd {@link Compressor} implementations. |
| 26 | + * |
| 27 | + * This enables plugins, modules, extensions to register their own compression implementations through SPI |
| 28 | + * |
| 29 | + * @opensearch.experimental |
| 30 | + * @opensearch.internal |
| 31 | + */ |
| 32 | +@InternalApi |
| 33 | +public final class CompressorRegistry { |
| 34 | + |
| 35 | + // the backing registry map |
| 36 | + private static final Map<String, Compressor> registeredCompressors = ServiceLoader.load( |
| 37 | + CompressorProvider.class, |
| 38 | + CompressorProvider.class.getClassLoader() |
| 39 | + ) |
| 40 | + .stream() |
| 41 | + .flatMap(p -> p.get().getCompressors().stream()) |
| 42 | + .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 43 | + |
| 44 | + // no instance: |
| 45 | + private CompressorRegistry() {} |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns the default compressor |
| 49 | + */ |
| 50 | + public static Compressor defaultCompressor() { |
| 51 | + return registeredCompressors.get("DEFLATE"); |
| 52 | + } |
| 53 | + |
| 54 | + public static Compressor none() { |
| 55 | + return registeredCompressors.get(NoneCompressor.NAME); |
| 56 | + } |
| 57 | + |
| 58 | + public static boolean isCompressed(BytesReference bytes) { |
| 59 | + return compressor(bytes) != null; |
| 60 | + } |
| 61 | + |
| 62 | + @Nullable |
| 63 | + public static Compressor compressor(final BytesReference bytes) { |
| 64 | + for (Compressor compressor : registeredCompressors.values()) { |
| 65 | + if (compressor.isCompressed(bytes) == true) { |
| 66 | + // bytes should be either detected as compressed or as xcontent, |
| 67 | + // if we have bytes that can be either detected as compressed or |
| 68 | + // as a xcontent, we have a problem |
| 69 | + assert MediaTypeRegistry.xContentType(bytes) == null; |
| 70 | + return compressor; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (MediaTypeRegistry.xContentType(bytes) == null) { |
| 75 | + throw new NotXContentException("Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"); |
| 76 | + } |
| 77 | + |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + /** Decompress the provided {@link BytesReference}. */ |
| 82 | + public static BytesReference uncompress(BytesReference bytes) throws IOException { |
| 83 | + Compressor compressor = compressor(bytes); |
| 84 | + if (compressor == null) { |
| 85 | + throw new NotCompressedException(); |
| 86 | + } |
| 87 | + return compressor.uncompress(bytes); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Uncompress the provided data, data can be detected as compressed using {@link #isCompressed(BytesReference)}. |
| 92 | + */ |
| 93 | + public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException { |
| 94 | + Compressor compressor = compressor(Objects.requireNonNull(bytes, "the BytesReference must not be null")); |
| 95 | + return compressor == null ? bytes : compressor.uncompress(bytes); |
| 96 | + } |
| 97 | + |
| 98 | + /** Returns a registered compressor by its registered name */ |
| 99 | + public static Compressor getCompressor(final String name) { |
| 100 | + if (registeredCompressors.containsKey(name)) { |
| 101 | + return registeredCompressors.get(name); |
| 102 | + } |
| 103 | + throw new IllegalArgumentException("No registered compressor found by name [" + name + "]"); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns the registered compressors as an Immutable collection |
| 108 | + * |
| 109 | + * note: used for testing |
| 110 | + */ |
| 111 | + public static Map<String, Compressor> registeredCompressors() { |
| 112 | + // no destructive danger as backing map is immutable |
| 113 | + return registeredCompressors; |
| 114 | + } |
| 115 | +} |
0 commit comments