1
1
#![ allow( missing_docs) ]
2
2
3
- use std:: collections:: HashMap ;
3
+ use std:: { collections:: HashMap , marker :: PhantomData } ;
4
4
5
5
use crate :: {
6
6
bson:: { Bson , Document } ,
7
7
error:: { BulkWriteError , Error , ErrorKind , Result } ,
8
8
operation:: bulk_write:: BulkWrite as BulkWriteOperation ,
9
9
options:: { BulkWriteOptions , WriteConcern , WriteModel } ,
10
- results:: BulkWriteResult ,
10
+ results:: { BulkWriteResult , SummaryBulkWriteResult , VerboseBulkWriteResult } ,
11
11
Client ,
12
12
ClientSession ,
13
13
} ;
14
14
15
15
use super :: { action_impl, option_setters} ;
16
16
17
17
impl Client {
18
- pub fn bulk_write ( & self , models : impl IntoIterator < Item = WriteModel > ) -> BulkWrite {
18
+ pub fn bulk_write (
19
+ & self ,
20
+ models : impl IntoIterator < Item = WriteModel > ,
21
+ ) -> BulkWrite < SummaryBulkWriteResult > {
19
22
BulkWrite :: new ( self , models. into_iter ( ) . collect ( ) )
20
23
}
21
24
}
22
25
23
26
#[ must_use]
24
- pub struct BulkWrite < ' a > {
27
+ pub struct BulkWrite < ' a , R > {
25
28
client : & ' a Client ,
26
29
models : Vec < WriteModel > ,
27
30
options : Option < BulkWriteOptions > ,
28
31
session : Option < & ' a mut ClientSession > ,
32
+ _phantom : PhantomData < R > ,
29
33
}
30
34
31
- impl < ' a > BulkWrite < ' a > {
35
+ impl < ' a > BulkWrite < ' a , SummaryBulkWriteResult > {
36
+ pub fn verbose_results ( self ) -> BulkWrite < ' a , VerboseBulkWriteResult > {
37
+ BulkWrite {
38
+ client : self . client ,
39
+ models : self . models ,
40
+ options : self . options ,
41
+ session : self . session ,
42
+ _phantom : PhantomData ,
43
+ }
44
+ }
45
+ }
46
+
47
+ impl < ' a , R > BulkWrite < ' a , R >
48
+ where
49
+ R : BulkWriteResult ,
50
+ {
32
51
option_setters ! ( options: BulkWriteOptions ;
33
52
ordered: bool ,
34
53
bypass_document_validation: bool ,
35
54
comment: Bson ,
36
55
let_vars: Document ,
37
- verbose_results: bool ,
38
56
write_concern: WriteConcern ,
39
57
) ;
40
58
41
- pub fn session ( mut self , session : & ' a mut ClientSession ) -> BulkWrite < ' a > {
59
+ pub fn session ( mut self , session : & ' a mut ClientSession ) -> Self {
42
60
self . session = Some ( session) ;
43
61
self
44
62
}
@@ -49,6 +67,7 @@ impl<'a> BulkWrite<'a> {
49
67
models,
50
68
options : None ,
51
69
session : None ,
70
+ _phantom : PhantomData ,
52
71
}
53
72
}
54
73
@@ -58,13 +77,8 @@ impl<'a> BulkWrite<'a> {
58
77
. and_then ( |options| options. ordered )
59
78
. unwrap_or ( true )
60
79
}
61
- }
62
80
63
- #[ action_impl]
64
- impl < ' a > Action for BulkWrite < ' a > {
65
- type Future = BulkWriteFuture ;
66
-
67
- async fn execute ( mut self ) -> Result < BulkWriteResult > {
81
+ async fn execute_inner ( mut self ) -> Result < R > {
68
82
#[ cfg( feature = "in-use-encryption-unstable" ) ]
69
83
if self . client . should_auto_encrypt ( ) . await {
70
84
use mongocrypt:: error:: { Error as EncryptionError , ErrorKind as EncryptionErrorKind } ;
@@ -100,7 +114,7 @@ impl<'a> Action for BulkWrite<'a> {
100
114
. await ;
101
115
let result = self
102
116
. client
103
- . execute_operation :: < BulkWriteOperation > (
117
+ . execute_operation :: < BulkWriteOperation < R > > (
104
118
& mut operation,
105
119
self . session . as_deref_mut ( ) ,
106
120
)
@@ -128,18 +142,42 @@ impl<'a> Action for BulkWrite<'a> {
128
142
}
129
143
}
130
144
145
+ #[ action_impl]
146
+ impl < ' a > Action for BulkWrite < ' a , SummaryBulkWriteResult > {
147
+ type Future = SummaryBulkWriteFuture ;
148
+
149
+ async fn execute ( mut self ) -> Result < SummaryBulkWriteResult > {
150
+ self . execute_inner ( ) . await
151
+ }
152
+ }
153
+
154
+ #[ action_impl]
155
+ impl < ' a > Action for BulkWrite < ' a , VerboseBulkWriteResult > {
156
+ type Future = VerboseBulkWriteFuture ;
157
+
158
+ async fn execute ( mut self ) -> Result < VerboseBulkWriteResult > {
159
+ self . execute_inner ( ) . await
160
+ }
161
+ }
162
+
131
163
/// Represents the execution status of a bulk write. The status starts at `None`, indicating that no
132
164
/// writes have been attempted yet, and transitions to either `Success` or `Error` as batches are
133
165
/// executed. The contents of `Error` can be inspected to determine whether a bulk write can
134
166
/// continue with further batches or should be terminated.
135
- enum ExecutionStatus {
136
- Success ( BulkWriteResult ) ,
167
+ enum ExecutionStatus < R >
168
+ where
169
+ R : BulkWriteResult ,
170
+ {
171
+ Success ( R ) ,
137
172
Error ( Error ) ,
138
173
None ,
139
174
}
140
175
141
- impl ExecutionStatus {
142
- fn with_success ( mut self , result : BulkWriteResult ) -> Self {
176
+ impl < R > ExecutionStatus < R >
177
+ where
178
+ R : BulkWriteResult ,
179
+ {
180
+ fn with_success ( mut self , result : R ) -> Self {
143
181
match self {
144
182
// Merge two successful sets of results together.
145
183
Self :: Success ( ref mut current_result) => {
@@ -149,7 +187,7 @@ impl ExecutionStatus {
149
187
// Merge the results of the new batch into the existing bulk write error.
150
188
Self :: Error ( ref mut current_error) => {
151
189
let bulk_write_error = Self :: get_current_bulk_write_error ( current_error) ;
152
- bulk_write_error. merge_partial_results ( result) ;
190
+ bulk_write_error. merge_partial_results ( result. into_partial_result ( ) ) ;
153
191
self
154
192
}
155
193
Self :: None => Self :: Success ( result) ,
@@ -163,14 +201,14 @@ impl ExecutionStatus {
163
201
// set its source as the error that just occurred.
164
202
Self :: Success ( current_result) => match * error. kind {
165
203
ErrorKind :: BulkWrite ( ref mut bulk_write_error) => {
166
- bulk_write_error. merge_partial_results ( current_result) ;
204
+ bulk_write_error. merge_partial_results ( current_result. into_partial_result ( ) ) ;
167
205
Self :: Error ( error)
168
206
}
169
207
_ => {
170
208
let bulk_write_error: Error = ErrorKind :: BulkWrite ( BulkWriteError {
171
209
write_errors : HashMap :: new ( ) ,
172
210
write_concern_errors : Vec :: new ( ) ,
173
- partial_result : Some ( current_result) ,
211
+ partial_result : Some ( current_result. into_partial_result ( ) ) ,
174
212
} )
175
213
. into ( ) ;
176
214
Self :: Error ( bulk_write_error. with_source ( error) )
0 commit comments