Skip to content

Commit a757fab

Browse files
committed
Merge pull request #315
2 parents 291b489 + 18bc299 commit a757fab

File tree

10 files changed

+176
-19
lines changed

10 files changed

+176
-19
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ typedef struct {
8888
zend_object std;
8989
mongoc_bulk_operation_t *bulk;
9090
size_t num_ops;
91+
bool ordered;
92+
int bypass;
93+
char *database;
94+
char *collection;
95+
bool executed;
9196
} php_phongo_bulkwrite_t;
9297

9398
typedef struct {

php_phongo_structs-7.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ typedef struct {
8787
typedef struct {
8888
mongoc_bulk_operation_t *bulk;
8989
size_t num_ops;
90+
bool ordered;
91+
int bypass;
92+
char *database;
93+
char *collection;
94+
bool executed;
9095
zend_object std;
9196
} php_phongo_bulkwrite_t;
9297

src/MongoDB/BulkWrite.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "php_phongo.h"
4747
#include "php_bson.h"
4848

49+
#define BYPASS_UNSET -1
4950

5051
PHONGO_API zend_class_entry *php_phongo_bulkwrite_ce;
5152

@@ -76,10 +77,14 @@ PHP_METHOD(BulkWrite, __construct)
7677
}
7778

7879
intern->bulk = phongo_bulkwrite_init(ordered);
80+
intern->ordered = ordered;
81+
intern->bypass = BYPASS_UNSET;
7982
intern->num_ops = 0;
8083

8184
if (options && php_array_exists(options, "bypassDocumentValidation")) {
82-
mongoc_bulk_operation_set_bypass_document_validation(intern->bulk, php_array_fetch_bool(options, "bypassDocumentValidation"));
85+
zend_bool bypass = php_array_fetch_bool(options, "bypassDocumentValidation");
86+
mongoc_bulk_operation_set_bypass_document_validation(intern->bulk, bypass);
87+
intern->bypass = bypass;
8388
}
8489
}
8590
/* }}} */
@@ -286,6 +291,14 @@ static void php_phongo_bulkwrite_free_object(phongo_free_object_arg *object TSRM
286291
mongoc_bulk_operation_destroy(intern->bulk);
287292
}
288293

