Ladsgroup has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/351641 )

Change subject: [WIP] Two wb_terms reading modes
......................................................................

[WIP] Two wb_terms reading modes

Bug: T162673
Change-Id: I958152bc099fd6a68f2fc6ea006417c548d83dd4
---
M lib/includes/Store/Sql/TermSqlIndex.php
M lib/tests/phpunit/Store/Sql/TermSqlIndexTest.php
M repo/includes/Store/Sql/SqlEntitiesWithoutTermFinder.php
M repo/tests/phpunit/includes/Store/Sql/SqlEntitiesWithoutTermFinderTest.php
4 files changed, 115 insertions(+), 42 deletions(-)


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

diff --git a/lib/includes/Store/Sql/TermSqlIndex.php 
b/lib/includes/Store/Sql/TermSqlIndex.php
index 10ac82e..34d4405 100644
--- a/lib/includes/Store/Sql/TermSqlIndex.php
+++ b/lib/includes/Store/Sql/TermSqlIndex.php
@@ -54,6 +54,11 @@
        private $hasFullEntityIdColumn;
 
        /**
+        * @var bool
+        */
+       private $canReadFullEntityIdColumn;
+
+       /**
         * @var int
         */
        private $maxConflicts = 500;
@@ -64,13 +69,15 @@
         * @param string|bool $wikiDb
         * @param string $repositoryName
         * @param bool $hasFullEntityIdColumn Allow use (e.g. writing) of 
column.
+        * @param bool $canReadFullEntityIdColumn Whether it can read 
term_entity_full_id or not
         */
        public function __construct(
                StringNormalizer $stringNormalizer,
                EntityIdComposer $entityIdComposer,
                $wikiDb = false,
                $repositoryName = '',
-               $hasFullEntityIdColumn = true
+               $hasFullEntityIdColumn = true,
+               $canReadFullEntityIdColumn = true
        ) {
                RepositoryNameAssert::assertParameterIsValidRepositoryName( 
$repositoryName, '$repositoryName' );
                parent::__construct( $wikiDb );
@@ -78,6 +85,7 @@
                $this->stringNormalizer = $stringNormalizer;
                $this->entityIdComposer = $entityIdComposer;
                $this->hasFullEntityIdColumn = $hasFullEntityIdColumn;
+               $this->canReadFullEntityIdColumn = $canReadFullEntityIdColumn;
 
                $this->tableName = 'wb_terms';
        }
@@ -310,10 +318,17 @@
                $this->assertIsNumericEntityId( $entityId );
                /** @var EntityId|Int32EntityId $entityId */
 
-               $entityIdentifiers = array(
-                       'term_entity_id' => $entityId->getNumericId(),
+               $entityIdentifiers = [
                        'term_entity_type' => $entityId->getEntityType()
-               );
+               ];
+               if ( $this->hasFullEntityIdColumn ) {
+                       $numericEntityId = $dbw->addQuotes( 
$entityId->getNumericId() );
+                       $fullEntityId = $dbw->addQuotes( 
$entityId->getSerialization() );
+                       $entityIdentifiers[] = "(term_entity_id = 
{$numericEntityId}" .
+                               "OR term_full_entity_id = {$fullEntityId})";
+               } else {
+                       $entityIdentifiers['term_entity_id'] = 
$entityId->getNumericId();
+               }
 
                $uniqueKeyFields = array( 'term_entity_type', 'term_entity_id', 
'term_language', 'term_type', 'term_text' );
 
@@ -398,12 +413,19 @@
 
                $dbw = $this->getConnection( DB_MASTER );
 
