|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Operation; |
| 4 | + |
| 5 | +use BSON\Serializable; |
| 6 | +use MongoDB\FeatureDetection; |
| 7 | +use MongoDB\Driver\Command; |
| 8 | +use MongoDB\Driver\Server; |
| 9 | +use MongoDB\Exception\InvalidArgumentException; |
| 10 | +use MongoDB\Exception\InvalidArgumentTypeException; |
| 11 | +use MongoDB\Exception\RuntimeException; |
| 12 | +use ArrayIterator; |
| 13 | +use stdClass; |
| 14 | + |
| 15 | +/** |
| 16 | + * Operation for the aggregate command. |
| 17 | + * |
| 18 | + * @api |
| 19 | + * @see MongoDB\Collection::aggregate() |
| 20 | + * @see http://docs.mongodb.org/manual/reference/command/aggregate/ |
| 21 | + */ |
| 22 | +class Aggregate implements Serializable, Executable |
| 23 | +{ |
| 24 | + const API_AGGREGATE_CURSOR = 2; |
| 25 | + |
| 26 | + private $collectionName; |
| 27 | + private $pipeline; |
| 28 | + private $options; |
| 29 | + |
| 30 | + /** |
| 31 | + * Constructs an aggregate command. |
| 32 | + * |
| 33 | + * Supported options: |
| 34 | + * |
| 35 | + * * allowDiskUse (boolean): Enables writing to temporary files. When set |
| 36 | + * to true, aggregation stages can write data to the _tmp sub-directory |
| 37 | + * in the dbPath directory. The default is false. |
| 38 | + * |
| 39 | + * * batchSize (integer): The number of documents to return per batch. |
| 40 | + * |
| 41 | + * * maxTimeMS (integer): The maximum amount of time to allow the query to |
| 42 | + * run. |
| 43 | + * |
| 44 | + * * useCursor (boolean): Indicates whether the command will request that |
| 45 | + * the server provide results using a cursor. The default is true. |
| 46 | + * |
| 47 | + * For servers < 2.6, this option is ignored as aggregation cursors are |
| 48 | + * not available. |
| 49 | + * |
| 50 | + * For servers >= 2.6, this option allows users to turn off cursors if |
| 51 | + * necessary to aid in mongod/mongos upgrades. |
| 52 | + * |
| 53 | + * @param string $collectionName |
| 54 | + * @param array $pipeline List of pipeline operations |
| 55 | + * @param array $options Command options |
| 56 | + * @throws InvalidArgumentException |
| 57 | + */ |
| 58 | + public function __construct($collectionName, array $pipeline, array $options = array()) |
| 59 | + { |
| 60 | + $options += array( |
| 61 | + 'allowDiskUse' => false, |
| 62 | + 'useCursor' => true, |
| 63 | + ); |
| 64 | + |
| 65 | + if (isset($options['allowDiskUse']) && ! is_bool($options['allowDiskUse'])) { |
| 66 | + throw new InvalidArgumentTypeException('allowDiskUse option', $options['allowDiskUse'], 'boolean'); |
| 67 | + } |
| 68 | + |
| 69 | + if (isset($options['batchSize']) && ! is_integer($options['batchSize'])) { |
| 70 | + throw new InvalidArgumentTypeException('batchSize option', $options['batchSize'], 'integer'); |
| 71 | + } |
| 72 | + |
| 73 | + if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) { |
| 74 | + throw new InvalidArgumentTypeException('maxTimeMS option', $options['maxTimeMS'], 'integer'); |
| 75 | + } |
| 76 | + |
| 77 | + if ( ! is_bool($options['useCursor'])) { |
| 78 | + throw new InvalidArgumentTypeException('useCursor option', $options['useCursor'], 'boolean'); |
| 79 | + } |
| 80 | + |
| 81 | + if (isset($options['batchSize']) && ! $options['useCursor']) { |
| 82 | + throw new InvalidArgumentException('batchSize option should not be used if useCursor is false'); |
| 83 | + } |
| 84 | + |
| 85 | + $expectedIndex = 0; |
| 86 | + |
| 87 | + foreach ($pipeline as $i => $op) { |
| 88 | + if ($i !== $expectedIndex) { |
| 89 | + throw new InvalidArgumentException(sprintf('Pipeline is not a list (unexpected index: "%s")', $i)); |
| 90 | + } |
| 91 | + |
| 92 | + if ( ! is_array($op) && ! is_object($op)) { |
| 93 | + throw new InvalidArgumentTypeException('Pipeline operation #' . $i, $op, 'array or object'); |
| 94 | + } |
| 95 | + |
| 96 | + ++$expectedIndex; |
| 97 | + } |
| 98 | + |
| 99 | + $this->collectionName = $collectionName; |
| 100 | + $this->pipeline = $pipeline; |
| 101 | + $this->options = $options; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Serialize the index information to BSON for index creation. |
| 106 | + * |
| 107 | + * @see MongoDB\Collection::createIndexes() |
| 108 | + * @see http://php.net/bson-serializable.bsonserialize |
| 109 | + */ |
| 110 | + public function bsonSerialize() |
| 111 | + { |
| 112 | + $options = $this->options; |
| 113 | + |
| 114 | + $bson = array( |
| 115 | + 'aggregate' => $this->collectionName, |
| 116 | + 'pipeline' => $this->pipeline, |
| 117 | + ); |
| 118 | + |
| 119 | + if ($options['useCursor']) { |
| 120 | + $bson['cursor'] = isset($options["batchSize"]) |
| 121 | + ? array('batchSize' => $options["batchSize"]) |
| 122 | + : new stdClass; |
| 123 | + |
| 124 | + unset($options['useCursor'], $options['batchSize']); |
| 125 | + } |
| 126 | + |
| 127 | + return $bson + $options; |
| 128 | + } |
| 129 | + |
| 130 | + public function execute(Server $server, $databaseName) |
| 131 | + { |
| 132 | + if ($this->options['useCursor'] && ! FeatureDetection::isSupported($server, self::API_AGGREGATE_CURSOR)) { |
| 133 | + throw new RuntimeException('useCursor and server < 2.6'); |
| 134 | + } |
| 135 | + |
| 136 | + $cursor = $server->executeCommand($databaseName, new Command($this)); |
| 137 | + |
| 138 | + if ($this->options['useCursor']) { |
| 139 | + return $cursor; |
| 140 | + } |
| 141 | + |
| 142 | + $result = current($cursor->toArray()); |
| 143 | + |
| 144 | + if (empty($result['ok'])) { |
| 145 | + throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); |
| 146 | + } |
| 147 | + |
| 148 | + return new ArrayIterator(array_map( |
| 149 | + function (\stdClass $document) { return (array) $document; }, |
| 150 | + $result['result'] |
| 151 | + )); |
| 152 | + } |
| 153 | +} |
0 commit comments