/**
  * Retrieve a sibling by sibling number.
  *
  * @param  integer $i - Sibling number.
  * @param  integer $r - R-Value. Wait until this many partitions
  * have responded before returning to client.
  * @return Object.
  */
 public function getSibling($i, $r = null)
 {
     # Use defaults if not specified.
     $r = $this->bucket->getR($r);
     # Run the request...
     $vtag = $this->siblings[$i];
     $params = array('r' => $r, 'vtag' => $vtag);
     $url = Utils::buildRestPath($this->client, $this->bucket, $this->key, null, $params);
     $response = Utils::httpRequest('GET', $url);
     # Respond with a new object...
     $obj = new Object($this->client, $this->bucket, $this->key);
     $obj->jsonize = $this->jsonize;
     $obj->populate($response, array(200));
     return $obj;
 }
 /**
  * Check if a given key exists in a bucket
  *
  * @author Edgar Veiga <*****@*****.**>
  * @param string $key - The key to check
  * @return bool
  */
 public function hasKey($key)
 {
     $url = Utils::buildRestPath($this->client, $this, $key);
     $response = Utils::httpRequest('HEAD', $url);
     if ($response == null) {
         throw new Exception("Error checking if key exists.");
     }
     $status = $response[0]['http_code'];
     if ($status === 200) {
         return true;
     }
     return false;
 }
 /**
  * Check if the Riak server for this Client is alive
  *
  * @return boolean
  */
 public function isAlive()
 {
     $url = 'http://' . $this->host . ':' . $this->port . '/ping';
     $response = Utils::httpRequest('GET', $url);
     return $response != null && $response[1] == 'OK';
 }
 /**
  * Run the map/reduce operation
  *
  * Returns an array of results, or an
  * array of Link objects if the last phase is a link phase.
  *
  * @param integer $timeout - Timeout in seconds.
  * @return array()
  */
 public function run($timeout = null)
 {
     $num_phases = count($this->phases);
     $linkResultsFlag = false;
     # If there are no phases, then just echo the inputs back to the user.
     if ($num_phases == 0) {
         $this->reduce(array("riak_kv_mapreduce", "reduce_identity"));
         $num_phases = 1;
         $linkResultsFlag = true;
     }
     # Convert all phases to associative arrays. Also,
     # if none of the phases are accumulating, then set the last one to
     # accumulate.
     $keep_flag = false;
     $query = array();
     for ($i = 0; $i < $num_phases; $i++) {
         $phase = $this->phases[$i];
         if ($i == $num_phases - 1 && !$keep_flag) {
             $phase->keep = true;
         }
         if ($phase->keep) {
             $keep_flag = true;
         }
         $query[] = $phase->to_array();
     }
     # Add key filters if applicable
     if ($this->input_mode == 'bucket' && count($this->key_filters) > 0) {
         $this->inputs = array('bucket' => $this->inputs, 'key_filters' => $this->key_filters);
     }
     # Add index search if applicable
     if ($this->input_mode == 'bucket' && count($this->index) > 0) {
         $this->inputs = array_merge(array('bucket' => $this->inputs), $this->index);
     }
     # Construct the job, optionally set the timeout...
     $job = array("inputs" => $this->inputs, "query" => $query);
     if ($timeout != null) {
         $job["timeout"] = $timeout;
     }
     $content = json_encode($job);
     # Do the request...
     $url = "http://" . $this->client->host . ":" . $this->client->port . "/" . $this->client->mapred_prefix;
     $response = Utils::httpRequest('POST', $url, array('Content-type: application/json'), $content);
     $result = json_decode($response[1]);
     # If the last phase is NOT a link phase, then return the result.
     $linkResultsFlag |= end($this->phases) instanceof LinkPhase;
     # If we don't need to link results, then just return.
     if (!$linkResultsFlag) {
         return $result;
     }
     # Otherwise, if the last phase IS a link phase, then convert the
     # results to Link objects.
     $a = array();
     foreach ($result as $r) {
         $tag = isset($r[2]) ? $r[2] : null;
         $link = new Link($r[0], $r[1], $tag);
         $link->client = $this->client;
         $a[] = $link;
     }
     return $a;
 }