294+
if (intern->database) {
295+
efree(intern->database);
296+
}
297+
298+
if (intern->collection) {
299+
efree(intern->collection);
300+
}
301+
289302
#if PHP_VERSION_ID < 70000
290303
efree(intern);
291304
#endif
@@ -329,20 +342,27 @@ HashTable *php_phongo_bulkwrite_get_debug_info(zval *object, int *is_temp TSRMLS
329342
intern = Z_BULKWRITE_OBJ_P(object);
330343
array_init(&retval);
331344

332-
if (intern->bulk->database) {
333-
ADD_ASSOC_STRING(&retval, "database", intern->bulk->database);
345+
if (intern->database) {
346+
ADD_ASSOC_STRING(&retval, "database", intern->database);
334347
} else {
335348
ADD_ASSOC_NULL_EX(&retval, "database");
336349
}
337350

338-
if (intern->bulk->collection) {
339-
ADD_ASSOC_STRING(&retval, "collection", intern->bulk->collection);
351+
if (intern->collection) {
352+
ADD_ASSOC_STRING(&retval, "collection", intern->collection);
340353
} else {
341354
ADD_ASSOC_NULL_EX(&retval, "collection");
342355
}
343356

344-
ADD_ASSOC_BOOL_EX(&retval, "ordered", intern->bulk->flags.ordered);
345-
ADD_ASSOC_BOOL_EX(&retval, "executed", intern->bulk->executed);
357+
ADD_ASSOC_BOOL_EX(&retval, "ordered", intern->ordered);
358+
359+
if (intern->bypass != BYPASS_UNSET) {
360+
ADD_ASSOC_BOOL_EX(&retval, "bypassDocumentValidation", intern->bypass);
361+
} else {
362+
ADD_ASSOC_NULL_EX(&retval, "bypassDocumentValidation");
363+
}
364+
365+
ADD_ASSOC_BOOL_EX(&retval, "executed", intern->executed);
346366
ADD_ASSOC_LONG_EX(&retval, "server_id", mongoc_bulk_operation_get_hint(intern->bulk));
347367

348368
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()

tests/bulk/bulkwrite-debug-001.phpt

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWrite debug output before execution
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$tests = [
10+
[],
11+
['ordered' => true],
12+
['ordered' => false],
13+
['bypassDocumentValidation' => true],
14+
['bypassDocumentValidation' => false],
15+
];
16+
17+
foreach ($tests as $options) {
18+
var_dump(new MongoDB\Driver\BulkWrite($options));
19+
}
20+
21+
?>
22+
===DONE===
23+
<?php exit(0); ?>
24+
--EXPECTF--
25+
object(MongoDB\Driver\BulkWrite)#%d (%d) {
26+
["database"]=>
27+
NULL
28+
["collection"]=>
29+
NULL
30+
["ordered"]=>
31+
bool(true)
32+
["bypassDocumentValidation"]=>
33+
NULL
34+
["executed"]=>
35+
bool(false)
36+
["server_id"]=>
37+
int(0)
38+
["write_concern"]=>
39+
NULL
40+
}
41+
object(MongoDB\Driver\BulkWrite)#%d (%d) {
42+
["database"]=>
43+
NULL
44+
["collection"]=>
45+
NULL
46+
["ordered"]=>
47+
bool(true)
48+
["bypassDocumentValidation"]=>
49+
NULL
50+
["executed"]=>
51+
bool(false)
52+
["server_id"]=>
53+
int(0)
54+
["write_concern"]=>
55+
NULL
56+
}
57+
object(MongoDB\Driver\BulkWrite)#%d (%d) {
58+
["database"]=>
59+
NULL
60+
["collection"]=>
61+
NULL
62+
["ordered"]=>
63+
bool(false)
64+
["bypassDocumentValidation"]=>
65+
NULL
66+
["executed"]=>
67+
bool(false)
68+
["server_id"]=>
69+
int(0)
70+
["write_concern"]=>
71+
NULL
72+
}
73+
object(MongoDB\Driver\BulkWrite)#%d (%d) {
74+
["database"]=>
75+
NULL
76+
["collection"]=>
77+
NULL
78+
["ordered"]=>
79+
bool(true)
80+
["bypassDocumentValidation"]=>
81+
bool(true)
82+
["executed"]=>
83+
bool(false)
84+
["server_id"]=>
85+
int(0)
86+
["write_concern"]=>
87+
NULL
88+
}
89+
object(MongoDB\Driver\BulkWrite)#%d (%d) {
90+
["database"]=>
91+
NULL
92+
["collection"]=>
93+
NULL
94+
["ordered"]=>
95+
bool(true)
96+
["bypassDocumentValidation"]=>
97+
bool(false)
98+
["executed"]=>
99+
bool(false)
100+
["server_id"]=>
101+
int(0)
102+
["write_concern"]=>
103+
NULL
104+
}
105+
===DONE===

tests/bulk/write-0001.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
4545
NULL
4646
["ordered"]=>
4747
bool(true)
48+
["bypassDocumentValidation"]=>
49+
NULL
4850
["executed"]=>
4951
bool(false)
5052
["server_id"]=>
@@ -59,6 +61,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
5961
NULL
6062
["ordered"]=>
6163
bool(true)
64+
["bypassDocumentValidation"]=>
65+
NULL
6266
["executed"]=>
6367
bool(false)
6468
["server_id"]=>
@@ -73,6 +77,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
7377
NULL
7478
["ordered"]=>
7579
bool(true)
80+
["bypassDocumentValidation"]=>
81+
NULL
7682
["executed"]=>
7783
bool(false)
7884
["server_id"]=>
@@ -87,6 +93,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
8793
NULL
8894
["ordered"]=>
8995
bool(true)
96+
["bypassDocumentValidation"]=>
97+
NULL
9098
["executed"]=>
9199
bool(false)
92100
["server_id"]=>
@@ -101,6 +109,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
101109
string(15) "bulk_write_0001"
102110
["ordered"]=>
103111
bool(true)
112+
["bypassDocumentValidation"]=>
113+
NULL
104114
["executed"]=>
105115
bool(true)
106116
["server_id"]=>

tests/bulk/write-0002.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
4141
NULL
4242
["ordered"]=>
4343
bool(true)
44+
["bypassDocumentValidation"]=>
45+
NULL
4446
["executed"]=>
4547
bool(false)
4648
["server_id"]=>
@@ -55,6 +57,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
5557
string(15) "bulk_write_0002"
5658
["ordered"]=>
5759
bool(true)
60+
["bypassDocumentValidation"]=>
61+
NULL
5862
["executed"]=>
5963
bool(true)
6064
["server_id"]=>

0 commit comments

Comments
 (0)