@@ -15,19 +15,21 @@ use tokio::sync::Semaphore;
15
15
#[ derive( Clone , Debug ) ]
16
16
pub struct DB {
17
17
pub client : Client ,
18
- pub db : String
18
+ pub db : String ,
19
+ pub renamedb : Option < String >
19
20
}
20
21
21
22
type BoxResult < T > = std:: result:: Result < T , Box < dyn error:: Error + Send + Sync > > ;
22
23
23
24
impl DB {
24
- pub async fn init ( url : & str , db : & str ) -> BoxResult < Self > {
25
+ pub async fn init ( url : & str , db : & str , renamedb : Option < & str > ) -> BoxResult < Self > {
25
26
let mut client_options = ClientOptions :: parse ( url) . await ?;
26
27
client_options. app_name = Some ( "mongodb-stream-rs" . to_string ( ) ) ;
27
28
client_options. read_concern = Some ( ReadConcern :: local ( ) ) ;
28
29
Ok ( Self {
29
30
client : Client :: with_options ( client_options) ?,
30
- db : db. to_owned ( )
31
+ db : db. to_owned ( ) ,
32
+ renamedb : renamedb. map ( |s| s. into ( ) )
31
33
} )
32
34
}
33
35
@@ -113,7 +115,7 @@ impl DB {
113
115
} ;
114
116
115
117
// Set counter to total
116
- counter. total ( total) ;
118
+ counter. set_total ( total) ;
117
119
118
120
let cursor = collection_handle. find ( query, find_options) . await ?;
119
121
@@ -122,7 +124,10 @@ impl DB {
122
124
123
125
pub async fn insert_cursor ( & mut self , collection : & str , mut cursor : Cursor , mut counter : Counter ) -> BoxResult < ( ) > {
124
126
// Get handle on collection
125
- let coll = self . client . database ( & self . db ) . collection ( collection) ;
127
+ let coll = match & self . renamedb {
128
+ Some ( db) => self . client . database ( & db) . collection ( collection) ,
129
+ None => self . client . database ( & self . db ) . collection ( collection)
130
+ } ;
126
131
127
132
log:: info!( "{}.{}: Inserting {} docs" , self . db, collection, counter. total) ;
128
133
@@ -155,7 +160,10 @@ impl DB {
155
160
156
161
pub async fn validate_docs ( & mut self , collection : & str , mut cursor : Cursor , mut counter : Counter ) -> BoxResult < ( ) > {
157
162
// Get handle on collection
158
- let coll = self . client . database ( & self . db ) . collection ( collection) ;
163
+ let coll = match & self . renamedb {
164
+ Some ( db) => self . client . database ( & db) . collection ( collection) ,
165
+ None => self . client . database ( & self . db ) . collection ( collection)
166
+ } ;
159
167
160
168
log:: info!( "{}.{}: Validating that {} docs in destination exist in source" , self . db, collection, counter. total) ;
161
169
@@ -386,14 +394,18 @@ impl Counter {
386
394
// self.count = count as f64;
387
395
// }
388
396
389
- pub fn total ( & mut self , total : f64 ) {
397
+ pub fn set_total ( & mut self , total : f64 ) {
390
398
self . total = total;
391
399
}
392
400
393
401
pub fn count ( & self ) -> f64 {
394
402
self . count
395
403
}
396
404
405
+ pub fn total ( & self ) -> f64 {
406
+ self . total
407
+ }
408
+
397
409
pub fn incr ( & mut self , db : & str , collection : & str , count : f64 , start : i64 ) {
398
410
self . count += count;
399
411
let percent = self . count / self . total * 100.0 ;
0 commit comments