Skip to content

PHPC-682, PHPC-684: BulkWrite debug handler improvements #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,22 +517,32 @@ mongoc_bulk_operation_t *phongo_bulkwrite_init(zend_bool ordered) { /* {{{ */
return mongoc_bulk_operation_new(ordered);
} /* }}} */

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) /* {{{ */
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) /* {{{ */
{
bson_error_t error;
char *dbname;
char *collname;
int success;
bson_t reply = BSON_INITIALIZER;
mongoc_bulk_operation_t *bulk = bulk_write->bulk;
php_phongo_writeresult_t *writeresult;

if (!phongo_split_namespace(namespace, &dbname, &collname)) {
/* Since BulkWrite objects can currently be executed multiple times, ensure
* that the database and collection name are freed before we overwrite them.
* This may be removed once PHPC-676 is implemented. */
if (bulk_write->database) {
efree(bulk_write->database);
}

if (bulk_write->collection) {
efree(bulk_write->collection);
}

if (!phongo_split_namespace(namespace, &bulk_write->database, &bulk_write->collection)) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s: %s", "Invalid namespace provided", namespace);
return false;
}

mongoc_bulk_operation_set_database(bulk, dbname);
mongoc_bulk_operation_set_collection(bulk, collname);
mongoc_bulk_operation_set_database(bulk, bulk_write->database);
mongoc_bulk_operation_set_collection(bulk, bulk_write->collection);
mongoc_bulk_operation_set_client(bulk, client);

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

efree(dbname);
efree(collname);

if (server_id > 0) {
mongoc_bulk_operation_set_hint(bulk, server_id);
}

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

/* Write succeeded and the user doesn't care for the results */
if (success && !return_value_used) {
Expand Down
2 changes: 1 addition & 1 deletion php_phongo.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void phongo_readpreference_init (zval *return_value, const
void phongo_writeconcern_init (zval *return_value, const mongoc_write_concern_t *write_concern TSRMLS_DC);
bool phongo_query_init (php_phongo_query_t *query, bson_t *filter, bson_t *options TSRMLS_DC);
mongoc_bulk_operation_t* phongo_bulkwrite_init (zend_bool ordered);
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);
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);
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);
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);

Expand Down
5 changes: 5 additions & 0 deletions php_phongo_structs-5.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ typedef struct {
zend_object std;
mongoc_bulk_operation_t *bulk;
size_t num_ops;
bool ordered;
int bypass;
char *database;
char *collection;
bool executed;
} php_phongo_bulkwrite_t;

typedef struct {
Expand Down
5 changes: 5 additions & 0 deletions php_phongo_structs-7.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ typedef struct {
typedef struct {
mongoc_bulk_operation_t *bulk;
size_t num_ops;
bool ordered;
int bypass;
char *database;
char *collection;
bool executed;
zend_object std;
} php_phongo_bulkwrite_t;

Expand Down
34 changes: 27 additions & 7 deletions src/MongoDB/BulkWrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "php_phongo.h"
#include "php_bson.h"

#define BYPASS_UNSET -1

PHONGO_API zend_class_entry *php_phongo_bulkwrite_ce;

Expand Down Expand Up @@ -76,10 +77,14 @@ PHP_METHOD(BulkWrite, __construct)
}

intern->bulk = phongo_bulkwrite_init(ordered);
intern->ordered = ordered;
intern->bypass = BYPASS_UNSET;
intern->num_ops = 0;

if (options && php_array_exists(options, "bypassDocumentValidation")) {
mongoc_bulk_operation_set_bypass_document_validation(intern->bulk, php_array_fetch_bool(options, "bypassDocumentValidation"));
zend_bool bypass = php_array_fetch_bool(options, "bypassDocumentValidation");
mongoc_bulk_operation_set_bypass_document_validation(intern->bulk, bypass);
intern->bypass = bypass;
}
}
/* }}} */
Expand Down Expand Up @@ -286,6 +291,14 @@ static void php_phongo_bulkwrite_free_object(phongo_free_object_arg *object TSRM
mongoc_bulk_operation_destroy(intern->bulk);
}

if (intern->database) {
efree(intern->database);
}

if (intern->collection) {
efree(intern->collection);
}

