11use std:: fs;
22use std:: path:: { Path , PathBuf } ;
3- use { BlockType , Box3 , File , Header , Mat , Result , Vec3 } ;
3+ use { Box3 , File , Header , Mat , Result , Vec3 } ;
44
55#[ derive( Debug , Clone ) ]
66pub struct Dataset {
@@ -140,7 +140,7 @@ impl Dataset {
140140 }
141141
142142 let file_len_vx_log2 = self . header . file_len_vx_log2 ( ) as u32 ;
143- if self . header . block_type == BlockType :: LZ4 || self . header . block_type == BlockType :: LZ4HC {
143+ if self . header . is_compressed ( ) {
144144 let file_len_vec = Vec3 :: from ( 1 << file_len_vx_log2) ;
145145 let is_dst_aligned = dst_pos % file_len_vec == Vec3 :: from ( 0 ) ;
146146 let is_shape_aligned = mat. shape % file_len_vec == Vec3 :: from ( 0 ) ;
@@ -170,6 +170,11 @@ impl Dataset {
170170 cur_path. push ( format ! ( "y{}" , cur_y) ) ;
171171 cur_path. push ( format ! ( "x{}.wkw" , cur_x) ) ;
172172
173+ // writing compressed file into temporary file first
174+ if self . header . is_compressed ( ) {
175+ cur_path. set_extension ( "wkw_tmp" ) ;
176+ }
177+
173178 // bounding box
174179 let cur_file_ids = Vec3 {
175180 x : cur_x,
@@ -186,22 +191,38 @@ impl Dataset {
186191 let cur_src_pos = cur_box. min ( ) - dst_pos;
187192 let cur_dst_pos = cur_box. min ( ) - cur_file_box. min ( ) ;
188193
189- let mut file = match File :: open_or_create ( & cur_path, & self . header ) {
190- Ok ( file) => file,
191- Err ( err) => {
192- return Err ( format ! (
193- "Error while open file {:?} for writing: {}" ,
194- & cur_path, err
195- ) ) ;
194+ {
195+ let mut file = match File :: open_or_create ( & cur_path, & self . header ) {
196+ Ok ( file) => file,
197+ Err ( err) => {
198+ return Err ( format ! (
199+ "Error while open file {:?} for writing: {}" ,
200+ & cur_path, err
201+ ) ) ;
202+ }
203+ } ;
204+ match file. write_mat ( cur_dst_pos, mat, cur_src_pos) {
205+ Ok ( _) => { }
206+ Err ( err) => {
207+ return Err ( format ! (
208+ "Error while writing to file {:?}: {}" ,
209+ & cur_path, err
210+ ) ) ;
211+ }
196212 }
197- } ;
198- match file. write_mat ( cur_dst_pos, mat, cur_src_pos) {
199- Ok ( _) => { }
200- Err ( err) => {
201- return Err ( format ! (
202- "Error while writing to file {:?}: {}" ,
203- & cur_path, err
204- ) ) ;
213+ }
214+ // moving compressed file into final file
215+ if self . header . is_compressed ( ) {
216+ let mut new_path = cur_path. clone ( ) ;
217+ new_path. set_extension ( "wkw" ) ;
218+ match File :: rename ( & cur_path, & new_path) {
219+ Ok ( _) => { }
220+ Err ( err) => {
221+ return Err ( format ! (
222+ "Error while renaming temporary file {:?} to {:?}: {}" ,
223+ & cur_path, & new_path, err
224+ ) ) ;
225+ }
205226 }
206227 }
207228 }
0 commit comments