PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/io/CodecPool.java

#
Java | 164 lines | 85 code | 16 blank | 63 comment | 17 complexity | 3630c900fb7658f18adb6be2e6808c95 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * 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, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hive.ql.io;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.apache.hadoop.io.compress.CompressionCodec;
  26. import org.apache.hadoop.io.compress.Compressor;
  27. import org.apache.hadoop.io.compress.Decompressor;
  28. /**
  29. * A global compressor/decompressor pool used to save and reuse (possibly
  30. * native) compression/decompression codecs.
  31. */
  32. public final class CodecPool {
  33. private static final Log LOG = LogFactory.getLog(CodecPool.class);
  34. /**
  35. * A global compressor pool used to save the expensive
  36. * construction/destruction of (possibly native) decompression codecs.
  37. */
  38. private static final Map<Class<Compressor>, List<Compressor>> COMPRESSOR_POOL =
  39. new HashMap<Class<Compressor>, List<Compressor>>();
  40. /**
  41. * A global decompressor pool used to save the expensive
  42. * construction/destruction of (possibly native) decompression codecs.
  43. */
  44. private static final Map<Class<Decompressor>, List<Decompressor>> DECOMPRESSOR_POOL =
  45. new HashMap<Class<Decompressor>, List<Decompressor>>();
  46. private static <T> T borrow(Map<Class<T>, List<T>> pool,
  47. Class<? extends T> codecClass) {
  48. T codec = null;
  49. // Check if an appropriate codec is available
  50. synchronized (pool) {
  51. if (pool.containsKey(codecClass)) {
  52. List<T> codecList = pool.get(codecClass);
  53. if (codecList != null) {
  54. synchronized (codecList) {
  55. if (!codecList.isEmpty()) {
  56. codec = codecList.remove(codecList.size() - 1);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. return codec;
  63. }
  64. private static <T> void payback(Map<Class<T>, List<T>> pool, T codec) {
  65. if (codec != null) {
  66. Class<T> codecClass = (Class<T>) codec.getClass();
  67. synchronized (pool) {
  68. if (!pool.containsKey(codecClass)) {
  69. pool.put(codecClass, new ArrayList<T>());
  70. }
  71. List<T> codecList = pool.get(codecClass);
  72. synchronized (codecList) {
  73. codecList.add(codec);
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * Get a {@link Compressor} for the given {@link CompressionCodec} from the
  80. * pool or a new one.
  81. *
  82. * @param codec
  83. * the <code>CompressionCodec</code> for which to get the
  84. * <code>Compressor</code>
  85. * @return <code>Compressor</code> for the given <code>CompressionCodec</code>
  86. * from the pool or a new one
  87. */
  88. public static Compressor getCompressor(CompressionCodec codec) {
  89. Compressor compressor = borrow(COMPRESSOR_POOL, codec.getCompressorType());
  90. if (compressor == null) {
  91. compressor = codec.createCompressor();
  92. LOG.info("Got brand-new compressor");
  93. } else {
  94. LOG.debug("Got recycled compressor");
  95. }
  96. return compressor;
  97. }
  98. /**
  99. * Get a {@link Decompressor} for the given {@link CompressionCodec} from the
  100. * pool or a new one.
  101. *
  102. * @param codec
  103. * the <code>CompressionCodec</code> for which to get the
  104. * <code>Decompressor</code>
  105. * @return <code>Decompressor</code> for the given
  106. * <code>CompressionCodec</code> the pool or a new one
  107. */
  108. public static Decompressor getDecompressor(CompressionCodec codec) {
  109. Decompressor decompressor = borrow(DECOMPRESSOR_POOL, codec
  110. .getDecompressorType());
  111. if (decompressor == null) {
  112. decompressor = codec.createDecompressor();
  113. LOG.info("Got brand-new decompressor");
  114. } else {
  115. LOG.debug("Got recycled decompressor");
  116. }
  117. return decompressor;
  118. }
  119. /**
  120. * Return the {@link Compressor} to the pool.
  121. *
  122. * @param compressor
  123. * the <code>Compressor</code> to be returned to the pool
  124. */
  125. public static void returnCompressor(Compressor compressor) {
  126. if (compressor == null) {
  127. return;
  128. }
  129. compressor.reset();
  130. payback(COMPRESSOR_POOL, compressor);
  131. }
  132. /**
  133. * Return the {@link Decompressor} to the pool.
  134. *
  135. * @param decompressor
  136. * the <code>Decompressor</code> to be returned to the pool
  137. */
  138. public static void returnDecompressor(Decompressor decompressor) {
  139. if (decompressor == null) {
  140. return;
  141. }
  142. decompressor.reset();
  143. payback(DECOMPRESSOR_POOL, decompressor);
  144. }
  145. private CodecPool() {
  146. // prevent instantiation
  147. }
  148. }