This commit is contained in:
Daniil Gentili 2024-03-29 19:56:29 +01:00
parent 16e4448dd8
commit 47f7a3a2d2
8 changed files with 19 additions and 33 deletions

View File

@ -35,8 +35,7 @@
"phpunit/phpunit": "^11.0.6", "phpunit/phpunit": "^11.0.6",
"amphp/php-cs-fixer-config": "^2.0.1", "amphp/php-cs-fixer-config": "^2.0.1",
"friendsofphp/php-cs-fixer": "^3.51", "friendsofphp/php-cs-fixer": "^3.51",
"amphp/process": "^2.0", "amphp/process": "^2.0"
"brianium/paratest": "^7.4"
}, },
"scripts": { "scripts": {
"cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 php -d pcre.jit=0 vendor/bin/php-cs-fixer fix -v" "cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 php -d pcre.jit=0 vendor/bin/php-cs-fixer fix -v"

View File

@ -48,7 +48,7 @@ final class OrmMappedArray
* *
* @var int<1, max>|null * @var int<1, max>|null
*/ */
public readonly ?int $optimizeIfWastedGtMb = null, public readonly ?int $optimizeIfWastedMb = null,
/** /**
* Table name postfix, if null defaults to the property name. * Table name postfix, if null defaults to the property name.
*/ */

View File

@ -56,12 +56,12 @@ trait DbAutoProperties
)); ));
} }
if ($settings instanceof Mysql) { if ($settings instanceof Mysql) {
$optimize = $attr->optimizeIfWastedGtMb ?? $settings->optimizeIfWastedGtMb; $optimize = $attr->optimizeIfWastedMb ?? $settings->optimizeIfWastedMb;
if ($optimize !== $settings->optimizeIfWastedGtMb) { if ($optimize !== $settings->optimizeIfWastedMb) {
$settings = new $settings(\array_merge( $settings = new $settings(\array_merge(
(array) $settings, (array) $settings,
['optimizeIfWastedGtMb' => $optimize] ['optimizeIfWastedMb' => $optimize]
)); ));
} }
} }

View File

@ -73,7 +73,8 @@ abstract class DriverArray extends DbArray
} else { } else {
$promises = []; $promises = [];
foreach ($previous->getIterator() as $key => $value) { foreach ($previous->getIterator() as $key => $value) {
$promises []= async($instance->set(...), $key, $value); $promises []= async($previous->unset(...), $key)
->map(static fn () => $instance->set($key, $value));
if (\count($promises) % 500 === 0) { if (\count($promises) % 500 === 0) {
await($promises); await($promises);
$promises = []; $promises = [];
@ -82,7 +83,6 @@ abstract class DriverArray extends DbArray
if ($promises) { if ($promises) {
await($promises); await($promises);
} }
$previous->clear();
} }
return $instance; return $instance;

View File

@ -164,7 +164,7 @@ final class MysqlArray extends SqlArray
} }
} }
if ($settings->optimizeIfWastedGtMb !== null) { if ($settings->optimizeIfWastedMb !== null) {
$database = $settings->config->getDatabase(); $database = $settings->config->getDatabase();
$result = $db->prepare("SELECT data_free FROM information_schema.tables WHERE table_schema=? AND table_name=?") $result = $db->prepare("SELECT data_free FROM information_schema.tables WHERE table_schema=? AND table_name=?")
->execute([$database, $config->table]) ->execute([$database, $config->table])
@ -172,7 +172,7 @@ final class MysqlArray extends SqlArray
Assert::notNull($result); Assert::notNull($result);
$result = $result['data_free'] ?? $result['DATA_FREE']; $result = $result['data_free'] ?? $result['DATA_FREE'];
Assert::integer($result, "Could not optimize table!"); Assert::integer($result, "Could not optimize table!");
if (($result >> 20) > $settings->optimizeIfWastedGtMb) { if (($result >> 20) >= $settings->optimizeIfWastedMb) {
$db->query("OPTIMIZE TABLE `{$config->table}`"); $db->query("OPTIMIZE TABLE `{$config->table}`");
} }
} }

View File

@ -85,30 +85,12 @@ final class RedisArray extends DriverArray
$this->db = self::$connections[$dbKey]; $this->db = self::$connections[$dbKey];
} }
protected function moveDataFromTableToTable(string $from, string $to): void
{
$from = "va:$from";
$to = "va:$to";
$request = $this->db->scan($from.'*');
$lenK = \strlen($from);
foreach ($request as $oldKey) {
$newKey = $to.\substr($oldKey, $lenK);
$value = $this->db->get($oldKey);
if ($value !== null) {
$this->db->set($newKey, $value);
$this->db->delete($oldKey);
}
}
}
/** /**
* Get redis key name. * Get redis key name.
*/ */
private function rKey(string $key): string private function rKey(string $key): string
{ {
return 'va:'.$this->config->table.':'.$key; return $this->config->table.':'.$key;
} }
/** /**
@ -116,7 +98,7 @@ final class RedisArray extends DriverArray
*/ */
private function itKey(): string private function itKey(): string
{ {
return 'va:'.$this->config->table.'*'; return $this->config->table.':*';
} }
public function set(string|int $key, mixed $value): void public function set(string|int $key, mixed $value): void

View File

@ -50,9 +50,9 @@ final readonly class Mysql extends SqlSettings
* *
* If null disables optimization. * If null disables optimization.
* *
* @var int<1, max>|null $optimizeIfWastedGtMb * @var int<1, max>|null $optimizeIfWastedMb
*/ */
public ?int $optimizeIfWastedGtMb = null, public ?int $optimizeIfWastedMb = null,
) { ) {
parent::__construct($config, $serializer, $cacheTtl, $maxConnections, $idleTimeout); parent::__construct($config, $serializer, $cacheTtl, $maxConnections, $idleTimeout);
} }

View File

@ -333,7 +333,6 @@ final class OrmTest extends TestCase
} }
$this->assertEquals(1, $cnt); $this->assertEquals(1, $cnt);
$field = new FieldConfig( $field = new FieldConfig(
$table.'_new', $table.'_new',
$settings, $settings,
@ -521,6 +520,12 @@ final class OrmTest extends TestCase
$serializer, $serializer,
$ttl, $ttl,
)], )],
[$key++, new Mysql(
MysqlConfig::fromString('host=127.0.0.1:3306 user=root db=test'),
$serializer,
$ttl,
optimizeIfWastedMb: 0,
)],
]; ];
} }
} }