@@ -24,6 +24,16 @@ pub struct DummyImmutableDb {
24
24
pub non_immutables_files : Vec < PathBuf > ,
25
25
}
26
26
27
+ impl DummyImmutableDb {
28
+ /// Add an immutable chunk file and its primary & secondary to the dummy DB.
29
+ pub fn add_immutable_file ( & mut self ) {
30
+ let last_file_number = self . immutables_files . last ( ) . map ( |f| f. number ) . unwrap_or ( 1 ) ;
31
+ let mut new_files = write_immutable_trio ( None , & self . dir , last_file_number + 1 ) ;
32
+
33
+ self . immutables_files . append ( & mut new_files) ;
34
+ }
35
+ }
36
+
27
37
impl DummyImmutablesDbBuilder {
28
38
/// [DummyImmutablesDbBuilder] factory, will create a folder with the given `dirname` in the
29
39
/// system temp directory, if it exists already it will be cleaned.
@@ -73,21 +83,25 @@ impl DummyImmutablesDbBuilder {
73
83
immutable_numbers. sort ( ) ;
74
84
75
85
if self . append_uncompleted_trio {
76
- self . write_immutable_trio ( match immutable_numbers. last ( ) {
77
- None => 0 ,
78
- Some ( last) => last + 1 ,
79
- } ) ;
86
+ write_immutable_trio (
87
+ self . file_size ,
88
+ & self . dir ,
89
+ match immutable_numbers. last ( ) {
90
+ None => 0 ,
91
+ Some ( last) => last + 1 ,
92
+ } ,
93
+ ) ;
80
94
}
81
95
82
96
for non_immutable in & self . non_immutables_to_write {
83
- non_immutables_files. push ( self . write_dummy_file ( non_immutable) ) ;
97
+ non_immutables_files. push ( write_dummy_file ( self . file_size , & self . dir , non_immutable) ) ;
84
98
}
85
99
86
100
DummyImmutableDb {
87
101
dir : self . dir . clone ( ) ,
88
102
immutables_files : immutable_numbers
89
103
. into_iter ( )
90
- . flat_map ( |ifn| self . write_immutable_trio ( ifn) )
104
+ . flat_map ( |ifn| write_immutable_trio ( self . file_size , & self . dir , ifn) )
91
105
. collect :: < Vec < _ > > ( ) ,
92
106
non_immutables_files,
93
107
}
@@ -108,37 +122,41 @@ impl DummyImmutablesDbBuilder {
108
122
109
123
parent_dir
110
124
}
125
+ }
111
126
112
- fn write_immutable_trio ( & self , immutable : ImmutableFileNumber ) -> Vec < ImmutableFile > {
113
- let mut result = vec ! [ ] ;
114
- for filename in [
115
- format ! ( "{immutable:05}.chunk" ) ,
116
- format ! ( "{immutable:05}.primary" ) ,
117
- format ! ( "{immutable:05}.secondary" ) ,
118
- ] {
119
- let file = self . write_dummy_file ( & filename) ;
120
- result. push ( ImmutableFile {
121
- number : immutable. to_owned ( ) ,
122
- path : file,
123
- filename : filename. to_string ( ) ,
124
- } ) ;
125
- }
126
- result
127
+ fn write_immutable_trio (
128
+ optional_size : Option < u64 > ,
129
+ dir : & Path ,
130
+ immutable : ImmutableFileNumber ,
131
+ ) -> Vec < ImmutableFile > {
132
+ let mut result = vec ! [ ] ;
133
+ for filename in [
134
+ format ! ( "{immutable:05}.chunk" ) ,
135
+ format ! ( "{immutable:05}.primary" ) ,
136
+ format ! ( "{immutable:05}.secondary" ) ,
137
+ ] {
138
+ let file = write_dummy_file ( optional_size, dir, & filename) ;
139
+ result. push ( ImmutableFile {
140
+ number : immutable. to_owned ( ) ,
141
+ path : file,
142
+ filename : filename. to_string ( ) ,
143
+ } ) ;
127
144
}
145
+ result
146
+ }
128
147
129
- /// Create a file with the given name in the given dir, write some text to it, and then
130
- /// return its path.
131
- fn write_dummy_file ( & self , filename : & str ) -> PathBuf {
132
- let file = self . dir . join ( Path :: new ( filename) ) ;
133
- let mut source_file = File :: create ( & file) . unwrap ( ) ;
148
+ /// Create a file with the given name in the given dir, write some text to it, and then
149
+ /// return its path.
150
+ fn write_dummy_file ( optional_size : Option < u64 > , dir : & Path , filename : & str ) -> PathBuf {
151
+ let file = dir. join ( Path :: new ( filename) ) ;
152
+ let mut source_file = File :: create ( & file) . unwrap ( ) ;
134
153
135
- write ! ( source_file, "This is a test file named '{filename}'" ) . unwrap ( ) ;
154
+ write ! ( source_file, "This is a test file named '{filename}'" ) . unwrap ( ) ;
136
155
137
- if let Some ( file_size) = self . file_size {
138
- writeln ! ( source_file) . unwrap ( ) ;
139
- source_file. set_len ( file_size) . unwrap ( ) ;
140
- }
141
-
142
- file
156
+ if let Some ( file_size) = optional_size {
157
+ writeln ! ( source_file) . unwrap ( ) ;
158
+ source_file. set_len ( file_size) . unwrap ( ) ;
143
159
}
160
+
161
+ file
144
162
}
0 commit comments