Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit b148818

Browse files
1.3
* [*] optimize code * [+] SphinxToolkitHelper { RTIndexGetStatus(), RTIndexOptimize(), RTIndexTruncate(), RTIndexCheckExist() } static methods
1 parent db00cb8 commit b148818

File tree

3 files changed

+82
-24
lines changed

3 files changed

+82
-24
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"php": ">=7",
1616
"ext-mbstring": "*",
1717
"ext-pdo": "*",
18+
"ext-json": "*",
1819
"foolz/sphinxql-query-builder": "^2.1",
1920
"psr/log": "^1.1",
2021
"karelwintersky/arris.toolkit.cli-console": "^0.9.0"

src/SphinxToolkit.php

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public function __construct(PDO $mysql_connection, PDO $sphinx_connection)
4242
$this->sphinx_connection = $sphinx_connection;
4343
}
4444

45-
4645
public function setRebuildIndexOptions(array $options = []):array
4746
{
4847
// на самом деле разворачиваем опции с установкой дефолтов
@@ -74,16 +73,16 @@ public function rebuildAbstractIndex(string $mysql_table, string $sphinx_index,
7473
$sphinx_connection = $this->sphinx_connection;
7574

7675
// проверяем, существует ли индекс
77-
if (!$this->checkIndexExist($sphinx_index))
76+
if (! SphinxToolkitHelper::RTIndexCheckExist($this->sphinx_connection, $sphinx_index))
7877
throw new Exception("`{$sphinx_index}` not present", 1);
7978

8079
$chunk_size = $this->rai_options['chunk_length'];
8180

8281
// truncate
83-
$sphinx_connection->query("TRUNCATE RTINDEX {$sphinx_index} WITH RECONFIGURE");
82+
SphinxToolkitHelper::RTIndexTruncate($sphinx_connection, $sphinx_index);
8483

8584
// get total count
86-
$total_count = $this->mysql_GetRowCount($mysql_connection, $mysql_table, $condition);
85+
$total_count = SphinxToolkitHelper::MySQL_GetRowsCount($mysql_connection, $mysql_table, $condition);
8786
$total_updated = 0;
8887

8988
if ($this->rai_options['log_before_index'])
@@ -152,14 +151,14 @@ public function rebuildAbstractIndexMVA(string $mysql_table, string $sphinx_inde
152151
$chunk_size = $this->rai_options['chunk_length'];
153152

154153
// проверяем, существует ли индекс
155-
if (!$this->checkIndexExist($sphinx_index))
154+
if (! SphinxToolkitHelper::RTIndexCheckExist($this->sphinx_connection, $sphinx_index))
156155
throw new Exception("`{$sphinx_index}` not present", 1);
157156

158157
// truncate
159-
$sphinx_connection->query("TRUNCATE RTINDEX {$sphinx_index} WITH RECONFIGURE");
158+
SphinxToolkitHelper::RTIndexTruncate($sphinx_connection, $sphinx_index);
160159

161160
// get total count
162-
$total_count = $this->mysql_GetRowCount($mysql_connection, $mysql_table, $condition);
161+
$total_count = SphinxToolkitHelper::MySQL_GetRowsCount($mysql_connection, $mysql_table, $condition);
163162
$total_updated = 0;
164163

165164
if ($this->rai_options['log_before_index'])
@@ -281,29 +280,15 @@ private static function buildReplaceQuery(string $table, array $dataset):string
281280
return $query;
282281
}
283282

284-
285-
/**
286-
* @param PDO $pdo
287-
* @param string $table
288-
* @param string $condition
289-
* @return int
290-
* @throws PDOException
291-
*/
292-
private function mysql_GetRowCount(PDO $pdo, string $table, string $condition)
293-
{
294-
$query = "SELECT COUNT(*) AS cnt FROM {$table}";
295-
if ($condition != '') $query .= " WHERE {$condition}";
296-
297-
return $pdo->query($query)->fetchColumn();
298-
} // mysql_GetRowCount
299-
300283
public function checkIndexExist(string $sphinx_index)
301284
{
302285
$index_definition = $this->sphinx_connection->query("SHOW TABLES LIKE '{$sphinx_index}' ")->fetchAll();
303286

304287
return count($index_definition) > 0;
305288
}
306-
289+
290+
291+
307292
/* =========================== СТАТИЧЕСКИЕ МЕТОДЫ ==================================== */
308293

309294
public static function EmulateBuildExcerpts($source, $needle, $options)

traits/SphinxToolkitHelper.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Arris\Toolkit;
44

5+
use PDO;
6+
57
/**
68
* Trait SphinxToolkitHelper
79
*
@@ -111,4 +113,74 @@ public static function mb_str_replace($search, $replace, $subject, &$count = 0)
111113
return $subject;
112114
}
113115

116+
/**
117+
* @param PDO $connection
118+
* @param $index
119+
*
120+
* @return array
121+
*/
122+
public static function RTIndexGetStatus(PDO $connection, $index)
123+
{
124+
$query = "SHOW INDEX {$index} STATUS";
125+
126+
$sth = $connection->query($query);
127+
128+
$set = $sth->fetchAll();
129+
130+
$json_set = [
131+
'query_time_1min', 'query_time_5min', 'query_time_15min', 'query_time_total',
132+
'found_rows_1min', 'found_rows_5min', 'found_rows_15min', 'found_rows_total'
133+
];
134+
foreach ($json_set as $key) {
135+
if (array_key_exists($key, $set)) {
136+
$set[$key] = json_decode($set[$key], true);
137+
}
138+
}
139+
140+
return $set;
141+
}
142+
143+
/**
144+
* @param PDO $connection
145+
* @param $index
146+
* @return false|\PDOStatement
147+
*/
148+
public static function RTIndexOptimize(PDO $connection, $index)
149+
{
150+
$query = "OPTIMIZE INDEX {$index}";
151+
return $connection->query($query);
152+
}
153+
154+
/**
155+
* @param PDO $connection
156+
* @param $index
157+
* @param bool $reconfigure
158+
* @return bool
159+
*/
160+
public static function RTIndexTruncate(PDO $connection, $index, $reconfigure = true)
161+
{
162+
$with = $reconfigure ? 'WITH RECONFIGURE' : '';
163+
return (bool)$connection->query("TRUNCATE RTINDEX {$index} {$with}");
164+
}
165+
166+
/**
167+
* @param PDO $connection
168+
* @param $index
169+
* @return bool
170+
*/
171+
public static function RTIndexCheckExist(PDO $connection, $index)
172+
{
173+
$index_definition = $connection->query("SHOW TABLES LIKE '{$index}' ")->fetchAll();
174+
175+
return count($index_definition) > 0;
176+
}
177+
178+
public static function MySQL_GetRowsCount(PDO $pdo, string $table, string $condition)
179+
{
180+
$query = "SELECT COUNT(*) AS cnt FROM {$table}";
181+
if ($condition != '') $query .= " WHERE {$condition}";
182+
183+
return $pdo->query($query)->fetchColumn();
184+
}
185+
114186
}

0 commit comments

Comments
 (0)