Skip to content

Commit a7e8395

Browse files
committed
Improved GraphHandler cache to also cover getEdgeCollections().
Also reversed the default behavior of the cache to being disabled, as the initial implementation of being enabled, would break BC.
1 parent 7d61268 commit a7e8395

File tree

2 files changed

+168
-58
lines changed

2 files changed

+168
-58
lines changed

lib/triagens/ArangoDb/GraphHandler.php

Lines changed: 91 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ public function deleteOrphanCollection($graph, $orphanCollection, $dropCollectio
357357
* <p>Options are :<br>
358358
* <li>'excludeOrphans' - boolean value: true to exclude the orphans or false to include orphans in the result.<br>
359359
* Defaults to false</li>
360-
* <li>'_noCache' - boolean: true to not use the handler's cache for looking up prior fetched results.<br>
361-
* This will also not store the result of this call to the cache.<br>
362-
* or false to use the cache.</li>
360+
* <li>'_useCache' - boolean: true to use the handler's cache for looking up prior fetched results.<br>
361+
* or false to not use the cache. This will also not store the result of this call to the cache.<br>
362+
* Defaults to false.</li>
363363
* </p>
364364
*
365365
* @return array
@@ -372,23 +372,23 @@ public function getVertexCollections($graph, array $options = [])
372372
}
373373

374374
$excludeOrphans = false;
375-
$_noCache = false;
375+
$_useCache = false;
376376

