123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- <?php
- /**
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- */
- namespace MediaWiki\Storage;
- use IExpiringStore;
- use Psr\Log\LoggerInterface;
- use WANObjectCache;
- use Wikimedia\Assert\Assert;
- use Wikimedia\Rdbms\Database;
- use Wikimedia\Rdbms\IDatabase;
- use Wikimedia\Rdbms\LoadBalancer;
- /**
- * @author Addshore
- * @since 1.31
- */
- class NameTableStore {
- /** @var LoadBalancer */
- private $loadBalancer;
- /** @var WANObjectCache */
- private $cache;
- /** @var LoggerInterface */
- private $logger;
- /** @var string[] */
- private $tableCache = null;
- /** @var bool|string */
- private $wikiId = false;
- /** @var int */
- private $cacheTTL;
- /** @var string */
- private $table;
- /** @var string */
- private $idField;
- /** @var string */
- private $nameField;
- /** @var null|callable */
- private $normalizationCallback = null;
- /**
- * @param LoadBalancer $dbLoadBalancer A load balancer for acquiring database connections
- * @param WANObjectCache $cache A cache manager for caching data
- * @param LoggerInterface $logger
- * @param string $table
- * @param string $idField
- * @param string $nameField
- * @param callable $normalizationCallback Normalization to be applied to names before being
- * saved or queried. This should be a callback that accepts and returns a single string.
- * @param bool|string $wikiId The ID of the target wiki database. Use false for the local wiki.
- */
- public function __construct(
- LoadBalancer $dbLoadBalancer,
- WANObjectCache $cache,
- LoggerInterface $logger,
- $table,
- $idField,
- $nameField,
- callable $normalizationCallback = null,
- $wikiId = false
- ) {
- $this->loadBalancer = $dbLoadBalancer;
- $this->cache = $cache;
- $this->logger = $logger;
- $this->table = $table;
- $this->idField = $idField;
- $this->nameField = $nameField;
- $this->normalizationCallback = $normalizationCallback;
- $this->wikiId = $wikiId;
- $this->cacheTTL = IExpiringStore::TTL_MONTH;
- }
- /**
- * @param int $index A database index, like DB_MASTER or DB_REPLICA
- * @param int $flags Database connection flags
- *
- * @return IDatabase
- */
- private function getDBConnection( $index, $flags = 0 ) {
- return $this->loadBalancer->getConnection( $index, [], $this->wikiId, $flags );
- }
- private function getCacheKey() {
- return $this->cache->makeKey( 'NameTableSqlStore', $this->table, $this->wikiId );
- }
- /**
- * @param string $name
- * @return string
- */
- private function normalizeName( $name ) {
- if ( $this->normalizationCallback === null ) {
- return $name;
- }
- return call_user_func( $this->normalizationCallback, $name );
- }
- /**
- * Acquire the id of the given name.
- * This creates a row in the table if it doesn't already exist.
- *
- * @param string $name
- * @throws NameTableAccessException
- * @return int
- */
- public function acquireId( $name ) {
- Assert::parameterType( 'string', $name, '$name' );
- $name = $this->normalizeName( $name );
- $table = $this->getTableFromCachesOrReplica();
- $searchResult = array_search( $name, $table, true );
- if ( $searchResult === false ) {
- $id = $this->store( $name );
- if ( $id === null ) {
- // RACE: $name was already in the db, probably just inserted, so load from master
- // Use DBO_TRX to avoid missing inserts due to other threads or REPEATABLE-READs
- $table = $this->loadTable(
- $this->getDBConnection( DB_MASTER, LoadBalancer::CONN_TRX_AUTOCOMMIT )
- );
- $searchResult = array_search( $name, $table, true );
- if ( $searchResult === false ) {
- // Insert failed due to IGNORE flag, but DB_MASTER didn't give us the data
- $m = "No insert possible but master didn't give us a record for " .
- "'{$name}' in '{$this->table}'";
- $this->logger->error( $m );
- throw new NameTableAccessException( $m );
- }
- $this->purgeWANCache(
- function () {
- $this->cache->reap( $this->getCacheKey(), INF );
- }
- );
- } else {
- $table[$id] = $name;
- $searchResult = $id;
- // As store returned an ID we know we inserted so delete from WAN cache
- $this->purgeWANCache(
- function () {
- $this->cache->delete( $this->getCacheKey() );
- }
- );
- }
- $this->tableCache = $table;
- }
- return $searchResult;
- }
- /**
- * Get the id of the given name.
- * If the name doesn't exist this will throw.
- * This should be used in cases where we believe the name already exists or want to check for
- * existence.
- *
- * @param string $name
- * @throws NameTableAccessException The name does not exist
- * @return int Id
- */
- public function getId( $name ) {
- Assert::parameterType( 'string', $name, '$name' );
- $name = $this->normalizeName( $name );
- $table = $this->getTableFromCachesOrReplica();
- $searchResult = array_search( $name, $table, true );
- if ( $searchResult !== false ) {
- return $searchResult;
- }
- throw NameTableAccessException::newFromDetails( $this->table, 'name', $name );
- }
- /**
- * Get the name of the given id.
- * If the id doesn't exist this will throw.
- * This should be used in cases where we believe the id already exists.
- *
- * Note: Calls to this method will result in a master select for non existing IDs.
- *
- * @param int $id
- * @throws NameTableAccessException The id does not exist
- * @return string name
- */
- public function getName( $id ) {
- Assert::parameterType( 'integer', $id, '$id' );
- $table = $this->getTableFromCachesOrReplica();
- if ( array_key_exists( $id, $table ) ) {
- return $table[$id];
- }
- $table = $this->cache->getWithSetCallback(
- $this->getCacheKey(),
- $this->cacheTTL,
- function ( $oldValue, &$ttl, &$setOpts ) use ( $id ) {
- // Check if cached value is up-to-date enough to have $id
- if ( is_array( $oldValue ) && array_key_exists( $id, $oldValue ) ) {
- // Completely leave the cache key alone
- $ttl = WANObjectCache::TTL_UNCACHEABLE;
- // Use the old value
- return $oldValue;
- }
- // Regenerate from replica DB, and master DB if needed
- foreach ( [ DB_REPLICA, DB_MASTER ] as $source ) {
- // Log a fallback to master
- if ( $source === DB_MASTER ) {
- $this->logger->info(
- __METHOD__ . 'falling back to master select from ' .
- $this->table . ' with id ' . $id
- );
- }
- $db = $this->getDBConnection( $source );
- $cacheSetOpts = Database::getCacheSetOptions( $db );
- $table = $this->loadTable( $db );
- if ( array_key_exists( $id, $table ) ) {
- break; // found it
- }
- }
- // Use the value from last source checked
- $setOpts += $cacheSetOpts;
- return $table;
- },
- [ 'minAsOf' => INF ] // force callback run
- );
- $this->tableCache = $table;
- if ( array_key_exists( $id, $table ) ) {
- return $table[$id];
- }
- throw NameTableAccessException::newFromDetails( $this->table, 'id', $id );
- }
- /**
- * Get the whole table, in no particular order as a map of ids to names.
- * This method could be subject to DB or cache lag.
- *
- * @return string[] keys are the name ids, values are the names themselves
- * Example: [ 1 => 'foo', 3 => 'bar' ]
- */
- public function getMap() {
- return $this->getTableFromCachesOrReplica();
- }
- /**
- * @return string[]
- */
- private function getTableFromCachesOrReplica() {
- if ( $this->tableCache !== null ) {
- return $this->tableCache;
- }
- $table = $this->cache->getWithSetCallback(
- $this->getCacheKey(),
- $this->cacheTTL,
- function ( $oldValue, &$ttl, &$setOpts ) {
- $dbr = $this->getDBConnection( DB_REPLICA );
- $setOpts += Database::getCacheSetOptions( $dbr );
- return $this->loadTable( $dbr );
- }
- );
- $this->tableCache = $table;
- return $table;
- }
- /**
- * Reap the WANCache entry for this table.
- *
- * @param callable $purgeCallback callback to 'purge' the WAN cache
- */
- private function purgeWANCache( $purgeCallback ) {
- // If the LB has no DB changes don't both with onTransactionPreCommitOrIdle
- if ( !$this->loadBalancer->hasOrMadeRecentMasterChanges() ) {
- $purgeCallback();
- return;
- }
- $this->getDBConnection( DB_MASTER )
- ->onTransactionPreCommitOrIdle( $purgeCallback, __METHOD__ );
- }
- /**
- * Gets the table from the db
- *
- * @param IDatabase $db
- *
- * @return string[]
- */
- private function loadTable( IDatabase $db ) {
- $result = $db->select(
- $this->table,
- [
- 'id' => $this->idField,
- 'name' => $this->nameField
- ],
- [],
- __METHOD__,
- [ 'ORDER BY' => 'id' ]
- );
- $assocArray = [];
- foreach ( $result as $row ) {
- $assocArray[$row->id] = $row->name;
- }
- return $assocArray;
- }
- /**
- * Stores the given name in the DB, returning the ID when an insert occurs.
- *
- * @param string $name
- * @return int|null int if we know the ID, null if we don't
- */
- private function store( $name ) {
- Assert::parameterType( 'string', $name, '$name' );
- Assert::parameter( $name !== '', '$name', 'should not be an empty string' );
- // Note: this is only called internally so normalization of $name has already occurred.
- $dbw = $this->getDBConnection( DB_MASTER );
- $dbw->insert(
- $this->table,
- [ $this->nameField => $name ],
- __METHOD__,
- [ 'IGNORE' ]
- );
- if ( $dbw->affectedRows() === 0 ) {
- $this->logger->info(
- 'Tried to insert name into table ' . $this->table . ', but value already existed.'
- );
- return null;
- }
- return $dbw->insertId();
- }
- }
|