Skip to content

Commit

Permalink
1.3
Browse files Browse the repository at this point in the history
* [*] optimize code
* [+] SphinxToolkitHelper { RTIndexGetStatus(), RTIndexOptimize(), RTIndexTruncate(), RTIndexCheckExist() } static methods
  • Loading branch information
KarelWintersky committed Oct 27, 2020
1 parent db00cb8 commit b148818
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 24 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"php": ">=7",
"ext-mbstring": "*",
"ext-pdo": "*",
"ext-json": "*",
"foolz/sphinxql-query-builder": "^2.1",
"psr/log": "^1.1",
"karelwintersky/arris.toolkit.cli-console": "^0.9.0"
Expand Down
33 changes: 9 additions & 24 deletions src/SphinxToolkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function __construct(PDO $mysql_connection, PDO $sphinx_connection)
$this->sphinx_connection = $sphinx_connection;
}


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

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

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

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

// get total count
$total_count = $this->mysql_GetRowCount($mysql_connection, $mysql_table, $condition);
$total_count = SphinxToolkitHelper::MySQL_GetRowsCount($mysql_connection, $mysql_table, $condition);
$total_updated = 0;

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

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

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

// get total count
$total_count = $this->mysql_GetRowCount($mysql_connection, $mysql_table, $condition);
$total_count = SphinxToolkitHelper::MySQL_GetRowsCount($mysql_connection, $mysql_table, $condition);
$total_updated = 0;

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


/**
* @param PDO $pdo
* @param string $table
* @param string $condition
* @return int
* @throws PDOException
*/
private function mysql_GetRowCount(PDO $pdo, string $table, string $condition)
{
$query = "SELECT COUNT(*) AS cnt FROM {$table}";
if ($condition != '') $query .= " WHERE {$condition}";

return $pdo->query($query)->fetchColumn();
} // mysql_GetRowCount

public function checkIndexExist(string $sphinx_index)
{
$index_definition = $this->sphinx_connection->query("SHOW TABLES LIKE '{$sphinx_index}' ")->fetchAll();

return count($index_definition) > 0;
}




/* =========================== СТАТИЧЕСКИЕ МЕТОДЫ ==================================== */

public static function EmulateBuildExcerpts($source, $needle, $options)
Expand Down
72 changes: 72 additions & 0 deletions traits/SphinxToolkitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Arris\Toolkit;

use PDO;

/**
* Trait SphinxToolkitHelper
*
Expand Down Expand Up @@ -111,4 +113,74 @@ public static function mb_str_replace($search, $replace, $subject, &$count = 0)
return $subject;
}

/**
* @param PDO $connection
* @param $index
*
* @return array
*/
public static function RTIndexGetStatus(PDO $connection, $index)
{
$query = "SHOW INDEX {$index} STATUS";

$sth = $connection->query($query);

$set = $sth->fetchAll();

$json_set = [
'query_time_1min', 'query_time_5min', 'query_time_15min', 'query_time_total',
'found_rows_1min', 'found_rows_5min', 'found_rows_15min', 'found_rows_total'
];
foreach ($json_set as $key) {
if (array_key_exists($key, $set)) {
$set[$key] = json_decode($set[$key], true);
}
}

return $set;
}

/**
* @param PDO $connection
* @param $index
* @return false|\PDOStatement
*/
public static function RTIndexOptimize(PDO $connection, $index)
{
$query = "OPTIMIZE INDEX {$index}";
return $connection->query($query);
}

/**
* @param PDO $connection
* @param $index
* @param bool $reconfigure
* @return bool
*/
public static function RTIndexTruncate(PDO $connection, $index, $reconfigure = true)
{
$with = $reconfigure ? 'WITH RECONFIGURE' : '';
return (bool)$connection->query("TRUNCATE RTINDEX {$index} {$with}");
}

/**
* @param PDO $connection
* @param $index
* @return bool
*/
public static function RTIndexCheckExist(PDO $connection, $index)
{
$index_definition = $connection->query("SHOW TABLES LIKE '{$index}' ")->fetchAll();

return count($index_definition) > 0;
}

public static function MySQL_GetRowsCount(PDO $pdo, string $table, string $condition)
{
$query = "SELECT COUNT(*) AS cnt FROM {$table}";
if ($condition != '') $query .= " WHERE {$condition}";

return $pdo->query($query)->fetchColumn();
}

}

0 comments on commit b148818

Please sign in to comment.