+               $conditions = [ 'term_entity_type' => 
$entityId->getEntityType() ];
+               if ( $this->hasFullEntityIdColumn ) {
+                       $numericEntityId = $dbw->addQuotes( 
$entityId->getNumericId() );
+                       $fullEntityId = $dbw->addQuotes( 
$entityId->getSerialization() );
+                       $conditions[] = "(term_entity_id = {$numericEntityId}" .
+                                              "OR term_full_entity_id = 
{$fullEntityId})";
+               } else {
+                       $conditions['term_entity_id'] = 
$entityId->getNumericId();
+               }
+
                $success = $dbw->delete(
                        $this->tableName,
-                       array(
-                               'term_entity_id' => $entityId->getNumericId(),
-                               'term_entity_type' => $entityId->getEntityType()
-                       ),
+                       $conditions,
                        __METHOD__
                );
 
@@ -494,7 +516,8 @@
                }
 
                $entityType = null;
-               $numericIds = array();
+               $numericIds = [];
+               $fullIds = [];
 
                foreach ( $entityIds as $id ) {
                        if ( $entityType === null ) {
@@ -506,12 +529,16 @@
                        $this->assertIsNumericEntityId( $id );
                        /** @var Int32EntityId $id */
                        $numericIds[] = $id->getNumericId();
+                       $fullIds[] = $id->getSerialization();
                }
 
-               $conditions = array(
-                       'term_entity_type' => $entityType,
-                       'term_entity_id' => $numericIds,
-               );
+               $conditions = [ 'term_entity_type' => $entityType ];
+
+               if ( $this->canReadFullEntityIdColumn ) {
+                       $conditions['term_full_entity_id'] = $fullIds;
+               } else {
+                       $conditions['term_entity_id'] = $numericIds;
+               }
 
                if ( $languageCodes !== null ) {
                        $conditions['term_language'] = $languageCodes;
@@ -521,11 +548,18 @@
                        $conditions['term_type'] = $termTypes;
                }
 
+               $fields = [ 'term_entity_type', 'term_type', 'term_language', 
'term_text' ];
+               if ( $this->canReadFullEntityIdColumn ) {
+                       $fields[] = 'term_full_entity_id';
+               } else {
+                       $fields[] = 'term_entity_id';
+               }
+
                $dbr = $this->getReadDb();
 
                $res = $dbr->select(
                        $this->tableName,
-                       [ 'term_entity_type', 'term_type', 'term_language', 
'term_text', 'term_entity_id' ],
+                       $fields,
                        $conditions,
                        __METHOD__
                );
@@ -584,16 +618,15 @@
                        $queryOptions['LIMIT'] = $options['LIMIT'];
                }
 
+               $fields = [ 'term_entity_type', 'term_type', 'term_language', 
'term_text', 'term_weight' ];
+               if ( $this->canReadFullEntityIdColumn ) {
+                       $fields[] = 'term_full_entity_id';
+               } else {
+                       $fields[] = 'term_entity_id';
+               }
                $rows = $dbr->select(
                        $this->tableName,
-                       [
-                               'term_entity_type',
-                               'term_type',
-                               'term_language',
-                               'term_text',
-                               'term_entity_id',
-                               'term_weight'
-                       ],
+                       $fields,
                        array( $dbr->makeList( $termConditions, LIST_OR ) ),
                        __METHOD__,
                        $queryOptions
@@ -674,8 +707,13 @@
                                $row->term_text .
                                $row->term_type .
                                $row->term_language .
-                               $row->term_entity_type .
-                               $row->term_entity_id;
+                               $row->term_entity_type;
+                       if ( $this->canReadFullEntityIdColumn ) {
+                               $sortData[$key]['string'] .= 
$row->term_full_entity_id;
+                       } else {
+                               $sortData[$key]['string'] .= 
$row->term_entity_id;
+                       }
+
                        $rowMap[$key] = $row;
                }
 
@@ -828,9 +866,16 @@
                                $termRow->term_entity_type,
                                $termRow->term_entity_id
                        );
+               } elseif ( isset( $termRow->term_full_entity_id ) ) {
+                       // TODO: I don't think this would work
+                       return $this->entityIdComposer->composeEntityId(
+                               $this->repositoryName,
+                               $termRow->term_entity_type,
+                               $termRow->term_full_entity_id
+                       );
+               } else {
+                       return null;
                }
