From 261032f77820d0b620707ace75cfa82043558581 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Wed, 23 Oct 2019 13:29:12 +0200 Subject: cleanup Zstd ex --- preload-redis.inc | 6 +----- preload-zstd.inc | 44 ++++++++------------------------------------ zstd.php | 26 ++++++++++++++++---------- 3 files changed, 25 insertions(+), 51 deletions(-) diff --git a/preload-redis.inc b/preload-redis.inc index 199b0fd..cc8f9dd 100644 --- a/preload-redis.inc +++ b/preload-redis.inc @@ -47,11 +47,7 @@ class Redis { self::$ffi = \FFI::scope("_REMI_REDIS_"); } catch (\FFI\Exception $e) { // Try direct load - if (PHP_SAPI === 'cli' || (int)ini_get("ffi.enable")) { - self::$ffi = \FFI::load(__DIR__ . '/preload-redis.h'); - } else { - throw $e; - } + self::$ffi = \FFI::load(__DIR__ . '/preload-redis.h'); } if (!self::$ffi) { throw new \RuntimeException("FFI parse fails"); diff --git a/preload-zstd.inc b/preload-zstd.inc index f56c833..a42e5be 100644 --- a/preload-zstd.inc +++ b/preload-zstd.inc @@ -12,7 +12,7 @@ namespace Remi; class Zstd { static private $ffi = null; - public static function init() { + private static function init() { if (self::$ffi) { return; } @@ -22,70 +22,42 @@ class Zstd { echo "Using FFI::scope OK\n"; } catch (\FFI\Exception $e) { // Try direct load - if (PHP_SAPI === 'cli' || (int)ini_get("ffi.enable")) { - self::$ffi = \FFI::load(__DIR__ . '/preload-zstd.h'); - echo "Using FFI::load OK\n"; - } else { - throw $e; - } + self::$ffi = \FFI::load(__DIR__ . '/preload-zstd.h'); + echo "Using FFI::load OK\n"; } if (!self::$ffi) { throw new \RuntimeException("FFI parse fails"); } } - public static function compress($in, $out) { + public static function compress(string $src): string { self::init(); - $ret = []; - $src = file_get_contents($in); - if ($src === false) { - throw new \RuntimeException("Read fails"); - } $len = strlen($src); - $ret['in_len'] = $len; - $max = self::$ffi->ZSTD_compressBound($len); - $ret['max_len'] = $max; $comp = str_repeat(' ', $max); $clen = self::$ffi->ZSTD_compress($comp, $max, $src, $len, 6); if (self::$ffi->ZSTD_isError($clen)) { throw new \RuntimeException("Compression fails"); } - $ret['out_len'] = $clen; - if (file_put_contents($out, substr($comp, 0, $clen)) !== $clen) { - throw new \RuntimeException("Save fails"); - } - return $ret; + return substr($comp, 0, $clen); } - public static function decompress($in, $out) { + public static function decompress(string $comp): string { self::init(); - $ret = []; - $comp = file_get_contents($in); - if ($comp === false) { - throw new \RuntimeException("Read fails"); - } $clen = strlen($comp); - $ret['in_len'] = $clen; - $max = self::$ffi->ZSTD_decompressBound($comp, $clen); - $ret['max_len'] = $max; $unco = str_repeat(' ', $max); $ulen = self::$ffi->ZSTD_decompress($unco, $max, $comp, $clen); if (self::$ffi->ZSTD_isError($clen)) { - throw new \RuntimeException("Compression fails"); - } - $ret['out_len'] = $ulen; - if (file_put_contents($out, substr($unco, 0, $ulen)) !== $ulen) { - throw new \RuntimeException("Save fails"); + throw new \RuntimeException("Decompression fails"); } - return $ret; + return substr($unco, 0, $ulen); } } diff --git a/zstd.php b/zstd.php index 7faebea..0ec22d7 100644 --- a/zstd.php +++ b/zstd.php @@ -1,4 +1,5 @@ -