| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace Wikimedia\DependencyStore; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use Wikimedia\Rdbms\IDatabase; |
| 25 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 26 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class SqlModuleDependencyStore extends DependencyStore { |
| 42 |
|
| 43 |
private $lb; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function __construct( ILoadBalancer $lb ) { |
| 49 |
$this->lb = $lb; |
| 50 |
} |
| 51 |
|
| 52 |
public function retrieveMulti( $type, array $entities ) { |
| 53 |
$dbr = $this->getReplicaDb(); |
| 54 |
|
| 55 |
$depsBlobByEntity = $this->fetchDependencyBlobs( $entities, $dbr ); |
| 56 |
|
| 57 |
$storedPathsByEntity = []; |
| 58 |
foreach ( $depsBlobByEntity as $entity => $depsBlob ) { |
| 59 |
$storedPathsByEntity[$entity] = json_decode( $depsBlob, true ); |
| 60 |
} |
| 61 |
|
| 62 |
$results = []; |
| 63 |
foreach ( $entities as $entity ) { |
| 64 |
$paths = $storedPathsByEntity[$entity] ?? []; |
| 65 |
$results[$entity] = $this->newEntityDependencies( $paths, null ); |
| 66 |
} |
| 67 |
|
| 68 |
return $results; |
| 69 |
} |
| 70 |
|
| 71 |
public function storeMulti( $type, array $dataByEntity, $ttl ) { |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
if ( !$dataByEntity ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
$dbw = $this->getPrimaryDb(); |
| 80 |
$depsBlobByEntity = $this->fetchDependencyBlobs( array_keys( $dataByEntity ), $dbw ); |
| 81 |
|
| 82 |
$rows = []; |
| 83 |
foreach ( $dataByEntity as $entity => $data ) { |
| 84 |
[ $module, $variant ] = $this->getEntityNameComponents( $entity ); |
| 85 |
if ( !is_array( $data[self::KEY_PATHS] ) ) { |
| 86 |
throw new InvalidArgumentException( "Invalid entry for '$entity'" ); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
$paths = array_values( array_unique( $data[self::KEY_PATHS] ) ); |
| 91 |
sort( $paths, SORT_STRING ); |
| 92 |
$blob = json_encode( $paths, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
| 93 |
|
| 94 |
$existingBlob = $depsBlobByEntity[$entity] ?? null; |
| 95 |
if ( $blob !== $existingBlob ) { |
| 96 |
$rows[] = [ |
| 97 |
'md_module' => $module, |
| 98 |
'md_skin' => $variant, |
| 99 |
'md_deps' => $blob |
| 100 |
]; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
foreach ( $rows as $row ) { |
| 107 |
$dbw->newInsertQueryBuilder() |
| 108 |
->insertInto( 'module_deps' ) |
| 109 |
->row( $row ) |
| 110 |
->onDuplicateKeyUpdate() |
| 111 |
->uniqueIndexFields( [ 'md_module', 'md_skin' ] ) |
| 112 |
->set( [ 'md_deps' => $row['md_deps'] ] ) |
| 113 |
->caller( __METHOD__ )->execute(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public function remove( $type, $entities ) { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
if ( !$entities ) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
$dbw = $this->getPrimaryDb(); |
| 126 |
$disjunctionConds = []; |
| 127 |
foreach ( (array)$entities as $entity ) { |
| 128 |
[ $module, $variant ] = $this->getEntityNameComponents( $entity ); |
| 129 |
$disjunctionConds[] = $dbw |
| 130 |
->expr( 'md_skin', '=', $variant ) |
| 131 |
->and( 'md_module', '=', $module ); |
| 132 |
} |
| 133 |
|
| 134 |
if ( $disjunctionConds ) { |
| 135 |
$dbw->newDeleteQueryBuilder() |
| 136 |
->deleteFrom( 'module_deps' ) |
| 137 |
->where( $dbw->orExpr( $disjunctionConds ) ) |
| 138 |
->caller( __METHOD__ )->execute(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
private function fetchDependencyBlobs( array $entities, IReadableDatabase $db ) { |
| 148 |
$modulesByVariant = []; |
| 149 |
foreach ( $entities as $entity ) { |
| 150 |
[ $module, $variant ] = $this->getEntityNameComponents( $entity ); |
| 151 |
$modulesByVariant[$variant][] = $module; |
| 152 |
} |
| 153 |
|
| 154 |
$disjunctionConds = []; |
| 155 |
foreach ( $modulesByVariant as $variant => $modules ) { |
| 156 |
$disjunctionConds[] = $db |
| 157 |
->expr( 'md_skin', '=', $variant ) |
| 158 |
->and( 'md_module', '=', $modules ); |
| 159 |
} |
| 160 |
|
| 161 |
$depsBlobByEntity = []; |
| 162 |
|
| 163 |
if ( $disjunctionConds ) { |
| 164 |
$res = $db->newSelectQueryBuilder() |
| 165 |
->select( [ 'md_module', 'md_skin', 'md_deps' ] ) |
| 166 |
->from( 'module_deps' ) |
| 167 |
->where( $db->orExpr( $disjunctionConds ) ) |
| 168 |
->caller( __METHOD__ )->fetchResultSet(); |
| 169 |
|
| 170 |
foreach ( $res as $row ) { |
| 171 |
$entity = "{$row->md_module}|{$row->md_skin}"; |
| 172 |
$depsBlobByEntity[$entity] = $row->md_deps; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return $depsBlobByEntity; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
private function getReplicaDb() { |
| 183 |
return $this->lb |
| 184 |
->getConnection( DB_REPLICA, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT ); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
private function getPrimaryDb() { |
| 191 |
return $this->lb |
| 192 |
->getConnection( DB_PRIMARY, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT ); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private function getEntityNameComponents( $entity ) { |
| 200 |
$parts = explode( '|', $entity, 2 ); |
| 201 |
if ( count( $parts ) !== 2 ) { |
| 202 |
throw new InvalidArgumentException( "Invalid module entity '$entity'" ); |
| 203 |
} |
| 204 |
|
| 205 |
return $parts; |
| 206 |
} |
| 207 |
} |