Esempio n. 1
0
 /**
  * Compresses the given content
  *
  * @param  string $content
  * @return string
  * @throws Exception\RuntimeException on memory, output length or data warning
  */
 public function compress($content)
 {
     $compressed = snappy_compress($content);
     if ($compressed === false) {
         throw new Exception\RuntimeException('Error while compressing.');
     }
     return $compressed;
 }
Esempio n. 2
0
 /**
  * Builds a binary frame, from a frame object.
  * 
  * @param McFrazier\PhpBinaryCql\CqlFrame $frame
  * @return string
  */
 public function generateBinaryFrame($frame)
 {
     // get flag status
     $flagStatus = $this->_checkFrameFlags($frame->getFlag());
     // binary body length
     $frameBody = $frame->getBody();
     $frameBodyLength = NULL;
     // @TODO need to add a check to check which compression was used
     // in the frame, can't assume snappy.
     if ($flagStatus->compression) {
         // compressed
         $frameBody = snappy_compress($frameBody);
         $bodyLength = strlen($frameBody);
         $frameBodyLength = $this->generateInt($bodyLength);
     } else {
         // not compressed
         $bodyLength = strlen($frameBody);
         $frameBodyLength = $this->generateInt($bodyLength);
     }
     // build frame header
     $header = pack("hhhh", $frame->getVersion(), $frame->getFlag(), $frame->getStream(), $frame->getOpcode());
     $header .= $frameBodyLength;
     return $header . $frameBody;
 }
Esempio n. 3
0
 /**
  * Public for testing purposes only.
  *
  * @param string $data
  * @return string
  */
 public function _encodeData($data)
 {
     $this->profilerStart(__METHOD__);
     $originalDataSize = strlen($data);
     if ($this->_compressionThreshold > 0 && $this->_compressionLib != 'none' && $originalDataSize >= $this->_compressionThreshold) {
         if ($this->_logLevel >= \Zend_Log::DEBUG) {
             $this->_log(sprintf("Compressing %s bytes with %s", $originalDataSize, $this->_compressionLib));
             $timeStart = microtime(true);
         }
         $prefix = ':' . substr($this->_compressionLib, 0, 2) . ':';
         switch ($this->_compressionLib) {
             case 'snappy':
                 $data = snappy_compress($data);
                 break;
             case 'lzf':
                 $data = lzf_compress($data);
                 break;
             case 'lz4':
                 $data = lz4_compress($data);
                 $prefix = ':l4:';
                 break;
             case 'gzip':
                 $data = gzcompress($data, 1);
                 break;
         }
         if ($data) {
             $data = $prefix . $data;
             if ($this->_logLevel >= \Zend_Log::DEBUG) {
                 $this->_log(sprintf("Data compressed by %.1f percent in %.5f seconds", $originalDataSize == 0 ? 0 : 100 - strlen($data) / $originalDataSize * 100, microtime(true) - $timeStart));
             }
         } else {
             if ($this->_logLevel >= \Zend_Log::WARN) {
                 $this->_log(sprintf("Could not compress session data using %s", $this->_compressionLib), \Zend_Log::WARN);
             }
         }
     }
     $this->profilerStop(__METHOD__);
     return $data;
 }
 /**
  * @param string $data
  * @param int $level
  * @throws Exception
  * @return string
  */
 protected function _encodeData($data, $level)
 {
     if ($level && strlen($data) >= $this->_compressThreshold) {
         switch ($this->_compressionLib) {
             case 'snappy':
                 $data = snappy_compress($data);
                 break;
             case 'lzf':
                 $data = lzf_compress($data);
                 break;
             case 'gzip':
                 $data = gzcompress($data, $level);
                 break;
         }
         if (!$data) {
             throw new Exception("Could not compress cache data.");
         }
         return $this->_compressPrefix . $data;
     }
     return $data;
 }
Esempio n. 5
0
 /**
  * @param array $data
  * @return array
  */
 public function Compress($data)
 {
     if (!empty($this->_currentCompressFields)) {
         foreach ($this->_currentCompressFields as $field) {
             if (isset($data[$field]) && is_string($data[$field])) {
                 $data[$field] = snappy_compress($data[$field]);
                 $data[$field] = new MongoBinData($data[$field], MongoBinData::GENERIC);
             }
         }
     }
     return $data;
 }
Esempio n. 6
0
 /**
  * Encode data
  *
  * @param string $data
  * @return string
  */
 protected function _encodeData($data)
 {
     $originalDataSize = strlen($data);
     if ($this->_compressionThreshold > 0 && $this->_compressionLibrary != 'none' && $originalDataSize >= $this->_compressionThreshold) {
         $this->_log(sprintf("Compressing %s bytes with %s", $originalDataSize, $this->_compressionLibrary));
         $timeStart = microtime(true);
         $prefix = ':' . substr($this->_compressionLibrary, 0, 2) . ':';
         switch ($this->_compressionLibrary) {
             case 'snappy':
                 $data = snappy_compress($data);
                 break;
             case 'lzf':
                 $data = lzf_compress($data);
                 break;
             case 'lz4':
                 $data = lz4_compress($data);
                 $prefix = ':l4:';
                 break;
             case 'gzip':
                 $data = gzcompress($data, 1);
                 break;
         }
         if ($data) {
             $data = $prefix . $data;
             $this->_log(sprintf("Data compressed by %.1f percent in %.5f seconds", $originalDataSize == 0 ? 0 : 100 - strlen($data) / $originalDataSize * 100, microtime(true) - $timeStart));
         } else {
             $this->_log(sprintf("Could not compress session data using %s", $this->_compressionLibrary), LoggerInterface::WARNING);
         }
     }
     return $data;
 }
