|
| 1 | +/* |
| 2 | + * Copyright 2024-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <bson/bson.h> |
| 18 | +#include <mongoc/mongoc-error.h> |
| 19 | +#include <mongoc-bulkwrite.h> |
| 20 | + |
| 21 | +struct _mongoc_bulkwriteoptions_t { |
| 22 | + bool ordered; |
| 23 | + bool bypassdocumentvalidation; |
| 24 | + const bson_t *let; |
| 25 | + const mongoc_write_concern_t *writeconcern; |
| 26 | + bool verboseresults; |
| 27 | + const bson_t *comment; |
| 28 | + mongoc_client_session_t *session; |
| 29 | + const bson_t *extra; |
| 30 | + uint32_t serverid; |
| 31 | +}; |
| 32 | + |
| 33 | +mongoc_bulkwriteoptions_t * |
| 34 | +mongoc_bulkwriteoptions_new (void) |
| 35 | +{ |
| 36 | + return bson_malloc0 (sizeof (mongoc_bulkwriteoptions_t)); |
| 37 | +} |
| 38 | +void |
| 39 | +mongoc_bulkwriteoptions_set_ordered (mongoc_bulkwriteoptions_t *self, bool ordered) |
| 40 | +{ |
| 41 | + BSON_ASSERT_PARAM (self); |
| 42 | + self->ordered = ordered; |
| 43 | +} |
| 44 | +void |
| 45 | +mongoc_bulkwriteoptions_set_bypassdocumentvalidation (mongoc_bulkwriteoptions_t *self, bool bypassdocumentvalidation) |
| 46 | +{ |
| 47 | + BSON_ASSERT_PARAM (self); |
| 48 | + self->bypassdocumentvalidation = bypassdocumentvalidation; |
| 49 | +} |
| 50 | +void |
| 51 | +mongoc_bulkwriteoptions_set_let (mongoc_bulkwriteoptions_t *self, const bson_t *let) |
| 52 | +{ |
| 53 | + BSON_ASSERT_PARAM (self); |
| 54 | + self->let = let; |
| 55 | +} |
| 56 | +void |
| 57 | +mongoc_bulkwriteoptions_set_writeconcern (mongoc_bulkwriteoptions_t *self, const mongoc_write_concern_t *writeconcern) |
| 58 | +{ |
| 59 | + BSON_ASSERT_PARAM (self); |
| 60 | + self->writeconcern = writeconcern; |
| 61 | +} |
| 62 | +void |
| 63 | +mongoc_bulkwriteoptions_set_verboseresults (mongoc_bulkwriteoptions_t *self, bool verboseresults) |
| 64 | +{ |
| 65 | + BSON_ASSERT_PARAM (self); |
| 66 | + self->verboseresults = verboseresults; |
| 67 | +} |
| 68 | +void |
| 69 | +mongoc_bulkwriteoptions_set_comment (mongoc_bulkwriteoptions_t *self, const bson_t *comment) |
| 70 | +{ |
| 71 | + BSON_ASSERT_PARAM (self); |
| 72 | + self->comment = comment; |
| 73 | +} |
| 74 | +void |
| 75 | +mongoc_bulkwriteoptions_set_session (mongoc_bulkwriteoptions_t *self, mongoc_client_session_t *session) |
| 76 | +{ |
| 77 | + BSON_ASSERT_PARAM (self); |
| 78 | + self->session = session; |
| 79 | +} |
| 80 | +void |
| 81 | +mongoc_bulkwriteoptions_set_extra (mongoc_bulkwriteoptions_t *self, const bson_t *extra) |
| 82 | +{ |
| 83 | + BSON_ASSERT_PARAM (self); |
| 84 | + self->extra = extra; |
| 85 | +} |
| 86 | +BSON_EXPORT (void) |
| 87 | +mongoc_bulkwriteoptions_set_serverid (mongoc_bulkwriteoptions_t *self, uint32_t serverid) |
| 88 | +{ |
| 89 | + BSON_ASSERT_PARAM (self); |
| 90 | + self->serverid = serverid; |
| 91 | +} |
| 92 | +void |
| 93 | +mongoc_bulkwriteoptions_destroy (mongoc_bulkwriteoptions_t *self) |
| 94 | +{ |
| 95 | + if (!self) { |
| 96 | + return; |
| 97 | + } |
| 98 | + bson_free (self); |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +struct _mongoc_bulkwrite_t { |
| 103 | + mongoc_client_t *client; |
| 104 | + bool executed; |
| 105 | +}; |
| 106 | + |
| 107 | + |
| 108 | +// `mongoc_client_bulkwrite_new` creates a new bulk write operation. |
| 109 | +mongoc_bulkwrite_t * |
| 110 | +mongoc_client_bulkwrite_new (mongoc_client_t *self, mongoc_bulkwriteoptions_t *opts) |
| 111 | +{ |
| 112 | + BSON_ASSERT_PARAM (self); |
| 113 | + BSON_UNUSED (opts); |
| 114 | + mongoc_bulkwrite_t *bw = bson_malloc0 (sizeof (mongoc_bulkwrite_t)); |
| 115 | + bw->client = self; |
| 116 | + return bw; |
| 117 | +} |
| 118 | + |
| 119 | +void |
| 120 | +mongoc_bulkwrite_destroy (mongoc_bulkwrite_t *self) |
| 121 | +{ |
| 122 | + if (!self) { |
| 123 | + return; |
| 124 | + } |
| 125 | + bson_free (self); |
| 126 | +} |
| 127 | + |
| 128 | +struct _mongoc_insertoneopts_t { |
| 129 | + bson_validate_flags_t vflags; |
| 130 | +}; |
| 131 | + |
| 132 | +mongoc_insertoneopts_t * |
| 133 | +mongoc_insertoneopts_new (void) |
| 134 | +{ |
| 135 | + return bson_malloc0 (sizeof (mongoc_insertoneopts_t)); |
| 136 | +} |
| 137 | + |
| 138 | +void |
| 139 | +mongoc_insertoneopts_set_validation (mongoc_insertoneopts_t *self, bson_validate_flags_t vflags) |
| 140 | +{ |
| 141 | + BSON_ASSERT_PARAM (self); |
| 142 | + self->vflags = vflags; |
| 143 | +} |
| 144 | + |
| 145 | +void |
| 146 | +mongoc_insertoneopts_destroy (mongoc_insertoneopts_t *self) |
| 147 | +{ |
| 148 | + if (!self) { |
| 149 | + return; |
| 150 | + } |
| 151 | + bson_free (self); |
| 152 | +} |
| 153 | + |
| 154 | +bool |
| 155 | +mongoc_client_bulkwrite_append_insertone (mongoc_bulkwrite_t *self, |
| 156 | + const char *ns, |
| 157 | + int ns_len, |
| 158 | + const bson_t *document, |
| 159 | + mongoc_insertoneopts_t *opts, // may be NULL |
| 160 | + bson_error_t *error) |
| 161 | +{ |
| 162 | + BSON_ASSERT_PARAM (self); |
| 163 | + BSON_UNUSED (ns); |
| 164 | + BSON_UNUSED (ns_len); |
| 165 | + BSON_UNUSED (document); |
| 166 | + BSON_UNUSED (opts); |
| 167 | + |
| 168 | + if (self->executed) { |
| 169 | + bson_set_error (error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_COMMAND_INVALID_ARG, "bulk write already executed"); |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + // TODO: implement. |
| 174 | + return true; |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +struct _mongoc_bulkwriteresult_t { |
| 179 | + int64_t acknowledged; |
| 180 | + int64_t insertedCount; |
| 181 | + int64_t upsertedcount; |
| 182 | + int64_t matchedcount; |
| 183 | + int64_t modifiedcount; |
| 184 | + int64_t deletedcount; |
| 185 | + bson_t *verbose_results; |
| 186 | + uint32_t serverid; |
| 187 | +}; |
| 188 | + |
| 189 | +bool |
| 190 | +mongoc_bulkwriteresult_acknowledged (const mongoc_bulkwriteresult_t *self) |
| 191 | +{ |
| 192 | + BSON_ASSERT_PARAM (self); |
| 193 | + return self->acknowledged; |
| 194 | +} |
| 195 | + |
| 196 | +int64_t |
| 197 | +mongoc_bulkwriteresult_insertedcount (const mongoc_bulkwriteresult_t *self) |
| 198 | +{ |
| 199 | + BSON_ASSERT_PARAM (self); |
| 200 | + return self->insertedCount; |
| 201 | +} |
| 202 | + |
| 203 | +int64_t |
| 204 | +mongoc_bulkwriteresult_upsertedcount (const mongoc_bulkwriteresult_t *self) |
| 205 | +{ |
| 206 | + BSON_ASSERT_PARAM (self); |
| 207 | + return self->upsertedcount; |
| 208 | +} |
| 209 | + |
| 210 | +int64_t |
| 211 | +mongoc_bulkwriteresult_matchedcount (const mongoc_bulkwriteresult_t *self) |
| 212 | +{ |
| 213 | + BSON_ASSERT_PARAM (self); |
| 214 | + return self->matchedcount; |
| 215 | +} |
| 216 | + |
| 217 | +int64_t |
| 218 | +mongoc_bulkwriteresult_modifiedcount (const mongoc_bulkwriteresult_t *self) |
| 219 | +{ |
| 220 | + BSON_ASSERT_PARAM (self); |
| 221 | + return self->modifiedcount; |
| 222 | +} |
| 223 | + |
| 224 | +int64_t |
| 225 | +mongoc_bulkwriteresult_deletedcount (const mongoc_bulkwriteresult_t *self) |
| 226 | +{ |
| 227 | + BSON_ASSERT_PARAM (self); |
| 228 | + return self->deletedcount; |
| 229 | +} |
| 230 | + |
| 231 | +const bson_t * |
| 232 | +mongoc_bulkwriteresult_verboseresults (const mongoc_bulkwriteresult_t *self) |
| 233 | +{ |
| 234 | + BSON_ASSERT_PARAM (self); |
| 235 | + return self->verbose_results; |
| 236 | +} |
| 237 | + |
| 238 | +uint32_t |
| 239 | +mongoc_bulkwriteresult_serverid (const mongoc_bulkwriteresult_t *self) |
| 240 | +{ |
| 241 | + BSON_ASSERT_PARAM (self); |
| 242 | + return self->serverid; |
| 243 | +} |
| 244 | + |
| 245 | +void |
| 246 | +mongoc_bulkwriteresult_destroy (mongoc_bulkwriteresult_t *self) |
| 247 | +{ |
| 248 | + if (!self) { |
| 249 | + return; |
| 250 | + } |
| 251 | + bson_destroy (self->verbose_results); |
| 252 | + bson_free (self); |
| 253 | +} |
| 254 | + |
| 255 | +struct _mongoc_bulkwriteexception_t { |
| 256 | + bson_error_t error; |
| 257 | + bson_t *error_document; |
| 258 | +}; |
| 259 | + |
| 260 | +void |
| 261 | +mongoc_bulkwriteexception_error (const mongoc_bulkwriteexception_t *self, |
| 262 | + bson_error_t *error, |
| 263 | + const bson_t **error_document) |
| 264 | +{ |
| 265 | + BSON_ASSERT_PARAM (self); |
| 266 | + memcpy (error, &self->error, sizeof (*error)); |
| 267 | + if (error_document) { |
| 268 | + *error_document = self->error_document; |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +void |
| 273 | +mongoc_bulkwriteexception_destroy (mongoc_bulkwriteexception_t *self) |
| 274 | +{ |
| 275 | + if (!self) { |
| 276 | + return; |
| 277 | + } |
| 278 | + bson_destroy (self->error_document); |
| 279 | + bson_free (self); |
| 280 | +} |
| 281 | + |
| 282 | +mongoc_bulkwritereturn_t |
| 283 | +mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self) |
| 284 | +{ |
| 285 | + BSON_ASSERT_PARAM (self); |
| 286 | + // TODO: implement. |
| 287 | + self->executed = true; |
| 288 | + // Create stub results. |
| 289 | + mongoc_bulkwriteresult_t *bwr = bson_malloc0 (sizeof (mongoc_bulkwriteresult_t)); |
| 290 | + bwr->insertedCount = 123; |
| 291 | + bwr->verbose_results = bson_new_from_json ((const uint8_t *) BSON_STR ({"foo" : "bar"}), -1, NULL); |
| 292 | + BSON_ASSERT (bwr->verbose_results); |
| 293 | + |
| 294 | + mongoc_bulkwriteexception_t *bwe = bson_malloc0 (sizeof (mongoc_bulkwriteexception_t)); |
| 295 | + bwe->error_document = bson_new_from_json ( |
| 296 | + (const uint8_t *) BSON_STR ( |
| 297 | + {"errorLabels" : ["RetryableWriteError"], "writeErrors" : [], "writeConcernErrors" : [], "errorReplies" : []}), |
| 298 | + -1, |
| 299 | + NULL); |
| 300 | + BSON_ASSERT (bwe->error_document); |
| 301 | + bson_set_error (&bwe->error, MONGOC_ERROR_SERVER, 123, "This is a stub error"); |
| 302 | + return (mongoc_bulkwritereturn_t){.res = bwr, .exc = bwe}; |
| 303 | +} |
0 commit comments