Skip to content

Commit 41f2c6b

Browse files
committed
refactor: simplify and enhance database size computation and improve tests clarity
1 parent c16d697 commit 41f2c6b

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

mithril-aggregator/src/artifact_builder/cardano_database.rs

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,41 +68,43 @@ impl ArtifactBuilder<CardanoDbBeacon, CardanoDatabase> for CardanoDatabaseArtifa
6868
}
6969
}
7070

71-
// Return the sum of the files size contained in the subdirectories 'immutable', 'ledger' and 'volatile'.
71+
/// Return the sum of the files size contained in the subdirectories 'immutable', 'ledger' and 'volatile'.
7272
fn compute_uncompressed_database_size(db_directory: &Path) -> StdResult<u64> {
7373
let subdirs = ["immutable", "ledger", "volatile"];
7474

75-
let mut total_db_size_uncompressed = 0;
76-
for subdir in subdirs {
77-
let dir_path = db_directory.join(subdir);
75+
subdirs
76+
.iter()
77+
.map(|subdir| {
78+
let dir_path = db_directory.join(subdir);
79+
compute_fs_entry_size(&dir_path)
80+
.with_context(|| format!("Failed to read metadata for directory: {:?}", dir_path))
81+
})
82+
.sum()
83+
}
7884

79-
total_db_size_uncompressed += get_directory_size(&dir_path)
80-
.with_context(|| format!("Failed to read metadata for directory: {:?}", dir_path))?;
81-
}
85+
fn compute_fs_entry_size(path: &Path) -> StdResult<u64> {
86+
if path.is_file() {
87+
let metadata = std::fs::metadata(path)
88+
.with_context(|| format!("Failed to read metadata for file: {:?}", path))?;
8289

83-
Ok(total_db_size_uncompressed)
84-
}
90+
return Ok(metadata.len());
91+
}
8592

86-
fn get_directory_size(path: &Path) -> StdResult<u64> {
87-
let entries =
88-
std::fs::read_dir(path).with_context(|| format!("Failed to read directory: {:?}", path))?;
89-
90-
let mut directory_size = 0;
91-
for entry in entries {
92-
let path = entry
93-
.with_context(|| format!("Failed to read directory entry in {:?}", path))?
94-
.path();
95-
96-
if path.is_file() {
97-
let metadata = std::fs::metadata(&path)
98-
.with_context(|| format!("Failed to read metadata for file: {:?}", path))?;
99-
directory_size += metadata.len();
100-
} else if path.is_dir() {
101-
directory_size += get_directory_size(&path)?;
93+
if path.is_dir() {
94+
let entries = std::fs::read_dir(path)
95+
.with_context(|| format!("Failed to read directory: {:?}", path))?;
96+
let mut directory_size = 0;
97+
for entry in entries {
98+
let path = entry
99+
.with_context(|| format!("Failed to read directory entry in {:?}", path))?
100+
.path();
101+
directory_size += compute_fs_entry_size(&path)?;
102102
}
103+
104+
return Ok(directory_size);
103105
}
104106

105-
Ok(directory_size)
107+
Ok(0)
106108
}
107109

108110
#[cfg(test)]
@@ -125,16 +127,20 @@ mod tests {
125127
fn should_compute_the_size_of_the_uncompressed_database_only_immutable_ledger_and_volatile() {
126128
let test_dir = get_test_directory("should_compute_the_size_of_the_uncompressed_database_only_immutable_ledger_and_volatile");
127129

130+
let immutable_file_size = 777;
131+
let ledger_file_size = 6666;
132+
let volatile_file_size = 99;
128133
DummyImmutablesDbBuilder::new(test_dir.as_os_str().to_str().unwrap())
129134
.with_immutables(&[1, 2])
130-
.set_immutable_file_size(1000)
135+
.set_immutable_file_size(immutable_file_size)
131136
.with_ledger_files(vec!["blocks-0.dat".to_string()])
132-
.set_ledger_file_size(5000)
137+
.set_ledger_file_size(ledger_file_size)
133138
.with_volatile_files(vec!["437".to_string(), "537".to_string()])
134-
.set_volatile_file_size(2000)
139+
.set_volatile_file_size(volatile_file_size)
135140
.build();
136141
// Number of immutable files = 2 × 3 ('chunk', 'primary' and 'secondary').
137-
let expected_total_size = 2 * 3 * 1000 + 5000 + 2000 * 2;
142+
let expected_total_size =
143+
(2 * 3 * immutable_file_size) + ledger_file_size + (2 * volatile_file_size);
138144

139145
std::fs::write(test_dir.join("non_computed_file.txt"), "file inside root").unwrap();
140146
let non_computed_dir = test_dir.join("non_computed_dir");
@@ -155,15 +161,15 @@ mod tests {
155161
let test_dir = get_test_directory("should_compute_valid_artifact");
156162

157163
DummyImmutablesDbBuilder::new(test_dir.as_os_str().to_str().unwrap())
158-
.with_immutables(&[1, 2])
159-
.set_immutable_file_size(1000)
164+
.with_immutables(&[1])
165+
.set_immutable_file_size(100)
160166
.with_ledger_files(vec!["blocks-0.dat".to_string()])
161-
.set_ledger_file_size(5000)
162-
.with_volatile_files(vec!["437".to_string(), "537".to_string()])
163-
.set_volatile_file_size(2000)
167+
.set_ledger_file_size(100)
168+
.with_volatile_files(vec!["437".to_string()])
169+
.set_volatile_file_size(100)
164170
.build();
165-
// Number of immutable files = 2 × 3 ('chunk', 'primary' and 'secondary').
166-
let expected_total_size = 2 * 3 * 1000 + 5000 + 2000 * 2;
171+
// Number of immutable files = 1 × 3 ('chunk', 'primary' and 'secondary').
172+
let expected_total_size = (3 * 100) + 100 + 100;
167173

168174
let cardano_database_artifact_builder = CardanoDatabaseArtifactBuilder::new(
169175
test_dir,

0 commit comments

Comments
 (0)