-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add zstd and lz4 compression support and auto compression detec…
…t on db import
- Loading branch information
Showing
8 changed files
with
266 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace N98\Magento\Command\Database\Compressor; | ||
|
||
/** | ||
* Class LZ4 | ||
* @package N98\Magento\Command\Database\Compressor | ||
*/ | ||
class LZ4 extends AbstractCompressor | ||
{ | ||
/** | ||
* Returns the command line for compressing the dump file. | ||
* | ||
* @param string $command | ||
* @param bool $pipe | ||
* @return string | ||
*/ | ||
public function getCompressingCommand($command, $pipe = true) | ||
{ | ||
if ($pipe) { | ||
return $command . ' | lz4 -c '; | ||
} else { | ||
return sprintf( | ||
"tar -I 'lz4' -cf %s", | ||
$command, | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the command line for decompressing the dump file. | ||
* | ||
* @param string $command | ||
* @param string $fileName Filename (shell argument escaped) | ||
* @param bool $pipe | ||
* @return string | ||
*/ | ||
public function getDecompressingCommand($command, $fileName, $pipe = true) | ||
{ | ||
if ($pipe) { | ||
if ($this->hasPipeViewer()) { | ||
return 'pv -cN lz4 ' . escapeshellarg($fileName) . ' | lz4 -d | pv -cN mysql | ' . $command; | ||
} | ||
|
||
return 'lz4 -dc < ' . escapeshellarg($fileName) . ' | ' . $command; | ||
} else { | ||
if ($this->hasPipeViewer()) { | ||
return 'pv -cN tar -zxf ' . escapeshellarg($fileName) . ' && pv -cN mysql | ' . $command; | ||
} | ||
|
||
return 'tar -zxf ' . escapeshellarg($fileName) . ' -C ' . dirname($fileName) . ' && ' . $command . ' < ' | ||
. escapeshellarg(substr($fileName, 0, -4)); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the file name for the compressed dump file. | ||
* | ||
* @param string $fileName | ||
* @param bool $pipe | ||
* @return string | ||
*/ | ||
public function getFileName($fileName, $pipe = true) | ||
{ | ||
if ($fileName === null) { | ||
$fileName = ''; | ||
} | ||
|
||
if (!strlen($fileName)) { | ||
return $fileName; | ||
} | ||
|
||
if ($pipe) { | ||
if (substr($fileName, -4, 4) === '.lz4') { | ||
return $fileName; | ||
} elseif (substr($fileName, -4, 4) === '.sql') { | ||
$fileName .= '.lz4'; | ||
} else { | ||
$fileName .= '.sql.lz4'; | ||
} | ||
} elseif (substr($fileName, -8, 8) === '.tar.lz4') { | ||
return $fileName; | ||
} else { | ||
$fileName .= '.tar.lz4'; | ||
} | ||
|
||
return $fileName; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
src/N98/Magento/Command/Database/Compressor/Zstandard.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace N98\Magento\Command\Database\Compressor; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
|
||
/** | ||
* Class Zstandard | ||
* @package N98\Magento\Command\Database\Compressor | ||
*/ | ||
class Zstandard extends AbstractCompressor | ||
{ | ||
protected int $compressionLevel; | ||
|
||
protected string $extraArgs; | ||
|
||
public function __construct(InputInterface $input) | ||
{ | ||
$this->compressionLevel = (int)$input->getOption('zstd-level'); | ||
$this->extraArgs = (string)$input->getOption('zstd-extra-args'); | ||
|
||
parent::__construct($input); | ||
} | ||
|
||
/** | ||
* Returns the command line for compressing the dump file. | ||
* | ||
* @param string $command | ||
* @param bool $pipe | ||
* @return string | ||
*/ | ||
public function getCompressingCommand($command, $pipe = true) | ||
{ | ||
if ($pipe) { | ||
return sprintf( | ||
"%s | zstd -c -%s %s", | ||
$command, | ||
$this->compressionLevel, | ||
$this->extraArgs, | ||
); | ||
} else { | ||
return sprintf( | ||
"tar -I 'zstd %s -%s' -cf %s", | ||
$this->extraArgs, | ||
$this->compressionLevel, | ||
$command, | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the command line for decompressing the dump file. | ||
* | ||
* @param string $command | ||
* @param string $fileName Filename (shell argument escaped) | ||
* @param bool $pipe | ||
* @return string | ||
*/ | ||
public function getDecompressingCommand($command, $fileName, $pipe = true) | ||
{ | ||
if ($pipe) { | ||
if ($this->hasPipeViewer()) { | ||
return 'pv -cN zstd ' . escapeshellarg($fileName) . ' | zstd -d | pv -cN mysql | ' . $command; | ||
} | ||
|
||
return 'zstd -dc < ' . escapeshellarg($fileName) . ' | ' . $command; | ||
} else { | ||
if ($this->hasPipeViewer()) { | ||
return 'pv -cN tar -zxf ' . escapeshellarg($fileName) . ' && pv -cN mysql | ' . $command; | ||
} | ||
|
||
return 'tar -zxf ' . escapeshellarg($fileName) . ' -C ' . dirname($fileName) . ' && ' . $command . ' < ' | ||
. escapeshellarg(substr($fileName, 0, -4)); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the file name for the compressed dump file. | ||
* | ||
* @param string $fileName | ||
* @param bool $pipe | ||
* @return string | ||
*/ | ||
public function getFileName($fileName, $pipe = true) | ||
{ | ||
if ($fileName === null) { | ||
$fileName = ''; | ||
} | ||
|
||
if (!strlen($fileName)) { | ||
return $fileName; | ||
} | ||
|
||
if ($pipe) { | ||
if (substr($fileName, -5, 5) === '.zstd') { | ||
return $fileName; | ||
} elseif (substr($fileName, -4, 4) === '.sql') { | ||
$fileName .= '.zstd'; | ||
} else { | ||
$fileName .= '.sql.zstd'; | ||
} | ||
} elseif (substr($fileName, -9, 9) === '.tar.zstd') { | ||
return $fileName; | ||
} else { | ||
$fileName .= '.tar.zstd'; | ||
} | ||
|
||
return $fileName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters