/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java
Java | 191 lines | 144 code | 24 blank | 23 comment | 23 complexity | ab9cd2f0cd0a4c2f14d0e7b8a1508007 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, Apache-2.0, AGPL-3.0, MIT, LGPL-3.0, MPL-2.0-no-copyleft-exception
- /*
- * Licensed to ElasticSearch and Shay Banon under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. ElasticSearch licenses this
- * file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- package org.elasticsearch.common.compress;
- import com.google.common.collect.ImmutableMap;
- import com.google.common.collect.Lists;
- import org.apache.lucene.store.IndexInput;
- import org.elasticsearch.common.Nullable;
- import org.elasticsearch.common.bytes.BytesArray;
- import org.elasticsearch.common.bytes.BytesReference;
- import org.elasticsearch.common.collect.MapBuilder;
- import org.elasticsearch.common.compress.lzf.LZFCompressor;
- import org.elasticsearch.common.compress.snappy.UnavailableSnappyCompressor;
- import org.elasticsearch.common.compress.snappy.xerial.XerialSnappy;
- import org.elasticsearch.common.compress.snappy.xerial.XerialSnappyCompressor;
- import org.elasticsearch.common.io.Streams;
- import org.elasticsearch.common.io.stream.CachedStreamOutput;
- import org.elasticsearch.common.io.stream.StreamInput;
- import org.elasticsearch.common.logging.Loggers;
- import org.elasticsearch.common.settings.Settings;
- import org.jboss.netty.buffer.ChannelBuffer;
- import java.io.IOException;
- import java.util.List;
- import java.util.Locale;
- /**
- */
- public class CompressorFactory {
- private static final LZFCompressor LZF = new LZFCompressor();
- private static final Compressor[] compressors;
- private static final ImmutableMap<String, Compressor> compressorsByType;
- private static Compressor defaultCompressor;
- static {
- List<Compressor> compressorsX = Lists.newArrayList();
- compressorsX.add(LZF);
- boolean addedSnappy = false;
- if (XerialSnappy.available) {
- compressorsX.add(new XerialSnappyCompressor());
- addedSnappy = true;
- } else {
- Loggers.getLogger(CompressorFactory.class).debug("failed to load xerial snappy-java", XerialSnappy.failure);
- }
- if (!addedSnappy) {
- compressorsX.add(new UnavailableSnappyCompressor());
- }
- compressors = compressorsX.toArray(new Compressor[compressorsX.size()]);
- MapBuilder<String, Compressor> compressorsByTypeX = MapBuilder.newMapBuilder();
- for (Compressor compressor : compressors) {
- compressorsByTypeX.put(compressor.type(), compressor);
- }
- compressorsByType = compressorsByTypeX.immutableMap();
- defaultCompressor = LZF;
- }
- public static synchronized void configure(Settings settings) {
- for (Compressor compressor : compressors) {
- compressor.configure(settings);
- }
- String defaultType = settings.get("compress.default.type", "lzf").toLowerCase(Locale.ENGLISH);
- boolean found = false;
- for (Compressor compressor : compressors) {
- if (defaultType.equalsIgnoreCase(compressor.type())) {
- defaultCompressor = compressor;
- found = true;
- break;
- }
- }
- if (!found) {
- Loggers.getLogger(CompressorFactory.class).warn("failed to find default type [{}]", defaultType);
- }
- }
- public static synchronized void setDefaultCompressor(Compressor defaultCompressor) {
- CompressorFactory.defaultCompressor = defaultCompressor;
- }
- public static Compressor defaultCompressor() {
- return defaultCompressor;
- }
- public static boolean isCompressed(BytesReference bytes) {
- return compressor(bytes) != null;
- }
- public static boolean isCompressed(byte[] data) {
- return compressor(data, 0, data.length) != null;
- }
- public static boolean isCompressed(byte[] data, int offset, int length) {
- return compressor(data, offset, length) != null;
- }
- public static boolean isCompressed(IndexInput in) throws IOException {
- return compressor(in) != null;
- }
- @Nullable
- public static Compressor compressor(BytesReference bytes) {
- for (Compressor compressor : compressors) {
- if (compressor.isCompressed(bytes)) {
- return compressor;
- }
- }
- return null;
- }
- @Nullable
- public static Compressor compressor(byte[] data) {
- return compressor(data, 0, data.length);
- }
- @Nullable
- public static Compressor compressor(byte[] data, int offset, int length) {
- for (Compressor compressor : compressors) {
- if (compressor.isCompressed(data, offset, length)) {
- return compressor;
- }
- }
- return null;
- }
- @Nullable
- public static Compressor compressor(ChannelBuffer buffer) {
- for (Compressor compressor : compressors) {
- if (compressor.isCompressed(buffer)) {
- return compressor;
- }
- }
- return null;
- }
- @Nullable
- public static Compressor compressor(IndexInput in) throws IOException {
- for (Compressor compressor : compressors) {
- if (compressor.isCompressed(in)) {
- return compressor;
- }
- }
- return null;
- }
- public static Compressor compressor(String type) {
- return compressorsByType.get(type);
- }
- /**
- * Uncompress the provided data, data can be detected as compressed using {@link #isCompressed(byte[], int, int)}.
- */
- public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException {
- Compressor compressor = compressor(bytes);
- if (compressor != null) {
- if (bytes.hasArray()) {
- return new BytesArray(compressor.uncompress(bytes.array(), bytes.arrayOffset(), bytes.length()));
- }
- StreamInput compressed = compressor.streamInput(bytes.streamInput());
- CachedStreamOutput.Entry entry = CachedStreamOutput.popEntry();
- try {
- Streams.copy(compressed, entry.bytes());
- compressed.close();
- return new BytesArray(entry.bytes().bytes().toBytes());
- } finally {
- CachedStreamOutput.pushEntry(entry);
- }
- }
- return bytes;
- }
- }