1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use bsonrs::Document;
use crate::core::bsonc::Bsonc;
use crate::core::database::Database as CoreDatabase;
use crate::client::{Mongo, Client};
use crate::collection::Collection;
use crate::ReadPreference;
use crate::ReadConcern;
use crate::WriteConcern;
use crate::command::CommandOptions;
use crate::change_stream::{ChangeStream, WatchOptions};
use crate::aggregate::AggregateOptions;
use crate::cursor::Cursor;
use crate::error::Result;
pub mod options;
#[derive(Clone)]
pub struct DB {
pub(crate) name: String,
pub(crate) mongo: Mongo,
pub(crate) read_preference: Option<ReadPreference>,
pub(crate) read_concern: Option<ReadConcern>,
pub(crate) write_concern: Option<WriteConcern>
}
impl DB {
pub fn new(mongo: Mongo, name: &str) -> DB {
DB::new_with_option(mongo, name, None, None, None)
}
pub fn new_with_option(
mongo: Mongo,
name: &str,
read_preference: Option<ReadPreference>,
read_concern: Option<ReadConcern>,
write_concern: Option<WriteConcern>
) -> DB {
is_send::<DB>();
is_sync::<DB>();
DB {
name: name.to_string(),
mongo,
read_preference,
read_concern,
write_concern
}
}
#[inline]
pub fn name(&self) -> &str {
&self.name
}
#[inline]
pub fn acquire_client(&self) -> Client {
self.mongo.acquire_client()
}
pub fn get_core_db(&self, client: &Client) -> CoreDatabase {
let db = client.database(self.name());
if let Some(read_prefs) = &self.read_preference {
db.set_read_prefs(read_prefs.as_core());
}
if let Some(read_concern) = &self.read_concern {
db.set_read_concern(&read_concern.as_core());
}
if let Some(write_concern) = &self.write_concern {
db.set_write_concern(write_concern.as_core());
}
db
}
#[inline]
pub fn collection(&self, name: &str) -> Collection {
Collection::new(self.clone(), name)
}
pub fn command_simple(
&self,
command: Document,
read_prefs: Option<&ReadPreference>
) -> Result<Document> {
let command = Bsonc::from_doc(&command)?;
let read_prefs = read_prefs.map(|r| r.as_core());
let client = self.acquire_client();
let db = self.get_core_db(&client);
let reply = db.command_simple(&command, read_prefs)?;
Ok(reply.as_doc()?)
}
pub fn command(
&self,
command: Document,
read_prefs: Option<&ReadPreference>,
opts: Option<CommandOptions>
) -> Result<Document> {
let command = Bsonc::from_doc(&command)?;
let read_prefs = read_prefs.map(|r| r.as_core());
let opts = match opts {
Some(opts) => Some(Bsonc::from_doc(&opts.into())?),
None => None
};
let client = self.acquire_client();
let db = self.get_core_db(&client);
let reply = db.command(&command, read_prefs, opts.as_ref())?;
Ok(reply.as_doc()?)
}
pub fn watch(
&self,
pipeline: Document,
opts: Option<WatchOptions>
) -> Result<ChangeStream> {
let client = self.acquire_client();
let db = self.get_core_db(&client);
let pipeline = Bsonc::from_doc(&pipeline)?;
let opts = match opts {
Some(opts) => Some(Bsonc::from_doc(&opts.into())?),
None => None
};
let core_charge_stream = db.watch(&pipeline, opts.as_ref());
Ok(ChangeStream::new(core_charge_stream, client))
}
pub fn aggregate(
&self,
pipeline: Document,
opts: Option<AggregateOptions>,
read_prefs: Option<&ReadPreference>
) -> Result<Cursor> {
let client = self.acquire_client();
let db = self.get_core_db(&client);
let pipeline = Bsonc::from_doc(&pipeline)?;
let opts = match opts {
Some(opts) => Some(Bsonc::from_doc(&opts.into())?),
None => None
};
let read_prefs = read_prefs.map(|r| r.as_core());
let core_cursor = db.aggregate(&pipeline, opts.as_ref(), read_prefs);
Ok(Cursor::new(core_cursor, client))
}
}
unsafe impl Send for DB {}
unsafe impl Sync for DB {}
fn is_send<T: Send>() {}
fn is_sync<T: Sync>() {}