예제 #1
0
 public function saveBlogPost(BlogPostInterface $blog)
 {
     $identifier = $blog->getIdentifier();
     $blogJson = json_encode($blog, JSON_PRETTY_PRINT);
     $obj = new Object($identifier);
     $obj->setContent($blogJson);
     $obj->setContentType('application/json');
     try {
         $this->bucket->put($obj);
     } catch (RiakException $ex) {
         // Retry
         $this->bucket->put($obj);
     }
 }
예제 #2
0
 /**
  * On-read conflict resolution. Applied approach here is last write wins.
  * Specific needs may override this method to apply alternate conflict resolutions.
  *
  * {@internal Riak does not attempt to resolve a write conflict, and store
  * it as sibling of conflicted one. By following this approach, it is up to
  * the next read to resolve the conflict. When this happens, your fetched
  * object will have a list of siblings (read as a list of objects).
  * In our specific case, we do not care about the intermediate ones since
  * they are all the same read from storage, and we do apply a last sibling
  * (last write) wins logic.
  * If by any means our resolution generates another conflict, it'll up to
  * next read to properly solve it.}
  *
  * @param string $id
  * @param string $vClock
  * @param array  $objectList
  *
  * @return \Riak\Object
  */
 protected function resolveConflict($id, $vClock, array $objectList)
 {
     // Our approach here is last-write wins
     $winner = $objectList[count($objectList)];
     $putInput = new Input\PutInput();
     $putInput->setVClock($vClock);
     $mergedObject = new Object($id);
     $mergedObject->setContent($winner->getContent());
     $this->bucket->put($mergedObject, $putInput);
     return $mergedObject;
 }
예제 #3
0
 /**
  * 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;
 }
예제 #4
0
 /**
  * {@inheritdoc}
  */
 protected function doSave($id, $data, $lifeTime = 0)
 {
     try {
         $object = new Object($id);
         $object->setContent(serialize($data));
         if ($lifeTime > 0) {
             $object->addMetadata(self::EXPIRES_HEADER, (string) (time() + $lifeTime));
         }
         $this->bucket->put($object);
         return true;
     } catch (Exception\RiakException $e) {
         // Do nothing
     }
     return false;
 }
예제 #5
0
 /**
  * Search a secondary index
  * @author Eric Stevens <*****@*****.**>
  * @param string $indexName - The name of the index to search
  * @param string $indexType - The type of index ('int' or 'bin')
  * @param string|int $startOrExact
  * @param string|int optional $end
  * @param bool $dedupe - whether to eliminate duplicate entries if any
  * @return array of Links
  */
 public function indexSearch($indexName, $indexType, $startOrExact, $end = NULL, $dedupe = false)
 {
     $url = Utils::buildIndexPath($this->client, $this, "{$indexName}_{$indexType}", $startOrExact, $end, NULL);
     $response = Utils::httpRequest('GET', $url);
     $obj = new Object($this->client, $this, NULL);
     $obj->populate($response, array(200));
     if (!$obj->exists()) {
         throw \Exception("Error searching index.");
     }
     $data = $obj->getData();
     $keys = array_map("urldecode", $data["keys"]);
     $seenKeys = array();
     foreach ($keys as $id => &$key) {
         if ($dedupe) {
             if (isset($seenKeys[$key])) {
                 unset($keys[$id]);
                 continue;
             }
             $seenKeys[$key] = true;
         }
         $key = new Link($this->name, $key);
         $key->client = $this->client;
     }
     return $keys;
 }