44
44
*/
45
45
46
46
47
+ mongoc_bulk_operation_t *
48
+ mongoc_bulk_operation_new (bool ordered )
49
+ {
50
+ mongoc_bulk_operation_t * bulk ;
51
+
52
+ bulk = bson_malloc0 (sizeof * bulk );
53
+ bulk -> ordered = ordered ;
54
+
55
+ _mongoc_array_init (& bulk -> commands , sizeof (mongoc_write_command_t ));
56
+
57
+ return bulk ;
58
+ }
59
+
60
+
47
61
mongoc_bulk_operation_t *
48
62
_mongoc_bulk_operation_new (mongoc_client_t * client , /* IN */
49
63
const char * database , /* IN */
@@ -57,23 +71,14 @@ _mongoc_bulk_operation_new (mongoc_client_t *client, /* IN *
57
71
BSON_ASSERT (client );
58
72
BSON_ASSERT (collection );
59
73
60
- bulk = bson_malloc0 (sizeof * bulk );
61
-
74
+ bulk = mongoc_bulk_operation_new (ordered );
62
75
bulk -> client = client ;
63
76
bulk -> database = bson_strdup (database );
64
77
bulk -> collection = bson_strdup (collection );
65
- bulk -> ordered = ordered ;
66
78
bulk -> hint = hint ;
67
79
bulk -> write_concern = mongoc_write_concern_copy (write_concern );
68
80
bulk -> executed = false;
69
81
70
- if (!bulk -> write_concern ) {
71
- bulk -> write_concern = mongoc_write_concern_new ();
72
- }
73
-
74
- _mongoc_write_result_init (& bulk -> result );
75
- _mongoc_array_init (& bulk -> commands , sizeof (mongoc_write_command_t ));
76
-
77
82
return bulk ;
78
83
}
79
84
@@ -95,7 +100,10 @@ mongoc_bulk_operation_destroy (mongoc_bulk_operation_t *bulk) /* IN */
95
100
bson_free (bulk -> collection );
96
101
mongoc_write_concern_destroy (bulk -> write_concern );
97
102
_mongoc_array_destroy (& bulk -> commands );
98
- _mongoc_write_result_destroy (& bulk -> result );
103
+
104
+ if (bulk -> executed ) {
105
+ _mongoc_write_result_destroy (& bulk -> result );
106
+ }
99
107
100
108
bson_free (bulk );
101
109
}
@@ -276,17 +284,41 @@ mongoc_bulk_operation_execute (mongoc_bulk_operation_t *bulk, /* IN */
276
284
277
285
bson_return_val_if_fail (bulk , false);
278
286
287
+ if (!bulk -> write_concern ) {
288
+ bulk -> write_concern = mongoc_write_concern_new ();
289
+ }
290
+
279
291
if (bulk -> executed ) {
292
+ _mongoc_write_result_destroy (& bulk -> result );
293
+ }
294
+
295
+ _mongoc_write_result_init (& bulk -> result );
296
+
297
+ bulk -> executed = true;
298
+
299
+ if (!bulk -> client ) {
300
+ bson_set_error (error ,
301
+ MONGOC_ERROR_COMMAND ,
302
+ MONGOC_ERROR_COMMAND_INVALID_ARG ,
303
+ "mongoc_bulk_operation_execute() requires a client "
304
+ "and one has not been set." );
305
+ return false;
306
+ } else if (!bulk -> database ) {
280
307
bson_set_error (error ,
281
308
MONGOC_ERROR_COMMAND ,
282
309
MONGOC_ERROR_COMMAND_INVALID_ARG ,
283
- "mongoc_bulk_operation_execute() may only be called "
284
- "once for a bulk operation." );
310
+ "mongoc_bulk_operation_execute() requires a database "
311
+ "and one has not been set." );
312
+ return false;
313
+ } else if (!bulk -> collection ) {
314
+ bson_set_error (error ,
315
+ MONGOC_ERROR_COMMAND ,
316
+ MONGOC_ERROR_COMMAND_INVALID_ARG ,
317
+ "mongoc_bulk_operation_execute() requires a collection "
318
+ "and one has not been set." );
285
319
return false;
286
320
}
287
321
288
- bulk -> executed = true;
289
-
290
322
if (reply ) {
291
323
bson_init (reply );
292
324
}
@@ -336,3 +368,51 @@ mongoc_bulk_operation_set_write_concern (mongoc_bulk_operation_t *bulk,
336
368
bulk -> write_concern = mongoc_write_concern_new ();
337
369
}
338
370
}
371
+
372
+
373
+ void
374
+ mongoc_bulk_operation_set_database (mongoc_bulk_operation_t * bulk ,
375
+ const char * database )
376
+ {
377
+ bson_return_if_fail (bulk );
378
+
379
+ if (bulk -> database ) {
380
+ bson_free (bulk -> database );
381
+ }
382
+
383
+ bulk -> database = bson_strdup (database );
384
+ }
385
+
386
+
387
+ void
388
+ mongoc_bulk_operation_set_collection (mongoc_bulk_operation_t * bulk ,
389
+ const char * collection )
390
+ {
391
+ bson_return_if_fail (bulk );
392
+
393
+ if (bulk -> collection ) {
394
+ bson_free (bulk -> collection );
395
+ }
396
+
397
+ bulk -> collection = bson_strdup (collection );
398
+ }
399
+
400
+
401
+ void
402
+ mongoc_bulk_operation_set_client (mongoc_bulk_operation_t * bulk ,
403
+ void * client )
404
+ {
405
+ bson_return_if_fail (bulk );
406
+
407
+ bulk -> client = client ;
408
+ }
409
+
410
+
411
+ void
412
+ mongoc_bulk_operation_set_hint (mongoc_bulk_operation_t * bulk ,
413
+ uint32_t hint )
414
+ {
415
+ bson_return_if_fail (bulk );
416
+
417
+ bulk -> hint = hint ;
418
+ }
0 commit comments