Esempio n. 7
0
 /**
  * Compress data
  * 
  * @param string $data
  * @return string
  * @throws Exception
  */
 protected function compress($data)
 {
     switch ($this->compression) {
         case self::COMPRESSION_LZ4:
             $compressedData = @lz4_compress($data);
             if (false === $compressedData) {
                 throw new Exception('Error compressing with lz4_compress.');
             }
             break;
         case self::COMPRESSION_SNAPPY:
             $compressedData = @snappy_compress($data);
             if (false === $compressedData) {
                 throw new Exception('Error compressing with snappy_compress.');
             }
             break;
         case self::COMPRESSION_LZF:
             $compressedData = @lzf_compress($data);
             if (false === $compressedData) {
                 throw new Exception('Error compressing with lzf_compress.');
             }
             break;
         case self::COMPRESSION_GZ:
             $compressedData = @gzcompress($data);
             if (false === $compressedData) {
                 throw new Exception('Error compressing with gzcompress.');
             }
             break;
         case self::COMPRESSION_NONE:
         default:
             $compressedData = $data;
     }
     return $compressedData;
 }
 /**
  * Public for testing purposes only.
  *
  * @param string $data
  * @return string
  */
 public function _encodeData($data)
 {
     $originalDataSize = strlen($data);
     if ($this->_compressionThreshold > 0 && $this->_compressionLib != 'none' && $originalDataSize >= $this->_compressionThreshold) {
         if ($this->_logLevel >= 7) {
             Mage::log(sprintf("%s: Compressing %s bytes with %s", $this->_getPid(), $originalDataSize, $this->_compressionLib), Zend_Log::DEBUG, self::LOG_FILE);
             // reset timer
             $this->_timeStart = microtime(true);
         }
         switch ($this->_compressionLib) {
             case 'snappy':
                 $data = snappy_compress($data);
                 break;
             case 'lzf':
                 $data = lzf_compress($data);
                 break;
             case 'gzip':
                 $data = gzcompress($data, 1);
                 break;
         }
         if ($data) {
             $data = ':' . substr($this->_compressionLib, 0, 2) . ':' . $data;
             if ($this->_logLevel >= 7) {
                 Mage::log(sprintf("%s: Data compressed by %.1f percent in %.5f seconds", $this->_getPid(), $originalDataSize == 0 ? 0 : 100 - strlen($data) / $originalDataSize * 100, microtime(true) - $this->_timeStart), Zend_Log::DEBUG, self::LOG_FILE);
             }
         } else {
             if ($this->_logLevel >= 4) {
                 Mage::log(sprintf("%s: Could not compress session data using %s", $this->_getPid(), $this->_compressionLib), Zend_Log::WARN, self::LOG_FILE);
             }
         }
     }
     return $data;
 }
Esempio n. 9
0
 /**
  * (non-PHPdoc)
  * @see Zend\Filter\Compress.CompressionAlgorithmInterface::compress()
  */
 public function compress($value)
 {
     return \snappy_compress($value);
 }
Esempio n. 10
0
$stat['length']['snappy/json'] = strlen($tmp);
$start = microtime(1);
for ($i = 0; $i < $iterationsCount; ++$i) {
    //$tmp2 = $unserialize($decompress($tmp));
    $tmp2 = json_decode(snappy_uncompress($tmp), true);
}
$stat['decode']['snappy/json'] = 1000 * (microtime(1) - $start);
//$stat['mem_usage']['snappy/json'] = memory_get_usage() - $mem_start;
$start = microtime(1);
//$mem_start = memory_get_usage();
for ($i = 0; $i < $iterationsCount; ++$i) {
    //$tmp = $compress($serialize($data)));
    $tmp = snappy_compress(igbinary_serialize($data));
}
$stat['encode']['snappy/igbinary'] = 1000 * (microtime(1) - $start);
$tmp = snappy_compress(igbinary_serialize($data));
$stat['length']['snappy/igbinary'] = strlen($tmp);
$start = microtime(1);
for ($i = 0; $i < $iterationsCount; ++$i) {
    //$tmp2 = $unserialize($decompress($tmp));
    $tmp2 = igbinary_unserialize(snappy_uncompress($tmp));
}
$stat['decode']['snappy/igbinary'] = 1000 * (microtime(1) - $start);
//$stat['mem_usage']['snappy/igbinary'] = memory_get_usage() - $mem_start;
$start = microtime(1);
//$mem_start = memory_get_usage();
for ($i = 0; $i < $iterationsCount; ++$i) {
    //$tmp = $compress($serialize($data)));
    $tmp = gzcompress(serialize($data), 9);
}
$stat['encode']['gzip-9/serialize'] = 1000 * (microtime(1) - $start);
Esempio n. 11
0
<?php

$str = "Holy f**k. Are they actually going to get there?";
$ret = snappy_compress($str);
$ret = snappy_compress($ret);
$ret = snappy_compress($ret);
$ret = snappy_uncompress($ret);
$ret = snappy_uncompress($ret);
$ret = snappy_uncompress($ret);
var_dump($ret === $str);
$str = str_repeat("x", 1000);
$ret = snappy_compress($str);
$ret = snappy_uncompress($ret);
var_dump($ret === $str);