Skip to content

Commit a64211c

Browse files
committed
added support for shard count
1 parent c0d5a44 commit a64211c

File tree

4 files changed

+81
-9
lines changed

4 files changed

+81
-9
lines changed

lib/ArangoDBClient/CollectionHandler.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,22 +394,21 @@ public function has($collection)
394394
* @throws Exception
395395
*
396396
* @param mixed $collection - collection id as a string or number
397+
* @param bool $details - optional, will provide per-shard counts in a cluster
397398
*
398-
* @return int - the number of documents in the collection
399+
* @return mixed - int if details=false, the number of documents in the collection, array if details=true
399400
*/
400-
public function count($collection)
401+
public function count($collection, $details = false)
401402
{
402403
$headers = [];
403404
$this->addTransactionHeader($headers, $collection);
404405

405406
$collection = $this->makeCollection($collection);
406407
$url = UrlHelper::buildUrl(Urls::URL_COLLECTION, [$collection, self::OPTION_COUNT]);
408+
$url = UrlHelper::appendParamsUrl($url, ['details' => $details]);
407409
$response = $this->getConnection()->get($url, $headers);
408-
409-
$data = $response->getJson();
410-
$count = $data[self::OPTION_COUNT];
411-
412-
return (int) $count;
410+
$data = $response->getJson();
411+
return $data[self::OPTION_COUNT];
413412
}
414413

415414

lib/ArangoDBClient/Connection.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,17 @@ private function updateHttpHeader()
424424
$this->_httpHeader .= sprintf('Host: %s%s', Endpoint::getHost($endpoint), HttpHelper::EOL);
425425
}
426426

427-
if (isset($this->_options[ConnectionOptions::OPTION_AUTH_TYPE], $this->_options[ConnectionOptions::OPTION_AUTH_USER])) {
427+
if (isset($this->_options[ConnectionOptions::OPTION_AUTH_JWT])) {
428+
// JWT, used as is
429+
$this->_httpHeader .= sprintf(
430+
'Authorization: Bearer %s%s',
431+
$this->_options[ConnectionOptions::OPTION_AUTH_JWT],
432+
HttpHelper::EOL
433+
);
434+
} else if (isset($this->_options[ConnectionOptions::OPTION_AUTH_TYPE], $this->_options[ConnectionOptions::OPTION_AUTH_USER])) {
435+
// create a JWT for a given user and server's JWT secret
428436
if ($this->_options[ConnectionOptions::OPTION_AUTH_TYPE] == 'Bearer') {
429-
// JWT
437+
// JWT secret
430438
$base = json_encode(['typ' => 'JWT', 'alg' => 'HS256']);
431439
$base64UrlHeader = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($base));
432440
$payload = json_encode([

lib/ArangoDBClient/ConnectionOptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ class ConnectionOptions implements \ArrayAccess
169169
* Wait for sync index constant
170170
*/
171171
const OPTION_IS_SYSTEM = 'isSystem';
172+
173+
/**
174+
* Authentication JWT
175+
*/
176+
const OPTION_AUTH_JWT = 'Jwt';
172177

173178
/**
174179
* Authentication user name

tests/CollectionBasicTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,66 @@ public function testHasCollectionReturnsTrueIfCollectionExists()
12871287
}
12881288

12891289

1290+
/**
1291+
* count
1292+
*/
1293+
public function testCollectionCountDetailed()
1294+
{
1295+
if (!isCluster($this->connection)) {
1296+
// don't execute this test in a non-cluster
1297+
$this->markTestSkipped("test is only meaningful in cluster");
1298+
return;
1299+
}
1300+
1301+
$connection = $this->connection;
1302+
$collectionHandler = new CollectionHandler($connection);
1303+
1304+
$name = 'ArangoDB_PHP_TestSuite_TestCollection_01' . '_' . static::$testsTimestamp;
1305+
1306+
try {
1307+
$collectionHandler->drop($name);
1308+
} catch (Exception $e) {
1309+
//Silence the exception
1310+
}
1311+
1312+
$collection = Collection::createFromArray(['name' => $name, 'numberOfShards' => 5]);
1313+
$collectionHandler->create($collection);
1314+
1315+
$count = $collectionHandler->count($collection, false);
1316+
static::assertEquals(0, $count);
1317+
1318+
$count = $collectionHandler->count($collection, true);
1319+
static::assertTrue(is_array($count));
1320+
static::assertEquals(5, count($count));
1321+
1322+
foreach ($count as $shard => $value) {
1323+
static::assertEquals('s', $shard[0]);
1324+
static::assertEquals(0, $value);
1325+
}
1326+
1327+
// fill with data
1328+
$statement = new Statement($connection, []);
1329+
$statement->setQuery('FOR i IN 1..1000 INSERT {} IN ' . $collection->getName());
1330+
$cursor = $statement->execute();
1331+
1332+
$count = $collectionHandler->count($collection, false);
1333+
static::assertEquals(1000, $count);
1334+
1335+
$count = $collectionHandler->count($collection, true);
1336+
static::assertTrue(is_array($count));
1337+
static::assertEquals(5, count($count));
1338+
1339+
$sum = 0;
1340+
foreach ($count as $shard => $value) {
1341+
static::assertEquals('s', $shard[0]);
1342+
static::assertTrue($value > 0);
1343+
$sum += $value;
1344+
}
1345+
1346+
static::assertEquals(1000, $sum);
1347+
}
1348+
1349+
12901350
/**
12911351
* get shards
12921352
*/

0 commit comments

Comments
 (0)