Passed
Pull Request — master (#11)
by Alaa
03:07
created

findExistingRecords()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 3
nc 3
nop 2
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\DBQueryError;
10
use Wikimedia\Rdbms\IDatabase;
11
12
/**
13
 * Allows acquiring ids of records in database table,
14
 * by insecting a given read-only replica database for initially
15
 * finding those records, falling back to a insert non-existing
16
 * records into a read-write master databas and getting those
17
 * ids as well from the master database.
18
 */
19
class ReplicaMasterAwareRecordIdsAcquirer {
20
21
	/**
22
	 * @var Database $dbMaster
23
	 */
24
	private $dbMaster;
25
26
	/**
27
	 * @var Database $dbReplica
28
	 */
29
	private $dbReplica;
30
31
	/** @var string $table */
32
	private $table;
33
34
	/** @var string $idColumn */
35
	private $idColumn;
36
37
	/** @var LoggerInterface $logger */
38
	private $logger;
39
40
	/**
41
	 * @param IDatabase $dbMaster master database to insert non-existing records into
42
	 * @param IDatabase $dbReplica replica database to initially query existing records in
43
	 * @param string $table the name of the table this acquirer is for
44
	 * @param string $idColumn the name of the column that contains the desired ids
45
	 * @param LoggerInterface $logger
46
	 */
47
	public function __construct(
48
		IDatabase $dbMaster,
49
		IDatabase $dbReplica,
50
		string $table,
51
		string $idColumn,
52
		LoggerInterface $logger = null
53
	) {
54
		$this->dbMaster = $dbMaster;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dbMaster of type object<Wikimedia\Rdbms\IDatabase> is incompatible with the declared type object<Wikibase\TermStor...ePrivate\Util\Database> of property $dbMaster.

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..

Loading history...
55
		$this->dbReplica = $dbReplica;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dbReplica of type object<Wikimedia\Rdbms\IDatabase> is incompatible with the declared type object<Wikibase\TermStor...ePrivate\Util\Database> of property $dbReplica.

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..

Loading history...
56
		$this->table = $table;
57
		$this->idColumn = $idColumn;
58
		$this->logger = $logger ?? new NullLogger();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger ?? new \Psr\Log\NullLogger() can also be of type object<Psr\Log\NullLogger>. However, the property $logger is declared as type object<Psr\Log\LoggerInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
	}
60
61
	/**
62
	 * Acquire ids of needed records in the table, inserting non-existing
63
	 * ones into master database.
64
	 *
65
	 * Note: this function assumes that all records given in this array specify
66
	 * the same columns. If some records specify less, more or different columns than
67
	 * the first one does, the behavior is not defined.
68
	 *
69
	 * @param array $neededRecords array of records to be looked-up or inserted.
70
	 *	Each entry in this array should an associative array of column => value pairs.
71
	 *	Example:
72
	 *	[
73
	 *		[ 'columnA' => 'valueA1', 'columnB' => 'valueB1' ],
74
	 *		[ 'columnA' => 'valueA2', 'columnB' => 'valueB2' ],
75
	 *		...
76
	 *	]
77
	 *
78
	 * @return array the array of input recrods along with their ids
79
	 *	Example:
80
	 *	[
81
	 *		[ 'columnA' => 'valueA1', 'columnB' => 'valueB1', 'idColumn' => '1' ],
82
	 *		[ 'columnA' => 'valueA2', 'columnB' => 'valueB2', 'idColumn' => '2' ],
83
	 *		...
84
	 *	]
85
	 */
86
	public function acquireIds( array $neededRecords ) {
87
		$existingRecords = $this->findExistingRecords( $this->dbReplica, $neededRecords );
88
		$neededRecords = $this->filterNonExistingRecords( $neededRecords, $existingRecords );
89
90
		while ( !empty( $neededRecords ) ) {
91
			$this->insertNonExistingRecords( $this->dbMaster, $neededRecords );
92
			$existingRecords = array_merge(
93
				$existingRecords,
94
				$this->findExistingRecords( $this->dbMaster, $neededRecords )
95
			);
96
			$neededRecords = $this->filterNonExistingRecords( $neededRecords, $existingRecords );
97
		}
98
99
		return $existingRecords;
100
	}
101
102
	private function findExistingRecords( IDatabase $db, array $neededRecords ): array {
103
		$recordsSelectConditions = array_map( function ( $record ) use ( $db ) {
104
			return $db->makeList( $record, IDatabase::LIST_AND );
105
		}, $neededRecords );
106
107
		$selectColumns = array_keys( $neededRecords[0] );
108
		$selectColumns[] = $this->idColumn;
109
110
		$existingRows = $db->select(
111
			$this->table,
112
			$selectColumns,
113
			$db->makeList( $recordsSelectConditions, IDatabase::LIST_OR )
114
		);
115
116
		$existingRecords = [];
117
		foreach ( $existingRows as $row ) {
118
			$existingRecord = [];
119
			foreach ( $selectColumns as $column ) {
120
				$existingRecord[$column] = $row->$column;
121
			}
122
			$existingRecords[] = $existingRecord;
123
		}
124
125
		return $existingRecords;
126
	}
127
128
	private function insertNonExistingRecords( IDatabase $db, array $neededRecords ) {
129
		$uniqueRecords = [];
130
		foreach ( $neededRecords as $record ) {
131
			$recordHash = $this->calcRecordHash( $record );
132
			if ( !isset( $uniqueRecords[$recordHash] ) ) {
133
				$uniqueRecords[$recordHash] = $record;
134
			}
135
		}
136
137
		try {
138
			$db->insert( $this->table, array_values( $uniqueRecords ) );
139
		} catch ( DBQueryError $dbError ) {
0 ignored issues
show
Bug introduced by
The class Wikimedia\Rdbms\DBQueryError does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
140
			$this->logger->info(
141
				'{method}: Inserting records {records} into {table} failed. {exception}',
142
				[
143
					'exception' => $dbError,
144
					'table' => $this->table,
145
					'records' => $uniqueRecords
146
				]
147
			);
148
		}
149
	}
150
151
	private function filterNonExistingRecords( $neededRecords, $existingRecords ): array {
152
		$existingRecordsHashes = [];
153
		foreach ( $existingRecords as $record ) {
154
			unset( $record[$this->idColumn] );
155
			$recordHash = $this->calcRecordHash( $record );
156
			$existingRecordsHashes[$recordHash] = true;
157
		}
158
159
		$nonExistingRecords = [];
160
		foreach ( $neededRecords as $record ) {
161
			$recordHash = $this->calcRecordHash( $record );
162
163
			if ( !isset( $existingRecordsHashes[$recordHash] ) ) {
164
				$nonExistingRecords[] = $record;
165
			}
166
		}
167
168
		return $nonExistingRecords;
169
	}
170
171
	private function calcRecordHash( $record ) {
172
		ksort( $record );
173
		return md5( serialize( $record ) );
174
	}
175
176
}
177