| 1 | #pragma once | 
|---|
| 2 |  | 
|---|
| 3 | #include <Compression/CompressionInfo.h> | 
|---|
| 4 | #include <Compression/ICompressionCodec.h> | 
|---|
| 5 | #include <DataTypes/IDataType.h> | 
|---|
| 6 | #include <Parsers/IAST_fwd.h> | 
|---|
| 7 | #include <Common/IFactoryWithAliases.h> | 
|---|
| 8 |  | 
|---|
| 9 | #include <functional> | 
|---|
| 10 | #include <memory> | 
|---|
| 11 | #include <optional> | 
|---|
| 12 | #include <unordered_map> | 
|---|
| 13 |  | 
|---|
| 14 | namespace DB | 
|---|
| 15 | { | 
|---|
| 16 |  | 
|---|
| 17 | class ICompressionCodec; | 
|---|
| 18 |  | 
|---|
| 19 | using CompressionCodecPtr = std::shared_ptr<ICompressionCodec>; | 
|---|
| 20 |  | 
|---|
| 21 | using CodecNameWithLevel = std::pair<String, std::optional<int>>; | 
|---|
| 22 |  | 
|---|
| 23 | /** Creates a codec object by name of compression algorithm family and parameters. | 
|---|
| 24 | */ | 
|---|
| 25 | class CompressionCodecFactory final : private boost::noncopyable | 
|---|
| 26 | { | 
|---|
| 27 | protected: | 
|---|
| 28 | using Creator = std::function<CompressionCodecPtr(const ASTPtr & parameters)>; | 
|---|
| 29 | using CreatorWithType = std::function<CompressionCodecPtr(const ASTPtr & parameters, DataTypePtr column_type)>; | 
|---|
| 30 | using SimpleCreator = std::function<CompressionCodecPtr()>; | 
|---|
| 31 | using CompressionCodecsDictionary = std::unordered_map<String, CreatorWithType>; | 
|---|
| 32 | using CompressionCodecsCodeDictionary = std::unordered_map<UInt8, CreatorWithType>; | 
|---|
| 33 | public: | 
|---|
| 34 |  | 
|---|
| 35 | static CompressionCodecFactory & instance(); | 
|---|
| 36 |  | 
|---|
| 37 | /// Return default codec (currently LZ4) | 
|---|
| 38 | CompressionCodecPtr getDefaultCodec() const; | 
|---|
| 39 |  | 
|---|
| 40 | /// Get codec by AST and possible column_type | 
|---|
| 41 | /// some codecs can use information about type to improve inner settings | 
|---|
| 42 | /// but every codec should be able to work without information about type | 
|---|
| 43 | CompressionCodecPtr get(const ASTPtr & ast, DataTypePtr column_type = nullptr) const; | 
|---|
| 44 |  | 
|---|
| 45 | /// Get codec by method byte (no params available) | 
|---|
| 46 | CompressionCodecPtr get(const UInt8 byte_code) const; | 
|---|
| 47 |  | 
|---|
| 48 | /// For backward compatibility with config settings | 
|---|
| 49 | CompressionCodecPtr get(const String & family_name, std::optional<int> level) const; | 
|---|
| 50 |  | 
|---|
| 51 | /// Register codec with parameters and column type | 
|---|
| 52 | void registerCompressionCodecWithType(const String & family_name, std::optional<UInt8> byte_code, CreatorWithType creator); | 
|---|
| 53 | /// Register codec with parameters | 
|---|
| 54 | void registerCompressionCodec(const String & family_name, std::optional<UInt8> byte_code, Creator creator); | 
|---|
| 55 |  | 
|---|
| 56 | /// Register codec without parameters | 
|---|
| 57 | void registerSimpleCompressionCodec(const String & family_name, std::optional<UInt8> byte_code, SimpleCreator creator); | 
|---|
| 58 |  | 
|---|
| 59 | protected: | 
|---|
| 60 | CompressionCodecPtr getImpl(const String & family_name, const ASTPtr & arguments, DataTypePtr column_type) const; | 
|---|
| 61 |  | 
|---|
| 62 | private: | 
|---|
| 63 | CompressionCodecsDictionary family_name_with_codec; | 
|---|
| 64 | CompressionCodecsCodeDictionary family_code_with_codec; | 
|---|
| 65 | CompressionCodecPtr default_codec; | 
|---|
| 66 |  | 
|---|
| 67 | CompressionCodecFactory(); | 
|---|
| 68 | }; | 
|---|
| 69 |  | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|