-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pure PHP implementation of Google Snappy library (#613)
- Loading branch information
1 parent
cfe19d4
commit 567535e
Showing
17 changed files
with
695 additions
and
14 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
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
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,9 @@ | ||
*.php text eol=lf | ||
|
||
/.github export-ignore | ||
/tests export-ignore | ||
|
||
/README.md export-ignore | ||
|
||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
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,6 @@ | ||
## Contributing | ||
|
||
This repo is **READ ONLY**, in order to contribute to Flow PHP project, please | ||
open PR against [flow](https://github.com/flow-php/flow) monorepo. | ||
|
||
Changes merged to monorepo are automatically propagated into sub repositories. |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Flow PHP | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,73 @@ | ||
# Snappy | ||
|
||
Pure PHP implementation of Google [Snappy](https://github.com/google/snappy) compression algorithm. | ||
|
||
This library is a port of javascript [snappyjs](https://github.com/zhipeng-jia/snappyjs). | ||
Whenever it's possible it's recommended to install [PHP Extension](https://github.com/kjdev/php-ext-snappy), | ||
otherwise this lib will register polyfill functions. | ||
|
||
## Installation | ||
|
||
``` | ||
composer require flow-php/snappy:1.x@dev | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
<?php | ||
|
||
$string = 'This is some random string'; | ||
|
||
echo \snappy_decompress(\snappy_compress($string)); // This is some random string | ||
``` | ||
|
||
## Performance | ||
|
||
PHP Implementation is significantly slower than extension, below you can find a benchmark script and results: | ||
|
||
```php | ||
<?php | ||
|
||
include __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$faker = \Faker\Factory::create(); | ||
|
||
$texts = []; | ||
for ($i = 0; $i < 10_000; $i++) { | ||
$textSize = \random_int(100, 5000); | ||
$texts[] = $faker->text($textSize); | ||
} | ||
|
||
$snappy = new \Flow\Snappy\Snappy(); | ||
|
||
echo "Starting Benchmark\n\n"; | ||
|
||
$flowStart = microtime(true); | ||
foreach ($texts as $text) { | ||
if ($snappy->uncompress($snappy->compress($text)) !== $text) { | ||
die('snappy flow failed'); | ||
} | ||
} | ||
$flowEnd = microtime(true); | ||
echo "Snappy Flow time: " . ($flowEnd - $flowStart) . "\n"; | ||
|
||
$extStart = microtime(true); | ||
foreach ($texts as $text) { | ||
if (\snappy_uncompress(\snappy_compress($text)) !== $text) { | ||
die('snappy ext failed'); | ||
} | ||
} | ||
$extEnd = microtime(true); | ||
echo "Snappy PHP Extension time: " . ($extEnd - $extStart) . "\n"; | ||
``` | ||
|
||
Output: | ||
|
||
```console | ||
$ php benchmark_snappy.php | ||
Starting Benchmark | ||
|
||
Snappy Flow time: 6.6838178634644 | ||
Snappy PHP Extension time: 0.31190991401672 | ||
``` |
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,38 @@ | ||
{ | ||
"name": "flow-php/snappy", | ||
"type": "library", | ||
"description": "PHP ETL - Google Snappy compression algorithm implementation", | ||
"keywords": [ | ||
"etl", | ||
"extract", | ||
"transform", | ||
"load", | ||
"filter", | ||
"snappy", | ||
"algorithm", | ||
"compression" | ||
], | ||
"require": { | ||
"php": "~8.1 || ~8.2" | ||
}, | ||
"config": { | ||
"optimize-autoloader": true, | ||
"sort-packages": true | ||
}, | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"Flow\\": [ | ||
"src/Flow" | ||
] | ||
}, | ||
"files": [ "polifil.php" ] | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Flow\\": "tests/Flow" | ||
} | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Flow\Snappy\Snappy; | ||
|
||
if (!\function_exists('snappy_compress')) { | ||
function snappy_compress(string $plainText) : string | ||
{ | ||
return (new Snappy())->compress($plainText); | ||
} | ||
} | ||
|
||
if (!\function_exists('snappy_uncompress')) { | ||
function snappy_uncompress(string $compressedText) : string | ||
{ | ||
return (new Snappy())->uncompress($compressedText); | ||
} | ||
} | ||
|
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,38 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Flow\Snappy; | ||
|
||
final class Snappy | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function compress(string $plainText) : string | ||
{ | ||
if ($plainText === '') { | ||
return $plainText; | ||
} | ||
|
||
$byteArray = \array_values(\unpack('C*', $plainText)); | ||
|
||
$outputBuffer = []; | ||
(new SnappyCompressor($byteArray))->compressToBuffer($outputBuffer); | ||
|
||
return \pack('C*', ...$outputBuffer); | ||
} | ||
|
||
public function uncompress(string $compressedText) : string | ||
{ | ||
if ($compressedText === '') { | ||
return $compressedText; | ||
} | ||
|
||
$byteArray = \array_values(\unpack('C*', $compressedText)); | ||
|
||
$outputBuffer = []; | ||
(new SnappyDecompressor($byteArray))->uncompressToBuffer($outputBuffer); | ||
|
||
return \pack('C*', ...$outputBuffer); | ||
} | ||
} |
Oops, something went wrong.