Skip to content

PHPLIB-94: Implement BulkWriteResult class for CRUD spec #11

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 11 commits into from
May 3, 2015
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
136 changes: 136 additions & 0 deletions src/BulkWriteResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace MongoDB;

use BSON\ObjectId;
use MongoDB\Driver\WriteResult;

/**
* Result class for a bulk write operation.
*/
class BulkWriteResult
{
private $writeResult;
private $insertedIds;

/**
* Constructor.
*
* @param WriteResult $writeResult
* @param mixed[] $insertedIds
*/
public function __construct(WriteResult $writeResult, array $insertedIds)
{
$this->writeResult = $writeResult;
$this->insertedIds = $insertedIds;
}

/**
* Return the number of documents that were deleted.
*
* This value is undefined if the write was not acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
*/
public function getDeletedCount()
{
return $this->writeResult->getDeletedCount();
}

/**
* Return the number of documents that were inserted.
*
* This value is undefined if the write was not acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
*/
public function getInsertedCount()
{
return $this->writeResult->getInsertedCount();
}

/**
* Return a map of the inserted documents' IDs.
*
* The index of each ID in the map corresponds to the document's position
* in bulk operation. If the document had an ID prior to insertion (i.e. the
* driver did not generate an ID), this will contain its "_id" field value.
* Any driver-generated ID will be an MongoDB\Driver\ObjectID instance.
*
* @return mixed[]
*/
public function getInsertedIds()
{
return $this->insertedIds;
}

/**
* Return the number of documents that were matched by the filter.
*
* This value is undefined if the write was not acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
*/
public function getMatchedCount()
{
return $this->writeResult->getMatchedCount();
}

/**
* Return the number of documents that were modified.
*
* This value is undefined if the write was not acknowledged or if the write
* executed as a legacy operation instead of write command.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer|null
*/
public function getModifiedCount()
{
return $this->writeResult->getModifiedCount();
}

/**
* Return the number of documents that were upserted.
*
* This value is undefined if the write was not acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
*/
public function getUpsertedCount()
{
return $this->writeResult->getUpsertedCount();
}

/**
* Return a map of the upserted documents' IDs.
*
* The index of each ID in the map corresponds to the document's position
* in bulk operation. If the document had an ID prior to upserting (i.e. the
* server did not need to generate an ID), this will contain its "_id". Any
* server-generated ID will be an MongoDB\Driver\ObjectID instance.
*
* @return mixed[]
*/
public function getUpsertedIds()
{
return $this->writeResult->getUpsertedIds();
}

/**
* Return whether this update was acknowledged by the server.
*
* If the update was not acknowledged, other fields from the WriteResult
* (e.g. matchedCount) will be undefined.
*
* @return boolean
*/
public function isAcknowledged()
{
return $this->writeResult->isAcknowledged();
}
}
31 changes: 23 additions & 8 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,32 +198,44 @@ function (\stdClass $document) { return (array) $document; },
*
* @see Collection::getBulkOptions() for supported $options
*
* @param array $bulk Array of operations
* @param array $ops Array of operations
* @param array $options Additional options
* @return WriteResult
*/
public function bulkWrite(array $bulk, array $options = array())
public function bulkWrite(array $ops, array $options = array())
{
$options = array_merge($this->getBulkOptions(), $options);

$bulk = new BulkWrite($options["ordered"]);
$insertedIds = array();

foreach ($bulk as $n => $op) {
foreach ($ops as $n => $op) {
foreach ($op as $opname => $args) {
if (!isset($args[0])) {
throw new InvalidArgumentException(sprintf("Missing argument#1 for '%s' (operation#%d)", $opname, $n));
}

switch ($opname) {
case "insertOne":
$bulk->insert($args[0]);
$insertedId = $bulk->insert($args[0]);

if ($insertedId !== null) {
$insertedIds[$n] = $insertedId;
} else {
$insertedIds[$n] = is_array($args[0]) ? $args[0]['_id'] : $args[0]->_id;
}

break;

case "updateMany":
if (!isset($args[1])) {
throw new InvalidArgumentException(sprintf("Missing argument#2 for '%s' (operation#%d)", $opname, $n));
}
$options = array_merge($this->getWriteOptions(), isset($args[2]) ? $args[2] : array(), array("limit" => 0));
$options = array_merge($this->getWriteOptions(), isset($args[2]) ? $args[2] : array(), array("multi" => true));
$firstKey = key($args[1]);
if (!isset($firstKey[0]) || $firstKey[0] != '$') {
throw new InvalidArgumentException("First key in \$update must be a \$operator");
}

$bulk->update($args[0], $args[1], $options);
break;
Expand All @@ -232,7 +244,7 @@ public function bulkWrite(array $bulk, array $options = array())
if (!isset($args[1])) {
throw new InvalidArgumentException(sprintf("Missing argument#2 for '%s' (operation#%d)", $opname, $n));
}
$options = array_merge($this->getWriteOptions(), isset($args[2]) ? $args[2] : array(), array("limit" => 1));
$options = array_merge($this->getWriteOptions(), isset($args[2]) ? $args[2] : array(), array("multi" => false));
$firstKey = key($args[1]);
if (!isset($firstKey[0]) || $firstKey[0] != '$') {
throw new InvalidArgumentException("First key in \$update must be a \$operator");
Expand All @@ -245,7 +257,7 @@ public function bulkWrite(array $bulk, array $options = array())
if (!isset($args[1])) {
throw new InvalidArgumentException(sprintf("Missing argument#2 for '%s' (operation#%d)", $opname, $n));
}
$options = array_merge($this->getWriteOptions(), isset($args[2]) ? $args[2] : array(), array("limit" => 1));
$options = array_merge($this->getWriteOptions(), isset($args[2]) ? $args[2] : array(), array("multi" => false));
$firstKey = key($args[1]);
if (isset($firstKey[0]) && $firstKey[0] == '$') {
throw new InvalidArgumentException("First key in \$update must NOT be a \$operator");
Expand All @@ -269,7 +281,10 @@ public function bulkWrite(array $bulk, array $options = array())
}
}
}
return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);

$writeResult = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);

return new BulkWriteResult($writeResult, $insertedIds);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/InsertManyResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class InsertManyResult
* @param WriteResult $writeResult
* @param mixed[] $insertedIds
*/
public function __construct(WriteResult $writeResult, array $insertedIds = array())
public function __construct(WriteResult $writeResult, array $insertedIds)
{
$this->writeResult = $writeResult;
$this->insertedIds = $insertedIds;
Expand All @@ -38,11 +38,11 @@ public function getInsertedCount()
}

/**
* Return the map of inserted IDs that were generated by the driver.
* Return a map of the inserted documents' IDs.
*
* The index of each ID in the map corresponds to the document's position
* in bulk operation. If the document already an ID prior to insertion (i.e.
* the driver did not need to generate an ID), this will contain its "_id".
* in bulk operation. If the document had an ID prior to insertion (i.e. the
* driver did not generate an ID), this will contain its "_id" field value.
* Any driver-generated ID will be an MongoDB\Driver\ObjectID instance.
*
* @return mixed[]
Expand Down
2 changes: 1 addition & 1 deletion src/InsertOneResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getInsertedCount()
}

/**
* Return the inserted ID that was generated by the driver.
* Return the inserted document's ID.
*
* If the document already an ID prior to insertion (i.e. the driver did not
* need to generate an ID), this will contain its "_id". Any
Expand Down
19 changes: 16 additions & 3 deletions src/UpdateResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace MongoDB;

use BSON\ObjectId;
use MongoDB\Driver\WriteResult;

/**
Expand Down Expand Up @@ -38,7 +37,8 @@ public function getMatchedCount()
/**
* Return the number of documents that were modified.
*
* This value is undefined if the write was not acknowledged.
* This value is undefined if the write was not acknowledged or if the write
* executed as a legacy operation instead of write command.
*
* @see UpdateResult::isAcknowledged()
* @return integer
Expand All @@ -48,12 +48,25 @@ public function getModifiedCount()
return $this->writeResult->getModifiedCount();
}

/**
* Return the number of documents that were upserted.
*
* This value is undefined if the write was not acknowledged.
*
* @see UpdateResult::isAcknowledged()
* @return integer
*/
public function getUpsertedCount()
{
return $this->writeResult->getUpsertedCount();
}

/**
* Return the ID of the document inserted by an upsert operation.
*
* This value is undefined if an upsert did not take place.
*
* @return ObjectId|null
* @return mixed|null
*/
public function getUpsertedId()
{
Expand Down
Loading