Skip to content

Commit e967deb

Browse files
authored
feat: expose remove_all in FileIO (apache#643)
Signed-off-by: xxchan <[email protected]>
1 parent d03c4f8 commit e967deb

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

crates/iceberg/src/io/file_io.rs

+45-9
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,40 @@ impl FileIO {
7171
}
7272

7373
/// Deletes file.
74+
///
75+
/// # Arguments
76+
///
77+
/// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
7478
pub async fn delete(&self, path: impl AsRef<str>) -> Result<()> {
7579
let (op, relative_path) = self.inner.create_operator(&path)?;
7680
Ok(op.delete(relative_path).await?)
7781
}
7882

83+
/// Remove the path and all nested dirs and files recursively.
84+
///
85+
/// # Arguments
86+
///
87+
/// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
88+
pub async fn remove_all(&self, path: impl AsRef<str>) -> Result<()> {
89+
let (op, relative_path) = self.inner.create_operator(&path)?;
90+
Ok(op.remove_all(relative_path).await?)
91+
}
92+
7993
/// Check file exists.
94+
///
95+
/// # Arguments
96+
///
97+
/// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
8098
pub async fn is_exist(&self, path: impl AsRef<str>) -> Result<bool> {
8199
let (op, relative_path) = self.inner.create_operator(&path)?;
82100
Ok(op.is_exist(relative_path).await?)
83101
}
84102

85103
/// Creates input file.
104+
///
105+
/// # Arguments
106+
///
107+
/// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
86108
pub fn new_input(&self, path: impl AsRef<str>) -> Result<InputFile> {
87109
let (op, relative_path) = self.inner.create_operator(&path)?;
88110
let path = path.as_ref().to_string();
@@ -95,6 +117,10 @@ impl FileIO {
95117
}
96118

97119
/// Creates output file.
120+
///
121+
/// # Arguments
122+
///
123+
/// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
98124
pub fn new_output(&self, path: impl AsRef<str>) -> Result<OutputFile> {
99125
let (op, relative_path) = self.inner.create_operator(&path)?;
100126
let path = path.as_ref().to_string();
@@ -338,7 +364,7 @@ impl OutputFile {
338364

339365
#[cfg(test)]
340366
mod tests {
341-
use std::fs::File;
367+
use std::fs::{create_dir_all, File};
342368
use std::io::Write;
343369
use std::path::Path;
344370

@@ -353,6 +379,7 @@ mod tests {
353379
}
354380

355381
fn write_to_file<P: AsRef<Path>>(s: &str, path: P) {
382+
create_dir_all(path.as_ref().parent().unwrap()).unwrap();
356383
let mut f = File::create(path).unwrap();
357384
write!(f, "{s}").unwrap();
358385
}
@@ -389,16 +416,24 @@ mod tests {
389416
async fn test_delete_local_file() {
390417
let tmp_dir = TempDir::new().unwrap();
391418

392-
let file_name = "a.txt";
393-
let content = "Iceberg loves rust.";
394-
395-
let full_path = format!("{}/{}", tmp_dir.path().to_str().unwrap(), file_name);
396-
write_to_file(content, &full_path);
419+
let a_path = format!("{}/{}", tmp_dir.path().to_str().unwrap(), "a.txt");
420+
let sub_dir_path = format!("{}/sub", tmp_dir.path().to_str().unwrap());
421+
let b_path = format!("{}/{}", sub_dir_path, "b.txt");
422+
let c_path = format!("{}/{}", sub_dir_path, "c.txt");
423+
write_to_file("Iceberg loves rust.", &a_path);
424+
write_to_file("Iceberg loves rust.", &b_path);
425+
write_to_file("Iceberg loves rust.", &c_path);
397426

398427
let file_io = create_local_file_io();
399-
assert!(file_io.is_exist(&full_path).await.unwrap());
400-
file_io.delete(&full_path).await.unwrap();
401-
assert!(!file_io.is_exist(&full_path).await.unwrap());
428+
assert!(file_io.is_exist(&a_path).await.unwrap());
429+
430+
file_io.remove_all(&sub_dir_path).await.unwrap();
431+
assert!(!file_io.is_exist(&b_path).await.unwrap());
432+
assert!(!file_io.is_exist(&c_path).await.unwrap());
433+
assert!(file_io.is_exist(&a_path).await.unwrap());
434+
435+
file_io.delete(&a_path).await.unwrap();
436+
assert!(!file_io.is_exist(&a_path).await.unwrap());
402437
}
403438

404439
#[tokio::test]
@@ -411,6 +446,7 @@ mod tests {
411446
let file_io = create_local_file_io();
412447
assert!(!file_io.is_exist(&full_path).await.unwrap());
413448
assert!(file_io.delete(&full_path).await.is_ok());
449+
assert!(file_io.remove_all(&full_path).await.is_ok());
414450
}
415451

416452
#[tokio::test]

0 commit comments

Comments
 (0)