Example #1
0
 /**
  * Execute the mapReduce command.
  *
  * @see Collection::mapReduce()
  * @param string|\RiakCode $map
  * @param string|\RiakCode $reduce
  * @param array|string      $out
  * @param array             $query
  * @param array             $options
  * @return ArrayIterator
  * @throws ResultException if the command fails
  */
 protected function doMapReduce($map, $reduce, $out, array $query, array $options)
 {
     list($commandOptions, $clientOptions) = isset($options['socketTimeoutMS']) || isset($options['timeout']) ? $this->splitCommandAndClientOptions($options) : array($options, array());
     $command = array();
     $command['mapreduce'] = $this->riakCollection->getName();
     $command['map'] = $map;
     $command['reduce'] = $reduce;
     $command['query'] = (object) $query;
     $command['out'] = $out;
     $command = array_merge($command, $commandOptions);
     foreach (array('map', 'reduce', 'finalize') as $key) {
         if (isset($command[$key]) && is_string($command[$key])) {
             $command[$key] = new \RiakCode($command[$key]);
         }
     }
     $result = $this->database->command($command, $clientOptions);
     if (empty($result['ok'])) {
         throw new ResultException($result);
     }
     if (isset($result['result']) && is_string($result['result'])) {
         return $this->database->selectCollection($result['result'])->find();
     }
     if (isset($result['result']) && is_array($result['result']) && isset($result['result']['db'], $result['result']['collection'])) {
         return $this->database->getConnection()->selectCollection($result['result']['db'], $result['result']['collection'])->find();
     }
     $arrayIterator = new ArrayIterator(isset($result['results']) ? $result['results'] : array());
     $arrayIterator->setCommandResult($result);
     return $arrayIterator;
 }