#if PHP_VERSION_ID < 70000
efree(intern);
#endif
Expand Down Expand Up @@ -329,20 +342,27 @@ HashTable *php_phongo_bulkwrite_get_debug_info(zval *object, int *is_temp TSRMLS
intern = Z_BULKWRITE_OBJ_P(object);
array_init(&retval);

if (intern->bulk->database) {
ADD_ASSOC_STRING(&retval, "database", intern->bulk->database);
if (intern->database) {
ADD_ASSOC_STRING(&retval, "database", intern->database);
} else {
ADD_ASSOC_NULL_EX(&retval, "database");
}

if (intern->bulk->collection) {
ADD_ASSOC_STRING(&retval, "collection", intern->bulk->collection);
if (intern->collection) {
ADD_ASSOC_STRING(&retval, "collection", intern->collection);
} else {
ADD_ASSOC_NULL_EX(&retval, "collection");
}

ADD_ASSOC_BOOL_EX(&retval, "ordered", intern->bulk->flags.ordered);
ADD_ASSOC_BOOL_EX(&retval, "executed", intern->bulk->executed);
ADD_ASSOC_BOOL_EX(&retval, "ordered", intern->ordered);

if (intern->bypass != BYPASS_UNSET) {
ADD_ASSOC_BOOL_EX(&retval, "bypassDocumentValidation", intern->bypass);
} else {
ADD_ASSOC_NULL_EX(&retval, "bypassDocumentValidation");
}

ADD_ASSOC_BOOL_EX(&retval, "executed", intern->executed);
ADD_ASSOC_LONG_EX(&retval, "server_id", mongoc_bulk_operation_get_hint(intern->bulk));

if (mongoc_bulk_operation_get_write_concern(intern->bulk)) {
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB/Manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ PHP_METHOD(Manager, executeBulkWrite)


bulk = Z_BULKWRITE_OBJ_P(zbulk);
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);
phongo_execute_write(intern->client, namespace, bulk, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), -1, return_value, return_value_used TSRMLS_CC);
}
/* }}} */

Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB/Server.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ PHP_METHOD(Server, executeBulkWrite)


bulk = Z_BULKWRITE_OBJ_P(zbulk);
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);
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);
}
/* }}} */
/* {{{ proto string Server::getHost()
Expand Down
105 changes: 105 additions & 0 deletions tests/bulk/bulkwrite-debug-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
--TEST--
MongoDB\Driver\BulkWrite debug output before execution
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$tests = [
[],
['ordered' => true],
['ordered' => false],
['bypassDocumentValidation' => true],
['bypassDocumentValidation' => false],
];

foreach ($tests as $options) {
var_dump(new MongoDB\Driver\BulkWrite($options));
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
object(MongoDB\Driver\BulkWrite)#%d (%d) {
["database"]=>
NULL
["collection"]=>
NULL
["ordered"]=>
bool(true)
["bypassDocumentValidation"]=>
NULL
["executed"]=>
bool(false)
["server_id"]=>
int(0)
["write_concern"]=>
NULL
}
object(MongoDB\Driver\BulkWrite)#%d (%d) {
["database"]=>
NULL
["collection"]=>
NULL
["ordered"]=>
bool(true)
["bypassDocumentValidation"]=>
NULL
["executed"]=>
bool(false)
["server_id"]=>
int(0)
["write_concern"]=>
NULL
}
object(MongoDB\Driver\BulkWrite)#%d (%d) {
["database"]=>
NULL
["collection"]=>
NULL
["ordered"]=>
bool(false)
["bypassDocumentValidation"]=>
NULL
["executed"]=>
bool(false)
["server_id"]=>
int(0)
["write_concern"]=>
NULL
}
object(MongoDB\Driver\BulkWrite)#%d (%d) {
["database"]=>
NULL
["collection"]=>
NULL
["ordered"]=>
bool(true)
["bypassDocumentValidation"]=>
bool(true)
["executed"]=>
bool(false)
["server_id"]=>
int(0)
["write_concern"]=>
NULL
}
object(MongoDB\Driver\BulkWrite)#%d (%d) {
["database"]=>
NULL
["collection"]=>
NULL
["ordered"]=>
bool(true)
["bypassDocumentValidation"]=>
bool(false)
["executed"]=>
bool(false)
["server_id"]=>
int(0)
["write_concern"]=>
NULL
}
===DONE===
10 changes: 10 additions & 0 deletions tests/bulk/write-0001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
NULL
["ordered"]=>
bool(true)
["bypassDocumentValidation"]=>
NULL
["executed"]=>
bool(false)
["server_id"]=>
Expand All @@ -59,6 +61,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
NULL
["ordered"]=>
bool(true)
["bypassDocumentValidation"]=>
NULL
["executed"]=>
bool(false)
["server_id"]=>
Expand All @@ -73,6 +77,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
NULL
["ordered"]=>
bool(true)
["bypassDocumentValidation"]=>
NULL
["executed"]=>
bool(false)
["server_id"]=>
Expand All @@ -87,6 +93,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
NULL
["ordered"]=>
bool(true)
["bypassDocumentValidation"]=>
NULL
["executed"]=>
bool(false)
["server_id"]=>
Expand All @@ -101,6 +109,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
string(15) "bulk_write_0001"
["ordered"]=>
bool(true)
["bypassDocumentValidation"]=>
NULL
["executed"]=>
bool(true)
["server_id"]=>
Expand Down
4 changes: 4 additions & 0 deletions tests/bulk/write-0002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
NULL
["ordered"]=>
bool(true)
["bypassDocumentValidation"]=>
NULL
["executed"]=>
bool(false)
["server_id"]=>
Expand All @@ -55,6 +57,8 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
string(15) "bulk_write_0002"
["ordered"]=>
bool(true)
["bypassDocumentValidation"]=>
NULL
["executed"]=>
bool(true)
["server_id"]=>
Expand Down