Skip to content

Commit 3ad624e

Browse files
committed
PHPC-682: Do not use mongoc_bulk_operation_t private API
This adds php_phongo_bulkwrite_t fields to track extra information that we report in the BulkWrite debug handler.
1 parent 291b489 commit 3ad624e

File tree

7 files changed

+43
-18
lines changed

7 files changed

+43
-18
lines changed

php_phongo.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -517,22 +517,32 @@ mongoc_bulk_operation_t *phongo_bulkwrite_init(zend_bool ordered) { /* {{{ */
517517
return mongoc_bulk_operation_new(ordered);
518518
} /* }}} */
519519

520-
bool phongo_execute_write(mongoc_client_t *client, const char *namespace, mongoc_bulk_operation_t *bulk, const mongoc_write_concern_t *write_concern, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
520+
bool phongo_execute_write(mongoc_client_t *client, const char *namespace, php_phongo_bulkwrite_t *bulk_write, const mongoc_write_concern_t *write_concern, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
521521
{
522522
bson_error_t error;
523-
char *dbname;
524-
char *collname;
525523
int success;
526524
bson_t reply = BSON_INITIALIZER;
525+
mongoc_bulk_operation_t *bulk = bulk_write->bulk;
527526
php_phongo_writeresult_t *writeresult;
528527

529-
if (!phongo_split_namespace(namespace, &dbname, &collname)) {
528+
/* Since BulkWrite objects can currently be executed multiple times, ensure
529+
* that the database and collection name are freed before we overwrite them.
530+
* This may be removed once PHPC-676 is implemented. */
531+
if (bulk_write->database) {
532+
efree(bulk_write->database);
533+
}
534+
535+
if (bulk_write->collection) {
536+
efree(bulk_write->collection);
537+
}
538+
539+
if (!phongo_split_namespace(namespace, &bulk_write->database, &bulk_write->collection)) {
530540
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s: %s", "Invalid namespace provided", namespace);
531541
return false;
532542
}
533543

534-
mongoc_bulk_operation_set_database(bulk, dbname);
535-
mongoc_bulk_operation_set_collection(bulk, collname);
544+
mongoc_bulk_operation_set_database(bulk, bulk_write->database);
545+
mongoc_bulk_operation_set_collection(bulk, bulk_write->collection);
536546
mongoc_bulk_operation_set_client(bulk, client);
537547

538548
/* If a write concern was not specified, libmongoc will use the client's
@@ -543,14 +553,12 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, mongoc
543553
write_concern = mongoc_client_get_write_concern(client);
544554
}
545555

546-
efree(dbname);
547-
efree(collname);
548-
549556
if (server_id > 0) {
550557
mongoc_bulk_operation_set_hint(bulk, server_id);
551558
}
552559

553560
success = mongoc_bulk_operation_execute(bulk, &reply, &error);
561+
bulk_write->executed = true;
554562

555563
/* Write succeeded and the user doesn't care for the results */
556564
if (success && !return_value_used) {

php_phongo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void phongo_readpreference_init (zval *return_value, const
143143
void phongo_writeconcern_init (zval *return_value, const mongoc_write_concern_t *write_concern TSRMLS_DC);
144144
bool phongo_query_init (php_phongo_query_t *query, bson_t *filter, bson_t *options TSRMLS_DC);
145145
mongoc_bulk_operation_t* phongo_bulkwrite_init (zend_bool ordered);
146-
bool phongo_execute_write (mongoc_client_t *client, const char *namespace, mongoc_bulk_operation_t *bulk, const mongoc_write_concern_t *write_concern, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
146+
bool phongo_execute_write (mongoc_client_t *client, const char *namespace, php_phongo_bulkwrite_t *bulk_write, const mongoc_write_concern_t *write_concern, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
147147
int phongo_execute_command (mongoc_client_t *client, const char *db, const bson_t *command, const mongoc_read_prefs_t *read_preference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
148148
int phongo_execute_query (mongoc_client_t *client, const char *namespace, const php_phongo_query_t *query, const mongoc_read_prefs_t *read_preference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
149149

php_phongo_structs-5.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ typedef struct {
8888
zend_object std;
8989
mongoc_bulk_operation_t *bulk;
9090
size_t num_ops;
91+
bool ordered;
92+
char *database;
93+
char *collection;
94+
bool executed;
9195
} php_phongo_bulkwrite_t;
9296

9397
typedef struct {

php_phongo_structs-7.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ typedef struct {
8787
typedef struct {
8888
mongoc_bulk_operation_t *bulk;
8989
size_t num_ops;
90+
bool ordered;
91+
char *database;
92+
char *collection;
93+
bool executed;
9094
zend_object std;
9195
} php_phongo_bulkwrite_t;
9296

src/MongoDB/BulkWrite.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ PHP_METHOD(BulkWrite, __construct)
7676
}
7777

7878
intern->bulk = phongo_bulkwrite_init(ordered);
79+
intern->ordered = ordered;
7980
intern->num_ops = 0;
8081

8182
if (options && php_array_exists(options, "bypassDocumentValidation")) {
@@ -286,6 +287,14 @@ static void php_phongo_bulkwrite_free_object(phongo_free_object_arg *object TSRM
286287
mongoc_bulk_operation_destroy(intern->bulk);
287288
}
288289

290+
if (intern->database) {
291+
efree(intern->database);
292+
}
293+
294+
if (intern->collection) {
295+
efree(intern->collection);
296+
}
297+
289298
#if PHP_VERSION_ID < 70000
290299
efree(intern);
291300
#endif
@@ -329,20 +338,20 @@ HashTable *php_phongo_bulkwrite_get_debug_info(zval *object, int *is_temp TSRMLS
329338
intern = Z_BULKWRITE_OBJ_P(object);
330339
array_init(&retval);
331340

332-
if (intern->bulk->database) {
333-
ADD_ASSOC_STRING(&retval, "database", intern->bulk->database);
341+
if (intern->database) {
342+
ADD_ASSOC_STRING(&retval, "database", intern->database);
334343
} else {
335344
ADD_ASSOC_NULL_EX(&retval, "database");
336345
}
337346

338-
if (intern->bulk->collection) {
339-
ADD_ASSOC_STRING(&retval, "collection", intern->bulk->collection);
347+
if (intern->collection) {
348+
ADD_ASSOC_STRING(&retval, "collection", intern->collection);
340349
} else {
341350
ADD_ASSOC_NULL_EX(&retval, "collection");
342351
}
343352

344-
ADD_ASSOC_BOOL_EX(&retval, "ordered", intern->bulk->flags.ordered);
345-
ADD_ASSOC_BOOL_EX(&retval, "executed", intern->bulk->executed);
353+
ADD_ASSOC_BOOL_EX(&retval, "ordered", intern->ordered);
354+
ADD_ASSOC_BOOL_EX(&retval, "executed", intern->executed);
346355
ADD_ASSOC_LONG_EX(&retval, "server_id", mongoc_bulk_operation_get_hint(intern->bulk));
347356

348357
if (mongoc_bulk_operation_get_write_concern(intern->bulk)) {

src/MongoDB/Manager.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ PHP_METHOD(Manager, executeBulkWrite)
152152

153153

154154
bulk = Z_BULKWRITE_OBJ_P(zbulk);
155-
phongo_execute_write(intern->client, namespace, bulk->bulk, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), -1, return_value, return_value_used TSRMLS_CC);
155+
phongo_execute_write(intern->client, namespace, bulk, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), -1, return_value, return_value_used TSRMLS_CC);
156156
}
157157
/* }}} */
158158

src/MongoDB/Server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ PHP_METHOD(Server, executeBulkWrite)
128128

129129

130130
bulk = Z_BULKWRITE_OBJ_P(zbulk);
131-
phongo_execute_write(intern->client, namespace, bulk->bulk, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), intern->server_id, return_value, return_value_used TSRMLS_CC);
131+
phongo_execute_write(intern->client, namespace, bulk, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), intern->server_id, return_value, return_value_used TSRMLS_CC);
132132
}
133133
/* }}} */
134134
/* {{{ proto string Server::getHost()

0 commit comments

Comments
 (0)