PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms 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
  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 */
 18package org.apache.hadoop.hive.ql.io;
 19
 20import java.util.ArrayList;
 21import java.util.HashMap;
 22import java.util.List;
 23import java.util.Map;
 24
 25import org.apache.commons.logging.Log;
 26import org.apache.commons.logging.LogFactory;
 27import org.apache.hadoop.io.compress.CompressionCodec;
 28import org.apache.hadoop.io.compress.Compressor;
 29import org.apache.hadoop.io.compress.Decompressor;
 30
 31/**
 32 * A global compressor/decompressor pool used to save and reuse (possibly
 33 * native) compression/decompression codecs.
 34 */
 35public final class CodecPool {
 36  private static final Log LOG = LogFactory.getLog(CodecPool.class);
 37
 38  /**
 39   * A global compressor pool used to save the expensive
 40   * construction/destruction of (possibly native) decompression codecs.
 41   */
 42  private static final Map<Class<Compressor>, List<Compressor>> COMPRESSOR_POOL =
 43      new HashMap<Class<Compressor>, List<Compressor>>();
 44
 45  /**
 46   * A global decompressor pool used to save the expensive
 47   * construction/destruction of (possibly native) decompression codecs.
 48   */
 49  private static final Map<Class<Decompressor>, List<Decompressor>> DECOMPRESSOR_POOL =
 50      new HashMap<Class<Decompressor>, List<Decompressor>>();
 51
 52  private static <T> T borrow(Map<Class<T>, List<T>> pool,
 53      Class<? extends T> codecClass) {
 54    T codec = null;
 55
 56    // Check if an appropriate codec is available
 57    synchronized (pool) {
 58      if (pool.containsKey(codecClass)) {
 59        List<T> codecList = pool.get(codecClass);
 60
 61        if (codecList != null) {
 62          synchronized (codecList) {
 63            if (!codecList.isEmpty()) {
 64              codec = codecList.remove(codecList.size() - 1);
 65            }
 66          }
 67        }
 68      }
 69    }
 70
 71    return codec;
 72  }
 73
 74  private static <T> void payback(Map<Class<T>, List<T>> pool, T codec) {
 75    if (codec != null) {
 76      Class<T> codecClass = (Class<T>) codec.getClass();
 77      synchronized (pool) {
 78        if (!pool.containsKey(codecClass)) {
 79          pool.put(codecClass, new ArrayList<T>());
 80        }
 81
 82        List<T> codecList = pool.get(codecClass);
 83        synchronized (codecList) {
 84          codecList.add(codec);
 85        }
 86      }
 87    }
 88  }
 89
 90  /**
 91   * Get a {@link Compressor} for the given {@link CompressionCodec} from the
 92   * pool or a new one.
 93   * 
 94   * @param codec
 95   *          the <code>CompressionCodec</code> for which to get the
 96   *          <code>Compressor</code>
 97   * @return <code>Compressor</code> for the given <code>CompressionCodec</code>
 98   *         from the pool or a new one
 99   */
100  public static Compressor getCompressor(CompressionCodec codec) {
101    Compressor compressor = borrow(COMPRESSOR_POOL, codec.getCompressorType());
102    if (compressor == null) {
103      compressor = codec.createCompressor();
104      LOG.info("Got brand-new compressor");
105    } else {
106      LOG.debug("Got recycled compressor");
107    }
108    return compressor;
109  }
110
111  /**
112   * Get a {@link Decompressor} for the given {@link CompressionCodec} from the
113   * pool or a new one.
114   * 
115   * @param codec
116   *          the <code>CompressionCodec</code> for which to get the
117   *          <code>Decompressor</code>
118   * @return <code>Decompressor</code> for the given
119   *         <code>CompressionCodec</code> the pool or a new one
120   */
121  public static Decompressor getDecompressor(CompressionCodec codec) {
122    Decompressor decompressor = borrow(DECOMPRESSOR_POOL, codec
123        .getDecompressorType());
124    if (decompressor == null) {
125      decompressor = codec.createDecompressor();
126      LOG.info("Got brand-new decompressor");
127    } else {
128      LOG.debug("Got recycled decompressor");
129    }
130    return decompressor;
131  }
132
133  /**
134   * Return the {@link Compressor} to the pool.
135   * 
136   * @param compressor
137   *          the <code>Compressor</code> to be returned to the pool
138   */
139  public static void returnCompressor(Compressor compressor) {
140    if (compressor == null) {
141      return;
142    }
143    compressor.reset();
144    payback(COMPRESSOR_POOL, compressor);
145  }
146
147  /**
148   * Return the {@link Decompressor} to the pool.
149   * 
150   * @param decompressor
151   *          the <code>Decompressor</code> to be returned to the pool
152   */
153  public static void returnDecompressor(Decompressor decompressor) {
154    if (decompressor == null) {
155      return;
156    }
157    decompressor.reset();
158    payback(DECOMPRESSOR_POOL, decompressor);
159  }
160
161  private CodecPool() {
162    // prevent instantiation
163  }
164}