Skip to content

Commit

Permalink
Fixed response handling in plugins (#393)
Browse files Browse the repository at this point in the history
* Fixed response handling in plugins

* Fixed response handling in AlterRenameTable

* Fixed a typo

* Fixed response handling in CreateTableHandler

---------

Co-authored-by: nick <[email protected]>
  • Loading branch information
Nick-S-2018 and nick authored Nov 12, 2024
1 parent 5d89277 commit 6707485
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Plugin/AlterRenameTable/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function run(): Task {
}
self::createTableLike($payload->sourceTableName, $payload->destinationTableName, $client);

$result = (array)self::attachTable($payload->sourceTableName, $payload->destinationTableName, $client);
$result = self::attachTable($payload->sourceTableName, $payload->destinationTableName, $client);

self::dropTable($payload->sourceTableName, $client);

Expand Down
11 changes: 7 additions & 4 deletions src/Plugin/Knn/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,17 @@ function ($val) {

if (is_array($result['hits']) && isset($result['hits']['hits'])) {
// Removing requested doc from result set
$resultHits = $result['hits'];
$filteredResults = [];
foreach ($result['hits']['hits'] as $v) {
foreach ($resultHits['hits'] as $v) {
if ($v['_id'] === (int)$payload->docId) {
continue;
}

$filteredResults[] = $v;
}
$result['hits']['hits'] = $filteredResults;
$resultHits['hits'] = $filteredResults;
$result->offsetSet('hits', $resultHits);
}

return $result;
Expand All @@ -178,14 +180,15 @@ private static function knnSqlQuery(Client $manticoreClient, Payload $payload, s
$result = $request->getResult();

if (is_array($result[0])) {
$resultStruct = &$result[0];
$resultStruct = $result[0];
foreach ($resultStruct['data'] as $k => $v) {
if (!isset($v['id']) || $v['id'] !== (int)$payload->docId) {
continue;
}

unset($resultStruct[0]['data'][$k]);
unset($resultStruct['data'][$k]);
}
$result->offsetSet(0, $resultStruct);
}


Expand Down
10 changes: 6 additions & 4 deletions src/Plugin/Show/CreateTableHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ public function run(): Task {
$time0 = hrtime(true);
// First, get response from the manticore
$query = "SHOW CREATE TABLE {$payload->table}";
/** @var array{0:array{data:array<mixed>}} */
$result = $manticoreClient->sendRequest($query, $payload->path)->getResult();
/** @var array{data:array<int,array<string,string>>} $resultStruct */
$resultStruct = $result[0];
if ($payload->hasCliEndpoint) {
$total = sizeof($result[0]['data']);
return TaskResult::raw($tableFormatter->getTable($time0, $result[0]['data'], $total));
$total = sizeof($resultStruct['data']);
return TaskResult::raw($tableFormatter->getTable($time0, $resultStruct['data'], $total));
}

// It's important to have ` and 2 spaces for Apache Superset
foreach ($result[0]['data'] as &$row) {
foreach ($resultStruct['data'] as &$row) {
/** @var array{'Create Table':string} $row */
$lines = explode("\n", $row['Create Table']);
$lastN = sizeof($lines) - 1;
Expand All @@ -70,6 +71,7 @@ public function run(): Task {
}
$row['Create Table'] = implode("\n", $lines);
}
$result->offsetSet(0, $resultStruct);
return TaskResult::raw($result);
};

Expand Down
7 changes: 4 additions & 3 deletions src/Plugin/Show/VersionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Manticoresearch\Buddy\Base\Plugin\Show;

use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
use Manticoresearch\Buddy\Core\Network\Struct;
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithClient;
use Manticoresearch\Buddy\Core\Task\Column;
use Manticoresearch\Buddy\Core\Task\Task;
Expand Down Expand Up @@ -50,12 +51,12 @@ public function run(): Task {
}

/**
* @param mixed $result
* @param Struct<int|string, mixed> $result
* @return array<int<0, max>, array<string, string>>
*/
private static function parseVersions(mixed $result):array {
private static function parseVersions(Struct $result):array {
$versions = [];
if (is_array($result) && isset($result[0]['data'][0]['Value'])) {
if (is_array($result[0]) && isset($result[0]['data'][0]['Value'])) {
$value = $result[0]['data'][0]['Value'];

$splittedVersions = explode('(', $value);
Expand Down

0 comments on commit 6707485

Please sign in to comment.