WMDE-leszek has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/324732

Change subject: [WIP] Use DispatchingTermBuffer in WikibaseClient
......................................................................

[WIP] Use DispatchingTermBuffer in WikibaseClient

TODO:
 - more (proper) tests

Bug: TODO
Change-Id: I71a1aec47a1925f20e6ee5877d1ec1e81be6d3c3
---
M client/includes/RepositorySpecificServiceWiring.php
M client/includes/RepositorySpecificServices.php
M client/includes/WikibaseClient.php
M client/tests/phpunit/includes/WikibaseClientFederationIntegrationTest.php
4 files changed, 96 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/32/324732/1

diff --git a/client/includes/RepositorySpecificServiceWiring.php 
b/client/includes/RepositorySpecificServiceWiring.php
index 119d072..be4ef4f 100644
--- a/client/includes/RepositorySpecificServiceWiring.php
+++ b/client/includes/RepositorySpecificServiceWiring.php
@@ -3,6 +3,8 @@
 use Wikibase\Client\RepositorySpecificServices;
 use Wikibase\DataModel\Services\Lookup\RedirectResolvingEntityLookup;
 use Wikibase\Lib\Store\RevisionBasedEntityLookup;
+use Wikibase\Store\BufferingTermLookup;
+use Wikibase\TermSqlIndex;
 
 return [
 
@@ -30,4 +32,21 @@
                return $lookups;
        },
 
+       'TermBuffers' => function ( RepositorySpecificServices $services ) {
+               $databaseNames = $services->getDatabaseNames();
+               $buffers = [];
+               foreach ( $services->getRepositoryNames() as $repositoryName ) {
+                       $buffers[$repositoryName] = new BufferingTermLookup(
+                               new TermSqlIndex(
+                                       
$services->getClient()->getStringNormalizer(),
+                                       
$services->getClient()->getEntityIdComposer(),
+                                       $databaseNames[$repositoryName],
+                                       $repositoryName
+                               ),
+                               1000 // @todo: configure buffer size (TODO 
originally from WikibaseClient)
+                       );
+               }
+               return $buffers;
+       },
+
 ];
diff --git a/client/includes/RepositorySpecificServices.php 
b/client/includes/RepositorySpecificServices.php
index 6d59602..91a8c5d 100644
--- a/client/includes/RepositorySpecificServices.php
+++ b/client/includes/RepositorySpecificServices.php
@@ -7,6 +7,7 @@
 use Wikibase\DataModel\DeserializerFactory;
 use Wikibase\DataModel\Services\EntityId\PrefixMappingEntityIdParserFactory;
 use Wikibase\DataModel\Services\Lookup\EntityLookup;
+use Wikibase\DataModel\Services\Term\TermBuffer;
 use Wikibase\Lib\Serialization\RepositorySpecificDataValueDeserializerFactory;
 use Wikibase\Lib\Store\EntityRevisionLookup;
 use Wikibase\Lib\Store\RepositorySpecificEntityRevisionLookupFactory;
@@ -49,6 +50,14 @@
        public function getEntityRevisionLookups() {
                // TODO: should rather use some other method as it returns a 
repo name => service map, not a service
                return $this->getService( 'EntityRevisionLookups' );
+       }
+
+       /**
+        * @return TermBuffer[]
+        */
+       public function getTermBuffers() {
+               // TODO: should rather use some other method as it returns a 
repo name => service map, not a service
+               return $this->getService( 'TermBuffers' );
        }
 
        /**
@@ -100,7 +109,7 @@
        /**
         * @return string[]
         */
