Skip to main content

mongodb/results/
bulk_write.rs

1use std::{collections::HashMap, fmt::Debug};
2
3use crate::{
4    error::bulk_write::PartialBulkWriteResult,
5    results::{DeleteResult, InsertOneResult, UpdateResult},
6};
7
8/// Summary results returned from a [`bulk_write`](crate::Client::bulk_write) operation.
9#[cfg_attr(test, serde_with::skip_serializing_none)]
10#[derive(Clone, Debug, Default)]
11#[cfg_attr(test, derive(serde::Serialize))]
12#[cfg_attr(test, serde(rename_all = "camelCase"))]
13#[non_exhaustive]
14pub struct SummaryBulkWriteResult {
15    /// The total number of documents inserted across all operations.
16    pub inserted_count: i64,
17
18    /// The total number of documents matched across all operations.
19    pub matched_count: i64,
20
21    /// The total number of documents modified across all operations.
22    pub modified_count: i64,
23
24    /// The total number of documents upserted across all operations.
25    pub upserted_count: i64,
26
27    /// The total number of documents deleted across all operations.
28    pub deleted_count: i64,
29}
30
31/// Verbose results returned from a [`bulk_write`](crate::Client::bulk_write) operation.
32#[cfg_attr(test, serde_with::skip_serializing_none)]
33#[derive(Clone, Debug, Default)]
34#[cfg_attr(test, derive(serde::Serialize))]
35#[cfg_attr(test, serde(rename_all = "camelCase"))]
36#[non_exhaustive]
37pub struct VerboseBulkWriteResult {
38    /// The summary results.
39    #[cfg_attr(test, serde(flatten))]
40    pub summary: SummaryBulkWriteResult,
41
42    /// The results of each insert operation that was successfully performed.
43    #[cfg_attr(
44        test,
45        serde(serialize_with = "crate::serde_util::serialize_indexed_map")
46    )]
47    pub insert_results: HashMap<usize, InsertOneResult>,
48
49    /// The results of each update operation that was successfully performed.
50    #[cfg_attr(
51        test,
52        serde(serialize_with = "crate::serde_util::serialize_indexed_map")
53    )]
54    pub update_results: HashMap<usize, UpdateResult>,
55
56    /// The results of each delete operation that was successfully performed.
57    #[cfg_attr(
58        test,
59        serde(serialize_with = "crate::serde_util::serialize_indexed_map")
60    )]
61    pub delete_results: HashMap<usize, DeleteResult>,
62}
63
64mod result_trait {
65    use crate::{
66        error::bulk_write::PartialBulkWriteResult,
67        results::{DeleteResult, InsertOneResult, UpdateResult},
68    };
69
70    pub trait BulkWriteResult: Default + Send + Sync {
71        fn errors_only() -> bool;
72
73        fn merge(&mut self, other: Self);
74
75        fn into_partial_result(self) -> PartialBulkWriteResult;
76
77        fn populate_summary_info(
78            &mut self,
79            n_inserted: i64,
80            n_matched: i64,
81            n_modified: i64,
82            n_upserted: i64,
83            n_deleted: i64,
84        );
85
86        fn add_insert_result(&mut self, _index: usize, _insert_result: InsertOneResult) {}
87
88        fn add_update_result(&mut self, _index: usize, _update_result: UpdateResult) {}
89
90        fn add_delete_result(&mut self, _index: usize, _delete_result: DeleteResult) {}
91    }
92}
93
94pub(crate) use result_trait::BulkWriteResult;
95
96impl BulkWriteResult for SummaryBulkWriteResult {
97    fn errors_only() -> bool {
98        true
99    }
100
101    fn merge(&mut self, other: Self) {
102        let SummaryBulkWriteResult {
103            inserted_count: other_inserted_count,
104            matched_count: other_matched_count,
105            modified_count: other_modified_count,
106            upserted_count: other_upserted_count,
107            deleted_count: other_deleted_count,
108        } = other;
109
110        self.inserted_count += other_inserted_count;
111        self.matched_count += other_matched_count;
112        self.modified_count += other_modified_count;
113        self.upserted_count += other_upserted_count;
114        self.deleted_count += other_deleted_count;
115    }
116
117    fn into_partial_result(self) -> PartialBulkWriteResult {
118        PartialBulkWriteResult::Summary(self)
119    }
120
121    fn populate_summary_info(
122        &mut self,
123        n_inserted: i64,
124        n_matched: i64,
125        n_modified: i64,
126        n_upserted: i64,
127        n_deleted: i64,
128    ) {
129        self.inserted_count += n_inserted;
130        self.matched_count += n_matched;
131        self.modified_count += n_modified;
132        self.upserted_count += n_upserted;
133        self.deleted_count += n_deleted;
134    }
135}
136
137impl BulkWriteResult for VerboseBulkWriteResult {
138    fn errors_only() -> bool {
139        false
140    }
141
142    fn merge(&mut self, other: Self) {
143        let VerboseBulkWriteResult {
144            summary: other_summary,
145            insert_results: other_insert_results,
146            update_results: other_update_results,
147            delete_results: other_delete_results,
148        } = other;
149
150        self.summary.merge(other_summary);
151        self.insert_results.extend(other_insert_results);
152        self.update_results.extend(other_update_results);
153        self.delete_results.extend(other_delete_results);
154    }
155
156    fn into_partial_result(self) -> PartialBulkWriteResult {
157        PartialBulkWriteResult::Verbose(self)
158    }
159
160    fn populate_summary_info(
161        &mut self,
162        n_inserted: i64,
163        n_matched: i64,
164        n_modified: i64,
165        n_upserted: i64,
166        n_deleted: i64,
167    ) {
168        self.summary
169            .populate_summary_info(n_inserted, n_matched, n_modified, n_upserted, n_deleted);
170    }
171
172    fn add_insert_result(&mut self, index: usize, insert_result: InsertOneResult) {
173        self.insert_results.insert(index, insert_result);
174    }
175
176    fn add_update_result(&mut self, index: usize, update_result: UpdateResult) {
177        self.update_results.insert(index, update_result);
178    }
179
180    fn add_delete_result(&mut self, index: usize, delete_result: DeleteResult) {
181        self.delete_results.insert(index, delete_result);
182    }
183}