Skip to content

Commit

Permalink
Merge pull request #9 from TCB13/master
Browse files Browse the repository at this point in the history
 Cross partition queries with ranges
  • Loading branch information
Nuno Chaves authored Jan 8, 2019
2 parents 8652208 + 602fae8 commit 43784e5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 13 deletions.
78 changes: 65 additions & 13 deletions src/CosmosDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,71 @@ public function getInfo()
* @param boolean $isCrossPartition used for cross partition query
* @return string JSON response
*/
public function query($rid_id, $rid_col, $query, $isCrossPartition = false)
{
$headers = $this->getAuthHeaders('POST', 'docs', $rid_col);
$headers['Content-Length'] = strlen($query);
$headers['Content-Type'] = 'application/query+json';
$headers['x-ms-max-item-count'] = -1;
$headers['x-ms-documentdb-isquery'] = 'True';
if ($isCrossPartition) {
$headers['x-ms-documentdb-query-enablecrosspartition'] = 'True';
}

return $this->request("/dbs/" . $rid_id . "/colls/" . $rid_col . "/docs", "POST", $headers, $query);
}
public function query($rid_id, $rid_col, $query, $isCrossPartition = false)
{
$headers = $this->getAuthHeaders('POST', 'docs', $rid_col);
$headers['Content-Length'] = strlen($query);
$headers['Content-Type'] = 'application/query+json';
$headers['x-ms-max-item-count'] = -1;
$headers['x-ms-documentdb-isquery'] = 'True';

if ($isCrossPartition)
$headers['x-ms-documentdb-query-enablecrosspartition'] = 'True';

try {
$result = $this->request("/dbs/" . $rid_id . "/colls/" . $rid_col . "/docs", "POST", $headers, $query);
}
catch (\GuzzleHttp\Exception\ClientException $e) {
$responseError = \json_decode($e->getResponse()->getBody()->getContents());

// -- Retry the request with PK Ranges --
// The provided cross partition query can not be directly served by the gateway.
// This is a first chance (internal) exception that all newer clients will know how to
// handle gracefully. This exception is traced, but unless you see it bubble up as an
// exception (which only happens on older SDK clients), then you can safely ignore this message.
if ($responseError->code === "BadRequest" && $isCrossPartition) {
$headers["x-ms-documentdb-partitionkeyrangeid"] = $this->getPkFullRange($rid_id, $rid_col);
$result = $this->request("/dbs/" . $rid_id . "/colls/" . $rid_col . "/docs", "POST", $headers, $query);
}

}

return $result;

}

/**
* getPkRanges
*
* @param $rid_id
* @param $rid_col
* @param bool $raw
*
* @return mixed|string
*/
public function getPkRanges($rid_id, $rid_col)
{
$headers = $this->getAuthHeaders('GET', 'pkranges', $rid_col);
$headers['Accept'] = 'application/json';
$headers['x-ms-max-item-count'] = -1;
$result = $this->request("/dbs/" . $rid_id . "/colls/" . $rid_col . "/pkranges", "GET", $headers);
return json_decode($result);
}

/**
* getPkFullRange
*
* @param $rid_id
* @param $rid_col
*
* @return string
*/
public function getPkFullRange($rid_id, $rid_col)
{
$result = $this->getPkRanges($rid_id, $rid_col);
$ids = \array_column($result->PartitionKeyRanges, "id");
return $result->_rid . "," . \implode(",", $ids);
}

/**
* listDatabases
Expand Down
20 changes: 20 additions & 0 deletions src/CosmosDbCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ public function query($query, $params = [], $isCrossPartition = false)
return $this->document_db->query($this->rid_db, $this->rid_col, $query, $isCrossPartition);
}

/**
* getPkRanges
*
* @return mixed
*/
public function getPkRanges()
{
return $this->document_db->getPkRanges($this->rid_db, $this->rid_col);
}

/**
* getPkFullRange
*
* @return mixed
*/
public function getPkFullRange()
{
return $this->document_db->getPkFullRange($this->rid_db, $this->rid_col);
}

/**
* createDocument
*
Expand Down
4 changes: 4 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ public function setPartitionKey($fieldName)
return $this;
}

public function getPartitionKey()
{
return $this->partitionKey;
}

/**
* @param $document
Expand Down

0 comments on commit 43784e5

Please sign in to comment.