-
-               return null;
        }
 
        /**
@@ -929,7 +974,12 @@
                $where['L.term_type'] = TermIndexEntry::TYPE_LABEL;
                $where['D.term_type'] = TermIndexEntry::TYPE_DESCRIPTION;
 
-               $where[] = 'D.term_entity_id=' . 'L.term_entity_id';
+               if ( $this->canReadFullEntityIdColumn ) {
+                       $where[] = 'D.term_full_entity_id=' . 
'L.term_full_entity_id';
+               } else {
+                       $where[] = 'D.term_entity_id=' . 'L.term_entity_id';
+               }
+
                $where[] = 'D.term_entity_type=' . 'L.term_entity_type';
 
                $termConditions = array();
diff --git a/lib/tests/phpunit/Store/Sql/TermSqlIndexTest.php 
b/lib/tests/phpunit/Store/Sql/TermSqlIndexTest.php
index adf4c7c..8a8285f 100644
--- a/lib/tests/phpunit/Store/Sql/TermSqlIndexTest.php
+++ b/lib/tests/phpunit/Store/Sql/TermSqlIndexTest.php
@@ -72,10 +72,10 @@
                        new StringNormalizer(),
                        new EntityIdComposer( [
                                'item' => function( $repositoryName, 
$uniquePart ) {
-                                       return new ItemId( 'Q' . $uniquePart );
+                                       return new ItemId( $uniquePart );
                                },
                                'property' => function( $repositoryName, 
$uniquePart ) {
-                                       return new PropertyId( 'P' . 
$uniquePart );
+                                       return new PropertyId( $uniquePart );
                                },
                        ] )
                );
@@ -132,10 +132,12 @@
                        new StringNormalizer(),
                        new EntityIdComposer( [
                                'item' => function( $repositoryName, 
$uniquePart ) {
-                                       return 
ItemId::newFromRepositoryAndNumber( $repositoryName, $uniquePart );
+                               $numericId = ltrim( $uniquePart, 'Q' );
+                                       return 
ItemId::newFromRepositoryAndNumber( $repositoryName, $numericId );
                                },
                                'property' => function( $repositoryName, 
$uniquePart ) {
-                                       return 
PropertyId::newFromRepositoryAndNumber( $repositoryName, $uniquePart );
+                                       $numericId = ltrim( $uniquePart, 'P' );
+                                       return 
PropertyId::newFromRepositoryAndNumber( $repositoryName, $numericId );
                                },
                        ] ),
                        false,
diff --git a/repo/includes/Store/Sql/SqlEntitiesWithoutTermFinder.php 
b/repo/includes/Store/Sql/SqlEntitiesWithoutTermFinder.php
index d60dad2..6141095 100644
--- a/repo/includes/Store/Sql/SqlEntitiesWithoutTermFinder.php
+++ b/repo/includes/Store/Sql/SqlEntitiesWithoutTermFinder.php
@@ -39,18 +39,28 @@
        private $entityTypeToPrefixMap;
 
        /**
+        * Whether it's possible to read from term_full_entity_id
+        *
+        * @var bool
+        */
+       private $canReadFullEntityIdColumn;
+
+       /**
         * @param EntityIdParser $entityIdParser
         * @param EntityNamespaceLookup $entityNamespaceLookup
         * @param string[] $entityTypeToPrefixMap Maps (supported) entity types 
to their prefix (before the numerical part).
+        * @param bool $canReadFullEntityIdColumn Whether it can read 
term_entity_full_id or not
         */
        public function __construct(
                EntityIdParser $entityIdParser,
                EntityNamespaceLookup $entityNamespaceLookup,
-               array $entityTypeToPrefixMap
+               array $entityTypeToPrefixMap,
+               $canReadFullEntityIdColumn = true
        ) {
                $this->entityIdParser = $entityIdParser;
                $this->entityNamespaceLookup = $entityNamespaceLookup;
                $this->entityTypeToPrefixMap = $entityTypeToPrefixMap;
+               $this->canReadFullEntityIdColumn = $canReadFullEntityIdColumn;
 
                Assert::parameterElementType( 'string', $entityTypeToPrefixMap, 
'$entityTypeToPrefixMap' );
        }
