40 private $loadBalancer;
49 private $tableCache =
null;
64 private $normalizationCallback;
66 private $insertCallback;
89 LoggerInterface $logger,
93 callable $normalizationCallback =
null,
95 callable $insertCallback =
null
97 $this->loadBalancer = $dbLoadBalancer;
99 $this->logger = $logger;
100 $this->table = $table;
101 $this->idField = $idField;
102 $this->nameField = $nameField;
103 $this->normalizationCallback = $normalizationCallback;
104 $this->domain = $dbDomain;
105 $this->cacheTTL = IExpiringStore::TTL_MONTH;
106 $this->insertCallback = $insertCallback;
114 private function getDBConnection( $index, $flags = 0 ) {
115 return $this->loadBalancer->getConnection( $index, [], $this->domain, $flags );
126 private function getCacheKey() {
127 return $this->cache->makeGlobalKey(
130 $this->loadBalancer->resolveDomainID( $this->domain )
138 private function normalizeName( $name ) {
139 if ( $this->normalizationCallback ===
null ) {
142 return call_user_func( $this->normalizationCallback, $name );
163 $name = $this->normalizeName( $name );
165 $table = $this->getTableFromCachesOrReplica();
166 $searchResult = array_search( $name, $table,
true );
167 if ( $searchResult ===
false ) {
168 $id = $this->store( $name );
169 if ( $id ===
null ) {
172 $table = $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
174 $searchResult = array_search( $name, $table,
true );
175 if ( $searchResult ===
false ) {
177 $m =
"No insert possible but primary DB didn't give us a record for " .
178 "'{$name}' in '{$this->table}'";
179 $this->logger->error( $m );
183 if ( isset( $table[$id] ) ) {
190 $m =
"Got ID $id for '$name' from insert"
191 .
" into '{$this->table}', but ID $id was previously associated with"
192 .
" the name '{$table[$id]}'. Overriding the old value, which presumably"
193 .
" has been removed from the database due to a transaction rollback.";
195 $this->logger->warning( $m );
203 $dbw->onTransactionPreCommitOrIdle(
function () {
204 $this->cache->delete( $this->getCacheKey() );
207 $this->tableCache = $table;
210 return $searchResult;
226 if ( $connFlags !== 0 && defined(
'MW_PHPUNIT_TEST' ) ) {
232 $dbw = $this->getDBConnection(
DB_PRIMARY, $connFlags );
233 $this->tableCache = $this->loadTable( $dbw );
234 $dbw->onTransactionPreCommitOrIdle(
function () {
235 $this->cache->delete( $this->getCacheKey() );
238 return $this->tableCache;
251 public function getId(
string $name ) {
252 $name = $this->normalizeName( $name );
254 $table = $this->getTableFromCachesOrReplica();
255 $searchResult = array_search( $name, $table,
true );
257 if ( $searchResult !==
false ) {
258 return $searchResult;
276 $table = $this->getTableFromCachesOrReplica();
277 if ( array_key_exists( $id, $table ) ) {
282 $table = $this->cache->getWithSetCallback(
283 $this->getCacheKey(),
285 function ( $oldValue, &$ttl, &$setOpts ) use ( $id, $fname ) {
287 if ( is_array( $oldValue ) && array_key_exists( $id, $oldValue ) ) {
289 $ttl = WANObjectCache::TTL_UNCACHEABLE;
298 $fname .
' falling back to primary select from ' .
299 $this->table .
' with id ' . $id
302 $db = $this->getDBConnection(
$source );
304 $table = $this->loadTable( $db );
305 if ( array_key_exists( $id, $table ) ) {
310 $setOpts += $cacheSetOpts;
317 $this->tableCache = $table;
319 if ( array_key_exists( $id, $table ) ) {
334 return $this->getTableFromCachesOrReplica();
340 private function getTableFromCachesOrReplica() {
341 if ( $this->tableCache !==
null ) {
342 return $this->tableCache;
345 $table = $this->cache->getWithSetCallback(
346 $this->getCacheKey(),
348 function ( $oldValue, &$ttl, &$setOpts ) {
351 return $this->loadTable(
$dbr );
355 $this->tableCache = $table;
367 private function loadTable( IDatabase $db ) {
368 $result = $db->select(
371 'id' => $this->idField,
372 'name' => $this->nameField
376 [
'ORDER BY' =>
'id' ]
380 foreach ( $result as $row ) {
381 $assocArray[$row->id] = $row->name;
393 private function store(
string $name ) {
394 Assert::parameter( $name !==
'',
'$name',
'should not be an empty string' );
400 $dbw->doAtomicSection(
402 function ( IDatabase $unused, $fname )
403 use ( $name, &$id, $dbw ) {
409 $this->getFieldsToStore( $name ),
414 if ( $dbw->affectedRows() === 0 ) {
416 'Tried to insert name into table ' . $this->table .
', but value already existed.'
422 $id = $dbw->insertId();
426 $dbw->onAtomicSectionCancel(
427 function ( $trigger, IDatabase $unused ) use ( $name, $id, $dbw ) {
428 $this->retryStore( $dbw, $name, $id );
432 IDatabase::ATOMIC_CANCELABLE
446 private function retryStore( IDatabase $dbw, $name, $id ) {
453 $dbw->doAtomicSection(
455 function ( IDatabase $unused, $fname ) use ( $name, $id, $dbw ) {
460 $this->getFieldsToStore( $name, $id ),
467 $dbw->onAtomicSectionCancel(
468 function ( $trigger, IDatabase $unused ) {
469 $this->logger->warning(
470 'Re-insertion of name into table ' . $this->table
471 .
' was rolled back. Giving up and reloading the cache.'
473 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
479 'Re-insert name into table ' . $this->table .
' after failed transaction.'
482 IDatabase::ATOMIC_CANCELABLE
484 }
catch ( TimeoutException $e ) {
486 }
catch ( Exception $ex ) {
487 $this->logger->error(
488 'Re-insertion of name into table ' . $this->table .
' failed: ' . $ex->getMessage()
502 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
511 private function getFieldsToStore( $name, $id =
null ) {
514 $fields[$this->nameField] = $name;
516 if ( $id !==
null ) {
517 $fields[$this->idField] = $id;
520 if ( $this->insertCallback !==
null ) {
521 $fields = call_user_func( $this->insertCallback, $fields );