Skip to content

Commit

Permalink
complete 2i queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Mancini committed Dec 21, 2015
1 parent d7d6243 commit ee57291
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions src/Riak/Api/Pb.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,15 @@ protected function prepareMessage()
$message->setQtype($command->isRangeQuery());
$message->setRangeMax($command->getUpperBound());
$message->setRangeMin($command->getLowerBound());
$message->setKey($command->getMatchValue());

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') {
$message->setReturnTerms($command->getParameter('return_terms'));
} 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') {
Expand Down Expand Up @@ -301,20 +303,25 @@ protected function parseResponse($message_code, $message = '')
$pbResponse->parseFromString($message);
$this->error = $pbResponse->getErrmsg();

// intercept error for malformed query
// intercept certain "errors" to provide consistent cross interface behavior
if (strpos($message, "Query unsuccessful") !== false) {
// HTTP status code for bad request, consider it a request success
$code = 400;
$this->response = new Command\Search\Response($this->success, $code, '', null);
break;
}

// intercept notfound "error"
if (strpos($message, "notfound") !== false) {
} elseif (strpos($message, "timeout") !== false) {
// set HTTP status code for service unaivalable, consider it a request success
$code = 503;
$this->response = new Command\Indexes\Response($this->success, $code, $this->error);
break;
} elseif (strpos($message, "notfound") !== false) {
// set HTTP status code for not found, consider it a request success
$code = 404;
} else {
$code = $pbResponse->getErrcode();
$this->success = false;
}

$this->response = new Command\Response($this->success, $code, $this->error);
break;
case Api\Pb\Message::RpbPutResp:
Expand Down Expand Up @@ -535,6 +542,35 @@ protected function parseResponse($message_code, $message = '')

$this->response = new Command\Search\Schema\Response($this->success, $code, '', $pbResponse->getSchema()->getContent(), Http::CONTENT_TYPE_XML);
break;
case Api\Pb\Message::RpbIndexResp:
$code = 200;
$pbResponse = new Api\Pb\Message\RpbIndexResp();
$pbResponse->parseFromString($message);

// sane defaults
$results = [];
$termsReturned = false;
$continuation = null;
$done = true;

if ($pbResponse->getKeys()) {
$results = $pbResponse->getKeys();
}

if ($pbResponse->getResultsCount()) {
foreach ($pbResponse->getResults() as $result) {
$results[] = [$result->getKey() => $result->getValue()];
}
$termsReturned = true;
}

if ($pbResponse->getContinuation()) {
$continuation = $pbResponse->getContinuation();
$done = false;
}

$this->response = new Command\Indexes\Response($this->success, $code, '', $results, $termsReturned, $continuation, $done);
break;
default:
throw new Api\Exception('Mishandled PB response.');
}
Expand Down

0 comments on commit ee57291

Please sign in to comment.