/** * @dataProvider providerTestStringInput */ public function testStringInput($data, $success) { $ob = new Horde_Compress_Fast(array('drivers' => array('Horde_Compress_Fast_Null'))); try { $ob->compress($data); if (!$success) { $this->fail('Expected exception.'); } } catch (Horde_Compress_Fast_Exception $e) { if ($success) { $this->fail('Unexpected exception.'); } } }
/** * Pack a string. * * @param mixed $data The data to pack. * @param array $opts Additional options: * <pre> * - compress: (mixed) If false, don't use compression. If true, uses * default compress length (DEFAULT). If 0, always compress. * All other integer values: compress only if data is * greater than this string length. * - drivers: (array) Only use these drivers to pack. By default, driver * to use is auto-determined. * - phpob: (boolean) If true, the data contains PHP serializable * objects (i.e. objects that have a PHP-specific serialized * representation). If false, the data does not contain any of * these objects. If not present, will auto-determine * existence of these objects. * </pre> * * @return string The packed string. * @throws Horde_Pack_Exception */ public function pack($data, array $opts = array()) { $opts = array_merge(array('compress' => true), $opts); if (!isset($opts['phpob'])) { $auto = new Horde_Pack_Autodetermine($data); $opts['phpob'] = $auto->phpob; } foreach (self::$_drivers as $key => $val) { if (!empty($opts['phpob']) && !$val->phpob) { continue; } if (isset($opts['drivers'])) { if (!in_array(get_class($val), $opts['drivers'])) { continue; } } /* Format of data: * First-byte: * 1,2,4,8,16,32 - Reserved for pack format. * 64 - Packed data has been compressed. * 128 - RESERVED for future use (if set, indicates that initial * byte will extend into next byte). * Packed (and compressed data) follows this byte. */ try { $packed = $val->pack($data); } catch (Horde_Pack_Exception $e) { continue; } if ($opts['compress'] !== false) { if ($opts['compress'] === 0) { $compress = true; } else { if ($opts['compress'] === true) { $opts['compress'] = self::DEFAULT_COMPRESS; } $compress = strlen($packed) > $opts['compress']; } if ($compress) { $packed = self::$_compress->compress($packed); $key |= self::COMPRESS_MASK; } } return pack('C', $key) . $packed; } throw new Horde_Pack_Exception('Could not pack data.'); }
/** * Store an object in the cache. * * @param string $key Object ID used as the caching key. * @param string $data Data to store in the cache. * @param integer $lifetime Object lifetime - i.e. the time before the * data becomes available for garbage * collection, in seconds. If null use the * default Horde GC time. If 0 will not be GC'd. */ public function set($key, $data, $lifetime = null) { if (!empty($this->_params['compress'])) { $compress = new Horde_Compress_Fast(); $data = $compress->compress($data); } $lifetime = is_null($lifetime) ? $this->_params['lifetime'] : $lifetime; $this->_storage->set($key, $data, $lifetime); }
/** * Convert data from/to storage format. * * @param mixed|MongoBinData $data The data object. * * @return mixed|MongoBinData The converted data. */ protected function _value($data) { static $compress; if (!isset($compress)) { $compress = new Horde_Compress_Fast(); } return $data instanceof MongoBinData ? @unserialize($compress->decompress($data->bin)) : new MongoBinData($compress->compress(serialize($data)), MongoBinData::BYTE_ARRAY); }
/** */ public function set($mailbox, $data, $uidvalid) { if ($uid = $this->_getUid($mailbox)) { $res = $this->get($mailbox, array_keys($data), array(), $uidvalid); } else { $res = array(); $uid = $this->_createUid($mailbox); } $compress = new Horde_Compress_Fast(); foreach ($data as $key => $val) { if (isset($res[$key])) { try { /* Update */ $this->_db->update(sprintf('UPDATE %s SET data = ? WHERE messageid = ? AND msguid = ?', self::MSG_TABLE), array(new Horde_Db_Value_Binary($compress->compress(serialize(array_merge($res[$key], $val)))), $uid, strval($key))); } catch (Horde_Db_Exception $e) { } } else { /* Insert */ try { $this->_db->insert(sprintf('INSERT INTO %s (data, msguid, messageid) VALUES (?, ?, ?)', self::MSG_TABLE), array(new Horde_Db_Value_Binary($compress->compress(serialize($val))), strval($key), $uid)); } catch (Horde_Db_Exception $e) { } } } /* Update modified time. */ try { $this->_db->update(sprintf('UPDATE %s SET modified = ? WHERE messageid = ?', self::BASE_TABLE), array(time(), $uid)); } catch (Horde_Db_Exception $e) { } /* Update uidvalidity. */ $this->setMetaData($mailbox, array('uidvalid' => $uidvalid)); }