2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <folly/Range.h>
24 #include <folly/io/IOBuf.h>
27 * Compression / decompression over IOBufs
30 namespace folly { namespace io {
32 enum class CodecType {
34 * This codec type is not defined; getCodec() will throw an exception
35 * if used. Useful if deriving your own classes from Codec without
36 * going through the getCodec() interface.
47 * Use LZ4 compression.
48 * Levels supported: 1 = fast, 2 = best; default = 1
53 * Use Snappy compression.
59 * Use zlib compression.
60 * Levels supported: 0 = no compression, 1 = fast, ..., 9 = best; default = 6
65 * Use LZ4 compression, prefixed with size (as Varint).
70 * Use LZMA2 compression.
71 * Levels supported: 0 = no compression, 1 = fast, ..., 9 = best; default = 6
74 LZMA2_VARINT_SIZE = 7,
77 * Use ZSTD compression.
82 * Use gzip compression. This is the same compression algorithm as ZLIB but
83 * gzip-compressed files tend to be easier to work with from the command line.
84 * Levels supported: 0 = no compression, 1 = fast, ..., 9 = best; default = 6
96 * Return the maximum length of data that may be compressed with this codec.
97 * NO_COMPRESSION and ZLIB support arbitrary lengths;
98 * LZ4 supports up to 1.9GiB; SNAPPY supports up to 4GiB.
99 * May return UNLIMITED_UNCOMPRESSED_LENGTH if unlimited.
101 uint64_t maxUncompressedLength() const;
104 * Return the codec's type.
106 CodecType type() const { return type_; }
109 * Does this codec need the exact uncompressed length on decompression?
111 bool needsUncompressedLength() const;
114 * Compress data, returning an IOBuf (which may share storage with data).
115 * Throws std::invalid_argument if data is larger than
116 * maxUncompressedLength().
118 * Regardless of the behavior of the underlying compressor, compressing
119 * an empty IOBuf chain will return an empty IOBuf chain.
121 std::unique_ptr<IOBuf> compress(const folly::IOBuf* data);
124 * Compresses data. May involve additional copies compared to the overload
125 * that takes and returns IOBufs. Has the same error semantics as the IOBuf
128 std::string compress(StringPiece data);
131 * Uncompress data. Throws std::runtime_error on decompression error.
133 * Some codecs (LZ4) require the exact uncompressed length; this is indicated
134 * by needsUncompressedLength().
136 * For other codes (zlib), knowing the exact uncompressed length ahead of
137 * time might be faster.
139 * Regardless of the behavior of the underlying compressor, uncompressing
140 * an empty IOBuf chain will return an empty IOBuf chain.
142 static constexpr uint64_t UNKNOWN_UNCOMPRESSED_LENGTH = uint64_t(-1);
143 static constexpr uint64_t UNLIMITED_UNCOMPRESSED_LENGTH = uint64_t(-2);
145 std::unique_ptr<IOBuf> uncompress(
147 uint64_t uncompressedLength = UNKNOWN_UNCOMPRESSED_LENGTH);
150 * Uncompresses data. May involve additional copies compared to the overload
151 * that takes and returns IOBufs. Has the same error semantics as the IOBuf
154 std::string uncompress(
156 uint64_t uncompressedLength = UNKNOWN_UNCOMPRESSED_LENGTH);
159 explicit Codec(CodecType type);
162 // default: no limits (save for special value UNKNOWN_UNCOMPRESSED_LENGTH)
163 virtual uint64_t doMaxUncompressedLength() const;
164 // default: doesn't need uncompressed length
165 virtual bool doNeedsUncompressedLength() const;
166 virtual std::unique_ptr<IOBuf> doCompress(const folly::IOBuf* data) = 0;
167 virtual std::unique_ptr<IOBuf> doUncompress(const folly::IOBuf* data,
168 uint64_t uncompressedLength) = 0;
169 // default: an implementation is provided by default to wrap the strings into
170 // IOBufs and delegate to the IOBuf methods. This incurs a copy of the output
171 // from IOBuf to string. Implementers, at their discretion, can override
172 // these methods to avoid the copy.
173 virtual std::string doCompressString(StringPiece data);
174 virtual std::string doUncompressString(
176 uint64_t uncompressedLength);
181 constexpr int COMPRESSION_LEVEL_FASTEST = -1;
182 constexpr int COMPRESSION_LEVEL_DEFAULT = -2;
183 constexpr int COMPRESSION_LEVEL_BEST = -3;
186 * Return a codec for the given type. Throws on error. The level
187 * is a non-negative codec-dependent integer indicating the level of
188 * compression desired, or one of the following constants:
190 * COMPRESSION_LEVEL_FASTEST is fastest (uses least CPU / memory,
192 * COMPRESSION_LEVEL_DEFAULT is the default (likely a tradeoff between
194 * COMPRESSION_LEVEL_BEST is the best compression (uses most CPU / memory,
197 * When decompressing, the compression level is ignored. All codecs will
198 * decompress all data compressed with the a codec of the same type, regardless
199 * of compression level.
201 std::unique_ptr<Codec> getCodec(CodecType type,
202 int level = COMPRESSION_LEVEL_DEFAULT);
205 * Check if a specified codec is supported.
207 bool hasCodec(CodecType type);