Ejemplo n.º 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;
 }
Ejemplo n.º 2
0
 /**
  * Executes a closure with a temporary read preference on a database or
  * collection.
  *
  * @param Database|Collection $object
  * @param \Closure            $closure
  * @return mixed
  */
 private function withReadPreference($object, \Closure $closure)
 {
     if (!isset($this->query['readPreference'])) {
         return $closure();
     }
     $prevReadPref = $object->getReadPreference();
     $object->setReadPreference($this->query['readPreference'], $this->query['readPreferenceTags']);
     try {
         $result = $closure();
     } catch (\Exception $e) {
     }
     $prevTags = !empty($prevReadPref['tagsets']) ? $prevReadPref['tagsets'] : null;
     $object->setReadPreference($prevReadPref['type'], $prevTags);
     if (isset($e)) {
         throw $e;
     }
     return $result;
 }
Ejemplo n.º 3
0
 /**
  * @see Database::getDBRef()
  */
 public function getDBRef(array $ref)
 {
     $this->log(array('getDBRef' => true, 'reference' => $ref));
     return parent::getDBRef($ref);
 }