Skip to content

Commit 3707820

Browse files
committed
Improved GraphHandler further. Removed _useCache parameter from options on getVertex and get EdgeCollections and instead implemented a global switch.
Also implemented a clearCache() method.
1 parent 33b179e commit 3707820

File tree

3 files changed

+188
-33
lines changed

3 files changed

+188
-33
lines changed

lib/triagens/ArangoDb/GraphHandler.php

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ class GraphHandler extends
7676
const OPTION_NAME = 'name';
7777

7878
/**
79-
* edge defintion parameter
79+
* edge definition parameter
8080
*/
8181
const OPTION_EDGE_DEFINITION = 'edgeDefinition';
8282

8383
/**
84-
* edge defintions parameter
84+
* edge definitions parameter
8585
*/
8686
const OPTION_EDGE_DEFINITIONS = 'edgeDefinitions';
8787

@@ -90,6 +90,11 @@ class GraphHandler extends
9090
*/
9191
protected $cache;
9292

93+
/**
94+
* @var $_useCache boolean GraphHandler use cache store
95+
*/
96+
protected $_useCache = false;
97+
9398
/**
9499
* Create a graph
95100
*
@@ -372,41 +377,48 @@ public function getVertexCollections($graph, array $options = [])
372377
}
373378

374379
$excludeOrphans = false;
375-
$_useCache = false;
380+
$_useCache = $this->_useCache;
376381

377382
if ((bool) $options){
378383
if (isset($options['excludeOrphans']) && !is_bool($options['excludeOrphans'])){
379384
$excludeOrphans = UrlHelper::getBoolString($options['excludeOrphans']);
380385
}
381-
382-
if (isset($options['_useCache'])){
383-
$_useCache = $options['_useCache'];
384-
}
385386
}
386387

387-
if ($_useCache === true){
388+
if ($_useCache === true){
388389
if ($excludeOrphans===true && !empty($this->cache[$graph]['excludeOrphans']['result'])){
389390
return $this->cache[$graph]['excludeOrphans']['vertexCollections'];
390391
}else if (!empty($this->cache[$graph]['vertexCollections'])) {
391392
return $this->cache[$graph]['vertexCollections'];
392393
}
393394
}
394-
395395
$url = UrlHelper::buildUrl(Urls::URL_GRAPH, [$graph, Urls::URLPART_VERTEX]);
396396

397397
if ($excludeOrphans===true){
398398
$url = UrlHelper::appendParamsUrl($url, ['excludeOrphans' => $excludeOrphans]);
399399
}
400400

401-
try {
401+
$connection = $this->getConnection();
402+
$batchCaptureMode = $connection->isInBatchCaptureMode();
403+
404+
if ($batchCaptureMode === true){
405+
$this->getConnection()->setBatchRequest(false);
406+
}
407+
408+
try {
402409
$response = $this->getConnection()->get($url);
403410
} catch (Exception $e) {
404411
throw new ClientException($e->getMessage());
405412
}
406413

414+
if ($batchCaptureMode === true){
415+
$this->getConnection()->setBatchRequest(true);
416+
}
417+
407418
$data = $response->getJson();
408-
sort($data[self::OPTION_COLLECTIONS]);
409-
$data = $data[self::OPTION_COLLECTIONS];
419+
$data = $data[self::OPTION_COLLECTIONS];
420+
421+
sort($data);
410422

411423
if ($_useCache === true){
412424
if ($excludeOrphans===true && !empty($this->cache[$graph]['excludeOrphans']['vertexCollections'])){
@@ -517,37 +529,47 @@ public function deleteEdgeDefinition($graph, $edgeDefinition, $dropCollection =
517529
* @return []
518530
* @since 2.2
519531
*/
520-
public function getEdgeCollections($graph, array $options = [])
532+
public function getEdgeCollections($graph)
521533
{
522534
if ($graph instanceof Graph) {
523535
$graph = $graph->getKey();
524536
}
525537

526-
$_useCache = false;
527-
528-
if ((bool) $options && isset($options['_useCache'])){
529-
$_useCache = $options['_useCache'];
530-
}
538+
$_useCache = $this->_useCache;
531539

532540
if ($_useCache === true && !empty($this->cache[$graph]['edgeCollections'])){
533541
return $this->cache[$graph]['edgeCollections'];
534542
}
535543

536544
$url = UrlHelper::buildUrl(Urls::URL_GRAPH, [$graph, Urls::URLPART_EDGE]);
537545

538-
try {
539-
$response = $this->getConnection()->get($url);
540-
} catch (Exception $e) {
541-
throw new ClientException($e->getMessage());
542-
}
543-
$data = $response->getJson();
544-
sort($data[self::OPTION_COLLECTIONS]);
546+
$connection = $this->getConnection();
547+
$batchCaptureMode = $connection->isInBatchCaptureMode();
548+
549+
if ($batchCaptureMode === true){
550+
$this->getConnection()->setBatchRequest(false);
551+
}
552+
553+
try {
554+
$response = $this->getConnection()->get($url);
555+
} catch (Exception $e) {
556+
throw new ClientException($e->getMessage());
557+
}
558+
559+
if ($batchCaptureMode === true){
560+
$this->getConnection()->setBatchRequest(true);
561+
}
562+
563+
$data = $response->getJson();
564+
$data = $data[self::OPTION_COLLECTIONS];
565+
566+
sort($data);
545567

546568
if ($_useCache === true && !empty($this->cache[$graph]['edgeCollections'])){
547569
$this->cache[$graph]['edgeCollections'] = $data;
548570
}
549571

550-
return $data[self::OPTION_COLLECTIONS];
572+
return $data;
551573
}
552574

