PageRenderTime 36ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java

http://github.com/elasticsearch/elasticsearch
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
  1. /*
  2. * Licensed to ElasticSearch and Shay Banon under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. ElasticSearch licenses this
  6. * file to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. 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. package org.elasticsearch.common.compress;
  20. import com.google.common.collect.ImmutableMap;
  21. import com.google.common.collect.Lists;
  22. import org.apache.lucene.store.IndexInput;
  23. import org.elasticsearch.common.Nullable;
  24. import org.elasticsearch.common.bytes.BytesArray;
  25. import org.elasticsearch.common.bytes.BytesReference;
  26. import org.elasticsearch.common.collect.MapBuilder;
  27. import org.elasticsearch.common.compress.lzf.LZFCompressor;
  28. import org.elasticsearch.common.compress.snappy.UnavailableSnappyCompressor;
  29. import org.elasticsearch.common.compress.snappy.xerial.XerialSnappy;
  30. import org.elasticsearch.common.compress.snappy.xerial.XerialSnappyCompressor;
  31. import org.elasticsearch.common.io.Streams;
  32. import org.elasticsearch.common.io.stream.CachedStreamOutput;
  33. import org.elasticsearch.common.io.stream.StreamInput;
  34. import org.elasticsearch.common.logging.Loggers;
  35. import org.elasticsearch.common.settings.Settings;
  36. import org.jboss.netty.buffer.ChannelBuffer;
  37. import java.io.IOException;
  38. import java.util.List;
  39. import java.util.Locale;
  40. /**
  41. */
  42. public class CompressorFactory {
  43. private static final LZFCompressor LZF = new LZFCompressor();
  44. private static final Compressor[] compressors;
  45. private static final ImmutableMap<String, Compressor> compressorsByType;
  46. private static Compressor defaultCompressor;
  47. static {
  48. List<Compressor> compressorsX = Lists.newArrayList();
  49. compressorsX.add(LZF);
  50. boolean addedSnappy = false;
  51. if (XerialSnappy.available) {
  52. compressorsX.add(new XerialSnappyCompressor());
  53. addedSnappy = true;
  54. } else {
  55. Loggers.getLogger(CompressorFactory.class).debug("failed to load xerial snappy-java", XerialSnappy.failure);
  56. }
  57. if (!addedSnappy) {
  58. compressorsX.add(new UnavailableSnappyCompressor());
  59. }
  60. compressors = compressorsX.toArray(new Compressor[compressorsX.size()]);
  61. MapBuilder<String, Compressor> compressorsByTypeX = MapBuilder.newMapBuilder();
  62. for (Compressor compressor : compressors) {
  63. compressorsByTypeX.put(compressor.type(), compressor);
  64. }
  65. compressorsByType = compressorsByTypeX.immutableMap();
  66. defaultCompressor = LZF;
  67. }
  68. public static synchronized void configure(Settings settings) {
  69. for (Compressor compressor : compressors) {
  70. compressor.configure(settings);
  71. }
  72. String defaultType = settings.get("compress.default.type", "lzf").toLowerCase(Locale.ENGLISH);
  73. boolean found = false;
  74. for (Compressor compressor : compressors) {
  75. if (defaultType.equalsIgnoreCase(compressor.type())) {
  76. defaultCompressor = compressor;
  77. found = true;
  78. break;
  79. }
  80. }
  81. if (!found) {
  82. Loggers.getLogger(CompressorFactory.class).warn("failed to find default type [{}]", defaultType);
  83. }
  84. }
  85. public static synchronized void setDefaultCompressor(Compressor defaultCompressor) {
  86. CompressorFactory.defaultCompressor = defaultCompressor;
  87. }
  88. public static Compressor defaultCompressor() {
  89. return defaultCompressor;
  90. }
  91. public static boolean isCompressed(BytesReference bytes) {
  92. return compressor(bytes) != null;
  93. }
  94. public static boolean isCompressed(byte[] data) {
  95. return compressor(data, 0, data.length) != null;
  96. }
  97. public static boolean isCompressed(byte[] data, int offset, int length) {
  98. return compressor(data, offset, length) != null;
  99. }
  100. public static boolean isCompressed(IndexInput in) throws IOException {
  101. return compressor(in) != null;
  102. }
  103. @Nullable
  104. public static Compressor compressor(BytesReference bytes) {
  105. for (Compressor compressor : compressors) {
  106. if (compressor.isCompressed(bytes)) {
  107. return compressor;
  108. }
  109. }
  110. return null;
  111. }
  112. @Nullable
  113. public static Compressor compressor(byte[] data) {
  114. return compressor(data, 0, data.length);
  115. }
  116. @Nullable
  117. public static Compressor compressor(byte[] data, int offset, int length) {
  118. for (Compressor compressor : compressors) {
  119. if (compressor.isCompressed(data, offset, length)) {
  120. return compressor;
  121. }
  122. }
  123. return null;
  124. }
  125. @Nullable
  126. public static Compressor compressor(ChannelBuffer buffer) {
  127. for (Compressor compressor : compressors) {
  128. if (compressor.isCompressed(buffer)) {
  129. return compressor;
  130. }
  131. }
  132. return null;
  133. }
  134. @Nullable
  135. public static Compressor compressor(IndexInput in) throws IOException {
  136. for (Compressor compressor : compressors) {
  137. if (compressor.isCompressed(in)) {
  138. return compressor;
  139. }
  140. }
  141. return null;
  142. }
  143. public static Compressor compressor(String type) {
  144. return compressorsByType.get(type);
  145. }
  146. /**
  147. * Uncompress the provided data, data can be detected as compressed using {@link #isCompressed(byte[], int, int)}.
  148. */
  149. public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException {
  150. Compressor compressor = compressor(bytes);
  151. if (compressor != null) {
  152. if (bytes.hasArray()) {
  153. return new BytesArray(compressor.uncompress(bytes.array(), bytes.arrayOffset(), bytes.length()));
  154. }
  155. StreamInput compressed = compressor.streamInput(bytes.streamInput());
  156. CachedStreamOutput.Entry entry = CachedStreamOutput.popEntry();
  157. try {
  158. Streams.copy(compressed, entry.bytes());
  159. compressed.close();
  160. return new BytesArray(entry.bytes().bytes().toBytes());
  161. } finally {
  162. CachedStreamOutput.pushEntry(entry);
  163. }
  164. }
  165. return bytes;
  166. }
  167. }