public function testToPbRow() { $now = new \DateTime(); $row = [(new Cell('string'))->setValue('test'), (new Cell('int'))->setIntValue(1), (new Cell('double'))->setDoubleValue(1.1), (new Cell('bool'))->setBooleanValue(true), (new Cell('timestamp'))->setTimestampValue($now->getTimestamp()), (new Cell('timestampdt'))->setDateTimeValue($now)]; $tsRow = TimeSeries::toPbRow($row); $this->assertEquals('test', $tsRow->getCellsAt(0)->getVarcharValue()); $this->assertEquals(1, $tsRow->getCellsAt(1)->getSint64Value()); $this->assertEquals(1.1, $tsRow->getCellsAt(2)->getDoubleValue()); $this->assertEquals(true, $tsRow->getCellsAt(3)->getBooleanValue()); $this->assertEquals($now->getTimestamp(), $tsRow->getCellsAt(4)->getTimestampValue()); $this->assertEquals($now->getTimestamp(), $tsRow->getCellsAt(5)->getTimestampValue()); }
/** * Sets up the PB message to be sent over the wire * * @return $this * @throws Api\Exception */ protected function prepareMessage() { $message = null; switch (get_class($this->command)) { case 'Basho\\Riak\\Command\\Bucket\\List': $this->messageCode = Api\Pb\Message::RpbListBucketsReq; $message = new Api\Pb\Message\RpbListBucketsReq(); break; case 'Basho\\Riak\\Command\\Bucket\\Fetch': $this->messageCode = Api\Pb\Message::RpbGetBucketReq; $message = new Api\Pb\Message\RpbGetBucketReq(); break; case 'Basho\\Riak\\Command\\Bucket\\Store': $this->messageCode = Api\Pb\Message::RpbSetBucketReq; $message = new Api\Pb\Message\RpbSetBucketReq(); $props = new Api\Pb\Message\RpbBucketProps(); foreach ($this->command->getData()['props'] as $prop => $value) { $method = 'set' . str_replace("_", "", ucwords($prop, "_")); $props->{$method}($value); } $message->setProps($props); break; case 'Basho\\Riak\\Command\\Bucket\\Delete': $this->messageCode = Api\Pb\Message::RpbPutReq; $message = new Api\Pb\Message\RpbPutReq(); break; case 'Basho\\Riak\\Command\\Bucket\\Keys': $this->messageCode = Api\Pb\Message::RpbListKeysReq; $message = new Api\Pb\Message\RpbListKeysReq(); break; case 'Basho\\Riak\\Command\\Object\\Fetch': $this->messageCode = Api\Pb\Message::RpbGetReq; $message = new Api\Pb\Message\RpbGetReq(); break; case 'Basho\\Riak\\Command\\Object\\Store': $this->messageCode = Api\Pb\Message::RpbPutReq; $message = new Api\Pb\Message\RpbPutReq(); $message->setVclock($this->command->getObject()->getVclock()); $message->setContent($this->prepareContent()); break; case 'Basho\\Riak\\Command\\Object\\Delete': $this->messageCode = Api\Pb\Message::RpbDelReq; $message = new Api\Pb\Message\RpbDelReq(); break; case 'Basho\\Riak\\Command\\Object\\FetchPreflist': $this->messageCode = Api\Pb\Message::RpbGetBucketKeyPreflistReq; $message = new Api\Pb\Message\RpbGetBucketKeyPreflistReq(); break; case 'Basho\\Riak\\Command\\DataType\\Counter\\Fetch': case 'Basho\\Riak\\Command\\DataType\\Set\\Fetch': case 'Basho\\Riak\\Command\\DataType\\Map\\Fetch': case 'Basho\\Riak\\Command\\DataType\\Hll\\Fetch': $this->messageCode = Api\Pb\Message::DtFetchReq; $message = new Api\Pb\Message\DtFetchReq(); break; case 'Basho\\Riak\\Command\\DataType\\Counter\\Store': $message = $this->buildCounterUpdateMessage($this->command->getData()['increment']); break; case 'Basho\\Riak\\Command\\DataType\\Set\\Store': $data = $this->command->getData(); $message = $this->buildSetUpdateMessage($data['add_all'], $data['remove_all']); break; case 'Basho\\Riak\\Command\\DataType\\Map\\Store': $data = $this->command->getData(); $message = $this->buildMapUpdateMessage($data['update'], !empty($data['remove']) ? $data['remove'] : []); break; case 'Basho\\Riak\\Command\\DataType\\Hll\\Store': $data = $this->command->getData(); $message = $this->buildHllUpdateMessage($data['add_all']); break; case 'Basho\\Riak\\Command\\Search\\Index\\Fetch': $this->messageCode = Api\Pb\Message::RpbYokozunaIndexGetReq; $message = new Api\Pb\Message\RpbYokozunaIndexGetReq(); $message->setName("{$this->command}"); break; case 'Basho\\Riak\\Command\\Search\\Index\\Store': $this->messageCode = Api\Pb\Message::RpbYokozunaIndexPutReq; $message = new Api\Pb\Message\RpbYokozunaIndexPutReq(); $index = new Api\Pb\Message\RpbYokozunaIndex(); $index->setName("{$this->command}"); $index->setSchema($this->command->getData()['schema']); $message->setIndex($index); break; case 'Basho\\Riak\\Command\\Search\\Index\\Delete': $this->messageCode = Api\Pb\Message::RpbYokozunaIndexDeleteReq; $message = new Api\Pb\Message\RpbYokozunaIndexDeleteReq(); $message->setName("{$this->command}"); break; case 'Basho\\Riak\\Command\\Search\\Schema\\Fetch': $this->messageCode = Api\Pb\Message::RpbYokozunaSchemaGetReq; $message = new Api\Pb\Message\RpbYokozunaSchemaGetReq(); $message->setName("{$this->command}"); break; case 'Basho\\Riak\\Command\\Search\\Schema\\Store': $this->messageCode = Api\Pb\Message::RpbYokozunaSchemaPutReq; $message = new Api\Pb\Message\RpbYokozunaSchemaPutReq(); $schema = new Api\Pb\Message\RpbYokozunaSchema(); $schema->setName("{$this->command}"); $schema->setContent($this->command->getData()); $message->setSchema($schema); break; case 'Basho\\Riak\\Command\\Search\\Fetch': $this->messageCode = Api\Pb\Message::RpbSearchQueryReq; $message = new Api\Pb\Message\RpbSearchQueryReq(); $message->setIndex("{$this->command}"); // Iterate over parameters, explicitly accepting known / allowed keys only for security reasons foreach ($this->command->getParameters() as $key => $value) { if ($key == 'fl') { foreach ($value as $field) { $message->appendFl($field); } } elseif ($key == 'q') { $message->setQ($value); } elseif ($key == 'rows') { $message->setRows($value); } elseif ($key == 'start') { $message->setStart($value); } elseif ($key == 'sort') { $message->setSort($value); } elseif ($key == 'fq') { $message->setFilter($value); } elseif ($key == 'df') { $message->setFilter($value); } } break; case 'Basho\\Riak\\Command\\MapReduce\\Fetch': $this->messageCode = Api\Pb\Message::RpbMapRedReq; $message = new Api\Pb\Message\RpbMapRedReq(); $message->setContentType(Http::CONTENT_TYPE_JSON); $message->setRequest($this->command->getEncodedData()); break; case 'Basho\\Riak\\Command\\Indexes\\Query': $this->messageCode = Api\Pb\Message::RpbIndexReq; $message = new Api\Pb\Message\RpbIndexReq(); /** @var Command\Indexes\Query $command */ $command = $this->command; $message->setIndex($command->getIndexName()); $message->setQtype($command->isRangeQuery()); $message->setRangeMax($command->getUpperBound()); $message->setRangeMin($command->getLowerBound()); $message->setKey($command->getMatchValue()); # TODO: when streaming is implemented # $message->setStream($command->getStreamValue()); foreach ($command->getParameters() as $key => $value) { if ($key == 'max_results') { $message->setMaxResults($command->getParameter('max_results')); } elseif ($key == 'continuation') { $message->setContinuation($command->getParameter('continuation')); } elseif ($key == 'return_terms' && $command->getParameter('return_terms') == 'true') { $message->setReturnTerms(true); } elseif ($key == 'pagination_sort') { $message->setPaginationSort($command->getParameter('pagination_sort')); } elseif ($key == 'term_regex') { $message->setTermRegex($command->getParameter('term_regex')); } elseif ($key == 'timeout') { $message->setTimeout($command->getParameter('timeout')); } } break; case 'Basho\\Riak\\Command\\Ping': $this->messageCode = Api\Pb\Message::RpbPingReq; $message = ''; break; /** @noinspection PhpMissingBreakStatementInspection */ /** @noinspection PhpMissingBreakStatementInspection */ case 'Basho\\Riak\\Command\\TimeSeries\\Delete': $this->messageCode = Api\Pb\Message::TsDelReq; $message = new Api\Pb\Message\TsDelReq(); case 'Basho\\Riak\\Command\\TimeSeries\\Fetch': if (!$message) { $this->messageCode = Api\Pb\Message::TsGetReq; $message = new Api\Pb\Message\TsGetReq(); } /** @var $command Command\TimeSeries\Fetch */ $command = $this->command; $message->setTable($command->getTable()); foreach ($command->getData() as $cell) { $message->appendKey(Api\Pb\Translator\TimeSeries::toPbCell($cell)); } break; case 'Basho\\Riak\\Command\\TimeSeries\\Store': $this->messageCode = Api\Pb\Message::TsPutReq; $message = new Api\Pb\Message\TsPutReq(); /** @var $command Command\TimeSeries\Store */ $command = $this->command; $message->setTable($command->getTable()); foreach ($command->getData() as $row) { $message->appendRows(Api\Pb\Translator\TimeSeries::toPbRow($row)); } break; case 'Basho\\Riak\\Command\\TimeSeries\\Query\\Fetch': $this->messageCode = Api\Pb\Message::TsQueryReq; $message = new Api\Pb\Message\TsQueryReq(); /** @var $command Command\TimeSeries\Query\Fetch */ $command = $this->command; $message->setQuery(Api\Pb\Translator\TimeSeries::toPbQuery($command->getData()['query'], $command->getData()['interpolations'])); break; default: throw new Api\Exception('Command is invalid.'); } if ($message) { $this->setLocationOnMessage($message, $this->command->getLocation()); $this->setBucketOnMessage($message, $this->command->getBucket()); $this->setOptionsOnMessage($message, $this->command); } $this->requestMessage = $message; return $this; }