25 private $loadBalancer;
34 private $tableCache =
null;
49 private $normalizationCallback;
51 private $insertCallback;
74 LoggerInterface $logger,
78 ?callable $normalizationCallback =
null,
80 ?callable $insertCallback =
null
82 $this->loadBalancer = $dbLoadBalancer;
83 $this->cache = $cache;
84 $this->logger = $logger;
85 $this->table = $table;
86 $this->idField = $idField;
87 $this->nameField = $nameField;
88 $this->normalizationCallback = $normalizationCallback;
89 $this->domain = $dbDomain;
90 $this->cacheTTL = BagOStuff::TTL_MONTH;
91 $this->insertCallback = $insertCallback;
99 private function getDBConnection( $index, $flags = 0 ) {
100 return $this->loadBalancer->getConnection( $index, [], $this->domain, $flags );
111 private function getCacheKey() {
112 return $this->cache->makeGlobalKey(
115 $this->loadBalancer->resolveDomainID( $this->domain )
123 private function normalizeName( $name ) {
124 if ( $this->normalizationCallback ===
null ) {
127 return ( $this->normalizationCallback )( $name );
146 $name = $this->normalizeName( $name );
148 $table = $this->getTableFromCachesOrReplica();
149 $searchResult = array_search( $name, $table,
true );
150 if ( $searchResult ===
false ) {
151 $id = $this->store( $name );
153 if ( isset( $table[$id] ) ) {
159 $m =
"Got ID $id for '$name' from insert"
160 .
" into '{$this->table}', but ID $id was previously associated with"
161 .
" the name '{$table[$id]}'. Overriding the old value, which presumably"
162 .
" has been removed from the database due to a transaction rollback.";
163 $this->logger->warning( $m );
169 $this->tableCache = $table;
172 return $searchResult;
188 $dbw = $this->getDBConnection(
DB_PRIMARY, $connFlags );
189 $this->tableCache = $this->loadTable( $dbw );
190 $dbw->onTransactionPreCommitOrIdle(
function () {
191 $this->cache->delete( $this->getCacheKey() );
194 return $this->tableCache;
207 public function getId(
string $name ) {
208 $name = $this->normalizeName( $name );
210 $table = $this->getTableFromCachesOrReplica();
211 $searchResult = array_search( $name, $table,
true );
213 if ( $searchResult !==
false ) {
214 return $searchResult;
232 $table = $this->getTableFromCachesOrReplica();
233 if ( array_key_exists( $id, $table ) ) {
238 $table = $this->cache->getWithSetCallback(
239 $this->getCacheKey(),
241 function ( $oldValue, &$ttl, &$setOpts ) use ( $id, $fname ) {
243 if ( is_array( $oldValue ) && array_key_exists( $id, $oldValue ) ) {
245 $ttl = WANObjectCache::TTL_UNCACHEABLE;
254 $fname .
' falling back to primary select from ' .
255 $this->table .
' with id ' . $id
258 $db = $this->getDBConnection(
$source );
260 $table = $this->loadTable( $db );
261 if ( array_key_exists( $id, $table ) ) {
266 $setOpts += $cacheSetOpts;
273 $this->tableCache = $table;
275 if ( array_key_exists( $id, $table ) ) {
290 return $this->getTableFromCachesOrReplica();
296 private function getTableFromCachesOrReplica() {
297 if ( $this->tableCache !==
null ) {
298 return $this->tableCache;
301 $table = $this->cache->getWithSetCallback(
302 $this->getCacheKey(),
304 function ( $oldValue, &$ttl, &$setOpts ) {
307 return $this->loadTable( $dbr );
311 $this->tableCache = $table;
322 private function loadTable( IReadableDatabase $db ) {
323 $result = $db->newSelectQueryBuilder()
325 'id' => $this->idField,
326 'name' => $this->nameField
328 ->from( $this->table )
330 ->caller( __METHOD__ )->fetchResultSet();
333 foreach ( $result as $row ) {
334 $assocArray[(int)$row->id] = $row->name;
346 private function store(
string $name ) {
347 Assert::parameter( $name !==
'',
'$name',
'should not be an empty string' );
350 $dbw = $this->getDBConnection(
DB_PRIMARY, ILoadBalancer::CONN_TRX_AUTOCOMMIT );
352 $dbw->newInsertQueryBuilder()
353 ->insertInto( $this->table )
355 ->row( $this->getFieldsToStore( $name ) )
356 ->caller( __METHOD__ )->execute();
358 if ( $dbw->affectedRows() > 0 ) {
359 $id = $dbw->insertId();
361 $dbw->onTransactionPreCommitOrIdle(
363 $this->cache->delete( $this->getCacheKey() );
372 'Tried to insert name into table ' . $this->table .
', but value already existed.'
380 $id = $dbw->newSelectQueryBuilder()
381 ->select( [
'id' => $this->idField ] )
382 ->from( $this->table )
383 ->where( [ $this->nameField => $name ] )
384 ->caller( __METHOD__ )->fetchField();
386 if ( $id ===
false ) {
388 $m =
"No insert possible but primary DB didn't give us a record for " .
389 "'{$name}' in '{$this->table}'";
390 $this->logger->error( $m );
391 throw new NameTableAccessException( $m );
402 private function getFieldsToStore( $name, $id =
null ) {
405 $fields[$this->nameField] = $name;
407 if ( $id !==
null ) {
408 $fields[$this->idField] = $id;
411 if ( $this->insertCallback !==
null ) {
412 $fields = ( $this->insertCallback )( $fields );