@@ -147,12 +157,16 @@
         * @return string
         */
        private function getConditionsForEntityType( IDatabase $dbr, 
$entityType ) {
-               $prefix = $dbr->addQuotes( $this->entityTypeToPrefixMap[ 
$entityType ] );
                $conditions = [
-                       'term_entity_id = ' . $dbr->strreplace( 'page_title', 
"$prefix", "''" ),
                        'term_entity_type' => $entityType,
                        'page_namespace' => 
$this->entityNamespaceLookup->getEntityNamespace( $entityType )
                ];
+               if ( $this->canReadFullEntityIdColumn ) {
+                       $conditions['term_full_entity_id'] = 'page_title';
+               } else {
+                       $prefix = $dbr->addQuotes( 
$this->entityTypeToPrefixMap[ $entityType ] );
+                       $conditions[] = 'term_entity_id = ' . $dbr->strreplace( 
'page_title', "$prefix", "''" );
+               }
 
                return $dbr->makeList( $conditions, IDatabase::LIST_AND );
        }
diff --git 
a/repo/tests/phpunit/includes/Store/Sql/SqlEntitiesWithoutTermFinderTest.php 
b/repo/tests/phpunit/includes/Store/Sql/SqlEntitiesWithoutTermFinderTest.php
index ce3354b..d2c427c 100644
--- a/repo/tests/phpunit/includes/Store/Sql/SqlEntitiesWithoutTermFinderTest.php
+++ b/repo/tests/phpunit/includes/Store/Sql/SqlEntitiesWithoutTermFinderTest.php
@@ -59,13 +59,13 @@
                        );
 
                        $termsRows = [];
-                       $termsRows[] = $this->getTermRow( 100, 'item', 'en', 
'label' );
-                       $termsRows[] = $this->getTermRow( 100, 'item', 'en', 
'description' );
-                       $termsRows[] = $this->getTermRow( 102, 'item', 'es', 
'label' );
-                       $termsRows[] = $this->getTermRow( 102, 'item', 'en', 
'alias' );
-                       $termsRows[] = $this->getTermRow( 102, 'item', 'es', 
'alias' );
-                       $termsRows[] = $this->getTermRow( 102, 'property', 
'de', 'description' );
-                       $termsRows[] = $this->getTermRow( 103, 'property', 
'de', 'label' );
+                       $termsRows[] = $this->getTermRow( 100, 'Q100', 'item', 
'en', 'label' );
+                       $termsRows[] = $this->getTermRow( 100, 'Q100', 'item', 
'en', 'description' );
+                       $termsRows[] = $this->getTermRow( 102, 'Q102', 'item', 
'es', 'label' );
+                       $termsRows[] = $this->getTermRow( 102, 'Q102', 'item', 
'en', 'alias' );
+                       $termsRows[] = $this->getTermRow( 102, 'Q102', 'item', 
'es', 'alias' );
+                       $termsRows[] = $this->getTermRow( 102, 'P102', 
'property', 'de', 'description' );
+                       $termsRows[] = $this->getTermRow( 103, 'P103', 
'property', 'de', 'label' );
 
                        $dbw->insert(
                                'wb_terms',
@@ -211,9 +211,16 @@
                ];
        }
 
-       private function getTermRow( $numericEntityId, $entityType, 
$languageCode, $termType ) {
+       private function getTermRow(
+               $numericEntityId,
+               $fullEntityId,
+               $entityType,
+               $languageCode,
+               $termType
+       ) {
                return [
                        'term_entity_id' => $numericEntityId,
+                       'term_full_entity_id' => $fullEntityId,
                        'term_entity_type' => $entityType,
                        'term_language' => $languageCode,
                        'term_type' => $termType,

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I958152bc099fd6a68f2fc6ea006417c548d83dd4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <[email protected]>

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

Reply via email to