377377
if ((bool) $options){
378378
if (isset($options['excludeOrphans']) && !is_bool($options['excludeOrphans'])){
379379
$excludeOrphans = UrlHelper::getBoolString($options['excludeOrphans']);
380380
}
381381

382-
if (isset($options['_noCache'])){
383-
$_noCache = $options['_noCache'];
382+
if (isset($options['_useCache'])){
383+
$_useCache = $options['_useCache'];
384384
}
385385
}
386386

387-
if ($_noCache === false){
387+
if ($_useCache === true){
388388
if ($excludeOrphans===true && !empty($this->cache[$graph]['excludeOrphans']['result'])){
389-
return $this->cache[$graph]['excludeOrphans']['result'];
390-
}else if (!empty($this->cache[$graph]['result'])) {
391-
return $this->cache[$graph]['result'];
389+
return $this->cache[$graph]['excludeOrphans']['vertexCollections'];
390+
}else if (!empty($this->cache[$graph]['vertexCollections'])) {
391+
return $this->cache[$graph]['vertexCollections'];
392392
}
393393
}
394394

@@ -408,11 +408,11 @@ public function getVertexCollections($graph, array $options = [])
408408
sort($data[self::OPTION_COLLECTIONS]);
409409
$data = $data[self::OPTION_COLLECTIONS];
410410

411-
if (!empty($this->cache[$graph]) && (!isset($options['_noCache']) || isset($options['_noCache']) && $options['_noCache'] === false)){
412-
if ($excludeOrphans===true){
413-
$this->cache[$graph]['excludeOrphans']['result'] = $data;
411+
if ($_useCache === true){
412+
if ($excludeOrphans===true && !empty($this->cache[$graph]['excludeOrphans']['vertexCollections'])){
413+
$this->cache[$graph]['excludeOrphans']['vertexCollections'] = $data;
414414
}else{
415-
$this->cache[$graph]['result'] = $data;
415+
$this->cache[$graph]['vertexCollections'] = $data;
416416
}
417417
}
418418
return $data;
@@ -507,17 +507,33 @@ public function deleteEdgeDefinition($graph, $edgeDefinition, $dropCollection =
507507
* @throws Exception
508508
*
509509
* @param mixed $graph - graph name as a string or instance of Graph
510+
* @param array $options - optional, an array of options
511+
* <p>Options are :<br>
512+
* <li>'_useCache' - boolean: true to use the handler's cache for looking up prior fetched results.<br>
513+
* or false to not use the cache. This will also not store the result of this call to the cache.<br>
514+
* Defaults to false.</li>
515+
* </p>
510516
*
511517
* @return []
512518
* @since 2.2
513519
*/
514-
public function getEdgeCollections($graph)
520+
public function getEdgeCollections($graph, array $options = [])
515521
{
516522
if ($graph instanceof Graph) {
517523
$graph = $graph->getKey();
518524
}
519525

520-
$url = UrlHelper::buildUrl(Urls::URL_GRAPH, [$graph, Urls::URLPART_EDGE]);
526+
$_useCache = false;
527+
528+
if ((bool) $options && isset($options['_useCache'])){
529+
$_useCache = $options['_useCache'];
530+
}
531+
532+
if ($_useCache === true && !empty($this->cache[$graph]['edgeCollections'])){
533+
return $this->cache[$graph]['edgeCollections'];
534+
}
535+
536+
$url = UrlHelper::buildUrl(Urls::URL_GRAPH, [$graph, Urls::URLPART_EDGE]);
521537

522538
try {
523539
$response = $this->getConnection()->get($url);
@@ -526,7 +542,12 @@ public function getEdgeCollections($graph)
526542
}
527543
$data = $response->getJson();
528544
sort($data[self::OPTION_COLLECTIONS]);
529-
return $data[self::OPTION_COLLECTIONS];
545+
546+
if ($_useCache === true && !empty($this->cache[$graph]['edgeCollections'])){
547+
$this->cache[$graph]['edgeCollections'] = $data;
548+
}
549+
550+
return $data[self::OPTION_COLLECTIONS];
530551
}
531552

532553

@@ -605,6 +626,7 @@ public function saveVertex($graph, $document, $collection = null)
605626
$collection = $vertexCollections[0];
606627
}
607628
}
629+
608630
$data = $document->getAll();
609631
$url = UrlHelper::buildUrl(Urls::URL_GRAPH, [$graph, Urls::URLPART_VERTEX, $collection]);
610632

@@ -663,16 +685,19 @@ public function getVertex($graph, $vertexId, array $options = [], $collection =
663685
$vertexId = $parts[1];
664686
$collection = $parts[0];
665687
}
666-
if ($collection === null) {
667-
if (count($this->getVertexCollections($graph)) !== 1) {
668-
throw new ClientException('A collection must be provided.');
669-
}
670-
else if (count($this->getVertexCollections($graph)) === 1) {
671-
$collection = $this->getVertexCollections($graph);
672-
$collection = $collection[0];
673-
}
674-
}
675-
$url = UrlHelper::buildUrl(Urls::URL_GRAPH, [$graph, Urls::URLPART_VERTEX, $collection, $vertexId]);
688+
689+
$vertexCollections = $this->getVertexCollections($graph);
690+
$vertexCollectionsCount = count($vertexCollections);
691+
if ($collection === null) {
692+
if ($vertexCollectionsCount !== 1) {
693+
throw new ClientException('A collection must be provided.');
694+
}
695+
else if ($vertexCollectionsCount === 1) {
696+
$collection = $vertexCollections[0];
697+
}
698+
}
699+
700+
$url = UrlHelper::buildUrl(Urls::URL_GRAPH, [$graph, Urls::URLPART_VERTEX, $collection, $vertexId]);
676701
$response = $this->getConnection()->get($url);
677702

678703
$jsonArray = $response->getJson();
@@ -750,16 +775,19 @@ public function replaceVertex($graph, $vertexId, Document $document, array $opti
750775
$vertexId = $parts[1];
751776
$collection = $parts[0];
752777
}
753-
if ($collection === null) {
754-
if (count($this->getVertexCollections($graph)) !== 1) {
755-
throw new ClientException('A collection must be provided.');
756-
}
757-
else if (count($this->getVertexCollections($graph)) === 1) {
758-
$collection = $this->getVertexCollections($graph);
759-
$collection = $collection[0];
760-
}
761-
}
762-
$options = array_merge([self::OPTION_REVISION => false], $options);
778+
779+
$vertexCollections = $this->getVertexCollections($graph);
780+
$vertexCollectionsCount = count($vertexCollections);
781+
if ($collection === null) {
782+
if ($vertexCollectionsCount !== 1) {
783+
throw new ClientException('A collection must be provided.');
784+
}
785+
else if ($vertexCollectionsCount === 1) {
786+
$collection = $vertexCollections[0];
787+
}
788+
}
789+
790+
$options = array_merge([self::OPTION_REVISION => false], $options);
763791

764792
// This preserves compatibility for the old policy parameter.
765793
$params = [];
@@ -843,16 +871,19 @@ public function updateVertex($graph, $vertexId, Document $document, array $optio
843871
$vertexId = $parts[1];
844872
$collection = $parts[0];
845873
}
846-
if ($collection === null) {
847-
if (count($this->getVertexCollections($graph)) !== 1) {
848-
throw new ClientException('A collection must be provided.');
849-
}
850-
else if (count($this->getVertexCollections($graph)) === 1) {
851-
$collection = $this->getVertexCollections($graph);
852-
$collection = $collection[0];
853-
}
854-
}
855-
$options = array_merge([self::OPTION_REVISION => false], $options);
874+
875+
$vertexCollections = $this->getVertexCollections($graph);
876+
$vertexCollectionsCount = count($vertexCollections);
877+
if ($collection === null) {
878+
if ($vertexCollectionsCount !== 1) {
879+
throw new ClientException('A collection must be provided.');
880+
}
881+
else if ($vertexCollectionsCount === 1) {
882+
$collection = $vertexCollections[0];
883+
}
884+
}
885+
886+
$options = array_merge([self::OPTION_REVISION => false], $options);
856887
// This preserves compatibility for the old policy parameter.
857888
$params = [];
858889
$params = $this->validateAndIncludeOldSingleParameterInParams(
@@ -921,16 +952,19 @@ public function removeVertex($graph, $vertexId, $revision = null, array $options
921952
$vertexId = $parts[1];
922953
$collection = $parts[0];
923954
}
924-
if ($collection === null) {
925-
if (count($this->getVertexCollections($graph)) !== 1) {
926-
throw new ClientException('A collection must be provided.');
927-
}
928-
else if (count($this->getVertexCollections($graph)) === 1) {
929-
$collection = $this->getVertexCollections($graph);
930-
$collection = $collection[0];
931-
}
932-
}
933-
// This preserves compatibility for the old policy parameter.
955+
956+
$vertexCollections = $this->getVertexCollections($graph);
957+
$vertexCollectionsCount = count($vertexCollections);
958+
if ($collection === null) {
959+
if ($vertexCollectionsCount !== 1) {
960+
throw new ClientException('A collection must be provided.');
961+
}
962+
else if ($vertexCollectionsCount === 1) {
963+
$collection = $vertexCollections[0];
964+
}
965+
}
966+
967+
// This preserves compatibility for the old policy parameter.
934968
$params = [];
935969
$params = $this->validateAndIncludeOldSingleParameterInParams(
936970
$options,

tests/GraphBasicTest.php

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,83 @@ public function testAddGetDeleteCollections()
262262
static::assertTrue($result, 'Did not return true!');
263263
}
264264

265-
/**
265+
/**
266+
* Test adding, getting and deleting of collections
267+
*/
268+
public function testAddGetDeleteCollectionsWithCache()
269+
{
270+
$this->graph = new Graph('Graph1');
271+
$ed1 = EdgeDefinition::createUndirectedRelation('undirected', 'singleV');
272+
$this->graph->addOrphanCollection('ArangoDBPHPTestSuiteTestCollection04');
273+
$this->graph->addEdgeDefinition($ed1);
274+
$this->graphHandler = new GraphHandler($this->connection);
275+
276+
$result = $this->graphHandler->createGraph($this->graph);
277+
static::assertEquals($result['_key'], 'Graph1', 'Did not return Graph1!');
278+
279+
$this->graph = $this->graphHandler->addOrphanCollection($this->graph, 'orphan1');
280+
$this->graph = $this->graphHandler->addOrphanCollection($this->graph, 'orphan2');
281+
282+
static::assertSame(
283+
$this->graphHandler->getVertexCollections($this->graph, ['_useCache' => true]), [
284+
0 => 'ArangoDBPHPTestSuiteTestCollection04',
285+
1 => 'orphan1',
286+
2 => 'orphan2',
287+
3 => 'singleV'
288+
289+
]
290+
);
291+
292+
$this->graph = $this->graphHandler->deleteOrphanCollection($this->graph, 'orphan2');
293+
static::assertSame(
294+
$this->graphHandler->getVertexCollections($this->graph, ['_useCache' => true]), [
295+
0 => 'ArangoDBPHPTestSuiteTestCollection04',
296+
1 => 'orphan1',
297+
2 => 'orphan2',
298+
3 => 'singleV'
299+
300+
]
301+
);
302+
303+
static::assertSame(
304+
$this->graphHandler->getVertexCollections($this->graph), [
305+
0 => 'ArangoDBPHPTestSuiteTestCollection04',
306+
1 => 'orphan1',
307+
2 => 'singleV'
308+
309+
]
310+
);
311+
$error = null;
312+
try {
313+
$this->graph = $this->graphHandler->deleteOrphanCollection($this->graph, 'singleV');
314+
} catch (\Exception $e) {
315+
$error = $e->getMessage();
316+
}
317+
static::assertSame($error, 'not in orphan collection');
318+
319+
$error = null;
320+
try {
321+
$this->graph = $this->graphHandler->addOrphanCollection($this->graph, 'undirected');
322+
} catch (\Exception $e) {
323+
$error = $e->getMessage();
324+
}
325+
326+
static::assertSame($error, 'not a vertex collection');
327+
328+
$error = null;
329+
try {
330+
$this->graph = $this->graphHandler->getVertexCollections('notExisting');
331+
} catch (\Exception $e) {
332+
$error = $e->getMessage();
333+
}
334+
static::assertSame($error, 'graph not found');
335+
336+
$result = $this->graphHandler->dropGraph($this->graph);
337+
static::assertTrue($result, 'Did not return true!');
338+
}
339+
340+
341+
/**
266342
* Test adding, getting and deleting of edgecollections
267343
*/
268344
public function testAddGetDeleteEdgeCollections()

0 commit comments

Comments
 (0)