1+ use ckb_app_config:: ExportTarget ;
12use ckb_jsonrpc_types:: BlockView as JsonBlock ;
23use ckb_jsonrpc_types:: Either ;
34use ckb_shared:: Snapshot ;
@@ -13,13 +14,12 @@ use std::error::Error;
1314use std:: fs;
1415use std:: io;
1516use std:: io:: Write ;
16- use std:: path:: PathBuf ;
1717use std:: sync:: Arc ;
1818
1919/// Export block from database to specify file.
2020pub struct Export {
2121 /// export target path
22- pub target : PathBuf ,
22+ pub target : ExportTarget ,
2323 /// CKB shared data.
2424 pub shared : Shared ,
2525 /// the snapshot of the shared data
@@ -35,7 +35,7 @@ impl Export {
3535 /// Creates the export job.
3636 pub fn new (
3737 shared : Shared ,
38- target : PathBuf ,
38+ target : ExportTarget ,
3939 from : Option < Either < BlockNumber , H256 > > ,
4040 to : Option < Either < BlockNumber , H256 > > ,
4141 ) -> Self {
@@ -52,17 +52,18 @@ impl Export {
5252 /// export file name
5353 fn file_name ( & self , from_number : u64 , to_number : u64 ) -> Result < String , Box < dyn Error > > {
5454 Ok ( format ! (
55- "{}-{}-{}.{} " ,
55+ "{}-{}-{}.jsonl " ,
5656 self . shared. consensus( ) . id,
5757 from_number,
58- to_number,
59- "jsonl"
58+ to_number
6059 ) )
6160 }
6261
6362 /// Executes the export job.
6463 pub fn execute ( self ) -> Result < ( ) , Box < dyn Error > > {
65- fs:: create_dir_all ( & self . target ) ?;
64+ if let ExportTarget :: Path ( ref path) = self . target {
65+ fs:: create_dir_all ( path) ?;
66+ }
6667 self . write_to_json ( )
6768 }
6869
@@ -89,16 +90,24 @@ impl Export {
8990 return Err ( format ! ( "Invalid range: from {} to {}" , from_number, to_number) . into ( ) ) ;
9091 }
9192
92- let file_name = self . file_name ( from_number, to_number) ?;
93- let file_path = self . target . join ( file_name) ;
94- println ! ( "Writing to {}" , file_path. display( ) ) ;
95- let f = fs:: OpenOptions :: new ( )
96- . create_new ( true )
97- . read ( true )
98- . write ( true )
99- . open ( file_path) ?;
93+ let mut writer: Box < dyn Write > = {
94+ match self . target {
95+ ExportTarget :: Path ( ref path_buf) => {
96+ let file_name = self . file_name ( from_number, to_number) ?;
97+ let file_path = path_buf. join ( file_name) ;
98+ println ! ( "Writing to {}" , file_path. display( ) ) ;
99+ let f = fs:: OpenOptions :: new ( )
100+ . create_new ( true )
101+ . read ( true )
102+ . write ( true )
103+ . open ( file_path) ?;
104+
105+ Box :: new ( io:: BufWriter :: new ( f) )
106+ }
107+ ExportTarget :: Stdout => Box :: new ( std:: io:: stdout ( ) ) ,
108+ }
109+ } ;
100110
101- let mut writer = io:: BufWriter :: new ( f) ;
102111 let snapshot = self . shared . snapshot ( ) ;
103112
104113 #[ cfg( feature = "progress_bar" ) ]
0 commit comments