From 29754a128e8efc8a7d9b1c101b5c2bca053d9bae Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Tue, 26 Apr 2016 15:19:33 -0400 Subject: [PATCH 1/2] PHPC-680: BulkWrite::count() should return number of operations Previously, this returned the estimated number of round-trips, which is neither reliable (see: CDRIVER-653) nor particularly useful. --- php_phongo_structs-5.h | 1 + php_phongo_structs-7.h | 1 + src/MongoDB/BulkWrite.c | 9 ++++++- tests/bulk/bulkwrite-count-001.phpt | 41 +++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/bulk/bulkwrite-count-001.phpt diff --git a/php_phongo_structs-5.h b/php_phongo_structs-5.h index e50cb80e6..0f6f2ca3f 100644 --- a/php_phongo_structs-5.h +++ b/php_phongo_structs-5.h @@ -87,6 +87,7 @@ typedef struct { typedef struct { zend_object std; mongoc_bulk_operation_t *bulk; + size_t num_ops; } php_phongo_bulkwrite_t; typedef struct { diff --git a/php_phongo_structs-7.h b/php_phongo_structs-7.h index b879880f7..47f03824f 100644 --- a/php_phongo_structs-7.h +++ b/php_phongo_structs-7.h @@ -86,6 +86,7 @@ typedef struct { typedef struct { mongoc_bulk_operation_t *bulk; + size_t num_ops; zend_object std; } php_phongo_bulkwrite_t; diff --git a/src/MongoDB/BulkWrite.c b/src/MongoDB/BulkWrite.c index 9a0a5203b..f919158ca 100644 --- a/src/MongoDB/BulkWrite.c +++ b/src/MongoDB/BulkWrite.c @@ -76,6 +76,7 @@ PHP_METHOD(BulkWrite, __construct) } intern->bulk = phongo_bulkwrite_init(ordered); + 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")); @@ -111,6 +112,8 @@ PHP_METHOD(BulkWrite, insert) mongoc_bulk_operation_insert(intern->bulk, bson); bson_clear(&bson); + intern->num_ops++; + if (bson_out && return_value_used) { bson_iter_t iter; @@ -177,6 +180,8 @@ PHP_METHOD(BulkWrite, update) } } + intern->num_ops++; + bson_clear(&bquery); bson_clear(&bupdate); } @@ -208,6 +213,8 @@ PHP_METHOD(BulkWrite, delete) mongoc_bulk_operation_remove(intern->bulk, bson); } + intern->num_ops++; + bson_clear(&bson); } /* }}} */ @@ -225,7 +232,7 @@ PHP_METHOD(BulkWrite, count) return; } - RETURN_LONG(intern->bulk->commands.len); + RETURN_LONG(intern->num_ops); } /* }}} */ diff --git a/tests/bulk/bulkwrite-count-001.phpt b/tests/bulk/bulkwrite-count-001.phpt new file mode 100644 index 000000000..42c198614 --- /dev/null +++ b/tests/bulk/bulkwrite-count-001.phpt @@ -0,0 +1,41 @@ +--TEST-- +MongoDB\Driver\BulkWrite::count() should return the number of operations +--SKIPIF-- + +--FILE-- +count()); + +$bulk->insert(['x' => 1]); +var_dump($bulk->count()); + +$bulk->insert(['x' => 2]); +var_dump($bulk->count()); + +$bulk->update(['x' => 3], ['$set' => ['y' => 3]]); +var_dump($bulk->count()); + +$bulk->update(['x' => 4], ['$set' => ['y' => 4]]); +var_dump($bulk->count()); + +$bulk->delete(['x' => 5]); +var_dump($bulk->count()); + +$bulk->delete(['x' => 6]); +var_dump($bulk->count()); + +?> +===DONE=== + +--EXPECT-- +int(0) +int(1) +int(2) +int(3) +int(4) +int(5) +int(6) +===DONE=== From 03c651da7e22aa20686789136ab9cda81731d984 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Tue, 26 Apr 2016 15:20:40 -0400 Subject: [PATCH 2/2] Test that MongoDB\Driver\BulkWrite implements Countable --- tests/bulk/bulkwrite-countable-001.phpt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/bulk/bulkwrite-countable-001.phpt diff --git a/tests/bulk/bulkwrite-countable-001.phpt b/tests/bulk/bulkwrite-countable-001.phpt new file mode 100644 index 000000000..6db98876b --- /dev/null +++ b/tests/bulk/bulkwrite-countable-001.phpt @@ -0,0 +1,17 @@ +--TEST-- +MongoDB\Driver\BulkWrite implements Countable +--SKIPIF-- + +--FILE-- + +===DONE=== + +--EXPECT-- +bool(true) +===DONE===