@@ -68,41 +68,43 @@ impl ArtifactBuilder<CardanoDbBeacon, CardanoDatabase> for CardanoDatabaseArtifa
68
68
}
69
69
}
70
70
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'.
72
72
fn compute_uncompressed_database_size ( db_directory : & Path ) -> StdResult < u64 > {
73
73
let subdirs = [ "immutable" , "ledger" , "volatile" ] ;
74
74
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
+ }
78
84
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) ) ?;
82
89
83
- Ok ( total_db_size_uncompressed )
84
- }
90
+ return Ok ( metadata . len ( ) ) ;
91
+ }
85
92
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) ?;
102
102
}
103
+
104
+ return Ok ( directory_size) ;
103
105
}
104
106
105
- Ok ( directory_size )
107
+ Ok ( 0 )
106
108
}
107
109
108
110
#[ cfg( test) ]
@@ -125,16 +127,20 @@ mod tests {
125
127
fn should_compute_the_size_of_the_uncompressed_database_only_immutable_ledger_and_volatile ( ) {
126
128
let test_dir = get_test_directory ( "should_compute_the_size_of_the_uncompressed_database_only_immutable_ledger_and_volatile" ) ;
127
129
130
+ let immutable_file_size = 777 ;
131
+ let ledger_file_size = 6666 ;
132
+ let volatile_file_size = 99 ;
128
133
DummyImmutablesDbBuilder :: new ( test_dir. as_os_str ( ) . to_str ( ) . unwrap ( ) )
129
134
. with_immutables ( & [ 1 , 2 ] )
130
- . set_immutable_file_size ( 1000 )
135
+ . set_immutable_file_size ( immutable_file_size )
131
136
. with_ledger_files ( vec ! [ "blocks-0.dat" . to_string( ) ] )
132
- . set_ledger_file_size ( 5000 )
137
+ . set_ledger_file_size ( ledger_file_size )
133
138
. with_volatile_files ( vec ! [ "437" . to_string( ) , "537" . to_string( ) ] )
134
- . set_volatile_file_size ( 2000 )
139
+ . set_volatile_file_size ( volatile_file_size )
135
140
. build ( ) ;
136
141
// 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) ;
138
144
139
145
std:: fs:: write ( test_dir. join ( "non_computed_file.txt" ) , "file inside root" ) . unwrap ( ) ;
140
146
let non_computed_dir = test_dir. join ( "non_computed_dir" ) ;
@@ -155,15 +161,15 @@ mod tests {
155
161
let test_dir = get_test_directory ( "should_compute_valid_artifact" ) ;
156
162
157
163
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 )
160
166
. 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 )
164
170
. 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 ;
167
173
168
174
let cardano_database_artifact_builder = CardanoDatabaseArtifactBuilder :: new (
169
175
test_dir,
0 commit comments