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.xerial;
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.io.compress.BlockCompressorStream;
027import org.apache.hadoop.io.compress.BlockDecompressorStream;
028import org.apache.hadoop.io.compress.CompressionCodec;
029import org.apache.hadoop.io.compress.CompressionInputStream;
030import org.apache.hadoop.io.compress.CompressionOutputStream;
031import org.apache.hadoop.io.compress.Compressor;
032import org.apache.hadoop.io.compress.Decompressor;
033import org.apache.yetus.audience.InterfaceAudience;
034import org.xerial.snappy.Snappy;
035
036/**
037 * Hadoop Snappy codec implemented with Xerial Snappy.
038 * <p>
039 * This is data format compatible with Hadoop's native snappy codec.
040 */
041@InterfaceAudience.Private
042public class SnappyCodec implements Configurable, CompressionCodec {
043
044  public static final String SNAPPY_BUFFER_SIZE_KEY = "hbase.io.compress.snappy.buffersize";
045
046  private Configuration conf;
047
048  public SnappyCodec() {
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 SnappyCompressor(getBufferSize(conf));
065  }
066
067  @Override
068  public Decompressor createDecompressor() {
069    return new SnappyDecompressor(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      Snappy.maxCompressedLength(bufferSize) - bufferSize); // overhead only
094  }
095
096  @Override
097  public Class<? extends Compressor> getCompressorType() {
098    return SnappyCompressor.class;
099  }
100
101  @Override
102  public Class<? extends Decompressor> getDecompressorType() {
103    return SnappyDecompressor.class;
104  }
105
106  @Override
107  public String getDefaultExtension() {
108    return ".snappy";
109  }
110
111  // Package private
112
113  static int getBufferSize(Configuration conf) {
114    return conf.getInt(SNAPPY_BUFFER_SIZE_KEY,
115      conf.getInt(CommonConfigurationKeys.IO_COMPRESSION_CODEC_SNAPPY_BUFFERSIZE_KEY,
116        CommonConfigurationKeys.IO_COMPRESSION_CODEC_SNAPPY_BUFFERSIZE_DEFAULT));
117  }
118
119}