|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2017 MongoDB, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace MongoDB; |
| 19 | + |
| 20 | +use IteratorAggregate; |
| 21 | +use stdClass; |
| 22 | + |
| 23 | +/** |
| 24 | + * Result class for mapReduce command results. |
| 25 | + * |
| 26 | + * This class allows for iteration of mapReduce results irrespective of the |
| 27 | + * output method (e.g. inline, collection) via the IteratorAggregate interface. |
| 28 | + * It also provides access to command statistics. |
| 29 | + * |
| 30 | + * @api |
| 31 | + * @see \MongoDB\Collection::mapReduce() |
| 32 | + * @see https://docs.mongodb.com/manual/reference/command/mapReduce/ |
| 33 | + */ |
| 34 | +class MapReduceResult implements IteratorAggregate |
| 35 | +{ |
| 36 | + private $getIterator; |
| 37 | + private $executionTimeMS; |
| 38 | + private $counts; |
| 39 | + private $timing; |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructor. |
| 43 | + * |
| 44 | + * @internal |
| 45 | + * @param callable $getIterator Callback that returns a Traversable for mapReduce results |
| 46 | + * @param stdClass $result Result document from the mapReduce command |
| 47 | + */ |
| 48 | + public function __construct(callable $getIterator, stdClass $result) |
| 49 | + { |
| 50 | + $this->getIterator = $getIterator; |
| 51 | + $this->executionTimeMS = (integer) $result->timeMillis; |
| 52 | + $this->counts = (array) $result->counts; |
| 53 | + $this->timing = isset($result->timing) ? (array) $result->timing : []; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns various count statistics from the mapReduce command. |
| 58 | + * |
| 59 | + * @return array |
| 60 | + */ |
| 61 | + public function getCounts() |
| 62 | + { |
| 63 | + return $this->counts; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Return the command execution time in milliseconds. |
| 68 | + * |
| 69 | + * @return integer |
| 70 | + */ |
| 71 | + public function getExecutionTimeMS() |
| 72 | + { |
| 73 | + return $this->executionTimeMS; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Return the mapReduce results as a Traversable. |
| 78 | + * |
| 79 | + * @see http://php.net/iteratoraggregate.getiterator |
| 80 | + * @return Traversable |
| 81 | + */ |
| 82 | + public function getIterator() |
| 83 | + { |
| 84 | + return call_user_func($this->getIterator); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns various timing statistics from the mapReduce command. |
| 89 | + * |
| 90 | + * Note: timing statistics are only available if the mapReduce command's |
| 91 | + * "verbose" option was true; otherwise, an empty array will be returned. |
| 92 | + * |
| 93 | + * @return array |
| 94 | + */ |
| 95 | + public function getTiming() |
| 96 | + { |
| 97 | + return $this->timing; |
| 98 | + } |
| 99 | +} |
0 commit comments