39 private $loadBalancer;
48 private $tableCache =
null;
63 private $normalizationCallback;
65 private $insertCallback;
88 LoggerInterface $logger,
92 ?callable $normalizationCallback =
null,
94 ?callable $insertCallback =
null
96 $this->loadBalancer = $dbLoadBalancer;
97 $this->cache = $cache;
98 $this->logger = $logger;
99 $this->table = $table;
100 $this->idField = $idField;
101 $this->nameField = $nameField;
102 $this->normalizationCallback = $normalizationCallback;
103 $this->domain = $dbDomain;
104 $this->cacheTTL = BagOStuff::TTL_MONTH;
105 $this->insertCallback = $insertCallback;
113 private function getDBConnection( $index, $flags = 0 ) {
114 return $this->loadBalancer->getConnection( $index, [], $this->domain, $flags );
125 private function getCacheKey() {
126 return $this->cache->makeGlobalKey(
129 $this->loadBalancer->resolveDomainID( $this->domain )
137 private function normalizeName( $name ) {
138 if ( $this->normalizationCallback ===
null ) {
141 return call_user_func( $this->normalizationCallback, $name );
160 $name = $this->normalizeName( $name );
162 $table = $this->getTableFromCachesOrReplica();
163 $searchResult = array_search( $name, $table,
true );
164 if ( $searchResult ===
false ) {
165 $id = $this->store( $name );
167 if ( isset( $table[$id] ) ) {
173 $m =
"Got ID $id for '$name' from insert"
174 .
" into '{$this->table}', but ID $id was previously associated with"
175 .
" the name '{$table[$id]}'. Overriding the old value, which presumably"
176 .
" has been removed from the database due to a transaction rollback.";
177 $this->logger->warning( $m );
183 $this->tableCache = $table;
186 return $searchResult;
202 $dbw = $this->getDBConnection(
DB_PRIMARY, $connFlags );
203 $this->tableCache = $this->loadTable( $dbw );
204 $dbw->onTransactionPreCommitOrIdle(
function () {
205 $this->cache->delete( $this->getCacheKey() );
208 return $this->tableCache;
221 public function getId(
string $name ) {
222 $name = $this->normalizeName( $name );
224 $table = $this->getTableFromCachesOrReplica();
225 $searchResult = array_search( $name, $table,
true );
227 if ( $searchResult !==
false ) {
228 return $searchResult;
246 $table = $this->getTableFromCachesOrReplica();
247 if ( array_key_exists( $id, $table ) ) {
252 $table = $this->cache->getWithSetCallback(
253 $this->getCacheKey(),
255 function ( $oldValue, &$ttl, &$setOpts ) use ( $id, $fname ) {
257 if ( is_array( $oldValue ) && array_key_exists( $id, $oldValue ) ) {
259 $ttl = WANObjectCache::TTL_UNCACHEABLE;
268 $fname .
' falling back to primary select from ' .
269 $this->table .
' with id ' . $id
272 $db = $this->getDBConnection(
$source );
274 $table = $this->loadTable( $db );
275 if ( array_key_exists( $id, $table ) ) {
280 $setOpts += $cacheSetOpts;
287 $this->tableCache = $table;
289 if ( array_key_exists( $id, $table ) ) {
304 return $this->getTableFromCachesOrReplica();
310 private function getTableFromCachesOrReplica() {
311 if ( $this->tableCache !==
null ) {
312 return $this->tableCache;
315 $table = $this->cache->getWithSetCallback(
316 $this->getCacheKey(),
318 function ( $oldValue, &$ttl, &$setOpts ) {
321 return $this->loadTable( $dbr );
325 $this->tableCache = $table;
336 private function loadTable( IReadableDatabase $db ) {
337 $result = $db->newSelectQueryBuilder()
339 'id' => $this->idField,
340 'name' => $this->nameField
342 ->from( $this->table )
344 ->caller( __METHOD__ )->fetchResultSet();
347 foreach ( $result as $row ) {
348 $assocArray[(int)$row->id] = $row->name;
360 private function store(
string $name ) {
361 Assert::parameter( $name !==
'',
'$name',
'should not be an empty string' );
364 $dbw = $this->getDBConnection(
DB_PRIMARY, ILoadBalancer::CONN_TRX_AUTOCOMMIT );
366 $dbw->newInsertQueryBuilder()
367 ->insertInto( $this->table )
369 ->row( $this->getFieldsToStore( $name ) )
370 ->caller( __METHOD__ )->execute();
372 if ( $dbw->affectedRows() > 0 ) {
373 $id = $dbw->insertId();
375 $dbw->onTransactionPreCommitOrIdle(
377 $this->cache->delete( $this->getCacheKey() );
386 'Tried to insert name into table ' . $this->table .
', but value already existed.'
394 $id = $dbw->newSelectQueryBuilder()
395 ->select( [
'id' => $this->idField ] )
396 ->from( $this->table )
397 ->where( [ $this->nameField => $name ] )
398 ->caller( __METHOD__ )->fetchField();
400 if ( $id ===
false ) {
402 $m =
"No insert possible but primary DB didn't give us a record for " .
403 "'{$name}' in '{$this->table}'";
404 $this->logger->error( $m );
405 throw new NameTableAccessException( $m );
416 private function getFieldsToStore( $name, $id =
null ) {
419 $fields[$this->nameField] = $name;
421 if ( $id !==
null ) {
422 $fields[$this->idField] = $id;
425 if ( $this->insertCallback !==
null ) {
426 $fields = call_user_func( $this->insertCallback, $fields );