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