Skip to content

Commit

Permalink
Merge "Introduce TermsDomainDb"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Dec 17, 2024
2 parents d01c327 + 542a2d6 commit 794432c
Show file tree
Hide file tree
Showing 48 changed files with 493 additions and 251 deletions.
13 changes: 9 additions & 4 deletions client/WikibaseClient.ServiceWiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
use Wikibase\Lib\Rdbms\ClientDomainDbFactory;
use Wikibase\Lib\Rdbms\DomainDb;
use Wikibase\Lib\Rdbms\RepoDomainDbFactory;
use Wikibase\Lib\Rdbms\TermsDomainDbFactory;
use Wikibase\Lib\ServiceBySourceAndTypeDispatcher;
use Wikibase\Lib\SettingsArray;
use Wikibase\Lib\Store\CachingPropertyInfoLookup;
Expand Down Expand Up @@ -718,19 +719,19 @@ function ( EntityNamespaceLookup $nsLookup, DatabaseEntitySource $source ): Enti
// Cache key needs to be language specific
$cacheKey = $cacheKeyPrefix . ':TermPropertyLabelResolver' . '/' . $languageCode;

$repoDb = WikibaseClient::getRepoDomainDbFactory( $services )
$termsDb = WikibaseClient::getTermsDomainDbFactory( $services )
->newForEntitySource( WikibaseClient::getPropertySource( $services ) );
$wanObjectCache = $services->getMainWANObjectCache();

$typeIdsStore = new DatabaseTypeIdsStore(
$repoDb,
$termsDb,
$wanObjectCache
);

$databaseTermIdsResolver = new DatabaseTermInLangIdsResolver(
$typeIdsStore,
$typeIdsStore,
$repoDb
$termsDb
);

return new CachedDatabasePropertyLabelResolver(
Expand Down Expand Up @@ -1014,7 +1015,7 @@ function ( EntityNamespaceLookup $nsLookup, DatabaseEntitySource $source ): Enti
MediaWikiServices $services
): TermInLangIdsResolverFactory {
return new TermInLangIdsResolverFactory(
WikibaseClient::getRepoDomainDbFactory( $services ),
WikibaseClient::getTermsDomainDbFactory( $services ),
WikibaseClient::getLogger( $services ),
$services->getMainWANObjectCache()
);
Expand All @@ -1024,6 +1025,10 @@ function ( EntityNamespaceLookup $nsLookup, DatabaseEntitySource $source ): Enti
return WikibaseClient::getPrefetchingTermLookup( $services );
},

'WikibaseClient.TermsDomainDbFactory' => function ( MediaWikiServices $services ): TermsDomainDbFactory {
return new TermsDomainDbFactory( WikibaseClient::getRepoDomainDbFactory( $services ) );
},

