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

Feat/create table like #manticoresoftware/manticoresearch/issues/1788 #257

Merged
merged 9 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
152 changes: 152 additions & 0 deletions src/Plugin/Create/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php declare(strict_types=1);

/*
Copyright (c) 2023, Manticore Software LTD (https://manticoresearch.com)
djklim87 marked this conversation as resolved.
Show resolved Hide resolved

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 or any later
version. You should have received a copy of the GPL license along with this
program; if you did not, you can find it at http://www.gnu.org/
*/

namespace Manticoresearch\Buddy\Base\Plugin\Create;

use Manticoresearch\Buddy\Core\Error\GenericError;
use Manticoresearch\Buddy\Core\Error\ManticoreSearchClientError;
use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithClient;
use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Task\TaskResult;
use RuntimeException;

final class Handler extends BaseHandlerWithClient
{

/**
* Initialize the executor
*
* @param Payload $payload
* @return void
*/
public function __construct(public Payload $payload) {
}

/**
* Process the request
*
* @return Task
* @throws RuntimeException
*/
public function run(): Task {
$taskFn = static function (Payload $payload, Client $client): TaskResult {

if (!$client->hasTable($payload->sourceTableName)) {
throw GenericError::create("Source table $payload->sourceTableName not exists");
}

if ($client->hasTable($payload->destinationTableName)) {
throw GenericError::create("Destination table $payload->destinationTableName already exists");
}

try {
self::flushRamchunk($payload->sourceTableName, $client);
$freezeResult = self::freezeTable($payload->sourceTableName, $client);
$dataDirPath = self::parseTablePath($payload->sourceTableName, $freezeResult);
$destinationTablePath = $dataDirPath . $payload->sourceTableName .
DIRECTORY_SEPARATOR . $payload->sourceTableName;
self::importTable($payload->destinationTableName, $destinationTablePath, $client);
} catch (GenericError $exception) {
self::unfreezeTable($payload->sourceTableName, $client);
}

return TaskResult::none();
};

return Task::create(
$taskFn, [$this->payload, $this->manticoreClient]
)->run();
}


/**
* @param string $tableName
* @param Client $client
* @throws GenericError
* @throws ManticoreSearchClientError
*/
private static function flushRamchunk(string $tableName, Client $client): void {
$sql = "flush ramchunk $tableName";
$result = $client->sendRequest($sql);
if ($result->hasError()) {
throw GenericError::create("Can't flush ramchunk for $tableName. Reason: " . $result->getError());
}
}

/**
* @param string $tableName
* @param Client $client
* @return mixed
* @throws ManticoreSearchClientError|GenericError
*/
private static function freezeTable(string $tableName, Client $client): mixed {
$sql = "FREEZE $tableName";
$result = $client->sendRequest($sql);
if ($result->hasError()) {
throw GenericError::create("Can't freeze table $tableName. Reason: " . $result->getError());
}
return $result->getResult();
}

/**
* @param string $tableName
* @param mixed $freezeResult
* @return string
* @throws GenericError
*/
private static function parseTablePath(string $tableName, mixed $freezeResult): string {

djklim87 marked this conversation as resolved.
Show resolved Hide resolved
if ($tableName === '') {
throw GenericError::create("Table name can't be empty");
}
if (!is_array($freezeResult) || !isset($freezeResult[0]['data'][0]['normalized'])) {
throw GenericError::create('No normalized result in freeze response');
}

$explodedPath = explode($tableName, $freezeResult[0]['data'][0]['normalized']);

return $explodedPath[0];
}

/**
* @param string $tableName
* @param Client $client
* @return void
* @throws ManticoreSearchClientError|GenericError
*/
private static function unfreezeTable(string $tableName, Client $client): void {
$sql = "UNFREEZE $tableName";
$result = $client->sendRequest($sql);
if ($result->hasError()) {
throw GenericError::create("Can't unfreeze table $tableName. Reason: " . $result->getError());
}
}

djklim87 marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param string $tableName
* @param string $destinationTablePath
* @param Client $client
* @return true
* @throws GenericError
* @throws ManticoreSearchClientError
*/
public static function importTable(string $tableName, string $destinationTablePath, Client $client): true {
$sql = "import table $tableName from '$destinationTablePath'";
$result = $client->sendRequest($sql);
if ($result->hasError()) {
throw GenericError::create("Can't import table $tableName. Reason: " . $result->getError());
}
return true;
}

}
151 changes: 151 additions & 0 deletions src/Plugin/Create/Payload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php declare(strict_types=1);