553575

@@ -1020,7 +1042,7 @@ public function saveEdge($graph, $from, $to, $label = null, $document, $collecti
10201042
if ($collection === null) {
10211043
$edgeCollections = $this->getEdgeCollections($graph);
10221044
$edgeCollectionsCount = count($edgeCollections);
1023-
if ($edgeCollections !== 1) {
1045+
if ($edgeCollectionsCount !== 1) {
10241046
throw new ClientException('A collection must be provided.');
10251047
}
10261048
else if ($edgeCollectionsCount === 1) {
@@ -1091,7 +1113,7 @@ public function getEdge($graph, $edgeId, array $options = [], $collection = null
10911113
if ($collection === null) {
10921114
$edgeCollections = $this->getEdgeCollections($graph);
10931115
$edgeCollectionsCount = count($edgeCollections);
1094-
if ($edgeCollections !== 1) {
1116+
if ($edgeCollectionsCount !== 1) {
10951117
throw new ClientException('A collection must be provided.');
10961118
}
10971119
else if ($edgeCollectionsCount === 1) {
@@ -1182,7 +1204,7 @@ public function replaceEdge($graph, $edgeId, $label, Edge $document, array $opti
11821204
if ($collection === null) {
11831205
$edgeCollections = $this->getEdgeCollections($graph);
11841206
$edgeCollectionsCount = count($edgeCollections);
1185-
if ($edgeCollections !== 1) {
1207+
if ($edgeCollectionsCount !== 1) {
11861208
throw new ClientException('A collection must be provided.');
11871209
}
11881210
else if ($edgeCollectionsCount === 1) {
@@ -1287,7 +1309,7 @@ public function updateEdge($graph, $edgeId, $label, Edge $document, array $optio
12871309
if ($collection === null) {
12881310
$edgeCollections = $this->getEdgeCollections($graph);
12891311
$edgeCollectionsCount = count($edgeCollections);
1290-
if ($edgeCollections !== 1) {
1312+
if ($edgeCollectionsCount !== 1) {
12911313
throw new ClientException('A collection must be provided.');
12921314
}
12931315
else if ($edgeCollectionsCount === 1) {
@@ -1373,7 +1395,7 @@ public function removeEdge($graph, $edgeId, $revision = null, array $options = [
13731395
if ($collection === null) {
13741396
$edgeCollections = $this->getEdgeCollections($graph);
13751397
$edgeCollectionsCount = count($edgeCollections);
1376-
if ($edgeCollections !== 1) {
1398+
if ($edgeCollectionsCount !== 1) {
13771399
throw new ClientException('A collection must be provided.');
13781400
}
13791401
else if ($edgeCollectionsCount === 1) {
@@ -1406,4 +1428,34 @@ public function removeEdge($graph, $edgeId, $revision = null, array $options = [
14061428

14071429
return true;
14081430
}
1431+
1432+
/**
1433+
* Clears this handler's cache
1434+
*
1435+
* @return $this
1436+
*/
1437+
public function clearCache()
1438+
{
1439+
$this->cache = null;
1440+
return $this;
1441+
}
1442+
1443+
/**
1444+
* @return boolean
1445+
*/
1446+
public function isCacheActive()
1447+
{
1448+
return $this->_useCache;
1449+
}
1450+
1451+
/**
1452+
* @param boolean $useCache
1453+
*
1454+
* @return $this
1455+
*/
1456+
public function useCache($useCache)
1457+
{
1458+
$this->_useCache = $useCache;
1459+
return $this;
1460+
}
14091461
}

tests/GraphBasicTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,9 @@ public function testAddGetDeleteCollectionsWithCache()
279279
$this->graph = $this->graphHandler->addOrphanCollection($this->graph, 'orphan1');
280280
$this->graph = $this->graphHandler->addOrphanCollection($this->graph, 'orphan2');
281281

282+
$this->graphHandler->useCache(true);
282283
static::assertSame(
283-
$this->graphHandler->getVertexCollections($this->graph, ['_useCache' => true]), [
284+
$this->graphHandler->getVertexCollections($this->graph), [
284285
0 => 'ArangoDBPHPTestSuiteTestCollection04',
285286
1 => 'orphan1',
286287
2 => 'orphan2',
@@ -291,7 +292,7 @@ public function testAddGetDeleteCollectionsWithCache()
291292

292293
$this->graph = $this->graphHandler->deleteOrphanCollection($this->graph, 'orphan2');
293294
static::assertSame(
294-
$this->graphHandler->getVertexCollections($this->graph, ['_useCache' => true]), [
295+
$this->graphHandler->getVertexCollections($this->graph), [
295296
0 => 'ArangoDBPHPTestSuiteTestCollection04',
296297
1 => 'orphan1',
297298
2 => 'orphan2',
@@ -300,6 +301,7 @@ public function testAddGetDeleteCollectionsWithCache()
300301
]
301302
);
302303

304+
$this->graphHandler->useCache(false);
303305
static::assertSame(
304306
$this->graphHandler->getVertexCollections($this->graph), [
305307
0 => 'ArangoDBPHPTestSuiteTestCollection04',

tests/GraphExtendedTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,107 @@ public function testSaveVerticesAndEdgeBetweenThemAndRemoveOneByOne()
269269
}
270270

271271

272+
/**
273+
* Test if 2 Vertices can be saved and an edge can be saved connecting them
274+
* Then remove in this order Edge, Vertex1, Vertex2
275+
*/
276+
public function testSaveVerticesAndEdgeBetweenThemAndRemoveOneByOneWithCache()
277+
{
278+
// Setup Objects
279+
$vertex1 = Vertex::createFromArray($this->vertex1Array);
280+
$vertex2 = Vertex::createFromArray($this->vertex2Array);
281+
$edge1 = Edge::createFromArray($this->edge1Array);
282+
283+
$this->graphHandler->useCache(true);
284+
// Save vertices
285+
$result1 = $this->graphHandler->saveVertex($this->graphName, $vertex1);
286+
static::assertEquals($result1, 'ArangoDBPHPTestSuiteVertexTestCollection01/vertex1', 'Did not return vertex1!');
287+
288+
$result2 = $this->graphHandler->saveVertex($this->graphName, $vertex2);
289+
static::assertEquals($result2, 'ArangoDBPHPTestSuiteVertexTestCollection01/vertex2', 'Did not return vertex2!');
290+
291+
292+
// Get vertices
293+
$result1 = $this->graphHandler->getVertex($this->graphName, $this->vertex1Name);
294+
static::assertEquals($result1->getKey(), 'vertex1', 'Did not return vertex1!');
295+
296+
$result2 = $this->graphHandler->getVertex($this->graphName, $this->vertex2Name);
297+
static::assertEquals($result2->getKey(), 'vertex2', 'Did not return vertex2!');
298+
299+
300+
// Save edge
301+
$resultE = $this->graphHandler->saveEdge(
302+
$this->graphName,
303+
$result1->getInternalId(),
304+
$result2->getInternalId(),
305+
$this->edgeLabel1,
306+
$edge1
307+
);
308+
static::assertEquals($resultE, 'ArangoDBPHPTestSuiteTestEdgeCollection01/edge1', 'Did not return edge1!');
309+
310+
311+
// Get edge
312+
$resultE = $this->graphHandler->getEdge($this->graphName, $this->edge1Name);
313+
static::assertEquals($resultE->getKey(), 'edge1', 'Did not return edge1!');
314+
315+
316+
// Try to get the edge using GraphHandler
317+
$resultE = $this->graphHandler->getEdge($this->graphName, $this->edge1Name);
318+
static::assertInstanceOf('triagens\ArangoDb\Edge', $resultE);
319+
320+
321+
// Remove the edge
322+
$resultE = $this->graphHandler->removeEdge($this->graphName, $this->edge1Name);
323+
static::assertTrue($resultE, 'Did not return true!');
324+
325+
326+
// Remove one vertex using GraphHandler
327+
$result1 = $this->graphHandler->removeVertex($this->graphName, $this->vertex1Name);
328+
static::assertTrue($result1, 'Did not return true!');
329+
330+
331+
// Remove one vertex using GraphHandler | Testing
332+
// This should cause an exception with a code of 404
333+
try {
334+
$e = null;
335+
$this->graphHandler->removeVertex($this->graphName, $this->vertex1Name);
336+
} catch (\Exception $e) {
337+
// don't bother us... just give us the $e
338+
}
339+
static::assertInstanceOf('triagens\ArangoDb\ServerException', $e);
340+
static::assertEquals($e->getCode(), 404, 'Should be 404, instead got: ' . $e->getCode());
341+
342+
343+
// Try to get vertex using GraphHandler
344+
// This should cause an exception with a code of 404
345+
try {
346+
$e = null;
347+
$this->graphHandler->getVertex($this->graphName, $this->vertex1Name);
348+
} catch (\Exception $e) {
349+
// don't bother us... just give us the $e
350+
}
351+
static::assertInstanceOf('triagens\ArangoDb\ServerException', $e);
352+
static::assertEquals($e->getCode(), 404, 'Should be 404, instead got: ' . $e->getCode());
353+
354+
355+
// Remove the other vertex using GraphHandler
356+
$result2 = $this->graphHandler->removeVertex($this->graphName, $this->vertex2Name);
357+
static::assertTrue($result2, 'Did not return true!');
358+
359+
360+
// Try to get vertex using GraphHandler
361+
// This should cause an exception with a code of 404
362+
try {
363+
$e = null;
364+
$this->graphHandler->getVertex($this->graphName, $this->vertex2Name);
365+
} catch (\Exception $e) {
366+
// don't bother us... just give us the $e
367+
}
368+
static::assertInstanceOf('triagens\ArangoDb\ServerException', $e);
369+
static::assertEquals($e->getCode(), 404, 'Should be 404, instead got: ' . $e->getCode());
370+
}
371+
372+
272373
/**
273374
* Test if 2 Vertices can be saved and an edge can be saved connecting them, but remove the first vertex first
274375
* This should throw an exception on removing the edge, because it will be removed with

0 commit comments

Comments
 (0)