'WikibaseClient.TermsLanguages' => function ( MediaWikiServices $services ): ContentLanguages {
return WikibaseClient::getWikibaseContentLanguages( $services )
->getContentLanguages( WikibaseContentLanguages::CONTEXT_TERM );
Expand Down
6 changes: 6 additions & 0 deletions client/includes/WikibaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use Wikibase\Lib\MessageInLanguageProvider;
use Wikibase\Lib\Rdbms\ClientDomainDbFactory;
use Wikibase\Lib\Rdbms\RepoDomainDbFactory;
use Wikibase\Lib\Rdbms\TermsDomainDbFactory;
use Wikibase\Lib\SettingsArray;
use Wikibase\Lib\Store\EntityIdLookup;
use Wikibase\Lib\Store\EntityNamespaceLookup;
Expand Down Expand Up @@ -523,6 +524,11 @@ public static function getRepoDomainDbFactory( ?ContainerInterface $services = n
->get( 'WikibaseClient.RepoDomainDbFactory' );
}

public static function getTermsDomainDbFactory( ?ContainerInterface $services = null ): TermsDomainDbFactory {
return ( $services ?: MediaWikiServices::getInstance() )
->get( 'WikibaseClient.TermsDomainDbFactory' );
}

public static function getEntitySourceAndTypeDefinitions( ?ContainerInterface $services = null ): EntitySourceAndTypeDefinitions {
return ( $services ?: MediaWikiServices::getInstance() )
->get( 'WikibaseClient.EntitySourceAndTypeDefinitions' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Wikibase\Client\Tests\Unit\ServiceWiringTestCase;
use Wikibase\DataAccess\DatabaseEntitySource;
use Wikibase\DataModel\Services\Term\PropertyLabelResolver;
use Wikibase\Lib\Rdbms\RepoDomainDb;
use Wikibase\Lib\Rdbms\RepoDomainDbFactory;
use Wikibase\Lib\Rdbms\TermsDomainDb;
use Wikibase\Lib\Rdbms\TermsDomainDbFactory;
use Wikibase\Lib\SettingsArray;

/**
Expand All @@ -32,13 +32,13 @@ public function testConstruction(): void {
$propertySource = $this->createStub( DatabaseEntitySource::class );
$this->mockService( 'WikibaseClient.PropertySource', $propertySource );

$repoDb = $this->createStub( RepoDomainDb::class );
$dbFactory = $this->createMock( RepoDomainDbFactory::class );
$termsDb = $this->createStub( TermsDomainDb::class );
$dbFactory = $this->createMock( TermsDomainDbFactory::class );
$dbFactory->expects( $this->once() )
->method( 'newForEntitySource' )
->with( $propertySource )
->willReturn( $repoDb );
$this->mockService( 'WikibaseClient.RepoDomainDbFactory', $dbFactory );
->willReturn( $termsDb );
$this->mockService( 'WikibaseClient.TermsDomainDbFactory', $dbFactory );

$this->serviceContainer->expects( $this->once() )
->method( 'getObjectCacheFactory' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Psr\Log\NullLogger;
use Wikibase\Client\Tests\Unit\ServiceWiringTestCase;
use Wikibase\Lib\Rdbms\RepoDomainDbFactory;
use Wikibase\Lib\Rdbms\TermsDomainDbFactory;
use Wikibase\Lib\Store\Sql\Terms\TermInLangIdsResolverFactory;

/**
Expand All @@ -24,8 +24,8 @@ public function testConstruction(): void {
);

$this->mockService(
'WikibaseClient.RepoDomainDbFactory',
$this->createStub( RepoDomainDbFactory::class )
'WikibaseClient.TermsDomainDbFactory',
$this->createStub( TermsDomainDbFactory::class )
);

$this->serviceContainer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare( strict_types = 1 );

namespace Wikibase\Client\Tests\Unit\ServiceWiring;

use Wikibase\Client\Tests\Unit\ServiceWiringTestCase;
use Wikibase\Lib\Rdbms\RepoDomainDbFactory;
use Wikibase\Lib\Rdbms\TermsDomainDbFactory;

/**
* @coversNothing
*
* @group Wikibase
*
* @license GPL-2.0-or-later
*/
class TermsDomainDbFactoryTest extends ServiceWiringTestCase {

public function testConstruction() {
$this->mockService( 'WikibaseClient.RepoDomainDbFactory', $this->createStub( RepoDomainDbFactory::class ) );

$this->assertInstanceOf(
TermsDomainDbFactory::class,
$this->getService( 'WikibaseClient.TermsDomainDbFactory' )
);
}

}
48 changes: 48 additions & 0 deletions lib/includes/Rdbms/TermsDomainDb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare( strict_types=1 );

namespace Wikibase\Lib\Rdbms;

use Wikimedia\Rdbms\ConnectionManager;
use Wikimedia\Rdbms\ILoadBalancer;

/**
* Database abstraction to access terms (labels, descriptions, aliases) database tables created by the WikibaseRepository extension.
* (This access may happen in repo, client, or lib.)
*
* The underlying database is either the same as {@link RepoDomainDb}, or a dedicated virtual domain database.
*
* @license GPL-2.0-or-later
*/
class TermsDomainDb {

private RepoDomainDb $repoDb;

public function __construct( RepoDomainDb $repoDb ) {
$this->repoDb = $repoDb;
}

public function connections(): ConnectionManager {
return $this->repoDb->connections();
}

public function replication(): ReplicationWaiter {
return $this->repoDb->replication();
}

/**
* @deprecated Don't use this unless it needs to be passed to a service we don't control
*/
public function loadBalancer(): ILoadBalancer {
return $this->repoDb->loadBalancer();
}

/**
* @deprecated Don't use this unless it needs to be passed to a service we don't control
*/
public function domain(): string {
return $this->repoDb->domain();
}

}
28 changes: 28 additions & 0 deletions lib/includes/Rdbms/TermsDomainDbFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare( strict_types=1 );

namespace Wikibase\Lib\Rdbms;

use Wikibase\DataAccess\DatabaseEntitySource;

/**
* @license GPL-2.0-or-later
*/
class TermsDomainDbFactory {

private RepoDomainDbFactory $repoDomainDbFactory;

public function __construct( RepoDomainDbFactory $repoDomainDbFactory ) {
$this->repoDomainDbFactory = $repoDomainDbFactory;
}

public function newTermsDb(): TermsDomainDb {
return new TermsDomainDb( $this->repoDomainDbFactory->newRepoDb() );
}

public function newForEntitySource( DatabaseEntitySource $entitySource ): TermsDomainDb {
return new TermsDomainDb( $this->repoDomainDbFactory->newForEntitySource( $entitySource ) );
}

}
16 changes: 8 additions & 8 deletions lib/includes/Store/MatchingTermsLookupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Log\LoggerInterface;
use Wikibase\DataAccess\DatabaseEntitySource;
use Wikibase\DataModel\Services\EntityId\EntityIdComposer;
use Wikibase\Lib\Rdbms\RepoDomainDbFactory;
use Wikibase\Lib\Rdbms\TermsDomainDbFactory;
use Wikibase\Lib\Store\Sql\Terms\DatabaseMatchingTermsLookup;
use Wikibase\Lib\Store\Sql\Terms\DatabaseTypeIdsStore;
use Wikimedia\ObjectCache\WANObjectCache;
Expand All @@ -23,9 +23,9 @@ class MatchingTermsLookupFactory {
private $entityIdComposer;

/**
* @var RepoDomainDbFactory
* @var TermsDomainDbFactory
*/
private $repoDomainDbFactory;
private $termsDomainDbFactory;

/**
* @var LoggerInterface
Expand All @@ -39,27 +39,27 @@ class MatchingTermsLookupFactory {

public function __construct(
EntityIdComposer $entityIdComposer,
RepoDomainDbFactory $repoDomainDbFactory,
TermsDomainDbFactory $termsDomainDbFactory,
LoggerInterface $logger,
WANObjectCache $objectCache
) {
$this->entityIdComposer = $entityIdComposer;
$this->repoDomainDbFactory = $repoDomainDbFactory;
$this->termsDomainDbFactory = $termsDomainDbFactory;
$this->logger = $logger;
$this->objectCache = $objectCache;
}

public function getLookupForSource( DatabaseEntitySource $entitySource ): MatchingTermsLookup {
$repoDb = $this->repoDomainDbFactory->newForEntitySource( $entitySource );
$termsDb = $this->termsDomainDbFactory->newForEntitySource( $entitySource );

$databaseTypeIdsStore = new DatabaseTypeIdsStore(
$repoDb,
$termsDb,
$this->objectCache,
$this->logger
);

return new DatabaseMatchingTermsLookup(
$repoDb,
$termsDb,
$databaseTypeIdsStore,
$databaseTypeIdsStore,
$this->entityIdComposer,
Expand Down
4 changes: 2 additions & 2 deletions lib/includes/Store/Sql/Terms/CleanTermsIfUnusedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public static function getJobSpecification( Title $unused, array $params ): Clea

public static function getJobSpecificationNoTitle( array $params ): CleanTermsIfUnusedJob {
$repoDomainDb = MediaWikiServices::getInstance()
->get( 'WikibaseRepo.RepoDomainDbFactory' )
->newRepoDb();
->get( 'WikibaseRepo.TermsDomainDbFactory' )
->newTermsDb();

$logger = LoggerFactory::getInstance( 'Wikibase' );
$innerTermStoreCleaner = new DatabaseInnerTermStoreCleaner( $logger );
Expand Down
10 changes: 5 additions & 5 deletions lib/includes/Store/Sql/Terms/DatabaseMatchingTermsLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Psr\Log\LoggerInterface;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Services\EntityId\EntityIdComposer;
use Wikibase\Lib\Rdbms\RepoDomainDb;
use Wikibase\Lib\Rdbms\TermsDomainDb;
use Wikibase\Lib\Store\MatchingTermsLookup;
use Wikibase\Lib\Store\Sql\Terms\Util\StatsMonitoring;
use Wikibase\Lib\Store\TermIndexSearchCriteria;
Expand All @@ -30,7 +30,7 @@ class DatabaseMatchingTermsLookup implements MatchingTermsLookup {

use StatsMonitoring;

private RepoDomainDb $repoDb;
private TermsDomainDb $termsDb;

private LoggerInterface $logger;

Expand All @@ -41,13 +41,13 @@ class DatabaseMatchingTermsLookup implements MatchingTermsLookup {
private EntityIdComposer $entityIdComposer;

public function __construct(
RepoDomainDb $repoDb,
TermsDomainDb $termsDb,
TypeIdsAcquirer $typeIdsAcquirer,
TypeIdsResolver $typeIdsResolver,
EntityIdComposer $entityIdComposer,
LoggerInterface $logger
) {
$this->repoDb = $repoDb;
$this->termsDb = $termsDb;
$this->typeIdsAcquirer = $typeIdsAcquirer;
$this->typeIdsResolver = $typeIdsResolver;
$this->entityIdComposer = $entityIdComposer;
Expand Down Expand Up @@ -246,6 +246,6 @@ private function getEntityId( object $termRow ): ?EntityId {
}

private function getDbr(): IReadableDatabase {
return $this->repoDb->connections()->getReadConnection();
return $this->termsDb->connections()->getReadConnection();
}
}
18 changes: 9 additions & 9 deletions lib/includes/Store/Sql/Terms/DatabaseTermInLangIdsAcquirer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use MediaWiki\MediaWikiServices;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Wikibase\Lib\Rdbms\RepoDomainDb;
use Wikibase\Lib\Rdbms\TermsDomainDb;
use Wikibase\Lib\Store\Sql\Terms\Util\ReplicaPrimaryAwareRecordIdsAcquirer;

/**
Expand All @@ -23,9 +23,9 @@
class DatabaseTermInLangIdsAcquirer implements TermInLangIdsAcquirer {

/**
* @var RepoDomainDb
* @var TermsDomainDb
*/
private $repoDb;
private $termsDb;

/**
* @var TypeIdsAcquirer
Expand All @@ -36,11 +36,11 @@ class DatabaseTermInLangIdsAcquirer implements TermInLangIdsAcquirer {
private $logger;

public function __construct(
RepoDomainDb $repoDb,
TermsDomainDb $termsDb,
TypeIdsAcquirer $typeIdsAcquirer,
?LoggerInterface $logger = null
) {
$this->repoDb = $repoDb;
$this->termsDb = $termsDb;
$this->typeIdsAcquirer = $typeIdsAcquirer;
$this->logger = $logger ?? new NullLogger();
}
Expand Down Expand Up @@ -375,7 +375,7 @@ function ( $record, $idToRestore ) {
private function restoreCleanedUpIds( array $termsArray, array $termInLangIds = [] ) {
$uniqueTermIds = array_values( array_unique( $termInLangIds ) );

$dbMaster = $this->repoDb->connections()->getWriteConnection();
$dbMaster = $this->termsDb->connections()->getWriteConnection();
$persistedTermIds = $dbMaster->newSelectQueryBuilder()
->select( 'wbtl_id' )
->from( 'wbt_term_in_lang' )
Expand All @@ -397,13 +397,13 @@ private function mapTermsArrayToTermIds(
$ignoreReplica = false
): array {
$textIdsAcquirer = new ReplicaPrimaryAwareRecordIdsAcquirer(
$this->repoDb, 'wbt_text', 'wbx_id',
$this->termsDb, 'wbt_text', 'wbx_id',
$ignoreReplica ? ReplicaPrimaryAwareRecordIdsAcquirer::FLAG_IGNORE_REPLICA : 0x0 );
$textInLangIdsAcquirer = new ReplicaPrimaryAwareRecordIdsAcquirer(
$this->repoDb, 'wbt_text_in_lang', 'wbxl_id',
$this->termsDb, 'wbt_text_in_lang', 'wbxl_id',
$ignoreReplica ? ReplicaPrimaryAwareRecordIdsAcquirer::FLAG_IGNORE_REPLICA : 0x0 );
$termInLangIdsAcquirer = new ReplicaPrimaryAwareRecordIdsAcquirer(
$this->repoDb, 'wbt_term_in_lang', 'wbtl_id',
$this->termsDb, 'wbt_term_in_lang', 'wbtl_id',
$ignoreReplica ? ReplicaPrimaryAwareRecordIdsAcquirer::FLAG_IGNORE_REPLICA : 0x0 );

$termsArray = $this->mapToTextIds( $termsArray, $textIdsAcquirer );
Expand Down
Loading

0 comments on commit 794432c

Please sign in to comment.