-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-1192: Remove useCursor option in aggregate #1130
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
|
||
namespace MongoDB\Operation; | ||
|
||
use ArrayIterator; | ||
use MongoDB\Driver\Command; | ||
use MongoDB\Driver\Cursor; | ||
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; | ||
|
@@ -31,13 +30,11 @@ | |
use MongoDB\Exception\UnsupportedException; | ||
use stdClass; | ||
|
||
use function current; | ||
use function is_array; | ||
use function is_bool; | ||
use function is_integer; | ||
use function is_object; | ||
use function is_string; | ||
use function MongoDB\create_field_path_type_map; | ||
use function MongoDB\is_document; | ||
use function MongoDB\is_last_pipeline_operator_write; | ||
use function MongoDB\is_pipeline; | ||
|
@@ -58,8 +55,6 @@ class Aggregate implements Executable, Explainable | |
|
||
private array $options; | ||
|
||
private bool $isExplain; | ||
|
||
private bool $isWrite; | ||
|
||
/** | ||
|
@@ -112,12 +107,6 @@ class Aggregate implements Executable, Explainable | |
* * typeMap (array): Type map for BSON deserialization. This will be | ||
* applied to the returned Cursor (it is not sent to the server). | ||
* | ||
* * useCursor (boolean): Indicates whether the command will request that | ||
* the server provide results using a cursor. The default is true. | ||
* | ||
* This option allows users to turn off cursors if necessary to aid in | ||
* mongod/mongos upgrades. | ||
* | ||
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This only | ||
* applies when an $out or $merge stage is specified. | ||
* | ||
|
@@ -136,8 +125,6 @@ public function __construct(string $databaseName, ?string $collectionName, array | |
throw new InvalidArgumentException('$pipeline is not a valid aggregation pipeline'); | ||
} | ||
|
||
$options += ['useCursor' => true]; | ||
|
||
if (isset($options['allowDiskUse']) && ! is_bool($options['allowDiskUse'])) { | ||
throw InvalidArgumentException::invalidType('"allowDiskUse" option', $options['allowDiskUse'], 'boolean'); | ||
} | ||
|
@@ -190,18 +177,10 @@ public function __construct(string $databaseName, ?string $collectionName, array | |
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array'); | ||
} | ||
|
||
if (! is_bool($options['useCursor'])) { | ||
throw InvalidArgumentException::invalidType('"useCursor" option', $options['useCursor'], 'boolean'); | ||
} | ||
|
||
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) { | ||
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class); | ||
} | ||
|
||
if (isset($options['batchSize']) && ! $options['useCursor']) { | ||
throw new InvalidArgumentException('"batchSize" option should not be used if "useCursor" is false'); | ||
} | ||
|
||
if (isset($options['bypassDocumentValidation']) && ! $options['bypassDocumentValidation']) { | ||
unset($options['bypassDocumentValidation']); | ||
} | ||
|
@@ -214,14 +193,7 @@ public function __construct(string $databaseName, ?string $collectionName, array | |
unset($options['writeConcern']); | ||
} | ||
|
||
$this->isExplain = ! empty($options['explain']); | ||
$this->isWrite = is_last_pipeline_operator_write($pipeline) && ! $this->isExplain; | ||
|
||
// Explain does not use a cursor | ||
if ($this->isExplain) { | ||
$options['useCursor'] = false; | ||
unset($options['batchSize']); | ||
} | ||
$this->isWrite = is_last_pipeline_operator_write($pipeline) && ! ($options['explain'] ?? false); | ||
|
||
if ($this->isWrite) { | ||
/* Ignore batchSize for writes, since no documents are returned and | ||
|
@@ -241,7 +213,7 @@ public function __construct(string $databaseName, ?string $collectionName, array | |
* Execute the operation. | ||
* | ||
* @see Executable::execute() | ||
* @return ArrayIterator|Cursor | ||
* @return Cursor | ||
* @throws UnexpectedValueException if the command response was malformed | ||
* @throws UnsupportedException if read concern or write concern is used and unsupported | ||
* @throws DriverRuntimeException for other driver errors (e.g. connection errors) | ||
|
@@ -266,25 +238,11 @@ public function execute(Server $server) | |
|
||
$cursor = $this->executeCommand($server, $command); | ||
|
||
if ($this->options['useCursor'] || $this->isExplain) { | ||
if (isset($this->options['typeMap'])) { | ||
$cursor->setTypeMap($this->options['typeMap']); | ||
} | ||
|
||
return $cursor; | ||
} | ||
|
||
if (isset($this->options['typeMap'])) { | ||
$cursor->setTypeMap(create_field_path_type_map($this->options['typeMap'], 'result.$')); | ||
$cursor->setTypeMap($this->options['typeMap']); | ||
} | ||
|
||
$result = current($cursor->toArray()); | ||
|
||
if (! is_object($result) || ! isset($result->result) || ! is_array($result->result)) { | ||
throw new UnexpectedValueException('aggregate command did not return a "result" array'); | ||
} | ||
|
||
return new ArrayIterator($result->result); | ||
return $cursor; | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
|
@@ -331,11 +289,9 @@ private function createCommandDocument(): array | |
$cmd['hint'] = is_array($this->options['hint']) ? (object) $this->options['hint'] : $this->options['hint']; | ||
} | ||
|
||
if ($this->options['useCursor']) { | ||
$cmd['cursor'] = isset($this->options["batchSize"]) | ||
? ['batchSize' => $this->options["batchSize"]] | ||
: new stdClass(); | ||
} | ||
$cmd['cursor'] = isset($this->options['batchSize']) | ||
? ['batchSize' => $this->options['batchSize']] | ||
: new stdClass(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume always supplying a Quoting Aggregate: Example from the server docs:
It doesn't say anything about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd leave the option in unless |
||
|
||
return $cmd; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this applies regardless of whether
explain: true
is used. SGTM.