Skip to content

Commit

Permalink
Remove term type ID services
Browse files Browse the repository at this point in the history
This replaces services managing term type IDs through the wbt_type
database table with a simple map. The wbt_type table will be dropped in
a follow-up.

Bug: T351802
Change-Id: I1b1dc09be6f0cd42eefb4e9290aebe5ca4f5e7ab
  • Loading branch information
jakobw committed Jan 17, 2025
1 parent db60b26 commit 6ab2290
Show file tree
Hide file tree
Showing 40 changed files with 110 additions and 987 deletions.
16 changes: 2 additions & 14 deletions client/WikibaseClient.ServiceWiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
use Wikibase\Lib\Store\Sql\PropertyInfoTable;
use Wikibase\Lib\Store\Sql\Terms\CachedDatabasePropertyLabelResolver;
use Wikibase\Lib\Store\Sql\Terms\DatabaseTermInLangIdsResolver;
use Wikibase\Lib\Store\Sql\Terms\DatabaseTypeIdsStore;
use Wikibase\Lib\Store\Sql\Terms\TermInLangIdsResolverFactory;
use Wikibase\Lib\Store\TitleLookupBasedEntityExistenceChecker;
use Wikibase\Lib\Store\TitleLookupBasedEntityRedirectChecker;
Expand Down Expand Up @@ -722,18 +721,8 @@ function ( EntityNamespaceLookup $nsLookup, DatabaseEntitySource $source ): Enti

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

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

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

