Add Varint-length-prefixed flavor of LZ4
[folly.git] / folly / io / Compression.h
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef FOLLY_IO_COMPRESSION_H_
18 #define FOLLY_IO_COMPRESSION_H_
19
20 #include <cstdint>
21 #include <limits>
22 #include <memory>
23
24 #include "folly/io/IOBuf.h"
25
26 /**
27  * Compression / decompression over IOBufs
28  */
29
30 namespace folly { namespace io {
31
32 enum class CodecType {
33   /**
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.
37    */
38   USER_DEFINED = 0,
39
40   /**
41    * Use no compression.
42    * Levels supported: 0
43    */
44   NO_COMPRESSION = 1,
45
46   /**
47    * Use LZ4 compression.
48    * Levels supported: 1 = fast, 2 = best; default = 1
49    */
50   LZ4 = 2,
51
52   /**
53    * Use Snappy compression.
54    * Levels supported: 1
55    */
56   SNAPPY = 3,
57
58   /**
59    * Use zlib compression.
60    * Levels supported: 0 = no compression, 1 = fast, ..., 9 = best; default = 6
61    */
62   ZLIB = 4,
63
64   /**
65    * Use LZ4 compression, prefixed with size (as Varint).
66    */
67   LZ4_VARINT_SIZE = 5,
68
69   NUM_CODEC_TYPES = 6,
70 };
71
72 class Codec {
73  public:
74   virtual ~Codec() { }
75
76   /**
77    * Return the maximum length of data that may be compressed with this codec.
78    * NO_COMPRESSION and ZLIB support arbitrary lengths;
79    * LZ4 supports up to 1.9GiB; SNAPPY supports up to 4GiB.
80    */
81   uint64_t maxUncompressedLength() const;
82
83   /**
84    * Return the codec's type.
85    */
86   CodecType type() const { return type_; }
87
88   /**
89    * Does this codec need the exact uncompressed length on decompression?
90    */
91   bool needsUncompressedLength() const;
92
93   /**
94    * Compress data, returning an IOBuf (which may share storage with data).
95    * Throws std::invalid_argument if data is larger than
96    * maxUncompressedLength().
97    *
98    * Regardless of the behavior of the underlying compressor, compressing
99    * an empty IOBuf chain will return an empty IOBuf chain.
100    */
101   std::unique_ptr<IOBuf> compress(const folly::IOBuf* data);
102
103   /**
104    * Uncompress data. Throws std::runtime_error on decompression error.
105    *
106    * Some codecs (LZ4) require the exact uncompressed length; this is indicated
107    * by needsUncompressedLength().
108    *
109    * For other codes (zlib), knowing the exact uncompressed length ahead of
110    * time might be faster.
111    *
112    * Regardless of the behavior of the underlying compressor, uncompressing
113    * an empty IOBuf chain will return an empty IOBuf chain.
114    */
115   static constexpr uint64_t UNKNOWN_UNCOMPRESSED_LENGTH = uint64_t(-1);
116
117   std::unique_ptr<IOBuf> uncompress(
118       const IOBuf* data,
119       uint64_t uncompressedLength = UNKNOWN_UNCOMPRESSED_LENGTH);
120
121  protected:
122   explicit Codec(CodecType type);
123
124  private:
125   // default: no limits (save for special value UNKNOWN_UNCOMPRESSED_LENGTH)
126   virtual uint64_t doMaxUncompressedLength() const;
127   // default: doesn't need uncompressed length
128   virtual bool doNeedsUncompressedLength() const;
129   virtual std::unique_ptr<IOBuf> doCompress(const folly::IOBuf* data) = 0;
130   virtual std::unique_ptr<IOBuf> doUncompress(const folly::IOBuf* data,
131                                               uint64_t uncompressedLength) = 0;
132
133   CodecType type_;
134 };
135
136 constexpr int COMPRESSION_LEVEL_FASTEST = -1;
137 constexpr int COMPRESSION_LEVEL_DEFAULT = -2;
138 constexpr int COMPRESSION_LEVEL_BEST = -3;
139
140 /**
141  * Return a codec for the given type. Throws on error.  The level
142  * is a non-negative codec-dependent integer indicating the level of
143  * compression desired, or one of the following constants:
144  *
145  * COMPRESSION_LEVEL_FASTEST is fastest (uses least CPU / memory,
146  *   worst compression)
147  * COMPRESSION_LEVEL_DEFAULT is the default (likely a tradeoff between
148  *   FASTEST and BEST)
149  * COMPRESSION_LEVEL_BEST is the best compression (uses most CPU / memory,
150  *   best compression)
151  *
152  * When decompressing, the compression level is ignored. All codecs will
153  * decompress all data compressed with the a codec of the same type, regardless
154  * of compression level.
155  */
156 std::unique_ptr<Codec> getCodec(CodecType type,
157                                 int level = COMPRESSION_LEVEL_DEFAULT);
158
159 }}  // namespaces
160
161 #endif /* FOLLY_IO_COMPRESSION_H_ */
162