MediaWiki  master
DBSerialProvider.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
12 abstract class DBSerialProvider implements SerialProvider {
14  private $numShards;
15 
22  public function __construct( $config ) {
23  $this->numShards = $config['numShards'] ?? 1;
24  }
25 
26  public function acquireIndex(): int {
27  if ( $this->numShards ) {
28  $shard = mt_rand( 0, $this->numShards - 1 );
29  } else {
30  $shard = 0;
31  }
32 
33  $dbw = $this->getDB();
34  $table = $this->getTableName();
35  $dbw->startAtomic( __METHOD__ );
36  $dbw->upsert(
37  $table,
38  [
39  'uas_shard' => $shard,
40  'uas_value' => 1,
41  ],
42  [ [ 'uas_shard' ] ],
43  [ 'uas_value=uas_value+1' ],
44  __METHOD__
45  );
46  $value = $dbw->newSelectQueryBuilder()
47  ->select( 'uas_value' )
48  ->from( $table )
49  ->where( [ 'uas_shard' => $shard ] )
50  ->caller( __METHOD__ )
51  ->fetchField();
52  $dbw->endAtomic( __METHOD__ );
53  return $value * $this->numShards + $shard;
54  }
55 
59  abstract protected function getDB();
60 
64  abstract protected function getTableName();
65 }
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition: WebStart.php:82
Base class for serial acquisition code shared between core and CentralAuth.
acquireIndex()
Acquire an integer such that it is unlikely to be used again, and return it.
Interface for serial providers for temporary users.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40