All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.orc.impl.OrcCodecPool Maven / Gradle / Ivy

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.apache.orc.impl;

import java.util.concurrent.ConcurrentHashMap;

import java.util.ArrayList;
import java.util.List;

import org.apache.orc.CompressionCodec;
import org.apache.orc.CompressionKind;
import com.facebook.presto.hive.$internal.org.slf4j.Logger;
import com.facebook.presto.hive.$internal.org.slf4j.LoggerFactory;

/**
 * A clone of Hadoop codec pool for ORC; cause it has its own codecs...
 */
public final class OrcCodecPool {
  private static final Logger LOG = LoggerFactory.getLogger(OrcCodecPool.class);

  /**
   * A global decompressor pool used to save the expensive
   * construction/destruction of (possibly native) decompression codecs.
   */
  private static final ConcurrentHashMap> POOL =
      new ConcurrentHashMap<>();

  private static final int MAX_PER_KIND = 32;

  public static CompressionCodec getCodec(CompressionKind kind) {
    if (kind == CompressionKind.NONE) return null;
    CompressionCodec codec = null;
    List codecList = POOL.get(kind);
    if (codecList != null) {
      synchronized (codecList) {
        if (!codecList.isEmpty()) {
          codec = codecList.remove(codecList.size() - 1);
        }
      }
    }
    if (codec == null) {
      codec = WriterImpl.createCodec(kind);
      LOG.info("Got brand-new codec " + kind);
    } else {
      LOG.debug("Got recycled codec");
    }
    return codec;
  }

  public static void returnCodec(CompressionKind kind, CompressionCodec codec) {
    if (codec == null) {
      return;
    }
    assert kind != CompressionKind.NONE;
    codec.reset();
    List list = POOL.get(kind);
    if (list == null) {
      List newList = new ArrayList<>();
      List oldList = POOL.putIfAbsent(kind, newList);
      list = (oldList == null) ? newList : oldList;
    }
    synchronized (list) {
      if (list.size() < MAX_PER_KIND) {
        list.add(codec);
        return;
      }
    }
    // We didn't add the codec to the list.
    codec.close();
  }

  public static int getPoolSize(CompressionKind kind) {
    if (kind == CompressionKind.NONE) return 0;
    List codecList = POOL.get(kind);
    if (codecList == null) return 0;
    synchronized (codecList) {
      return codecList.size();
    }
  }

  private OrcCodecPool() {
    // prevent instantiation
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy