Skip to content

Commit

Permalink
Merge pull request #56 from shtelzerartem/feat-4002-add-snappy-compre…
Browse files Browse the repository at this point in the history
…ssion

Add Snappy compression
  • Loading branch information
christyjacob4 authored Dec 20, 2022
2 parents ad1c00f + 21d5d4d commit f1da955
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ RUN composer update \

FROM php:8.0-cli-alpine as compile

ENV PHP_ZSTD_VERSION="master"
ENV PHP_ZSTD_VERSION="master" \
PHP_SNAPPY_VERSION=bfefe4906e0abb1f6cc19005b35f9af5240d9025

RUN apk add --no-cache \
git \
Expand All @@ -34,20 +35,31 @@ RUN git clone --recursive --depth 1 --branch $PHP_ZSTD_VERSION https://github.co
&& ./configure --with-libzstd \
&& make && make install

## Snappy Extension
FROM compile AS snappy
RUN git clone --recursive --depth 1 https://github.com/kjdev/php-ext-snappy.git \
&& cd php-ext-snappy \
&& git checkout $PHP_SNAPPY_VERSION \
&& phpize \
&& ./configure \
&& make && make install

FROM compile as final

LABEL maintainer="[email protected]"

WORKDIR /usr/src/code

RUN echo extension=zstd.so >> /usr/local/etc/php/conf.d/zstd.ini
RUN echo extension=snappy.so >> /usr/local/etc/php/conf.d/snappy.ini

RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
&& echo "opcache.enable_cli=1" >> $PHP_INI_DIR/php.ini \
&& echo "memory_limit=1024M" >> $PHP_INI_DIR/php.ini

COPY --from=composer /usr/local/src/vendor /usr/src/code/vendor
COPY --from=zstd /usr/local/lib/php/extensions/no-debug-non-zts-20200930/zstd.so /usr/local/lib/php/extensions/no-debug-non-zts-20200930/
COPY --from=snappy /usr/local/lib/php/extensions/no-debug-non-zts-20200930/snappy.so /usr/local/lib/php/extensions/no-debug-non-zts-20200930/

# Add Source Code
COPY . /usr/src/code
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"ext-fileinfo": "*",
"ext-zlib": "*",
"ext-zstd": "*",
"ext-snappy": "*",
"php": ">=8.0",
"utopia-php/framework": "0.*.*"
},
Expand Down
40 changes: 40 additions & 0 deletions src/Storage/Compression/Algorithms/Snappy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Utopia\Storage\Compression\Algorithms;

use Utopia\Storage\Compression\Compression;

class Snappy extends Compression
{
/**
* @return string
*/
public function getName(): string
{
return 'snappy';
}

/**
* Compress.
*
* @param string $data
*
* @return string
*/
public function compress(string $data):string
{
return \snappy_compress($data);
}

/**
* Decompress.
*
* @param string $data
*
* @return string
*/
public function decompress(string $data):string
{
return \snappy_uncompress($data);
}
}
81 changes: 81 additions & 0 deletions tests/Storage/Compression/Algorithms/SnappyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Utopia\Tests\Storage\Compression\Algorithms;

use Utopia\Storage\Compression\Algorithms\Snappy;
use PHPUnit\Framework\TestCase;

class SnappyTest extends TestCase
{
/**
* @var Snappy
*/
protected $object = null;

public function setUp(): void
{
$this->object = new Snappy();
}

public function tearDown(): void
{
}

public function testName()
{
$this->assertEquals($this->object->getName(), 'snappy');
}

public function testCompressDecompressWithText()
{
$demo = 'This is a demo string';
$demoSize = \mb_strlen($demo, '8bit');

$data = $this->object->compress($demo);

$dataSize = \mb_strlen($data, '8bit');

$this->assertEquals(21, $demoSize);
$this->assertEquals(23, $dataSize);

$this->assertEquals($this->object->decompress($data), $demo);
}

public function testCompressDecompressWithJPGImage()
{
$demo = \file_get_contents(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg');
$demoSize = \mb_strlen($demo, '8bit');

$data = $this->object->compress($demo);
$dataSize = \mb_strlen($data, '8bit');

$this->assertEquals(599639, $demoSize);
$this->assertEquals(599504, $dataSize);

$this->assertGreaterThan($dataSize, $demoSize);

$data = $this->object->decompress($data);
$dataSize = \mb_strlen($data, '8bit');

$this->assertEquals(599639, $dataSize);
}

public function testCompressDecompressWithPNGImage()
{
$demo = \file_get_contents(__DIR__ . '/../../../resources/disk-b/kitten-1.png');
$demoSize = \mb_strlen($demo, '8bit');

$data = $this->object->compress($demo);
$dataSize = \mb_strlen($data, '8bit');

$this->assertEquals(3038056, $demoSize);
$this->assertEquals(3038200, $dataSize);

$this->assertGreaterThan($demoSize, $dataSize);

$data = $this->object->decompress($data);
$dataSize = \mb_strlen($data, '8bit');

$this->assertEquals(3038056, $dataSize);
}
}

0 comments on commit f1da955

Please sign in to comment.