001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.compress.lz4;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023import org.apache.hadoop.conf.Configurable;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.CommonConfigurationKeys;
026import org.apache.hadoop.hbase.io.compress.CompressionUtil;
027import org.apache.hadoop.io.compress.BlockCompressorStream;
028import org.apache.hadoop.io.compress.BlockDecompressorStream;
029import org.apache.hadoop.io.compress.CompressionCodec;
030import org.apache.hadoop.io.compress.CompressionInputStream;
031import org.apache.hadoop.io.compress.CompressionOutputStream;
032import org.apache.hadoop.io.compress.Compressor;
033import org.apache.hadoop.io.compress.Decompressor;
034import org.apache.yetus.audience.InterfaceAudience;
035
036/**
037 * Hadoop Lz4 codec implemented with lz4-java.
038 * <p>
039 * This is data format compatible with Hadoop's native lz4 codec.
040 */
041@InterfaceAudience.Private
042public class Lz4Codec implements Configurable, CompressionCodec {
043
044  public static final String LZ4_BUFFER_SIZE_KEY = "hbase.io.compress.lz4.buffersize";
045
046  private Configuration conf;
047
048  public Lz4Codec() {
049    conf = new Configuration();
050  }
051
052  @Override
053  public Configuration getConf() {
054    return conf;
055  }
056
057  @Override
058  public void setConf(Configuration conf) {
059    this.conf = conf;
060  }
061
062  @Override
063  public Compressor createCompressor() {
064    return new Lz4Compressor(getBufferSize(conf));
065  }
066
067  @Override
068  public Decompressor createDecompressor() {
069    return new Lz4Decompressor(getBufferSize(conf));
070  }
071
072  @Override
073  public CompressionInputStream createInputStream(InputStream in) throws IOException {
074    return createInputStream(in, createDecompressor());
075  }
076
077  @Override
078  public CompressionInputStream createInputStream(InputStream in, Decompressor d)
079    throws IOException {
080    return new BlockDecompressorStream(in, d, getBufferSize(conf));
081  }
082
083  @Override
084  public CompressionOutputStream createOutputStream(OutputStream out) throws IOException {
085    return createOutputStream(out, createCompressor());
086  }
087
088  @Override
089  public CompressionOutputStream createOutputStream(OutputStream out, Compressor c)
090    throws IOException {
091    int bufferSize = getBufferSize(conf);
092    return new BlockCompressorStream(out, c, bufferSize,
093      CompressionUtil.compressionOverhead(bufferSize));
094  }
095
096  @Override
097  public Class<? extends Compressor> getCompressorType() {
098    return Lz4Compressor.class;
099  }
100
101  @Override
102  public Class<? extends Decompressor> getDecompressorType() {
103    return Lz4Decompressor.class;
104  }
105
106  @Override
107  public String getDefaultExtension() {
108    return ".lz4";
109  }
110
111  // Package private
112
113  static int getBufferSize(Configuration conf) {
114    return conf.getInt(LZ4_BUFFER_SIZE_KEY,
115      conf.getInt(CommonConfigurationKeys.IO_COMPRESSION_CODEC_LZ4_BUFFERSIZE_KEY,
116        CommonConfigurationKeys.IO_COMPRESSION_CODEC_LZ4_BUFFERSIZE_DEFAULT));
117  }
118
119}