-       private function getDatabaseNames() {
+       public function getDatabaseNames() {
                $names = [ '' => $this->client->getSettings()->getSetting( 
'repoDatabase' ) ];
                $foreignRepositorySettings = $this->config->get( 
'ForeignRepositorySettings' );
                foreach ( $foreignRepositorySettings as $repositoryName => 
$settings ) {
@@ -122,6 +131,7 @@
         * @return callable[]
         */
        private function getDeserializerFactoryCallbacks() {
+               // TODO: this should be coming from WikibaseClient somehow
                return [
                        'item' => function( DeserializerFactory 
$deserializerFactory ) {
                                return 
$deserializerFactory->newItemDeserializer();
@@ -132,4 +142,8 @@
                ];
        }
 
+       public function getClient() {
+               return $this->client;
+       }
+
 }
diff --git a/client/includes/WikibaseClient.php 
b/client/includes/WikibaseClient.php
index 58675c7..2d12d90 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -73,6 +73,7 @@
 use Wikibase\Lib\EntityIdComposer;
 use Wikibase\Lib\EntityTypeDefinitions;
 use Wikibase\Lib\FormatterLabelDescriptionLookupFactory;
+use Wikibase\Lib\Store\DispatchingTermBuffer;
 use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookupFactory;
 use Wikibase\Lib\LanguageNameLookup;
 use Wikibase\Lib\MediaWikiContentLanguages;
@@ -398,10 +399,9 @@
         * @return BufferingTermLookup
         */
        public function getBufferingTermLookup() {
-               if ( !$this->termLookup ) {
-                       $this->termLookup = new BufferingTermLookup(
-                               $this->getStore()->getTermIndex(),
-                               1000 // @todo: configure buffer size
+               if ( $this->termLookup === null ) {
+                       $this->termLookup = new DispatchingTermBuffer(
+                               
$this->getRepositorySpecificServices()->getTermBuffers()
                        );
                }
 
@@ -507,14 +507,7 @@
                if ( $this->store === null ) {
                        $this->store = new DispatchingServiceStore(
                                $this->getSettings(),
-                               new RepositorySpecificServices(
-                                       $this,
-                                       // TODO: TBC
-                                       new HashConfig( [
-                                               'ServiceWiringFiles' => 
$this->settings->getSetting( 'repositorySpecificServiceWiringFiles' ),
-                                               'ForeignRepositorySettings' => 
$this->settings->getSetting( 'foreignRepositories' ),
-                                       ] )
-                               ),
+                               $this->getRepositorySpecificServices(),
                                $this->getDirectSqlStore()
                        );
                }
@@ -1300,4 +1293,18 @@
                return $setting;
        }
 
+       /**
+        * @return RepositorySpecificServices
+        */
+       private function getRepositorySpecificServices() {
+               return new RepositorySpecificServices(
+                       $this,
+                       // TODO: TBC
+                       new HashConfig( [
+                               'ServiceWiringFiles' => 
$this->settings->getSetting( 'repositorySpecificServiceWiringFiles' ),
+                               'ForeignRepositorySettings' => 
$this->settings->getSetting( 'foreignRepositories' ),
+                       ] )
+               );
+       }
+
 }
diff --git 
a/client/tests/phpunit/includes/WikibaseClientFederationIntegrationTest.php 
b/client/tests/phpunit/includes/WikibaseClientFederationIntegrationTest.php
index 5cac80a..993f212 100644
--- a/client/tests/phpunit/includes/WikibaseClientFederationIntegrationTest.php
+++ b/client/tests/phpunit/includes/WikibaseClientFederationIntegrationTest.php
@@ -12,6 +12,7 @@
 use ResultWrapper;
 use Wikibase\Client\WikibaseClient;
 use Wikibase\DataModel\DeserializerFactory;
+use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\SerializerFactory;
 use Wikibase\Lib\DataTypeDefinitions;
@@ -71,7 +72,7 @@
                return $factory;
        }
 
-       public function testFoo() {
+       public function testEntityLookup() {
                $defaultLb = $this->getMockLoadBalancer( $this->getMockDb( new 
FakeResultWrapper( [] ) ) );
                $lbFactory = $this->getLoadBalancerFactory( [
                        'foowiki' => $this->getMockLoadBalancer(
@@ -100,13 +101,43 @@
                ] );
 
                $client = $this->getWikibaseClient();
-               $lookup = $client->getRestrictedEntityLookup();
+               $entityLookup = $client->getRestrictedEntityLookup();
 
-               $this->assertTrue( $lookup->hasEntity( new ItemId( 'foo:Q123' ) 
) );
-               $this->assertFalse( $lookup->hasEntity( new ItemId( 'Q123' ) ) 
);
+               $this->assertTrue( $entityLookup->hasEntity( new ItemId( 
'foo:Q123' ) ) );
+               $this->assertFalse( $entityLookup->hasEntity( new ItemId( 
'Q123' ) ) );
 
-               $entity = $lookup->getEntity( new ItemId( 'foo:Q123' ) );
+               $entity = $entityLookup->getEntity( new ItemId( 'foo:Q123' ) );
                $this->assertEquals( 'foo:Q123', 
$entity->getId()->getSerialization() );
+       }
+
+       public function testTermLookup() {
+               $defaultLb = $this->getMockLoadBalancer( $this->getMockDb( new 
FakeResultWrapper( [] ) ) );
+               $lbFactory = $this->getLoadBalancerFactory( [
+                       'foowiki' => $this->getMockLoadBalancer(
+                               $this->getMockDb( new FakeResultWrapper( [ 
$this->getRowObject( [
+                                       'term_entity_type' => 'item',
+                                       'term_type' => 'label',
+                                       'term_language' => 'en',
+                                       'term_text' => 'Foo Item',
+                                       'term_entity_id' => 123,
+                               ] ) ] ) )
+                       ),
+                       
WikibaseClient::getDefaultInstance()->getSettings()->getSetting( 'repoDatabase' 
) => $defaultLb,
+               ] );
+               $this->overrideMwServices( null, [
+                       'DBLoadBalancerFactory' => function () use ( $lbFactory 
) {
+                               return $lbFactory;
+                       },
+                       'DBLoadBalancer' => function () use ( $defaultLb ) {
+                               return $defaultLb;
+                       },
+               ] );
+
+               $client = $this->getWikibaseClient();
+               $termLookup = $client->getTermLookup();
+
+               $this->assertSame( 'Foo Item', $termLookup->getLabel( new 
ItemId( 'foo:Q123' ), 'en' ) );
+               $this->assertNull( $termLookup->getLabel( new ItemId( 'Q123' ), 
'en' ) );
        }
 
        /**
@@ -131,6 +162,13 @@
                                        'entity-id-builder' => function( 
$serialization ) {
                                                return new ItemId( 
$serialization );
                                        },
+                                       'entity-id-composer-callback' => 
function( $repositoryName, $uniquePart ) {
+                                               return new ItemId( 
EntityId::joinSerialization( [
+                                                       $repositoryName,
+                                                       '',
+                                                       'Q' . $uniquePart
+                                               ] ) );
+                                       },
                                ],
                        ] ),
                        new HashSiteStore()

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I71a1aec47a1925f20e6ee5877d1ec1e81be6d3c3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: WMDE-leszek <leszek.mani...@wikimedia.de>
Gerrit-Reviewer: WMDE-leszek <leszek.mani...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to