1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\TermStore\MediaWiki\PackagePrivate\Util; |
4
|
|
|
|
5
|
|
|
use AppendIterator; |
6
|
|
|
use ArrayIterator; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Psr\Log\NullLogger; |
9
|
|
|
use Wikimedia\Rdbms\IDatabase; |
10
|
|
|
|
11
|
|
|
class ReplicaMasterAwareRecordIdsAcquirer { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var Database $dbw |
15
|
|
|
*/ |
16
|
|
|
private $dbw; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Database $dbr |
20
|
|
|
*/ |
21
|
|
|
private $dbr; |
22
|
|
|
|
23
|
|
|
/** @var string $table */ |
24
|
|
|
private $table; |
25
|
|
|
|
26
|
|
|
/** @var string $idColumn */ |
27
|
|
|
private $idColumn; |
28
|
|
|
|
29
|
|
|
/** @var LoggerInterface $logger */ |
30
|
|
|
private $logger; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
IDatabase $dbw, |
34
|
|
|
IDatabase $dbr, |
35
|
|
|
string $table, |
36
|
|
|
string $idColumn, |
37
|
|
|
LoggerInterface $logger = null |
38
|
|
|
) { |
39
|
|
|
$this->dbw = $dbw; |
|
|
|
|
40
|
|
|
$this->dbr = $dbr; |
|
|
|
|
41
|
|
|
$this->table = $table; |
42
|
|
|
$this->idColumn = $idColumn; |
43
|
|
|
$this->logger = $logger ?? new NullLogger(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function acquireIds( array $neededRecords ) { |
47
|
|
|
$existingRecords = $this->findExistingRecords( $this->dbr, $neededRecords ); |
48
|
|
|
$neededRecords = $this->filterNonExistingRecords( $neededRecords, $existingRecords ); |
49
|
|
|
|
50
|
|
|
while ( !empty( $neededRecords ) ) { |
51
|
|
|
$this->insertNonExistingRecords( $this->dbw, $neededRecords ); |
52
|
|
|
$existingRecords = array_merge( |
53
|
|
|
$existingRecords, |
54
|
|
|
$this->findExistingRecords( $this->dbw, $neededRecords ) |
55
|
|
|
); |
56
|
|
|
$neededRecords = $this->filterNonExistingRecords( $neededRecords, $existingRecords ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $existingRecords; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function findExistingRecords( IDatabase $dbr, array $neededRecords ) { |
63
|
|
|
$recordsSelectConditions = array_map( function ( $record ) use ( $dbr ) { |
64
|
|
|
return $dbr->makeList( $record, IDatabase::LIST_AND ); |
65
|
|
|
}, $neededRecords ); |
66
|
|
|
|
67
|
|
|
$selectColumns = array_keys( $neededRecords[0] ); |
68
|
|
|
$selectColumns[] = $this->idColumn; |
69
|
|
|
|
70
|
|
|
$existingRows = $dbr->select( |
71
|
|
|
$this->table, |
72
|
|
|
$selectColumns, |
73
|
|
|
$dbr->makeList( $recordsSelectConditions, IDatabase::LIST_OR ) |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$existingRecords = []; |
77
|
|
|
foreach ( $existingRows as $row ) { |
78
|
|
|
$existingRecord = []; |
79
|
|
|
foreach ( $selectColumns as $column ) { |
80
|
|
|
$existingRecord[$column] = $row->$column; |
81
|
|
|
} |
82
|
|
|
$existingRecords[] = $existingRecord; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $existingRecords; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function insertNonExistingRecords( IDatabase $dbw, array $neededRecords ) { |
|
|
|
|
89
|
|
|
$uniqueRecords = []; |
90
|
|
|
foreach ( $neededRecords as $record ) { |
91
|
|
|
$recordHash = $this->calcRecordHash( $record ); |
92
|
|
|
if ( !isset( $uniqueRecords[$recordHash] ) ) { |
93
|
|
|
$uniqueRecords[$recordHash] = $record; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->dbw->insert( $this->table, array_values( $uniqueRecords ) ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function filterNonExistingRecords( $neededRecords, $existingRecords ) { |
101
|
|
|
$existingRecordsHashes = []; |
102
|
|
|
foreach ( $existingRecords as $record ) { |
103
|
|
|
unset( $record[$this->idColumn] ); |
104
|
|
|
$recordHash = $this->calcRecordHash( $record ); |
105
|
|
|
$existingRecordsHashes[$recordHash] = true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$nonExistingRecords = []; |
109
|
|
|
foreach ( $neededRecords as $record ) { |
110
|
|
|
$recordHash = $this->calcRecordHash( $record ); |
111
|
|
|
|
112
|
|
|
if ( !isset( $existingRecordsHashes[$recordHash] ) ) { |
113
|
|
|
$nonExistingRecords[] = $record; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $nonExistingRecords; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function calcRecordHash( $record ) { |
121
|
|
|
ksort( $record ); |
122
|
|
|
return md5( serialize( $record ) ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..