Skip to content

Commit 0a6f003

Browse files
authored
refactor: refactor decompress and add compress utils. (databendlabs#13060)
* refactor: refactor decompress and add compress utils. * small refactor.
1 parent 57e0b5b commit 0a6f003

File tree

8 files changed

+468
-224
lines changed

8 files changed

+468
-224
lines changed

.typos.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"INOUT" = "INOUT"
1212
"ser" = "ser"
1313
"Ser" = "Ser"
14+
"flate" = "flate"
1415

1516
[files]
1617
extend-exclude = [

Cargo.lock

+16-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/compress/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ edition = { workspace = true }
88

99
[dependencies]
1010
# Temp workaround, should come back to tagged version after https://github.com/Nemo157/async-compression/issues/150 resolved.
11-
async-compression = { package = "async-compression-issue-150-workaround", version = "0.3.15-issue-150", features = [
11+
async-compression = { git = "https://github.com/youngsofun/async-compression", rev = "1568ceafd", features = [
1212
"futures-io",
1313
"all-algorithms",
1414
] }
15+
brotli = { version = "3.3.0", features = ["std"] }
1516
bytes = { workspace = true }
17+
common-exception = { path = "../exception" }
1618
futures = "0.3"
1719
log = { workspace = true }
1820
pin-project = "1"
@@ -21,5 +23,4 @@ serde = { workspace = true }
2123
[dev-dependencies]
2224
env_logger = "0.10"
2325
rand = "0.8"
24-
sha2 = "0.10"
2526
tokio = { version = "1", features = ["rt", "macros"] }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2021 Datafuse Labs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::path::PathBuf;
16+
17+
use serde::Deserialize;
18+
use serde::Serialize;
19+
20+
/// CompressAlgorithm represents all compress algorithm that OpenDAL supports.
21+
#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Debug)]
22+
pub enum CompressAlgorithm {
23+
/// [Brotli](https://github.com/google/brotli) compression format.
24+
Brotli,
25+
/// [bzip2](http://sourceware.org/bzip2/) compression format.
26+
Bz2,
27+
/// [Deflate](https://datatracker.ietf.org/doc/html/rfc1951) Compressed Data Format.
28+
///
29+
/// Similar to [`CompressAlgorithm::Gzip`] and [`CompressAlgorithm::Zlib`]
30+
Deflate,
31+
/// [Gzip](https://datatracker.ietf.org/doc/html/rfc1952) compress format.
32+
///
33+
/// Similar to [`CompressAlgorithm::Deflate`] and [`CompressAlgorithm::Zlib`]
34+
Gzip,
35+
/// [LZMA](https://www.7-zip.org/sdk.html) compress format.
36+
Lzma,
37+
/// [Xz](https://tukaani.org/xz/) compress format, the successor of [`CompressAlgorithm::Lzma`].
38+
Xz,
39+
/// [Zlib](https://datatracker.ietf.org/doc/html/rfc1950) compress format.
40+
///
41+
/// Similar to [`CompressAlgorithm::Deflate`] and [`CompressAlgorithm::Gzip`]
42+
Zlib,
43+
/// [Zstd](https://github.com/facebook/zstd) compression algorithm
44+
Zstd,
45+
}
46+
47+
impl CompressAlgorithm {
48+
/// Get the file extension of this compress algorithm.
49+
pub fn extension(&self) -> &str {
50+
match self {
51+
CompressAlgorithm::Brotli => "br",
52+
CompressAlgorithm::Bz2 => "bz2",
53+
CompressAlgorithm::Deflate => "deflate",
54+
CompressAlgorithm::Gzip => "gz",
55+
CompressAlgorithm::Lzma => "lzma",
56+
CompressAlgorithm::Xz => "xz",
57+
CompressAlgorithm::Zlib => "zl",
58+
CompressAlgorithm::Zstd => "zstd",
59+
}
60+
}
61+
62+
/// Create CompressAlgorithm from file extension.
63+
///
64+
/// If the file extension is not supported, `None` will be return instead.
65+
pub fn from_extension(ext: &str) -> Option<CompressAlgorithm> {
66+
match ext {
67+
"br" => Some(CompressAlgorithm::Brotli),
68+
"bz2" => Some(CompressAlgorithm::Bz2),
69+
"deflate" => Some(CompressAlgorithm::Deflate),
70+
"gz" => Some(CompressAlgorithm::Gzip),
71+
"lzma" => Some(CompressAlgorithm::Lzma),
72+
"xz" => Some(CompressAlgorithm::Xz),
73+
"zl" => Some(CompressAlgorithm::Zlib),
74+
"zstd" | "zst" => Some(CompressAlgorithm::Zstd),
75+
_ => None,
76+
}
77+
}
78+
79+
/// Create CompressAlgorithm from file path.
80+
///
81+
/// If the extension in file path is not supported, `None` will be return instead.
82+
pub fn from_path(path: &str) -> Option<CompressAlgorithm> {
83+
let ext = PathBuf::from(path)
84+
.extension()
85+
.map(|s| s.to_string_lossy())?
86+
.to_string();
87+
88+
CompressAlgorithm::from_extension(&ext)
89+
}
90+
}

0 commit comments

Comments
 (0)