Skip to content

Commit ae0219d

Browse files
committed
PHPC-168: Implement WriteResult::isAcknowledged()
We copy the write concern struct rather than store an is_acknowledged boolean, which allows us to include the write concern in the debug output. Down the line, we may decide to implement a getWriteConcern() method to help users in handling/logging write concern errors.
1 parent 9298ad8 commit ae0219d

7 files changed

+218
-2
lines changed

php_phongo.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,11 @@ bool phongo_execute_write(mongoc_client_t *client, char *namespace, mongoc_bulk_
486486
mongoc_bulk_operation_set_client(bulk, client);
487487

488488
/* If a write concern was not specified, libmongoc will use the client's
489-
* write concern. */
489+
* write concern; however, we should still fetch it for the write result. */
490490
if (write_concern) {
491491
mongoc_bulk_operation_set_write_concern(bulk, write_concern);
492+
} else {
493+
write_concern = mongoc_client_get_write_concern(client);
492494
}
493495

494496
efree(dbname);
@@ -511,6 +513,7 @@ bool phongo_execute_write(mongoc_client_t *client, char *namespace, mongoc_bulk_
511513
}
512514

513515
writeresult = phongo_writeresult_init(return_value, &bulk->result, server_hint TSRMLS_CC);
516+
writeresult->write_concern = mongoc_write_concern_copy(write_concern);
514517

515518
/* The Write failed */
516519
if (!hint) {

php_phongo_classes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ typedef struct {
129129
zend_object std;
130130
mongoc_write_result_t write_result;
131131
int hint;
132+
mongoc_write_concern_t *write_concern;
132133
} php_phongo_writeresult_t;
133134

134135
typedef struct {

src/MongoDB/WriteResult.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
/* External libs */
2828
#include <bson.h>
2929
#include <mongoc.h>
30+
#include <mongoc-write-concern-private.h>
3031

3132
/* PHP Core stuff */
3233
#include <php.h>
@@ -310,6 +311,24 @@ PHP_METHOD(WriteResult, getWriteErrors)
310311
}
311312
}
312313
/* }}} */
314+
/* {{{ proto boolean WriteResult::isAcknowledged()
315+
Returns the number of documents that were upserted */
316+
PHP_METHOD(WriteResult, isAcknowledged)
317+
{
318+
php_phongo_writeresult_t *intern;
319+
(void)return_value_ptr; (void)return_value_used;
320+
321+
322+
intern = (php_phongo_writeresult_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
323+
324+
if (zend_parse_parameters_none() == FAILURE) {
325+
return;
326+
}
327+
328+
329+
RETURN_BOOL(_mongoc_write_concern_needs_gle(intern->write_concern));
330+
}
331+
/* }}} */
313332

314333
/**
315334
* Result returned by Server and Manager executeBulkWrite() methods.
@@ -349,6 +368,9 @@ ZEND_END_ARG_INFO();
349368
ZEND_BEGIN_ARG_INFO_EX(ai_WriteResult_getWriteErrors, 0, 0, 0)
350369
ZEND_END_ARG_INFO();
351370

371+
ZEND_BEGIN_ARG_INFO_EX(ai_WriteResult_isAcknowledged, 0, 0, 0)
372+
ZEND_END_ARG_INFO();
373+
352374

353375
static zend_function_entry php_phongo_writeresult_me[] = {
354376
PHP_ME(WriteResult, getInsertedCount, ai_WriteResult_getInsertedCount, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
@@ -361,6 +383,7 @@ static zend_function_entry php_phongo_writeresult_me[] = {
361383
PHP_ME(WriteResult, getUpsertedIds, ai_WriteResult_getUpsertedIds, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
362384
PHP_ME(WriteResult, getwriteConcernError, ai_WriteResult_getwriteConcernError, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
363385
PHP_ME(WriteResult, getWriteErrors, ai_WriteResult_getWriteErrors, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
386+
PHP_ME(WriteResult, isAcknowledged, ai_WriteResult_isAcknowledged, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
364387
PHP_FE_END
365388
};
366389

@@ -376,6 +399,10 @@ static void php_phongo_writeresult_free_object(void *object TSRMLS_DC) /* {{{ */
376399

377400
_mongoc_write_result_destroy(&intern->write_result);
378401

402+
if (intern->write_concern) {
403+
mongoc_write_concern_destroy(intern->write_concern);
404+
}
405+
379406
efree(intern);
380407
} /* }}} */
381408

@@ -405,7 +432,7 @@ HashTable *php_phongo_writeresult_get_debug_info(zval *object, int *is_temp TSRM
405432

406433
intern = (php_phongo_writeresult_t *)zend_object_store_get_object(object TSRMLS_CC);
407434
*is_temp = 1;
408-
array_init_size(&retval, 8);
435+
array_init_size(&retval, 9);
409436

410437
add_assoc_long_ex(&retval, ZEND_STRS("nInserted"), intern->write_result.nInserted);
411438
add_assoc_long_ex(&retval, ZEND_STRS("nMatched"), intern->write_result.nMatched);
@@ -426,6 +453,15 @@ HashTable *php_phongo_writeresult_get_debug_info(zval *object, int *is_temp TSRM
426453
bson_to_zval(bson_get_data(&intern->write_result.writeConcernError), intern->write_result.writeConcernError.len, &state);
427454
add_assoc_zval_ex(&retval, ZEND_STRS("writeConcernError"), state.zchild);
428455

456+
if (intern->write_concern) {
457+
zval *write_concern = NULL;
458+
MAKE_STD_ZVAL(write_concern);
459+
php_phongo_write_concern_to_zval(write_concern, intern->write_concern);
460+
add_assoc_zval_ex(&retval, ZEND_STRS("writeConcern"), write_concern);
461+
} else {
462+
add_assoc_null_ex(&retval, ZEND_STRS("writeConcern"));
463+
}
464+
429465
return Z_ARRVAL(retval);
430466
} /* }}} */
431467
/* }}} */

tests/standalone/server-executeBulkWrite-001.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ object(MongoDB\Driver\WriteResult)#%d (%d) {
6969
["writeConcernError"]=>
7070
array(0) {
7171
}
72+
["writeConcern"]=>
73+
array(4) {
74+
["wmajority"]=>
75+
bool(false)
76+
["wtimeout"]=>
77+
int(0)
78+
["fsync"]=>
79+
bool(false)
80+
["journal"]=>
81+
bool(false)
82+
}
7283
}
7384

7485
===> Collection
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
MongoDB\Driver\WriteResult::isAcknowledged() with default WriteConcern
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(MONGODB_URI);
10+
11+
$result = $manager->executeInsert(NS, array('x' => 1));
12+
13+
printf("WriteResult::isAcknowledged(): %s\n", $result->isAcknowledged() ? 'true' : 'false');
14+
var_dump($result);
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECTF--
20+
WriteResult::isAcknowledged(): true
21+
object(MongoDB\Driver\WriteResult)#%d (%d) {
22+
["nInserted"]=>
23+
int(1)
24+
["nMatched"]=>
25+
int(0)
26+
["nModified"]=>
27+
int(0)
28+
["nRemoved"]=>
29+
int(0)
30+
["nUpserted"]=>
31+
int(0)
32+
["upsertedIds"]=>
33+
array(0) {
34+
}
35+
["writeErrors"]=>
36+
array(0) {
37+
}
38+
["writeConcernError"]=>
39+
array(0) {
40+
}
41+
["writeConcern"]=>
42+
array(4) {
43+
["wmajority"]=>
44+
bool(false)
45+
["wtimeout"]=>
46+
int(0)
47+
["fsync"]=>
48+
bool(false)
49+
["journal"]=>
50+
bool(false)
51+
}
52+
}
53+
===DONE===
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
MongoDB\Driver\WriteResult::isAcknowledged() with inherited WriteConcern
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
/* We use w:0 here because libmongoc detects w:1 as the server's default and
10+
*/
11+
$manager = new MongoDB\Driver\Manager(MONGODB_URI . "/?w=0");
12+
13+
$result = $manager->executeInsert(NS, array('x' => 1));
14+
15+
printf("WriteResult::isAcknowledged(): %s\n", $result->isAcknowledged() ? 'true' : 'false');
16+
var_dump($result);
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECTF--
22+
WriteResult::isAcknowledged(): false
23+
object(MongoDB\Driver\WriteResult)#%d (%d) {
24+
["nInserted"]=>
25+
int(0)
26+
["nMatched"]=>
27+
int(0)
28+
["nModified"]=>
29+
int(0)
30+
["nRemoved"]=>
31+
int(0)
32+
["nUpserted"]=>
33+
int(0)
34+
["upsertedIds"]=>
35+
array(0) {
36+
}
37+
["writeErrors"]=>
38+
array(0) {
39+
}
40+
["writeConcernError"]=>
41+
array(0) {
42+
}
43+
["writeConcern"]=>
44+
array(5) {
45+
["w"]=>
46+
int(0)
47+
["wmajority"]=>
48+
bool(false)
49+
["wtimeout"]=>
50+
int(0)
51+
["fsync"]=>
52+
bool(false)
53+
["journal"]=>
54+
bool(false)
55+
}
56+
}
57+
===DONE===
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
MongoDB\Driver\WriteResult::isAcknowledged() with custom WriteConcern
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(MONGODB_URI);
10+
11+
$result = $manager->executeInsert(NS, array('x' => 2), new MongoDB\Driver\WriteConcern(0));
12+
13+
printf("WriteResult::isAcknowledged(): %s\n", $result->isAcknowledged() ? 'true' : 'false');
14+
var_dump($result);
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECTF--
20+
WriteResult::isAcknowledged(): false
21+
object(MongoDB\Driver\WriteResult)#%d (%d) {
22+
["nInserted"]=>
23+
int(0)
24+
["nMatched"]=>
25+
int(0)
26+
["nModified"]=>
27+
int(0)
28+
["nRemoved"]=>
29+
int(0)
30+
["nUpserted"]=>
31+
int(0)
32+
["upsertedIds"]=>
33+
array(0) {
34+
}
35+
["writeErrors"]=>
36+
array(0) {
37+
}
38+
["writeConcernError"]=>
39+
array(0) {
40+
}
41+
["writeConcern"]=>
42+
array(5) {
43+
["w"]=>
44+
int(0)
45+
["wmajority"]=>
46+
bool(false)
47+
["wtimeout"]=>
48+
int(0)
49+
["fsync"]=>
50+
bool(false)
51+
["journal"]=>
52+
bool(false)
53+
}
54+
}
55+
===DONE===

0 commit comments

Comments
 (0)