Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/393 parallelize the search command #398

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cli/ratchet/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
use React\Socket\LimitingServer;
use React\Socket\Server;
use React\Socket\SecureServer;
use Spatie\Async\Pool;

require __DIR__ . '/../../vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__.'/../../');
$dotenv->load();

$pool = Pool::create();

$db = new Db([
'driver' => $_ENV['DB_DRIVER'],
'host' => $_ENV['DB_HOST'],
Expand All @@ -34,7 +37,7 @@
$logger = new Logger('log');
$logger->pushHandler(new StreamHandler(__DIR__.'/../../storage' . '/data.log', Logger::INFO));

$parser = new Parser(new Cli($db));
$parser = new Parser(new Cli($pool, $db));

$clientStorage = new ClientStorage($logger);

Expand Down
5 changes: 4 additions & 1 deletion cli/workerman/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
use Dotenv\Dotenv;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Spatie\Async\Pool;

require __DIR__ . '/../../vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__.'/../../');
$dotenv->load();

$pool = Pool::create();

$db = new Db([
'driver' => $_ENV['DB_DRIVER'],
'host' => $_ENV['DB_HOST'],
Expand All @@ -27,7 +30,7 @@
$logger = new Logger('data');
$logger->pushHandler(new StreamHandler(DataWebSocket::STORAGE_FOLDER . '/data.log', Logger::INFO));

$parser = new Parser(new Cli($db));
$parser = new Parser(new Cli($pool, $db));

$clientStorage = new ClientStorage($logger);

Expand Down
5 changes: 3 additions & 2 deletions src/Command/Data/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use ChessServer\Db;
use ChessServer\Command\AbstractCli;
use Spatie\Async\Pool;

class Cli extends AbstractCli
{
private Db $db;

public function __construct(Db $db)
public function __construct(Pool $pool, Db $db)
{
parent::__construct();

Expand All @@ -24,7 +25,7 @@ public function __construct(Db $db)
$this->commands->attach(new AutocompleteWhiteCommand($db));
$this->commands->attach(new ResultEventCommand($db));
$this->commands->attach(new ResultPlayerCommand($db));
$this->commands->attach(new SearchCommand($db));
$this->commands->attach((new SearchCommand())->setPool($pool));
}

public function getDb(): Db
Expand Down
80 changes: 80 additions & 0 deletions src/Command/Data/SearchAsyncTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace ChessServer\Command\Data;

use Chess\Movetext\SanMovetext;
use Chess\Variant\Classical\PGN\Move;
use ChessServer\Db;
use Spatie\Async\Task;

class SearchAsyncTask extends Task
{
const SQL_LIKE = [
'Date',
'movetext',
];

const SQL_EQUAL = [
'Event',
'White',
'Black',
'ECO',
'Result',
];

private array $params;

private array $conf;

private Db $db;

public function __construct(array $params, array $conf)
{
$this->params = $params;
$this->conf = $conf;
}

public function configure()
{
$this->db = new Db($this->conf);
}

public function run()
{
$sql = 'SELECT * FROM games WHERE ';

$values = [];

foreach ($this->params as $key => $val) {
if ($val) {
if (in_array($key, self::SQL_LIKE)) {
$sql .= "$key LIKE :$key AND ";
if ($key === 'movetext') {
$val = (new SanMovetext(new Move(), $this->params['movetext']))
->filtered($comments = false, $nags = false);
}
$values[] = [
'param' => ":$key",
'value' => '%'.$val.'%',
'type' => \PDO::PARAM_STR,
];
} else if (in_array($key, self::SQL_EQUAL) && $val) {
$sql .= "$key = :$key AND ";
$values[] = [
'param' => ":$key",
'value' => $val,
'type' => \PDO::PARAM_STR,
];
}
}
}

str_ends_with($sql, 'WHERE ')
? $sql = substr($sql, 0, -6)
: $sql = substr($sql, 0, -4);

$sql .= 'ORDER BY RAND() LIMIT 25';

return $this->db->query($sql, $values)->fetchAll(\PDO::FETCH_ASSOC);
}
}
71 changes: 14 additions & 57 deletions src/Command/Data/SearchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,13 @@

namespace ChessServer\Command\Data;

use Chess\Movetext\SanMovetext;
use Chess\Variant\Classical\PGN\Move;
use ChessServer\Db;
use ChessServer\Command\AbstractCommand;
use ChessServer\Socket\AbstractSocket;

class SearchCommand extends AbstractCommand
{
const SQL_LIKE = [
'Date',
'movetext',
];

const SQL_EQUAL = [
'Event',
'White',
'Black',
'ECO',
'Result',
];

public function __construct(Db $db)
public function __construct()
{
parent::__construct($db);

$this->name = '/search';
$this->description = 'Finds up to 25 games matching the criteria.';
$this->params = [
Expand All @@ -43,44 +25,19 @@ public function run(AbstractSocket $socket, array $argv, int $id)
{
$params = json_decode(stripslashes($argv[1]), true);

$sql = 'SELECT * FROM games WHERE ';

$values = [];

foreach ($params as $key => $val) {
if ($val) {
if (in_array($key, self::SQL_LIKE)) {
$sql .= "$key LIKE :$key AND ";
if ($key === 'movetext') {
$val = (new SanMovetext(new Move(), $params['movetext']))
->filtered($comments = false, $nags = false);
}
$values[] = [
'param' => ":$key",
'value' => '%'.$val.'%',
'type' => \PDO::PARAM_STR,
];
} else if (in_array($key, self::SQL_EQUAL) && $val) {
$sql .= "$key = :$key AND ";
$values[] = [
'param' => ":$key",
'value' => $val,
'type' => \PDO::PARAM_STR,
];
}
}
}

str_ends_with($sql, 'WHERE ')
? $sql = substr($sql, 0, -6)
: $sql = substr($sql, 0, -4);

$sql .= 'ORDER BY RAND() LIMIT 25';

$arr = $this->db->query($sql, $values)->fetchAll(\PDO::FETCH_ASSOC);
$conf = [
'driver' => $_ENV['DB_DRIVER'],
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_DATABASE'],
'username' => $_ENV['DB_USERNAME'],
'password' => $_ENV['DB_PASSWORD'],
];

return $socket->getClientStorage()->send([$id], [
$this->name => $arr,
]);
$this->pool->add(new SearchAsyncTask($params, $conf), 81920)
->then(function ($result) use ($socket, $id) {
return $socket->getClientStorage()->send([$id], [
$this->name => $result,
]);
});
}
}