/*
Copyright (c) 2023, Manticore Software LTD (https://manticoresearch.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 or any later
version. You should have received a copy of the GPL license along with this
program; if you did not, you can find it at http://www.gnu.org/
*/

namespace Manticoresearch\Buddy\Base\Plugin\Create;

use Manticoresearch\Buddy\Core\Network\Request;
use Manticoresearch\Buddy\Core\Plugin\BasePayload;

/**
* This is simple do nothing request that handle empty queries
* which can be as a result of only comments in it that we strip
* @extends BasePayload<array>
*/
final class Payload extends BasePayload {
public string $path;

public string $destinationTableName;
public string $sourceTableName;

public string $type;

/**
* Get description for this plugin
* @return string
*/
public static function getInfo(): string {
return 'Enables partial replaces';
}

/**
* @param Request $request
* @return static
*/
public static function fromRequest(Request $request): static {
$self = new static();

/**
* @var array{
* CREATE: array{
* expr_type: string,
* not-exists: bool,
* base_expr: string,
* sub_tree?: array<array{
* expr_type: string,
* base_expr: string
* }>
* },
* TABLE: array{
* base_expr: string,
* name?: string,
* no_quotes: array{
* delim: bool,
* parts: array<string>
* },
* create-def?: bool,
* options?: array<array{
* expr_type: string,
* base_expr: string,
* delim: string,
* sub_tree?: array<array{
* expr_type: string,
* base_expr: string
* }>
* }>
* },
* LIKE: array{
* expr_type: string,
* table?: string,
* base_expr: string,
* no_quotes: array{
* delim: bool,
* parts: array<string>
* }
* }
* } $payload
*/
$payload = Payload::$sqlQueryParser::parse($request->payload);

$self->destinationTableName = $payload['TABLE']['no_quotes']['parts'][0];
$self->sourceTableName = $payload['LIKE']['no_quotes']['parts'][0];
return $self;
}

/**
* @param Request $request
* @return bool
*/
public static function hasMatch(Request $request): bool {

/**
* @phpstan-var array{
* CREATE?: array{
* expr_type: string,
* not-exists: bool,
* base_expr: string,
* sub_tree?: array<array{
* expr_type: string,
* base_expr: string
* }>
* },
* TABLE?: array{
* base_expr: string,
* name?: string,
* no_quotes: array{
* delim: bool,
* parts: array<string>
* },
* create-def?: bool,
* options?: array<array{
* expr_type: string,
* base_expr: string,
* delim: string,
* sub_tree?: array<array{
* expr_type: string,
* base_expr: string
* }>
* }>
* },
* LIKE?: array{
* expr_type: string,
* table?: string,
* base_expr: string,
* no_quotes: array{
* delim: bool,
* parts: array<string>
* }
* }
* } $payload
*/
$payload = Payload::$sqlQueryParser::parse($request->payload);
djklim87 marked this conversation as resolved.
Show resolved Hide resolved

if (isset($payload['CREATE'])
&& isset($payload['TABLE']['no_quotes']['parts'][0])
&& isset($payload['LIKE']['no_quotes']['parts'][0])
&& isset($payload['TABLE']['options'][0]['base_expr'])
&& $payload['TABLE']['options'][0]['base_expr'] === 'WITH DATA'
) {
return true;
}

return false;
}
}
1 change: 1 addition & 0 deletions src/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'manticoresoftware/buddy-plugin-empty-string',
'manticoresoftware/buddy-plugin-backup',
'manticoresoftware/buddy-plugin-emulate-elastic',
'manticoresoftware/buddy-plugin-create',
'manticoresoftware/buddy-plugin-insert',
'manticoresoftware/buddy-plugin-insert-values',
'manticoresoftware/buddy-plugin-alias',
Expand Down
Loading