Commit 1ea4bd09 authored by Jeremy Mikola's avatar Jeremy Mikola

Move mapReduce inline output check to a function

The MapReduce operation class will also need to call this for deciding which execute method to use in PHPLIB-316.
parent 6a46d117
...@@ -831,7 +831,7 @@ class Collection ...@@ -831,7 +831,7 @@ class Collection
*/ */
public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce, $out, array $options = []) public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce, $out, array $options = [])
{ {
$hasOutputCollection = ! $this->isMapReduceOutputInline($out); $hasOutputCollection = ! \MongoDB\is_mapreduce_output_inline($out);
if ( ! isset($options['readPreference'])) { if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference; $options['readPreference'] = $this->readPreference;
...@@ -984,38 +984,4 @@ class Collection ...@@ -984,38 +984,4 @@ class Collection
return new Collection($this->manager, $this->databaseName, $this->collectionName, $options); return new Collection($this->manager, $this->databaseName, $this->collectionName, $options);
} }
/**
* Return whether the "out" option for a mapReduce operation is "inline".
*
* This is used to determine if a mapReduce command requires a primary.
*
* @see https://docs.mongodb.com/manual/reference/command/mapReduce/#output-inline
* @param string|array|object $out Output specification
* @return boolean
* @throws InvalidArgumentException
*/
private function isMapReduceOutputInline($out)
{
if ( ! is_array($out) && ! is_object($out)) {
return false;
}
if ($out instanceof Serializable) {
$out = $out->bsonSerialize();
}
if (is_object($out)) {
$out = get_object_vars($out);
}
if ( ! is_array($out)) {
throw InvalidArgumentException::invalidType('$out', $out, 'array or object');
}
reset($out);
$firstKey = (string) key($out);
return $firstKey === 'inline';
}
} }
...@@ -131,6 +131,40 @@ function is_last_pipeline_operator_out(array $pipeline) ...@@ -131,6 +131,40 @@ function is_last_pipeline_operator_out(array $pipeline)
return key($lastOp) === '$out'; return key($lastOp) === '$out';
} }
/**
* Return whether the "out" option for a mapReduce operation is "inline".
*
* This is used to determine if a mapReduce command requires a primary.
*
* @internal
* @see https://docs.mongodb.com/manual/reference/command/mapReduce/#output-inline
* @param string|array|object $out Output specification
* @return boolean
* @throws InvalidArgumentException
*/
function is_mapreduce_output_inline($out)
{
if ( ! is_array($out) && ! is_object($out)) {
return false;
}
if ($out instanceof Serializable) {
$out = $out->bsonSerialize();
}
if (is_object($out)) {
$out = get_object_vars($out);
}
if ( ! is_array($out)) {
throw InvalidArgumentException::invalidType('$out', $out, 'array or object');
}
reset($out);
return key($out) === 'inline';
}
/** /**
* Converts a ReadConcern instance to a stdClass for use in a BSON document. * Converts a ReadConcern instance to a stdClass for use in a BSON document.
* *
......
...@@ -117,6 +117,24 @@ class FunctionsTest extends TestCase ...@@ -117,6 +117,24 @@ class FunctionsTest extends TestCase
\MongoDB\is_first_key_operator($document); \MongoDB\is_first_key_operator($document);
} }
/**
* @dataProvider provideMapReduceOutValues
*/
public function testIsMapReduceOutputInline($out, $isInline)
{
$this->assertSame($isInline, \MongoDB\is_mapreduce_output_inline($out));
}
public function provideMapReduceOutValues()
{
return [
[ 'collectionName', false ],
[ ['inline' => 1], true ],
[ ['inline' => 0], true ], // only the key is significant
[ ['replace' => 'collectionName'], false ],
];
}
/** /**
* @dataProvider provideReadConcernsAndDocuments * @dataProvider provideReadConcernsAndDocuments
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment