-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathdb.rs
145 lines (129 loc) · 4.83 KB
/
db.rs
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
pub(crate) mod action;
pub mod options;
use std::{fmt::Debug, sync::Arc};
use crate::{
concern::{ReadConcern, WriteConcern},
gridfs::{options::GridFsBucketOptions, GridFsBucket},
options::{CollectionOptions, DatabaseOptions},
selection_criteria::SelectionCriteria,
Client,
Collection,
};
/// `Database` is the client-side abstraction of a MongoDB database. It can be used to perform
/// database-level operations or to obtain handles to specific collections within the database. A
/// `Database` can only be obtained through a [`Client`](struct.Client.html) by calling either
/// [`Client::database`](struct.Client.html#method.database) or
/// [`Client::database_with_options`](struct.Client.html#method.database_with_options).
///
/// `Database` uses [`std::sync::Arc`](https://doc.rust-lang.org/std/sync/struct.Arc.html) internally,
/// so it can safely be shared across threads or async tasks. For example:
///
/// ```rust
///
/// # use mongodb::{bson::Document, Client, error::Result};
/// #
/// #
/// # async fn start_workers() -> Result<()> {
/// # let client = Client::with_uri_str("mongodb://example.com").await?;
/// let db = client.database("items");
///
/// for i in 0..5 {
/// let db_ref = db.clone();
///
/// tokio::task::spawn(async move {
/// let collection = db_ref.collection::<Document>(&format!("coll{}", i));
///
/// // Do something with the collection
/// });
/// }
/// #
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct Database {
inner: Arc<DatabaseInner>,
}
#[derive(Debug)]
struct DatabaseInner {
client: Client,
name: String,
selection_criteria: Option<SelectionCriteria>,
read_concern: Option<ReadConcern>,
write_concern: Option<WriteConcern>,
}
impl Database {
pub(crate) fn new(client: Client, name: &str, options: Option<DatabaseOptions>) -> Self {
let options = options.unwrap_or_default();
let selection_criteria = options
.selection_criteria
.or_else(|| client.selection_criteria().cloned());
let read_concern = options
.read_concern
.or_else(|| client.read_concern().cloned());
let write_concern = options
.write_concern
.or_else(|| client.write_concern().cloned());
Self {
inner: Arc::new(DatabaseInner {
client,
name: name.to_string(),
selection_criteria,
read_concern,
write_concern,
}),
}
}
/// Get the `Client` that this collection descended from.
pub fn client(&self) -> &Client {
&self.inner.client
}
/// Gets the name of the `Database`.
pub fn name(&self) -> &str {
&self.inner.name
}
/// Gets the read preference of the `Database`.
pub fn selection_criteria(&self) -> Option<&SelectionCriteria> {
self.inner.selection_criteria.as_ref()
}
/// Gets the read concern of the `Database`.
pub fn read_concern(&self) -> Option<&ReadConcern> {
self.inner.read_concern.as_ref()
}
/// Gets the write concern of the `Database`.
pub fn write_concern(&self) -> Option<&WriteConcern> {
self.inner.write_concern.as_ref()
}
/// Gets a handle to a collection in this database with the provided name. The
/// [`Collection`] options (e.g. read preference and write concern) will default to those of
/// this [`Database`].
///
/// For more information on how the generic parameter `T` is used, check out the [`Collection`]
/// documentation.
///
/// This method does not send or receive anything across the wire to the database, so it can be
/// used repeatedly without incurring any costs from I/O.
pub fn collection<T: Send + Sync>(&self, name: &str) -> Collection<T> {
Collection::new(self.clone(), name, None)
}
/// Gets a handle to a collection in this database with the provided name.
/// Operations done with this `Collection` will use the options specified by
/// `options` and will otherwise default to those of this [`Database`].
///
/// For more information on how the generic parameter `T` is used, check out the [`Collection`]
/// documentation.
///
/// This method does not send or receive anything across the wire to the database, so it can be
/// used repeatedly without incurring any costs from I/O.
pub fn collection_with_options<T: Send + Sync>(
&self,
name: &str,
options: CollectionOptions,
) -> Collection<T> {
Collection::new(self.clone(), name, Some(options))
}
/// Creates a new [`GridFsBucket`] in the database with the given options.
pub fn gridfs_bucket(&self, options: impl Into<Option<GridFsBucketOptions>>) -> GridFsBucket {
GridFsBucket::new(self.clone(), options.into().unwrap_or_default())
}
}