Skip to content

PHPC-924: Avoid unnecessary BSON conversion in Cursor::setTypeMap() #541

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 1 commit into from
Mar 3, 2017
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
4 changes: 3 additions & 1 deletion src/MongoDB/Cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ PHP_METHOD(Cursor, setTypeMap)
php_phongo_cursor_t *intern;
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
zval *typemap = NULL;
bool restore_current_element = false;
SUPPRESS_UNUSED_WARNING(return_value) SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(return_value_used)


Expand All @@ -225,6 +226,7 @@ PHP_METHOD(Cursor, setTypeMap)
* visitor_data, which contains the only reference to it. */
if (!Z_ISUNDEF(intern->visitor_data.zchild)) {
php_phongo_cursor_free_current(intern);
restore_current_element = true;
}

phongo_bson_typemap_to_state(typemap, &state.map TSRMLS_CC);
Expand All @@ -233,7 +235,7 @@ PHP_METHOD(Cursor, setTypeMap)

/* If the cursor has a current element, we just freed it and should restore
* it with a new type map applied. */
if (mongoc_cursor_current(intern->cursor)) {
if (restore_current_element && mongoc_cursor_current(intern->cursor)) {
const bson_t *doc = mongoc_cursor_current(intern->cursor);

phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &intern->visitor_data);
Expand Down
64 changes: 64 additions & 0 deletions tests/cursor/bug0924-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--TEST--
PHPC-924: Cursor::setTypeMap() may unnecessarily convert first BSON document (type map)
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE) ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

class MyDocument implements MongoDB\BSON\Serializable, MongoDB\BSON\Unserializable
{
private $data;

public function __construct($id)
{
$this->data['_id'] = $id;
}

public function bsonSerialize()
{
return (object) $this->data;
}

public function bsonUnserialize(array $data)
{
printf("%s called for ID: %s\n", __METHOD__, $data['_id']);
$this->data = $data;
}
}

$manager = new MongoDB\Driver\Manager(STANDALONE);

$bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert(new MyDocument('a'));
$bulk->insert(new MyDocument('b'));
$manager->executeBulkWrite(NS, $bulk);

$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
$cursor->setTypeMap(['root' => 'MyDocument']);

foreach ($cursor as $i => $document) {
var_dump($document);
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
MyDocument::bsonUnserialize called for ID: a
object(MyDocument)#%d (%d) {
["data":"MyDocument":private]=>
array(1) {
["_id"]=>
string(1) "a"
}
}
MyDocument::bsonUnserialize called for ID: b
object(MyDocument)#%d (%d) {
["data":"MyDocument":private]=>
array(1) {
["_id"]=>
string(1) "b"
}
}
===DONE===
80 changes: 80 additions & 0 deletions tests/cursor/bug0924-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
--TEST--
PHPC-924: Cursor::setTypeMap() may unnecessarily convert first BSON document (__pclass)
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE) ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

class MyDocument implements MongoDB\BSON\Persistable
{
private $data;

public function __construct($id)
{
$this->data['_id'] = $id;
}

public function bsonSerialize()
{
return (object) $this->data;
}

public function bsonUnserialize(array $data)
{
printf("%s called for ID: %s\n", __METHOD__, $data['_id']);
$this->data = $data;
}
}

$manager = new MongoDB\Driver\Manager(STANDALONE);

$bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert(new MyDocument('a'));
$bulk->insert(new MyDocument('b'));
$manager->executeBulkWrite(NS, $bulk);

$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
/* This type map will have no effect on the query result, since the document
* only contains an ID, but it allows us to test for unnecessary conversion. */
$cursor->setTypeMap(['array' => 'array']);

foreach ($cursor as $i => $document) {
var_dump($document);
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
MyDocument::bsonUnserialize called for ID: a
object(MyDocument)#%d (%d) {
["data":"MyDocument":private]=>
array(2) {
["_id"]=>
string(1) "a"
["__pclass"]=>
object(MongoDB\BSON\Binary)#%d (%d) {
["data"]=>
string(10) "MyDocument"
["type"]=>
int(128)
}
}
}
MyDocument::bsonUnserialize called for ID: b
object(MyDocument)#%d (%d) {
["data":"MyDocument":private]=>
array(2) {
["_id"]=>
string(1) "b"
["__pclass"]=>
object(MongoDB\BSON\Binary)#%d (%d) {
["data"]=>
string(10) "MyDocument"
["type"]=>
int(128)
}
}
}
===DONE===