clickhouse/compression/mod.rs
1#[cfg(feature = "lz4")]
2pub(crate) mod lz4;
3#[cfg(feature = "zstd")]
4pub(crate) mod zstd;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum Compression {
9 /// Disables any compression.
10 /// Used by default if no compression feature is enabled.
11 None,
12 /// Uses `LZ4` codec to (de)compress.
13 /// Used by default if the `lz4` feature is enabled.
14 #[cfg(feature = "lz4")]
15 Lz4,
16 /// Uses `LZ4HC` codec to compress and `LZ4` to decompress.
17 /// High compression levels are useful in networks with low bandwidth.
18 /// Affects only `INSERT`s, because others are compressed by the server.
19 /// Possible levels: `[1, 12]`. Recommended level range: `[4, 9]`.
20 ///
21 /// Deprecated: `lz4_flex` doesn't support HC mode yet: [lz4_flex#165].
22 ///
23 /// [lz4_flex#165]: https://github.com/PSeitz/lz4_flex/issues/165
24 #[cfg(feature = "lz4")]
25 #[deprecated(note = "use `Compression::Lz4` instead")]
26 Lz4Hc(i32),
27 /// Uses `ZSTD` codec to (de)compress.
28 /// Used by default if the `zstd` feature is enabled and `lz4` is not.
29 /// The `i32` parameter specifies the compression level.
30 /// Use [`Compression::zstd()`] for the default level.
31 ///
32 /// **Note:** Extremely high compression levels (e.g. above 19) are very
33 /// CPU-intensive and likely unsuitable for real-time networked
34 /// applications. They can also block the async executor for a
35 /// significant amount of time. Prefer moderate levels for online usage.
36 #[cfg(feature = "zstd")]
37 Zstd(i32),
38}
39
40impl Default for Compression {
41 #[cfg(all(not(feature = "test-util"), feature = "lz4"))]
42 #[inline]
43 fn default() -> Self {
44 Compression::Lz4
45 }
46
47 #[cfg(all(not(feature = "test-util"), not(feature = "lz4"), feature = "zstd"))]
48 #[inline]
49 fn default() -> Self {
50 Compression::zstd()
51 }
52
53 #[cfg(any(feature = "test-util", not(any(feature = "lz4", feature = "zstd"))))]
54 #[inline]
55 fn default() -> Self {
56 Compression::None
57 }
58}
59
60impl Compression {
61 /// Creates a `Zstd` compression with the default level.
62 #[cfg(feature = "zstd")]
63 pub fn zstd() -> Self {
64 Compression::Zstd(::zstd::DEFAULT_COMPRESSION_LEVEL)
65 }
66
67 pub(crate) fn is_enabled(&self) -> bool {
68 *self != Compression::None
69 }
70}