mongo_sync/
config.rs

1/// Global mongo syncer configuration.
2#[derive(Debug)]
3pub struct OplogSyncerConfig {
4    src: Src,
5    oplog_storage: OplogStorage,
6}
7
8impl OplogSyncerConfig {
9    /// get source mongodb uri.
10    pub fn get_src_uri(&self) -> &str {
11        &self.src.uri
12    }
13
14    /// get target oplog storage mongodb uri.
15    pub fn get_oplog_storage_uri(&self) -> &str {
16        &self.oplog_storage.uri
17    }
18}
19
20#[derive(Debug)]
21pub struct OplogStorage {
22    /// Oplog storage uri
23    uri: String,
24}
25
26/// Source database configuration.
27#[derive(Debug)]
28pub struct Src {
29    /// Source database uri, it needs to be replica set, begins with 'mongodb://'
30    uri: String,
31}
32
33/// Detail sync config, it indicates which database to sync, or which collection to sync.
34#[derive(Debug)]
35pub struct DetailSyncConf {
36    /// target db uri.
37    dst_uri: String,
38    /// database name
39    db: String,
40    /// collections to sync, default it None, which means sync all collections.
41    colls: Option<Vec<String>>,
42    /// how many collections will be sync concurrently.
43    collection_concurrent: usize,
44    /// how many threads will used to sync one collection concurrently.
45    doc_concurrent: usize,
46}
47
48fn number_of_cpus() -> usize {
49    num_cpus::get()
50}
51
52fn half_number_of_cpus() -> usize {
53    num_cpus::get() / 2
54}
55
56#[derive(Debug)]
57/// database sync configuration.
58pub struct DbSyncConf {
59    src: Src,
60    oplog_storage: OplogStorage,
61    conf: DetailSyncConf,
62}
63
64impl DbSyncConf {
65    /// create a new configuration.
66    pub fn new(
67        src_uri: String,
68        target_uri: String,
69        oplog_storage_uri: String,
70        db: String,
71        colls: Option<Vec<String>>,
72        collection_concurrent: Option<usize>,
73        doc_concurrent: Option<usize>,
74    ) -> Self {
75        DbSyncConf {
76            src: Src { uri: src_uri },
77            oplog_storage: {
78                OplogStorage {
79                    uri: oplog_storage_uri,
80                }
81            },
82            conf: DetailSyncConf {
83                dst_uri: target_uri,
84                db,
85                colls,
86                collection_concurrent: collection_concurrent.unwrap_or_else(number_of_cpus),
87                doc_concurrent: doc_concurrent.unwrap_or_else(half_number_of_cpus),
88            },
89        }
90    }
91
92    /// get database to sync.
93    pub fn get_db(&self) -> &str {
94        &self.conf.db
95    }
96
97    /// get sync record collection name.
98    pub fn get_record_collection(&self) -> &str {
99        "oplog_records"
100    }
101
102    /// get oplog storage uri, which will save oplogs from source cluster.
103    pub fn get_oplog_storage_uri(&self) -> &str {
104        &self.oplog_storage.uri
105    }
106
107    /// get destination database uri.
108    pub fn get_dst_uri(&self) -> &str {
109        &self.conf.dst_uri
110    }
111
112    /// get source database uri.
113    pub fn get_src_uri(&self) -> &str {
114        &self.src.uri
115    }
116
117    /// return how many threads will be used when sync collecitons.
118    pub fn get_collection_concurrent(&self) -> usize {
119        self.conf.collection_concurrent
120    }
121
122    /// return how many threads will be used when sync one collection.
123    pub fn get_doc_concurrent(&self) -> usize {
124        self.conf.doc_concurrent
125    }
126
127    /// get collections to sync.
128    ///
129    /// When return None, it indicates that sync all collections in a database.
130    pub fn get_colls(&self) -> &Option<Vec<String>> {
131        &self.conf.colls
132    }
133}