Skip to content

Commit d15e4f9

Browse files
committed
refactor(aggregator): move artifact builder size utils to tools
As they will be usefull for computing the aggregated size of files to be included in a tar appender.
1 parent bb84eeb commit d15e4f9

File tree

6 files changed

+15
-13
lines changed

6 files changed

+15
-13
lines changed

mithril-aggregator/src/artifact_builder/cardano_database.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ use mithril_common::{
1313
CardanoNetwork, StdResult,
1414
};
1515

16-
use crate::artifact_builder::{
17-
utils::compute_uncompressed_database_size, AncillaryArtifactBuilder, ArtifactBuilder,
18-
};
16+
use crate::artifact_builder::{AncillaryArtifactBuilder, ArtifactBuilder};
17+
use crate::tools::file_size;
1918

2019
use super::{DigestArtifactBuilder, ImmutableArtifactBuilder};
2120

@@ -70,7 +69,7 @@ impl ArtifactBuilder<CardanoDbBeacon, CardanoDatabaseSnapshot> for CardanoDataba
7069
let total_db_size_uncompressed = {
7170
let db_directory = self.db_directory.clone();
7271
tokio::task::spawn_blocking(move || -> StdResult<u64> {
73-
compute_uncompressed_database_size(&db_directory)
72+
file_size::compute_size_of_path(&db_directory)
7473
})
7574
.await??
7675
};
@@ -180,7 +179,7 @@ mod tests {
180179
let expected_total_size =
181180
(2 * immutable_trio_file_size) + (4 * ledger_file_size) + (3 * volatile_file_size);
182181

183-
let total_size = compute_uncompressed_database_size(&test_dir).unwrap();
182+
let total_size = file_size::compute_size_of_path(&test_dir).unwrap();
184183

185184
assert_eq!(expected_total_size, total_size);
186185
}

mithril-aggregator/src/artifact_builder/cardano_database_artifacts/ancillary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use mithril_common::{
1515
};
1616

1717
use crate::{
18-
artifact_builder::utils::compute_size,
1918
file_uploaders::{GcpUploader, LocalUploader},
2019
services::Snapshotter,
2120
tools::file_archiver::FileArchive,
21+
tools::file_size,
2222
DumbUploader, FileUploader,
2323
};
2424

@@ -222,7 +222,7 @@ impl AncillaryArtifactBuilder {
222222
let paths_to_include =
223223
Self::get_files_and_directories_to_snapshot(beacon.immutable_file_number);
224224

225-
compute_size(
225+
file_size::compute_size(
226226
paths_to_include
227227
.iter()
228228
.map(|path| db_path.join(path))

mithril-aggregator/src/artifact_builder/cardano_database_artifacts/immutable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use mithril_common::{
1717
};
1818

1919
use crate::{
20-
artifact_builder::utils::compute_size,
2120
file_uploaders::{GcpUploader, LocalUploader},
2221
services::Snapshotter,
22+
tools::file_size,
2323
DumbUploader, FileUploader,
2424
};
2525

@@ -269,7 +269,7 @@ impl ImmutableArtifactBuilder {
269269
.map(|filename| db_path.join(IMMUTABLE_DIR).join(filename))
270270
.collect();
271271

272-
let total_size = compute_size(immutable_paths)?;
272+
let total_size = file_size::compute_size(immutable_paths)?;
273273

274274
Ok(total_size / up_to_immutable_file_number)
275275
}

mithril-aggregator/src/artifact_builder/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ mod cardano_stake_distribution;
66
mod cardano_transactions;
77
mod interface;
88
mod mithril_stake_distribution;
9-
mod utils;
109

1110
pub use cardano_database::*;
1211
pub use cardano_database_artifacts::*;

mithril-aggregator/src/artifact_builder/utils.rs renamed to mithril-aggregator/src/tools/file_size.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ pub(crate) fn compute_size(paths: Vec<PathBuf>) -> StdResult<u64> {
3434

3535
let mut total = 0;
3636
for path_to_include in paths {
37-
total += compute_uncompressed_database_size(&path_to_include)?;
37+
total += compute_size_of_path(&path_to_include)?;
3838
}
3939
Ok(total)
4040
}
4141

42-
pub(crate) fn compute_uncompressed_database_size(path: &Path) -> StdResult<u64> {
42+
/// Compute the size of one given path that could be a file or a folder.
43+
///
44+
/// Returns 0 if the path is not a file or a folder.
45+
pub(crate) fn compute_size_of_path(path: &Path) -> StdResult<u64> {
4346
if path.is_file() {
4447
let metadata = std::fs::metadata(path)
4548
.with_context(|| format!("Failed to read metadata for file: {:?}", path))?;
@@ -55,7 +58,7 @@ pub(crate) fn compute_uncompressed_database_size(path: &Path) -> StdResult<u64>
5558
let path = entry
5659
.with_context(|| format!("Failed to read directory entry in {:?}", path))?
5760
.path();
58-
directory_size += compute_uncompressed_database_size(&path)?;
61+
directory_size += compute_size_of_path(&path)?;
5962
}
6063

6164
return Ok(directory_size);

mithril-aggregator/src/tools/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod certificates_hash_migrator;
22
mod digest_helpers;
33
mod era;
44
pub mod file_archiver;
5+
pub mod file_size;
56
mod genesis;
67
#[cfg(test)]
78
pub mod mocks;

0 commit comments

Comments
 (0)