@@ -29,28 +29,65 @@ use std::path::PathBuf;
2929use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
3030
3131enum QueryOutputFileWriter {
32- Plain ( File ) ,
33- LZ4Compressed ( lz4:: Encoder < File > ) ,
34- GzipCompressed ( flate2:: write:: GzEncoder < File > ) ,
35- ZstdCompressed ( zstd:: stream:: write:: Encoder < ' static , File > ) ,
32+ Plain ( Option < File > ) ,
33+ LZ4Compressed ( Option < lz4:: Encoder < File > > ) ,
34+ GzipCompressed ( Option < flate2:: write:: GzEncoder < File > > ) ,
35+ ZstdCompressed ( Option < zstd:: stream:: write:: Encoder < ' static , File > > ) ,
36+ BZ2Compressed ( Option < bzip2:: write:: BzEncoder < File > > ) ,
37+ XZCompressed ( Option < xz2:: write:: XzEncoder < File > > ) ,
3638}
3739
3840impl Write for QueryOutputFileWriter {
3941 fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
4042 match self {
41- QueryOutputFileWriter :: Plain ( w) => w. write ( buf) ,
42- QueryOutputFileWriter :: LZ4Compressed ( w) => w. write ( buf) ,
43- QueryOutputFileWriter :: GzipCompressed ( w) => w. write ( buf) ,
44- QueryOutputFileWriter :: ZstdCompressed ( w) => w. write ( buf) ,
43+ QueryOutputFileWriter :: Plain ( w) => w. as_mut ( ) . unwrap ( ) . write ( buf) ,
44+ QueryOutputFileWriter :: LZ4Compressed ( w) => w. as_mut ( ) . unwrap ( ) . write ( buf) ,
45+ QueryOutputFileWriter :: GzipCompressed ( w) => w. as_mut ( ) . unwrap ( ) . write ( buf) ,
46+ QueryOutputFileWriter :: ZstdCompressed ( w) => w. as_mut ( ) . unwrap ( ) . write ( buf) ,
47+ QueryOutputFileWriter :: BZ2Compressed ( w) => w. as_mut ( ) . unwrap ( ) . write ( buf) ,
48+ QueryOutputFileWriter :: XZCompressed ( w) => w. as_mut ( ) . unwrap ( ) . write ( buf) ,
4549 }
4650 }
4751
4852 fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
4953 match self {
50- QueryOutputFileWriter :: Plain ( w) => w. flush ( ) ,
51- QueryOutputFileWriter :: LZ4Compressed ( w) => w. flush ( ) ,
52- QueryOutputFileWriter :: GzipCompressed ( w) => w. flush ( ) ,
53- QueryOutputFileWriter :: ZstdCompressed ( w) => w. flush ( ) ,
54+ QueryOutputFileWriter :: Plain ( w) => w. as_mut ( ) . unwrap ( ) . flush ( ) ,
55+ QueryOutputFileWriter :: LZ4Compressed ( w) => w. as_mut ( ) . unwrap ( ) . flush ( ) ,
56+ QueryOutputFileWriter :: GzipCompressed ( w) => w. as_mut ( ) . unwrap ( ) . flush ( ) ,
57+ QueryOutputFileWriter :: ZstdCompressed ( w) => w. as_mut ( ) . unwrap ( ) . flush ( ) ,
58+ QueryOutputFileWriter :: BZ2Compressed ( w) => w. as_mut ( ) . unwrap ( ) . flush ( ) ,
59+ QueryOutputFileWriter :: XZCompressed ( w) => w. as_mut ( ) . unwrap ( ) . flush ( ) ,
60+ }
61+ }
62+ }
63+
64+ impl Drop for QueryOutputFileWriter {
65+ fn drop ( & mut self ) {
66+ match self {
67+ QueryOutputFileWriter :: Plain ( writer) => {
68+ writer. as_mut ( ) . unwrap ( ) . flush ( ) . unwrap ( ) ;
69+ }
70+ QueryOutputFileWriter :: LZ4Compressed ( writer) => {
71+ let ( mut file, err) = writer. take ( ) . unwrap ( ) . finish ( ) ;
72+ err. unwrap ( ) ;
73+ file. flush ( ) . unwrap ( ) ;
74+ }
75+ QueryOutputFileWriter :: GzipCompressed ( writer) => {
76+ let mut file = writer. take ( ) . unwrap ( ) . finish ( ) . unwrap ( ) ;
77+ file. flush ( ) . unwrap ( ) ;
78+ }
79+ QueryOutputFileWriter :: ZstdCompressed ( writer) => {
80+ let mut file = writer. take ( ) . unwrap ( ) . finish ( ) . unwrap ( ) ;
81+ file. flush ( ) . unwrap ( ) ;
82+ }
83+ QueryOutputFileWriter :: BZ2Compressed ( writer) => {
84+ let mut file = writer. take ( ) . unwrap ( ) . finish ( ) . unwrap ( ) ;
85+ file. flush ( ) . unwrap ( ) ;
86+ }
87+ QueryOutputFileWriter :: XZCompressed ( writer) => {
88+ let mut file = writer. take ( ) . unwrap ( ) . finish ( ) . unwrap ( ) ;
89+ file. flush ( ) . unwrap ( ) ;
90+ }
5491 }
5592 }
5693}
@@ -93,26 +130,37 @@ pub fn colored_query_output<MH: HashFunctionFactory, CX: ColorsManager>(
93130 let query_output = Mutex :: new ( (
94131 BufWriter :: new (
95132 match output_file. extension ( ) . map ( |e| e. to_str ( ) ) . flatten ( ) {
96- Some ( "lz4" ) => QueryOutputFileWriter :: LZ4Compressed (
133+ Some ( "lz4" ) => QueryOutputFileWriter :: LZ4Compressed ( Some (
97134 lz4:: EncoderBuilder :: new ( )
98135 . level ( output_compression_level. min ( 16 ) )
99136 . build ( query_output_file)
100137 . unwrap ( ) ,
101- ) ,
138+ ) ) ,
102139 Some ( "gz" ) => {
103- QueryOutputFileWriter :: GzipCompressed ( flate2:: GzBuilder :: new ( ) . write (
104- query_output_file,
105- Compression :: new ( output_compression_level. min ( 9 ) ) ,
140+ QueryOutputFileWriter :: GzipCompressed ( Some (
141+ flate2:: GzBuilder :: new ( ) . write (
142+ query_output_file,
143+ Compression :: new ( output_compression_level. min ( 9 ) ) ,
144+ ) ,
106145 ) )
107146 }
108- Some ( "zst" ) | Some ( "zstd" ) => QueryOutputFileWriter :: ZstdCompressed (
147+ Some ( "zst" ) | Some ( "zstd" ) => QueryOutputFileWriter :: ZstdCompressed ( Some (
109148 zstd:: stream:: write:: Encoder :: new (
110149 query_output_file,
111150 output_compression_level. min ( 22 ) as i32 ,
112151 )
113152 . unwrap ( ) ,
114- ) ,
115- _ => QueryOutputFileWriter :: Plain ( query_output_file) ,
153+ ) ) ,
154+ Some ( "bz2" ) => QueryOutputFileWriter :: BZ2Compressed ( Some (
155+ bzip2:: write:: BzEncoder :: new (
156+ query_output_file,
157+ bzip2:: Compression :: new ( output_compression_level. min ( 9 ) ) ,
158+ ) ,
159+ ) ) ,
160+ Some ( "xz" ) => QueryOutputFileWriter :: XZCompressed ( Some (
161+ xz2:: write:: XzEncoder :: new ( query_output_file, output_compression_level. min ( 9 ) ) ,
162+ ) ) ,
163+ _ => QueryOutputFileWriter :: Plain ( Some ( query_output_file) ) ,
116164 } ,
117165 ) ,
118166 0 ,
0 commit comments