/** * @param string $key * @param string $content * @param string $contentType * @param string $fqcn * @param array $infos * @param \Guzzle\Http\Message\Header[] $headers * @return \Kbrw\RiakBundle\Model\KV\Data */ public function read($key, $content, $contentType, $fqcn, &$infos = array(), $headers = array()) { $data = new Data($key); try { $data->setStringContent($content); $normalizedContentType = $this->contentTypeNormalizer->getNormalizedContentType($contentType); if ($this->contentTypeNormalizer->isFormatSupportedForSerialization($normalizedContentType)) { $ts = microtime(true); $riakKVObject = $this->serializer->deserialize($data->getContent(true), $fqcn, $normalizedContentType); $infos["deserialization_time"] = microtime(true) - $ts; if ($riakKVObject !== false) { if ($riakKVObject instanceof Transmutable) { $riakKVObject = $riakKVObject->transmute(); } if ($riakKVObject instanceof BaseRiakObject) { if (array_key_exists("x-riak-vclock", $headers)) { $vclockHeader = $headers["x-riak-vclock"]; $riakKVObject->setRiakVectorClock($vclockHeader->__toString()); } if (array_key_exists("link", $headers)) { $linksHeader = $headers["link"]; foreach ($linksHeader->toArray() as $linkHeader) { $matches = array(); if (preg_match('/<(riak\\/.*\\/.* )>; riaktag="(.*)"/', $linkHeader, $matches)) { $riakKVObject->addRiakLink(new Link($matches[1], $matches[2])); } } } } $data->setContent($riakKVObject); } } } catch (\Exception $e) { $this->logger->error("Unable to create the Data object for key '{$key}'. Full message is : \n" . $e->getMessage() . ""); } return $data; }
/** * @param mixed | \Kbrw\RiakBundle\Model\KV\Data | array<string, mixed> | array<\Kbrw\RiakBundle\Model\KV\Data> | \Kbrw\RiakBundle\Model\KV\Datas $objects * @param string $format * @param string $clientId * @return \Kbrw\RiakBundle\Model\KV\Datas */ public function normalizeDatas($objects, $format, $clientId, $objectsAreKeys = false) { $datas = new Datas(); /* Normalize $objects to become an array<Data>*/ // $objects is already a Datas instance. if ($objects instanceof \Kbrw\RiakBundle\Model\KV\Datas) { $objects = $objects->getDatas(); } elseif ($objects instanceof \Kbrw\RiakBundle\Model\KV\Data) { $objects = array($objects); } elseif (is_string($objects) && $objectsAreKeys) { $objects = array(new Data($objects)); } elseif (is_object($objects) && !$objectsAreKeys) { $objects = array(new Data(null, $objects)); } else { $tmp = array(); // $objects is an array<string, mixed> foreach ($objects as $key => $value) { if ($value instanceof \Kbrw\RiakBundle\Model\KV\Data) { $tmp[] = $value; } else { if ($objectsAreKeys) { $data = new Data(trim($value)); } else { $data = new Data(trim($key), $value); } $tmp[] = $data; } } $objects = $tmp; } foreach ($objects as $data) { // prepare headers $data->getHeaderBag()->set("X-Riak-ClientId", $clientId); $data->getHeaderBag()->set("Content-Type", $this->contentTypeNormalizer->getContentType($format)); $content = $data->getContent(); if (isset($content) && $content instanceof BaseRiakObject) { $vectorClock = $content->getRiakVectorClock(); if (!empty($vectorClock)) { $data->getHeaderBag()->set("X-Riak-Vclock", $vectorClock); } $linkValue = ""; foreach ($content->getRiakLinks() as $link) { $linkValue .= (!empty($linkValue) ? ", " : "") . "<" . $link->getKv() . "> riaktag=\"" . $link->getRiakTag() . "\""; } if (!empty($linkValue)) { $data->getHeaderBag()->set("Link", $linkValue); } } // prepare string representation if (isset($content) && $this->contentTypeNormalizer->isFormatSupportedForSerialization($format)) { $data->setStringContent($this->serializer->serialize($content, $format)); } elseif (isset($content)) { $data->setStringContent($content); } $datas->add($data); } return $datas; }
/** * @param \Kbrw\RiakBundle\Model\Cluster\Cluster $cluster * @param \Kbrw\RiakBundle\Model\Bucket\Bucket $bucket * @param array<string> $keys * @return \Kbrw\RiakBundle\Model\KV\Datas */ public function fetch($cluster, $bucket, $keys) { if (!is_array($keys)) { $keys = array($keys); } $datas = new Datas(); // Split work in smaller pieces to avoid exception caused by too many opened connections if (count($keys) > $cluster->getMaxParallelCalls()) { $chunks = array_chunk($keys, $cluster->getMaxParallelCalls()); foreach ($chunks as $chunk) { $datas->addAll($this->fetch($cluster, $bucket, $chunk)); } return $datas; } foreach ($keys as $key) { $key = trim($key); $data = new Data($key); if (isset($this->content[$key])) { $data->setStringContent($this->content[$key]); if ($this->contentTypeNormalizer->isFormatSupportedForSerialization($bucket->getFormat())) { /*var_dump($data->getRawContent()); var_dump($bucket->getFullyQualifiedClassName()); var_dump($bucket->getFormat()); exit;*/ $riakKVObject = $this->serializer->deserialize($data->getContent(true), $bucket->getFullyQualifiedClassName(), $bucket->getFormat()); if ($riakKVObject !== false) { if ($riakKVObject instanceof Transmutable) { $riakKVObject = $riakKVObject->transmute(); } $data->setContent($riakKVObject); } } } $datas->add($data); } return $datas; }