/usr/share/php/Horde/HashTable/Base.php is in php-horde-hashtable 1.2.0-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | <?php
/**
* Copyright 2013-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2013-2014 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package HashTable
*/
/**
* The Horde_HashTable class provides an API to interact with various hash
* table implementations.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013-2014 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package HashTable
*
* @property-read boolean $locking Does hash table provide locking? (@since
* 1.1.0)
* @property-read boolean $persistent Does hash table provide persistent
* storage?
* @property-write string $prefix Set the hash key prefix.
*/
abstract class Horde_HashTable_Base implements ArrayAccess, Serializable
{
/**
* A list of items known not to exist.
*
* @var array
*/
protected $_noexist = array();
/**
* Configuration parameters.
*
* @var array
*/
protected $_params = array(
'prefix' => 'hht_'
);
/**
* Persistent storage provided by driver?
*
* @var boolean
*/
protected $_persistent = false;
/**
* Constructor.
*
* @param array $params Configuration parameters:
* <pre>
* - logger: (Horde_Log_Logger) Logger object.
* - prefix: (string) Prefix to use for key storage.
* </pre>
*
* @throws Horde_HashTable_Exception
*/
public function __construct(array $params = array())
{
$this->_params = array_merge($this->_params, $params);
$this->_init();
}
/**
* Do initialization.
*
* @throws Horde_HashTable_Exception
*/
protected function _init()
{
}
/**
*/
public function __set($name, $val)
{
switch ($name) {
case 'prefix':
$this->_params['prefix'] = $val;
break;
}
}
/**
*/
public function __get($name)
{
switch ($name) {
case 'locking':
return ($this instanceof Horde_HashTable_Lock);
case 'persistent':
return $this->_persistent;
}
}
/**
* Delete a key(s).
*
* @param mixed $keys The key or an array of keys to delete.
*
* @return boolean True on success.
*/
public function delete($keys)
{
if (!is_array($keys)) {
$keys = array($keys);
}
if ($todo = array_diff($keys, array_keys($this->_noexist))) {
$to_delete = array_fill_keys(array_map(array($this, 'hkey'), $todo), $todo);
if (!$this->_delete(array_keys($to_delete))) {
return false;
}
if (!empty($this->_params['logger'])) {
$this->_params['logger']->debug(sprintf(
'%s: Deleted keys (%s)',
get_class($this),
implode(',', array_keys($to_delete))
));
}
$this->_noexist = array_merge($this->_noexist, array_fill_keys(array_values($todo), true));
}
return true;
}
/**
* Delete keys.
*
* @param array $key An array of keys to delete.
*
* @return boolean True on success.
*/
abstract protected function _delete($keys);
/**
* Do the keys exists?
*
* @param mixed $keys The key or an array of keys.
*
* @return mixed A boolean/array of booleans indicating existence (return
* type is the type of $keys).
*/
public function exists($keys)
{
return $this->_getExists($keys, array($this, '_exists'));
}
/**
* Get data associated with keys.
*
* @param array $keys An array of keys.
*
* @return array Existence check. Values are boolean true/false.
*/
abstract protected function _exists($keys);
/**
* Get data associated with a key(s).
*
* @param mixed $keys The key or an array of keys.
*
* @return mixed The string/array on success (return type is the type of
* $keys); false value(s) on failure.
*/
public function get($keys)
{
return $this->_getExists($keys, array($this, '_get'));
}
/**
* Get data associated with keys.
*
* @param array $keys An array of keys.
*
* @return array The retrieved keys. Non-existent keys should return
* false as the value.
*/
abstract protected function _get($keys);
/**
* Does a get/exists action on a set of keys.
*
* @param mixed $keys The key or an array of keys.
* @param callable $callback The internal callback action.
*
* @return mixed The results.
*/
protected function _getExists($keys, $callback)
{
$noexist = $out = $todo = array();
if (!($ret_array = is_array($keys))) {
$keys = array($keys);
}
foreach ($keys as $val) {
if (isset($this->_noexist[$val])) {
$out[$val] = false;
$noexist[] = $val;
} else {
$todo[$this->hkey($val)] = $val;
}
}
if (!empty($todo)) {
foreach (call_user_func($callback, array_keys($todo)) as $key => $val) {
if ($val === false) {
$this->_noexist[$todo[$key]] = true;
$noexist[] = $todo[$key];
}
$out[$todo[$key]] = $val;
}
}
if (!empty($this->_params['logger'])) {
if ($tmp = array_diff(array_keys($out), $noexist)) {
$this->_params['logger']->debug(sprintf(
'%s: Retrieved keys (%s)',
get_class($this),
implode(',', $tmp)
));
}
if (!empty($noexist)) {
$this->_params['logger']->debug(sprintf(
'%s: Non-existent keys (%s)',
get_class($this),
implode(',', $noexist)
));
}
}
return $ret_array
? $out
: reset($out);
}
/**
* Set the value of a key.
*
* @param string $key The key.
* @param string $val The string to store.
* @param array $opts Additional options:
* <pre>
* - expire: (integer) Expiration time in seconds.
* DEFAULT: Doesn't expire.
* - replace: (boolean) Replace the value of key. If key doesn't exist,
* returns false.
* DEFAULT: false
* </pre>
*
* @return boolean True on success, false on error.
*/
public function set($key, $val, array $opts = array())
{
if (!empty($opts['replace']) && isset($this->_noexist[$key])) {
return false;
}
/* @todo BC: 'timeout' == 'expire' usage. */
if (isset($opts['timeout']) && !isset($opts['expire'])) {
$opts['expire'] = $opts['timeout'];
}
if ($this->_set($this->hkey($key), $val, $opts)) {
unset($this->_noexist[$key]);
$res = true;
} else {
$res = false;
}
if (!empty($this->_params['logger'])) {
$this->_params['logger']->debug(sprintf(
'%s: Set key %s(%s)',
get_class($this),
$res ? '' : 'FAILED ',
$key
));
}
return $res;
}
/**
* Set the value of a key.
*
* @param string $key The key.
* @param string $val The string to store.
* @param array $opts Additional options (see set()).
*
* @return boolean True on success.
*/
abstract protected function _set($key, $val, $opts);
/**
* Clear all hash table entries.
*/
abstract public function clear();
/**
* Add local prefix to beginning of key.
*
* @param string $key Key name.
*
* @return string Hash table key identifier.
*/
public function hkey($key)
{
return $this->_params['prefix'] . $key;
}
/* ArrayAccess methods. */
/**
*/
public function offsetExists($offset)
{
return $this->exists($offset);
}
/**
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
*/
public function offsetSet($offset, $value)
{
return $this->set($offset, $value);
}
/**
*/
public function offsetUnset($offset)
{
return $this->delete($offset);
}
/* Serializable methods. */
/**
*/
public function serialize()
{
return serialize($this->_params);
}
/**
*/
public function unserialize($data)
{
$this->_params = @unserialize($data);
$this->_init();
}
}
|