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