3
3
use std:: { collections:: HashMap , fmt:: Debug } ;
4
4
5
5
use serde:: Serialize ;
6
+ use serde_with:: skip_serializing_none;
6
7
7
8
use crate :: {
8
9
results:: { DeleteResult , InsertOneResult , UpdateResult } ,
9
10
serde_util:: serialize_indexed_map,
10
11
} ;
11
12
12
- #[ derive( Clone , Debug , Default , Serialize ) ]
13
+ #[ skip_serializing_none]
14
+ #[ derive( Clone , Debug , Serialize ) ]
13
15
#[ serde( rename_all = "camelCase" ) ]
14
16
#[ non_exhaustive]
15
17
pub struct BulkWriteResult {
@@ -19,24 +21,43 @@ pub struct BulkWriteResult {
19
21
pub modified_count : i64 ,
20
22
pub deleted_count : i64 ,
21
23
#[ serde( serialize_with = "serialize_indexed_map" ) ]
22
- pub insert_results : HashMap < usize , InsertOneResult > ,
24
+ pub insert_results : Option < HashMap < usize , InsertOneResult > > ,
23
25
#[ serde( serialize_with = "serialize_indexed_map" ) ]
24
- pub update_results : HashMap < usize , UpdateResult > ,
26
+ pub update_results : Option < HashMap < usize , UpdateResult > > ,
25
27
#[ serde( serialize_with = "serialize_indexed_map" ) ]
26
- pub delete_results : HashMap < usize , DeleteResult > ,
28
+ pub delete_results : Option < HashMap < usize , DeleteResult > > ,
27
29
}
28
30
29
31
impl BulkWriteResult {
32
+ pub ( crate ) fn new ( verbose : bool ) -> Self {
33
+ Self {
34
+ inserted_count : 0 ,
35
+ upserted_count : 0 ,
36
+ matched_count : 0 ,
37
+ modified_count : 0 ,
38
+ deleted_count : 0 ,
39
+ insert_results : verbose. then ( HashMap :: new) ,
40
+ update_results : verbose. then ( HashMap :: new) ,
41
+ delete_results : verbose. then ( HashMap :: new) ,
42
+ }
43
+ }
44
+
30
45
pub ( crate ) fn add_insert_result ( & mut self , index : usize , insert_result : InsertOneResult ) {
31
- self . insert_results . insert ( index, insert_result) ;
46
+ self . insert_results
47
+ . get_or_insert_with ( Default :: default)
48
+ . insert ( index, insert_result) ;
32
49
}
33
50
34
51
pub ( crate ) fn add_update_result ( & mut self , index : usize , update_result : UpdateResult ) {
35
- self . update_results . insert ( index, update_result) ;
52
+ self . update_results
53
+ . get_or_insert_with ( Default :: default)
54
+ . insert ( index, update_result) ;
36
55
}
37
56
38
57
pub ( crate ) fn add_delete_result ( & mut self , index : usize , delete_result : DeleteResult ) {
39
- self . delete_results . insert ( index, delete_result) ;
58
+ self . delete_results
59
+ . get_or_insert_with ( Default :: default)
60
+ . insert ( index, delete_result) ;
40
61
}
41
62
42
63
pub ( crate ) fn merge ( & mut self , other : Self ) {
@@ -45,8 +66,20 @@ impl BulkWriteResult {
45
66
self . matched_count += other. matched_count ;
46
67
self . modified_count += other. modified_count ;
47
68
self . deleted_count += other. deleted_count ;
48
- self . insert_results . extend ( other. insert_results ) ;
49
- self . update_results . extend ( other. update_results ) ;
50
- self . delete_results . extend ( other. delete_results ) ;
69
+ if let Some ( insert_results) = other. insert_results {
70
+ self . insert_results
71
+ . get_or_insert_with ( Default :: default)
72
+ . extend ( insert_results) ;
73
+ }
74
+ if let Some ( update_results) = other. update_results {
75
+ self . update_results
76
+ . get_or_insert_with ( Default :: default)
77
+ . extend ( update_results) ;
78
+ }
79
+ if let Some ( delete_results) = other. delete_results {
80
+ self . delete_results
81
+ . get_or_insert_with ( Default :: default)
82
+ . extend ( delete_results) ;
83
+ }
51
84
}
52
85
}
0 commit comments