jenkins-bot has submitted this change and it was merged.
Change subject: Remove reindexEntityIds from SqlUsageTracker and
UsageTableUpdater
......................................................................
Remove reindexEntityIds from SqlUsageTracker and UsageTableUpdater
Change-Id: I54fad7fba977c50f6da72e901c7df363ef6c3053
---
M client/includes/Usage/Sql/SqlUsageTracker.php
M client/includes/Usage/Sql/UsageTableUpdater.php
2 files changed, 47 insertions(+), 73 deletions(-)
Approvals:
Thiemo Mättig (WMDE): Looks good to me, approved
jenkins-bot: Verified
diff --git a/client/includes/Usage/Sql/SqlUsageTracker.php
b/client/includes/Usage/Sql/SqlUsageTracker.php
index 4f20421..701ccad 100644
--- a/client/includes/Usage/Sql/SqlUsageTracker.php
+++ b/client/includes/Usage/Sql/SqlUsageTracker.php
@@ -83,27 +83,13 @@
}
/**
- * Re-indexes the given list of EntityIds so that each EntityId can be
found by using its
- * string representation as a key.
+ * Returns the string serialization of an EntityId.
*
- * @param EntityId[] $entityIds
- *
- * @throws InvalidArgumentException
- * @return EntityId[]
+ * @param EntityId $entityId
+ * @return string
*/
- private function reindexEntityIds( array $entityIds ) {
- $reindexed = array();
-
- foreach ( $entityIds as $id ) {
- if ( !( $id instanceof EntityId ) ) {
- throw new InvalidArgumentException( '$entityIds
must contain EntityId objects.' );
- }
-
- $key = $id->getSerialization();
- $reindexed[$key] = $id;
- }
-
- return $reindexed;
+ private function getEntityIdSerialization( EntityId $entityId ) {
+ return $entityId->getSerialization();
}
/**
@@ -154,11 +140,13 @@
return;
}
+ $entityIds = array_map( array( $this,
'getEntityIdSerialization' ), $entities );
+
$db = $this->connectionManager->beginAtomicSection( __METHOD__
);
try {
$tableUpdater = $this->newTableUpdater( $db );
- $tableUpdater->removeEntities( $entities );
+ $tableUpdater->removeEntities( $entityIds );
$this->connectionManager->commitAtomicSection( $db,
__METHOD__ );
} catch ( Exception $ex ) {
@@ -245,9 +233,9 @@
return array();
}
- $entities = $this->reindexEntityIds( $entities );
+ $entityIds = array_map( array( $this,
'getEntityIdSerialization' ), $entities );
- $where = array( 'eu_entity_id' => array_keys( $entities ) );
+ $where = array( 'eu_entity_id' => $entityIds );
if ( !empty( $aspects ) ) {
$where['eu_aspect'] = $aspects;
@@ -273,19 +261,45 @@
/**
* @see UsageTracker::getUnusedEntities
*
- * @param EntityId[] $entities
+ * @param EntityId[] $entityIds
*
* @return EntityId[]
* @throws UsageTrackerException
*/
- public function getUnusedEntities( array $entities ) {
- if ( empty( $entities ) ) {
+ public function getUnusedEntities( array $entityIds ) {
+ if ( empty( $entityIds ) ) {
return array();
}
- $entities = $this->reindexEntityIds( $entities );
+ $entityIdsBySerialization = array();
+ $entityIdStrings = array();
- $where = array( 'eu_entity_id' => array_keys( $entities ) );
+ foreach ( $entityIds as $id ) {
+ $serialization = $this->getEntityIdSerialization( $id );
+ $entityIdStrings[] = $serialization;
+ $entityIdsBySerialization[ $serialization ] = $id;
+ }
+
+ $usedEntityIdStrings = $this->getUsedEntities( $entityIdStrings
);
+
+ $unusedEntityIdStrings = array_diff( $entityIdStrings,
$usedEntityIdStrings );
+
+ $unusedEntityIds = array_intersect_key(
+ $entityIdsBySerialization,
+ array_flip( $unusedEntityIdStrings )
+ );
+
+ return $unusedEntityIds;
+ }
+
+ /**
+ * Returns those entity ids which are used from a given set of entity
ids.
+ *
+ * @param string[] $entityIds
+ * @return string[]
+ */
+ private function getUsedEntities( array $entityIds ) {
+ $where = array( 'eu_entity_id' => $entityIds );
$db = $this->connectionManager->getReadConnection();
@@ -298,22 +312,7 @@
$this->connectionManager->releaseConnection( $db );
- $entityIds = $this->extractProperty( 'eu_entity_id', $res );
- $unused = $this->stripKeysFromList( $entityIds, $entities );
-
- return $unused;
- }
-
- /**
- * Returns a copy of $arr without all keys that are in $keys.
- *
- * @param string[] $keys
- * @param array $arr
- *
- * @return array
- */
- private function stripKeysFromList( array $keys, array $arr ) {
- return array_diff_key( $arr, array_flip( $keys ) );
+ return $this->extractProperty( 'eu_entity_id', $res );
}
/**
diff --git a/client/includes/Usage/Sql/UsageTableUpdater.php
b/client/includes/Usage/Sql/UsageTableUpdater.php
index 1309c61..77070b9 100644
--- a/client/includes/Usage/Sql/UsageTableUpdater.php
+++ b/client/includes/Usage/Sql/UsageTableUpdater.php
@@ -235,48 +235,23 @@
}
/**
- * Re-indexes the given list of EntityIds so that each EntityId can be
found by using its
- * string representation as a key.
- *
- * @param EntityId[] $entityIds
- *
- * @throws InvalidArgumentException
- * @return EntityId[]
- */
- private function reindexEntityIds( array $entityIds ) {
- $reindexed = array();
-
- foreach ( $entityIds as $id ) {
- if ( !( $id instanceof EntityId ) ) {
- throw new InvalidArgumentException( '$entityIds
must contain EntityId objects.' );
- }
-
- $key = $id->getSerialization();
- $reindexed[$key] = $id;
- }
-
- return $reindexed;
- }
-
- /**
* Removes usage tracking for the given set of entities.
* This is used typically when entities were deleted.
*
- * @param EntityId[] $entities
+ * @param string[] $entityIdStrings
*/
- public function removeEntities( array $entities ) {
- if ( empty( $entities ) ) {
+ public function removeEntities( array $entityIdStrings ) {
+ if ( empty( $entityIdStrings ) ) {
return;
}
- $entities = $this->reindexEntityIds( $entities );
- $batches = array_chunk( $entities, $this->batchSize, true );
+ $batches = array_chunk( $entityIdStrings, $this->batchSize );
foreach ( $batches as $batch ) {
$this->connection->delete(
$this->tableName,
array(
- 'eu_entity_id' => array_keys( $batch ),
+ 'eu_entity_id' => $batch,
),
__METHOD__
);
--
To view, visit https://gerrit.wikimedia.org/r/165194
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I54fad7fba977c50f6da72e901c7df363ef6c3053
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Adrian Lang <[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