return new CachedDatabasePropertyLabelResolver(
$languageCode,
Expand Down Expand Up @@ -1017,8 +1006,7 @@ function ( EntityNamespaceLookup $nsLookup, DatabaseEntitySource $source ): Enti
): TermInLangIdsResolverFactory {
return new TermInLangIdsResolverFactory(
WikibaseClient::getTermsDomainDbFactory( $services ),
WikibaseClient::getLogger( $services ),
$services->getMainWANObjectCache()
WikibaseClient::getLogger( $services )
);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public function testConstruction(): void {
$this->createStub( TermsDomainDbFactory::class )
);

$this->serviceContainer
->expects( $this->once() )
->method( 'getMainWANObjectCache' );

$this->assertInstanceOf(
TermInLangIdsResolverFactory::class,
$this->getService( 'WikibaseClient.TermInLangIdsResolverFactory' )
Expand Down
19 changes: 1 addition & 18 deletions lib/includes/Store/MatchingTermsLookupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Wikibase\DataModel\Services\EntityId\EntityIdComposer;
use Wikibase\Lib\Rdbms\TermsDomainDbFactory;
use Wikibase\Lib\Store\Sql\Terms\DatabaseMatchingTermsLookup;
use Wikibase\Lib\Store\Sql\Terms\DatabaseTypeIdsStore;
use Wikimedia\ObjectCache\WANObjectCache;

/**
* @license GPL-2.0-or-later
Expand All @@ -32,36 +30,21 @@ class MatchingTermsLookupFactory {
*/
private $logger;

/**
* @var WANObjectCache
*/
private $objectCache;

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

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

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

return new DatabaseMatchingTermsLookup(
$termsDb,
$databaseTypeIdsStore,
$databaseTypeIdsStore,
$this->entityIdComposer,
$this->logger
);
Expand Down
24 changes: 4 additions & 20 deletions lib/includes/Store/Sql/Terms/DatabaseMatchingTermsLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Wikibase\Lib\Store\Sql\Terms;

use InvalidArgumentException;
use MediaWiki\Storage\NameTableAccessException;
use Psr\Log\LoggerInterface;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Services\EntityId\EntityIdComposer;
Expand All @@ -14,7 +13,6 @@
use Wikibase\Lib\Store\Sql\Terms\Util\StatsMonitoring;
use Wikibase\Lib\Store\TermIndexSearchCriteria;
use Wikibase\Lib\TermIndexEntry;
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\IExpression;
use Wikimedia\Rdbms\IReadableDatabase;
use Wikimedia\Rdbms\IResultWrapper;
Expand All @@ -34,22 +32,14 @@ class DatabaseMatchingTermsLookup implements MatchingTermsLookup {

private LoggerInterface $logger;

private TypeIdsAcquirer $typeIdsAcquirer;

private TypeIdsResolver $typeIdsResolver;

private EntityIdComposer $entityIdComposer;

public function __construct(
TermsDomainDb $termsDb,
TypeIdsAcquirer $typeIdsAcquirer,
TypeIdsResolver $typeIdsResolver,
EntityIdComposer $entityIdComposer,
LoggerInterface $logger
) {
$this->termsDb = $termsDb;
$this->typeIdsAcquirer = $typeIdsAcquirer;
$this->typeIdsResolver = $typeIdsResolver;
$this->entityIdComposer = $entityIdComposer;
$this->logger = $logger;
}
Expand Down Expand Up @@ -179,15 +169,9 @@ private function getTermMatchQueries(
$termType = $mask->getTermType();
}
if ( $termType !== null ) {
try {
$queryBuilder->where( [
'wbtl_type_id' => $this->typeIdsAcquirer->acquireTypeIds( [ $termType ] )[$termType],
] );
} catch ( NameTableAccessException $e ) {
// Edge case: attempting to do a term lookup before the first insert of the respective term type. Unlikely to happen in
// production, but annoying/confusing if it happens in tests.
return new FakeResultWrapper( [] );
}
$queryBuilder->where( [
'wbtl_type_id' => TermTypeIds::TYPE_IDS[$termType],
] );
}

if ( isset( $options['LIMIT'] ) && $options['LIMIT'] > 0 ) {
Expand Down Expand Up @@ -217,7 +201,7 @@ private function buildTermResult( array $results, ?int $limit = null ): array {
$typeId = (int)$obtainedTerm->wbtl_type_id;
$matchingTerms[] = new TermIndexEntry( [
'entityId' => $this->getEntityId( $obtainedTerm ),
'termType' => $this->typeIdsResolver->resolveTypeIds( [ $typeId ] )[$typeId],
'termType' => array_flip( TermTypeIds::TYPE_IDS )[$typeId],
'termLanguage' => $obtainedTerm->wbxl_language,
'termText' => $obtainedTerm->wbx_text,
] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,14 @@ class DatabaseTermInLangIdsAcquirer implements TermInLangIdsAcquirer {
*/
private $termsDb;

/**
* @var TypeIdsAcquirer
*/
private $typeIdsAcquirer;

/** @var LoggerInterface */
private $logger;

public function __construct(
TermsDomainDb $termsDb,
TypeIdsAcquirer $typeIdsAcquirer,
?LoggerInterface $logger = null
) {
$this->termsDb = $termsDb;
$this->typeIdsAcquirer = $typeIdsAcquirer;
$this->logger = $logger ?? new NullLogger();
}

Expand Down Expand Up @@ -83,7 +76,7 @@ public function acquireTermInLangIds( array $termsArray, ?callable $callback = n
* ]
*/
private function mapToTypeIds( array $termsArray ) {
$typeIds = $this->typeIdsAcquirer->acquireTypeIds( array_keys( $termsArray ) );
$typeIds = array_intersect_key( TermTypeIds::TYPE_IDS, $termsArray );

$termsArrayByTypeId = [];
foreach ( $typeIds as $type => $typeId ) {
Expand Down
35 changes: 3 additions & 32 deletions lib/includes/Store/Sql/Terms/DatabaseTermInLangIdsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use stdClass;
use Wikibase\Lib\Rdbms\TermsDomainDb;
use Wikibase\Lib\Store\Sql\Terms\Util\StatsMonitoring;
use Wikimedia\Rdbms\IResultWrapper;
use Wikimedia\Rdbms\SelectQueryBuilder;

/**
Expand All @@ -21,29 +20,16 @@ class DatabaseTermInLangIdsResolver implements TermInLangIdsResolver {

use StatsMonitoring;

/** @var TypeIdsResolver */
private $typeIdsResolver;

/** @var TypeIdsLookup */
private $typeIdsLookup;

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

/** @var LoggerInterface */
private $logger;

/** @var string[] stash of data returned from the {@link TypeIdsResolver} */
private $typeNames = [];

public function __construct(
TypeIdsResolver $typeIdsResolver,
TypeIdsLookup $typeIdsLookup,
TermsDomainDb $termsDb,
?LoggerInterface $logger = null
) {
$this->typeIdsResolver = $typeIdsResolver;
$this->typeIdsLookup = $typeIdsLookup;
$this->termsDb = $termsDb;
$this->logger = $logger ?: new NullLogger();
}
Expand Down Expand Up @@ -87,7 +73,6 @@ public function resolveGroupedTermInLangIds(
$result = $this->newSelectQueryBuilder( $types, $languages )
->where( [ 'wbtl_id' => $allTermInLangIds ] )
->caller( __METHOD__ )->fetchResultSet();
$this->preloadTypes( $result );

foreach ( $result as $row ) {
foreach ( $groupNamesByTermInLangIds[$row->wbtl_id] as $groupName ) {
Expand Down Expand Up @@ -144,8 +129,6 @@ public function resolveTermsViaJoin(
->where( $conditions )
->caller( __METHOD__ )->fetchResultSet();

$this->preloadTypes( $records );

$termsByKeyColumn = [];
foreach ( $records as $record ) {
if ( !isset( $termsByKeyColumn[$record->$groupColumn] ) ) {
Expand Down Expand Up @@ -176,17 +159,6 @@ private function newSelectQueryBuilder(
return $queryBuilder;
}

private function preloadTypes( IResultWrapper $result ) {
$typeIds = [];
foreach ( $result as $row ) {
$typeId = $row->wbtl_type_id;
if ( !array_key_exists( $typeId, $this->typeNames ) ) {
$typeIds[$typeId] = true;
}
}
$this->typeNames += $this->typeIdsResolver->resolveTypeIds( array_keys( $typeIds ) );
}

private function addResultTerms( array &$terms, stdClass $row ) {
$type = $this->lookupTypeName( $row->wbtl_type_id );
$lang = $row->wbxl_language;
Expand All @@ -195,16 +167,15 @@ private function addResultTerms( array &$terms, stdClass $row ) {
}

private function lookupTypeName( $typeId ) {
$typeName = $this->typeNames[$typeId] ?? null;
$typeName = array_flip( TermTypeIds::TYPE_IDS )[$typeId] ?? null;
if ( $typeName === null ) {
throw new InvalidArgumentException(
'Type ID ' . $typeId . ' was requested but not preloaded!' );
throw new InvalidArgumentException( 'Unknown type ID: ' . $typeId );
}
return $typeName;
}

private function lookupTypeIds( array $typeNames ) {
return $this->typeIdsLookup->lookupTypeIds( $typeNames );
return array_intersect_key( TermTypeIds::TYPE_IDS, array_flip( $typeNames ) );
}

private function getDbr() {
Expand Down
74 changes: 0 additions & 74 deletions lib/includes/Store/Sql/Terms/DatabaseTypeIdsStore.php

This file was deleted.

Loading

0 comments on commit 6ab2290

Please # to comment.