Skip to content

Commit

Permalink
style: enforce rust code style (#14)
Browse files Browse the repository at this point in the history
Use `rustfmt` and `clippy` to enforce code style through CI check job.
  • Loading branch information
xushiyan authored May 5, 2024
1 parent 374a125 commit 586d210
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 197 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Check License Header
- name: Check license header
uses: apache/skywalking-eyes/[email protected]
- name: Check code style
run: make check

build:
runs-on: ${{ matrix.os }}
Expand All @@ -55,6 +56,5 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Unit Test
- name: Unit test
run: cargo test --no-fail-fast --all-targets --all-features --workspace
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ members = [
resolver = "2"

[workspace.package]
version = "0.2.0"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
rust-version = "1.75.0"
Expand All @@ -31,7 +31,7 @@ rust-version = "1.75.0"
# arrow
arrow = { version = "50" }
arrow-arith = { version = "50" }
arrow-array = { version = "50", features = ["chrono-tz"]}
arrow-array = { version = "50", features = ["chrono-tz"] }
arrow-buffer = { version = "50" }
arrow-cast = { version = "50" }
arrow-ipc = { version = "50" }
Expand Down Expand Up @@ -68,4 +68,4 @@ uuid = { version = "1" }
async-trait = { version = "0.1" }
futures = { version = "0.3" }
tokio = { version = "1" }
num_cpus = { version = "1" }
num_cpus = { version = "1" }
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

.EXPORT_ALL_VARIABLES:

RUST_LOG = debug

build:
cargo build

check-fmt:
cargo fmt --all -- --check

check-clippy:
cargo clippy --all-targets --all-features --workspace -- -D warnings

check: check-fmt check-clippy
11 changes: 0 additions & 11 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,15 @@

use std::error::Error;
use std::fmt::Debug;
use std::io;

use thiserror::Error;

#[derive(Debug, Error)]
pub enum HudiFileGroupError {
#[error("Base File {0} has unsupported format: {1}")]
UnsupportedBaseFileFormat(String, String),
#[error("Commit time {0} is already present in File Group {1}")]
CommitTimeAlreadyExists(String, String),
}

#[derive(Debug, Error)]
pub enum HudiTimelineError {
#[error("Error in reading commit metadata: {0}")]
FailToReadCommitMetadata(io::Error),
}

#[derive(Debug, Error)]
pub enum HudiFileSystemViewError {
#[error("Error in loading partitions: {0}")]
Expand All @@ -47,8 +38,6 @@ pub enum HudiFileSystemViewError {
pub enum HudiCoreError {
#[error("Failed to load file group")]
FailToLoadFileGroup(#[from] HudiFileGroupError),
#[error("Failed to init timeline")]
FailToInitTimeline(#[from] HudiTimelineError),
#[error("Failed to build file system view")]
FailToBuildFileSystemView(#[from] HudiFileSystemViewError),
}
66 changes: 37 additions & 29 deletions crates/core/src/file_group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use hudi_fs::file_systems::FileMetadata;
use crate::error::HudiFileGroupError;
use crate::error::HudiFileGroupError::CommitTimeAlreadyExists;

#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct BaseFile {
pub file_group_id: String,
pub commit_time: String,
Expand All @@ -53,12 +53,13 @@ impl BaseFile {
}
}

#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct FileSlice {
pub base_file: BaseFile,
pub partition_path: Option<String>,
}

#[allow(dead_code)]
impl FileSlice {
pub fn file_path(&self) -> Option<&str> {
match &self.base_file.metadata {
Expand Down Expand Up @@ -99,6 +100,7 @@ impl fmt::Display for FileGroup {
}
}

#[allow(dead_code)]
impl FileGroup {
pub fn new(id: String, partition_path: Option<String>) -> Self {
Self {
Expand Down Expand Up @@ -140,32 +142,38 @@ impl FileGroup {
}
}

#[test]
fn create_a_base_file_successfully() {
let base_file =
BaseFile::new("5a226868-2934-4f84-a16f-55124630c68d-0_0-7-24_20240402144910683.parquet");
assert_eq!(
base_file.file_group_id,
"5a226868-2934-4f84-a16f-55124630c68d-0"
);
assert_eq!(base_file.commit_time, "20240402144910683");
}
#[cfg(test)]
mod tests {
use crate::file_group::{BaseFile, FileGroup};

#[test]
fn create_a_base_file_successfully() {
let base_file = BaseFile::new(
"5a226868-2934-4f84-a16f-55124630c68d-0_0-7-24_20240402144910683.parquet",
);
assert_eq!(
base_file.file_group_id,
"5a226868-2934-4f84-a16f-55124630c68d-0"
);
assert_eq!(base_file.commit_time, "20240402144910683");
}

#[test]
fn load_a_valid_file_group() {
let mut fg = FileGroup::new("5a226868-2934-4f84-a16f-55124630c68d-0".to_owned(), None);
let _ = fg.add_base_file_from_name(
"5a226868-2934-4f84-a16f-55124630c68d-0_0-7-24_20240402144910683.parquet",
);
let _ = fg.add_base_file_from_name(
"5a226868-2934-4f84-a16f-55124630c68d-0_2-10-0_20240402123035233.parquet",
);
assert_eq!(fg.file_slices.len(), 2);
assert!(fg.partition_path.is_none());
let commit_times: Vec<&str> = fg.file_slices.keys().map(|k| k.as_str()).collect();
assert_eq!(commit_times, vec!["20240402123035233", "20240402144910683"]);
assert_eq!(
fg.get_latest_file_slice().unwrap().base_file.commit_time,
"20240402144910683"
)
#[test]
fn load_a_valid_file_group() {
let mut fg = FileGroup::new("5a226868-2934-4f84-a16f-55124630c68d-0".to_owned(), None);
let _ = fg.add_base_file_from_name(
"5a226868-2934-4f84-a16f-55124630c68d-0_0-7-24_20240402144910683.parquet",
);
let _ = fg.add_base_file_from_name(
"5a226868-2934-4f84-a16f-55124630c68d-0_2-10-0_20240402123035233.parquet",
);
assert_eq!(fg.file_slices.len(), 2);
assert!(fg.partition_path.is_none());
let commit_times: Vec<&str> = fg.file_slices.keys().map(|k| k.as_str()).collect();
assert_eq!(commit_times, vec!["20240402123035233", "20240402144910683"]);
assert_eq!(
fg.get_latest_file_slice().unwrap().base_file.commit_time,
"20240402144910683"
)
}
}
64 changes: 35 additions & 29 deletions crates/core/src/table/file_system_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ use crate::error::HudiFileSystemViewError::FailToLoadPartitions;
use crate::file_group::{FileGroup, FileSlice};
use crate::table::meta_client::MetaClient;
use hashbrown::HashMap;
use hudi_fs::test_utils::extract_test_table;
use std::collections::HashSet;
use std::path::Path;

pub struct FileSystemView {
meta_client: MetaClient,
Expand Down Expand Up @@ -62,37 +59,46 @@ impl FileSystemView {
let mut file_slices = Vec::new();
for fgs in self.partition_to_file_groups.values() {
for fg in fgs {
match fg.get_latest_file_slice() {
Some(file_slice) => file_slices.push(file_slice.clone()),
None => (),
if let Some(file_slice) = fg.get_latest_file_slice() {
file_slices.push(file_slice)
}
}
}
file_slices
}
}
#[test]
fn meta_client_get_file_groups() {
let fixture_path = Path::new("fixtures/table/0.x_cow_partitioned.zip");
let target_table_path = extract_test_table(fixture_path);
let meta_client = MetaClient::new(&target_table_path);
let fs_view = FileSystemView::init(meta_client).unwrap();
let file_slices = fs_view.get_latest_file_slices();
assert_eq!(file_slices.len(), 5);
let mut fg_ids = Vec::new();
for f in file_slices {
let fp = f.file_group_id();
fg_ids.push(fp);

#[cfg(test)]
mod tests {
use crate::table::file_system_view::FileSystemView;
use crate::table::meta_client::MetaClient;
use hudi_fs::test_utils::extract_test_table;
use std::collections::HashSet;
use std::path::Path;

#[test]
fn meta_client_get_file_groups() {
let fixture_path = Path::new("fixtures/table/0.x_cow_partitioned.zip");
let target_table_path = extract_test_table(fixture_path);
let meta_client = MetaClient::new(&target_table_path);
let fs_view = FileSystemView::init(meta_client).unwrap();
let file_slices = fs_view.get_latest_file_slices();
assert_eq!(file_slices.len(), 5);
let mut fg_ids = Vec::new();
for f in file_slices {
let fp = f.file_group_id();
fg_ids.push(fp);
}
let actual: HashSet<&str> = fg_ids.into_iter().collect();
assert_eq!(
actual,
HashSet::from_iter(vec![
"780b8586-3ad0-48ef-a6a1-d2217845ce4a-0",
"d9082ffd-2eb1-4394-aefc-deb4a61ecc57-0",
"ee915c68-d7f8-44f6-9759-e691add290d8-0",
"68d3c349-f621-4cd8-9e8b-c6dd8eb20d08-0",
"5a226868-2934-4f84-a16f-55124630c68d-0"
])
);
}
let actual: HashSet<&str> = fg_ids.into_iter().collect();
assert_eq!(
actual,
HashSet::from_iter(vec![
"780b8586-3ad0-48ef-a6a1-d2217845ce4a-0",
"d9082ffd-2eb1-4394-aefc-deb4a61ecc57-0",
"ee915c68-d7f8-44f6-9759-e691add290d8-0",
"68d3c349-f621-4cd8-9e8b-c6dd8eb20d08-0",
"5a226868-2934-4f84-a16f-55124630c68d-0"
])
);
}
Loading

0 comments on commit 586d210

Please sign in to comment.