jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/349949 )

Change subject: Cache constraints for property
......................................................................


Cache constraints for property

Avoid querying the database repeatedly for constraints of the same
property by introducing a CachingConstraintLookup.

(To be clear: this only caches the information that e.g. the property
“sibling” should be symmetric, not any individual check results.)

Also adds a test for the new class.

Bug: T89349
Change-Id: Icd42c22b17e9dc5628f59793bb13473fb5a60504
---
A includes/CachingConstraintLookup.php
M includes/ConstraintReportFactory.php
A tests/phpunit/CachingConstraintLookupTest.php
3 files changed, 79 insertions(+), 1 deletion(-)

Approvals:
  jenkins-bot: Verified
  Thiemo Mättig (WMDE): Looks good to me, approved



diff --git a/includes/CachingConstraintLookup.php 
b/includes/CachingConstraintLookup.php
new file mode 100644
index 0000000..c022317
--- /dev/null
+++ b/includes/CachingConstraintLookup.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport;
+
+use Wikibase\DataModel\Entity\PropertyId;
+
+/**
+ * A ConstraintLookup that caches the results of another lookup in memory
+ * (by storing the constraint array per property ID in a big array).
+ *
+ * @author Lucas Werkmeister
+ * @license GNU GPL v2+
+ */
+class CachingConstraintLookup implements ConstraintLookup {
+
+       /**
+        * @var ConstraintLookup
+        */
+       private $lookup;
+
+       /**
+        * @var Constraint[][]
+        */
+       private $cache = [];
+
+       /**
+        * @var ConstraintLookup $lookup The lookup to which all queries are 
delegated.
+        */
+       public function __construct( ConstraintLookup $lookup ) {
+               $this->lookup = $lookup;
+       }
+
+       /**
+        * @param PropertyId $propertyId
+        *
+        * @return Constraint[]
+        */
+       public function queryConstraintsForProperty( PropertyId $propertyId ) {
+               $id = $propertyId->getSerialization();
+               if ( !array_key_exists( $id, $this->cache ) ) {
+                       $this->cache[$id] = 
$this->lookup->queryConstraintsForProperty( $propertyId );
+               }
+               return $this->cache[$id];
+       }
+
+}
diff --git a/includes/ConstraintReportFactory.php 
b/includes/ConstraintReportFactory.php
index d63d968..25db4eb 100644
--- a/includes/ConstraintReportFactory.php
+++ b/includes/ConstraintReportFactory.php
@@ -107,7 +107,7 @@
                        $this->delegatingConstraintChecker = new 
DelegatingConstraintChecker(
                                $this->lookup,
                                $this->getConstraintCheckerMap(),
-                               new ConstraintRepository()
+                               new CachingConstraintLookup( 
$this->getConstraintRepository() )
                        );
                }
 
diff --git a/tests/phpunit/CachingConstraintLookupTest.php 
b/tests/phpunit/CachingConstraintLookupTest.php
new file mode 100644
index 0000000..7feda63
--- /dev/null
+++ b/tests/phpunit/CachingConstraintLookupTest.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\Tests;
+
+use Wikibase\DataModel\Entity\PropertyId;
+use WikibaseQuality\ConstraintReport\ConstraintLookup;
+use WikibaseQuality\ConstraintReport\CachingConstraintLookup;
+
+class CachingConstraintLookupTest extends \PHPUnit_Framework_TestCase {
+
+       public function testQuery_CalledOnce() {
+               $p2 = new PropertyId( 'P2' );
+               $p3 = new PropertyId( 'P3' );
+
+               $mock = $this->getMockBuilder( ConstraintLookup::class 
)->getMock();
+               $mock->expects( $this->exactly( 2 ) )
+                       ->method( 'queryConstraintsForProperty' )
+                       ->willReturn( [] )
+                       ->withConsecutive(
+                               [ $this->equalTo( $p2 ) ],
+                               [ $this->equalTo( $p3 ) ]
+                       );
+
+               $lookup = new CachingConstraintLookup( $mock );
+
+               $this->assertSame( [], $lookup->queryConstraintsForProperty( 
$p2 ) );
+               $this->assertSame( [], $lookup->queryConstraintsForProperty( 
$p2 ) );
+               $this->assertSame( [], $lookup->queryConstraintsForProperty( 
$p3 ) );
+               $this->assertSame( [], $lookup->queryConstraintsForProperty( 
$p2 ) );
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/349949
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Icd42c22b17e9dc5628f59793bb13473fb5a60504
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to