svnno****@sourc*****
svnno****@sourc*****
2009年 12月 30日 (水) 10:32:31 JST
Revision: 746 http://sourceforge.jp/projects/p2-php/svn/view?view=rev&revision=746 Author: rsk Date: 2009-12-30 10:32:31 +0900 (Wed, 30 Dec 2009) Log Message: ----------- expack: - KeyValueStore ãã¡ããªã¼ã®ã¯ã©ã¹åã P2KeyValueStore ã§å§ã¾ãããã«å¤æ´ã Added Paths: ----------- p2ex/trunk/lib/P2KeyValueStore/ p2ex/trunk/lib/P2KeyValueStore/Binary.php p2ex/trunk/lib/P2KeyValueStore/Compressing.php p2ex/trunk/lib/P2KeyValueStore/Iterator.php p2ex/trunk/lib/P2KeyValueStore/SJIS.php p2ex/trunk/lib/P2KeyValueStore/Serializing.php p2ex/trunk/lib/P2KeyValueStore.php Removed Paths: ------------- p2ex/trunk/lib/BinaryStore.php p2ex/trunk/lib/CompressingStore.php p2ex/trunk/lib/KeyValueStore.php p2ex/trunk/lib/KeyValueStoreIterator.php p2ex/trunk/lib/SerializingStore.php p2ex/trunk/lib/SjisStore.php -------------- next part -------------- Deleted: p2ex/trunk/lib/BinaryStore.php =================================================================== --- p2ex/trunk/lib/BinaryStore.php 2009-12-29 08:21:27 UTC (rev 745) +++ p2ex/trunk/lib/BinaryStore.php 2009-12-30 01:32:31 UTC (rev 746) @@ -1,52 +0,0 @@ -<?php -require_once dirname(__FILE__) . '/KeyValueStore.php'; - -// {{{ BinaryStore - -/** - * oCif[^ði±»·é - */ -class BinaryStore extends KeyValueStore -{ - // {{{ _encodeValue() - - /** - * f[^ðBase64GR[h·é - * - * @param string $value - * @return string - */ - protected function _encodeValue($value) - { - return base64_encode($value); - } - - // }}} - // {{{ _decodeValue() - - /** - * f[^ðBase64fR[h·é - * - * @param string $value - * @return string - */ - protected function _decodeValue($value) - { - return base64_decode($value); - } - - // }}} -} - -// }}} - -/* - * Local Variables: - * mode: php - * coding: cp932 - * tab-width: 4 - * c-basic-offset: 4 - * indent-tabs-mode: nil - * End: - */ -// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Deleted: p2ex/trunk/lib/CompressingStore.php =================================================================== --- p2ex/trunk/lib/CompressingStore.php 2009-12-29 08:21:27 UTC (rev 745) +++ p2ex/trunk/lib/CompressingStore.php 2009-12-30 01:32:31 UTC (rev 746) @@ -1,52 +0,0 @@ -<?php -require_once dirname(__FILE__) . '/BinaryStore.php'; - -// {{{ CompressingStore - -/** - * TCYÌå«¢f[^ð³kµÄi±»·é - */ -class CompressingStore extends BinaryStore -{ - // {{{ _encodeValue() - - /** - * f[^ð³k·é - * - * @param string $value - * @return string - */ - protected function _encodeValue($value) - { - return parent::_encodeValue(gzdeflate($value, 6)); - } - - // }}} - // {{{ _decodeValue() - - /** - * f[^ðWJ·é - * - * @param string $value - * @return string - */ - protected function _decodeValue($value) - { - return gzinflate(parent::_decodeValue($value)); - } - - // }}} -} - -// }}} - -/* - * Local Variables: - * mode: php - * coding: cp932 - * tab-width: 4 - * c-basic-offset: 4 - * indent-tabs-mode: nil - * End: - */ -// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Deleted: p2ex/trunk/lib/KeyValueStore.php =================================================================== --- p2ex/trunk/lib/KeyValueStore.php 2009-12-29 08:21:27 UTC (rev 745) +++ p2ex/trunk/lib/KeyValueStore.php 2009-12-30 01:32:31 UTC (rev 746) @@ -1,868 +0,0 @@ -<?php -// {{{ KeyValueStore - -/** - * L[/lÌyAðSQLite3Ìf[^x[XÉÛ¶·é Key-Value Store - */ -class KeyValueStore implements ArrayAccess, Countable, IteratorAggregate -{ - // {{{ constants - - const Q_TABLEEXISTS = 'SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = :table LIMIT 1'; - const Q_CREATETABLE = 'CREATE TABLE $__table ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - arkey TEXT NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT REPLACE, - value TEXT NOT NULL, - mtime INTEGER DEFAULT (strftime(\'%s\',\'now\')), - sort_order INTEGER DEFAULT 0 -)'; - const Q_COUNT = 'SELECT COUNT(*) FROM $__table LIMIT 1'; - const Q_EXSITS = 'SELECT id, mtime FROM $__table WHERE arkey = :key LIMIT 1'; - const Q_FINDBYID = 'SELECT * FROM $__table WHERE id = :id LIMIT 1'; - const Q_GET = 'SELECT * FROM $__table WHERE arkey = :key LIMIT 1'; - const Q_GETALL = 'SELECT * FROM $__table'; - const Q_GETIDS = 'SELECT id FROM $__table'; - const Q_GETKEYS = 'SELECT arkey FROM $__table'; - const Q_SAVE = 'INSERT INTO $__table (arkey, value, sort_order) VALUES (:key, :value, :order)'; - const Q_UPDATE = 'UPDATE $__table SET value = :value, mtime = strftime(\'%s\',\'now\'), sort_order = :order WHERE arkey = :key'; - const Q_TOUCH = 'UPDATE $__table SET mtime = (CASE WHEN :mtime IS NULL THEN strftime(\'%s\',\'now\') ELSE :mtime END) WHERE arkey = :key'; - const Q_SETORDER = 'UPDATE $__table SET sort_order = :order WHERE arkey = :key'; - const Q_DELETE = 'DELETE FROM $__table WHERE arkey = :key'; - const Q_DELETEBYID = 'DELETE FROM $__table WHERE id = :id'; - const Q_CLEAN = 'DELETE FROM $__table'; - const Q_GC = 'DELETE FROM $__table WHERE mtime < :expires'; - - // }}} - // {{{ staric private properties - - /** - * f[^x[XÉêÓÈPDO,PDOStatement,KeyValueStoreÌCX^XðÛ·ézñ - * - * @var array - */ - static private $_objects = array(); - - // }}} - // {{{ private properties - - /** - * PDOÌCX^X - * - * @var PDO - */ - private $_conn; - - /** - * SQLite3f[^x[XÌpX - * - * @var string - */ - private $_path; - - /** - * ¯ÊqƵÄNH[gÏÝÌe[u¼ - * - * @var string - */ - private $_quotedTableName; - - // }}} - // {{{ getStore() - - /** - * VOg\bh - * - * @param string $fileName - * @param string $className - * @param string &$openedPath - * @return KeyValueStore - * @throws InvalidArgumentException, UnexpectedValueException, RuntimeException, PDOException - */ - static public function getStore($fileName, $className = 'KeyValueStore', &$openedPath = null) - { - // øÌ^ð`FbN - if (!is_string($fileName)) { - throw new InvalidArgumentException('Parameter #1 \'$fileName\' should be a string value'); - } - if (!is_string($className)) { - throw new InvalidArgumentException('Parameter #2 \'$className\' should be a string value'); - } - - // NX¼ð`FbN - if (strcasecmp($className, 'KeyValueStore') != 0) { - if (!class_exists($className, false)) { - throw new UnexpectedValueException("Class '{$className}' is not declared"); - } - if (!is_subclass_of($className, 'KeyValueStore')) { - throw new UnexpectedValueException("Class '{$className}' is not a subclass of KeyValueStore"); - } - } - - // f[^x[Xt@Cð`FbN - if ($fileName == ':memory:') { - $path = $fileName; - $createTable = true; - } elseif (file_exists($fileName)) { - if (!is_file($fileName)) { - throw new RuntimeException("'{$fileName}' is not a standard file"); - } - if (!is_writable($fileName)) { - throw new RuntimeException("File '{$fileName}' is not writable"); - } - $path = realpath($fileName); - $createTable = false; - } else { - if (strpos($fileName, '/') !== false || - (strncasecmp(PHP_OS, 'WIN', 3) == 0 && strpos($fileName, '\\') !== false)) - { - $dirName = dirname($fileName); - $baseName = basename($fileName); - } else { - $dirName = getcwd(); - $baseName = $fileName; - } - if (!is_string($dirName) || !is_dir($dirName)) { - throw new RuntimeException("No directory for '{$fileName}'"); - } - if (!is_writable($dirName)) { - throw new RuntimeException("Directory '{$dirName}' is not writable"); - } - $path = realpath($dirName) . DIRECTORY_SEPARATOR . $baseName; - $createTable = true; - } - - $lcname = strtolower($className); - $tableName = 'kvs_' . $lcname; - $openedPath = $path; - - // CX^Xð쬵AÃIÏÉÛ - if (array_key_exists($path, self::$_objects)) { - if (array_key_exists($lcname, self::$_objects[$path]['persisters'])) { - $kvs = self::$_objects[$path]['persisters'][$lcname]; - } else { - $conn = self::$_objects[$path]['connection']; - $kvs = new $className($conn, $path, $tableName); - self::$_objects[$path]['persisters'][$lcname] = $kvs; - } - } else { - $conn = new PDO('sqlite:' . $path); - $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $kvs = new $className($conn, $path, $tableName); - self::$_objects[$path] = array( - 'connection' => $conn, - 'statements' => array(), - 'persisters' => array($lcname => $kvs), - ); - } - - return $kvs; - } - - // }}} - // {{{ constructor - - /** - * RXgN^ - * getStore()©çÄÑo³êé - * - * @param PDO $conn - * @param string $path - * @param string $tableName - * @throws PDOException - */ - private function __construct(PDO $conn, $path, $tableName) - { - $this->_conn = $conn; - $this->_path = $path; - $this->_quotedTableName = '"' . str_replace('"', '""', $tableName) . '"'; - - // e[uª¶Ý·é©ð²× - $stmt = $conn->prepare(self::Q_TABLEEXISTS); - $stmt->bindValue(':table', $tableName, PDO::PARAM_STR); - $stmt->execute(); - $exists = $stmt->fetchColumn(); - $stmt->closeCursor(); - unset($stmt); - - // ³¯êÎìé - if (!$exists) { - $conn->exec(str_replace('$__table', $this->_quotedTableName, self::Q_CREATETABLE)); - } - } - - // }}} - // {{{ _prepare() - - /** - * vyA[hXe[ggð쬷é - * - * @param string $query - * @param bool $isTemporary - * @return PDOStatement - * @throws PDOException - */ - private function _prepare($query, $isTemporary = false) - { - $query = str_replace('$__table', $this->_quotedTableName, $query); - - if (!$isTemporary && array_key_exists($query, self::$_objects[$this->_path]['statements'])) { - $stmt = self::$_objects[$this->_path]['statements'][$query]; - } else { - if (strncmp($query, 'SELECT ', 7) == 0) { - $stmt = $this->_conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); - } else { - $stmt = $this->_conn->prepare($query); - } - if (!$isTemporary) { - self::$_objects[$this->_path]['statements'][$query] = $stmt; - } - } - - return $stmt; - } - - // }}} - // {{{ _buildOrderBy() - - /** - * R[hðÜÆßÄæ¾·éÛÌOREDER BYå𶬷é - * - * @param array $orderBy - * @return string - */ - private function _buildOrderBy(array $orderBy = null) - { - if ($orderBy === null) { - return ' ORDER BY sort_order ASC, arkey ASC'; - } - - $terms = array(); - foreach ($orderBy as $column => $ascending) { - $direction = $ascending ? 'ASC' : 'DESC'; - switch ($column) { - case 'id': - $terms[] = 'id ' . $direction; - break; - case 'key': - $terms[] = 'arkey ' . $direction; - break; - case 'value': - $terms[] = 'value ' . $direction; - break; - case 'mtime': - $terms[] = 'mtime ' . $direction; - break; - case 'order': - $terms[] = 'sort_order ' . $direction; - break; - } - } - - if (count($terms)) { - return ' ORDER BY ' . implode(', ', $terms); - } else { - return ''; - } - } - - // }}} - // {{{ _buildLimit() - - /** - * R[hðÜÆßÄæ¾·éÛÌLIMITåÆOFFSETå𶬷é - * - * @param int $limit - * @param int $offset - * @return string - */ - private function _buildLimit($limit = null, $offset = null) - { - if ($limit === null) { - return ''; - } elseif ($offset === null) { - return sprintf(' LIMIT %d', $limit); - } else { - return sprintf(' LIMIT %d OFFSET %d', $limit, $offset); - } - } - - // }}} - // {{{ _encodeKey() - - /** - * L[ðUTF-8 or US-ASCII¶ñÉGR[h·é - * - * @param string $key - * @return string - */ - protected function _encodeKey($key) - { - return (string)$key; - } - - // }}} - // {{{ _decodeKey() - - /** - * L[ðfR[h·é - * - * @param string $key - * @return string - */ - protected function _decodeKey($key) - { - return $key; - } - - // }}} - // {{{ _encodeValue() - - /** - * lðUTF-8 or US-ASCII¶ñÉGR[h·é - * - * @param string $value - * @return string - */ - protected function _encodeValue($value) - { - return (string)$value; - } - - // }}} - // {{{ _decodeValue() - - /** - * lðfR[h·é - * - * @param string $value - * @return string - */ - protected function _decodeValue($value) - { - return $value; - } - - // }}} - // {{{ exists() - - /** - * L[ÉηélªÛ¶³êÄ¢é©ð²×é - * - * @param string $key - * @param int $lifeTime - * @return bool - */ - public function exists($key, $lifeTime = null) - { - $stmt = $this->_prepare(self::Q_EXSITS); - $stmt->setFetchMode(PDO::FETCH_ASSOC); - $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); - $stmt->execute(); - $row = $stmt->fetch(); - $stmt->closeCursor(); - if ($row === false) { - return false; - } elseif ($lifeTime !== null && $row['mtime'] < time() - $lifeTime) { - $this->deleteById($row['id']); - return false; - } else { - return true; - } - } - - // }}} - // {{{ findById() - - /** - * IDÉηéL[ÆlÌyAðæ¾·é - * åƵÄKeyValueStoreIteratorÅg¤ - * - * @param int $id - * @param int $lifeTime - * @return array - */ - public function findById($id, $lifeTime = null) - { - $stmt = $this->_prepare(self::Q_FINDBYID); - $stmt->setFetchMode(PDO::FETCH_ASSOC); - $stmt->bindValue(':id', (int)$id, PDO::PARAM_INT); - $stmt->execute(); - $row = $stmt->fetch(); - $stmt->closeCursor(); - if ($row === false) { - return null; - } elseif ($lifeTime !== null && $row['mtime'] < time() - $lifeTime) { - $this->deleteById($id); - return null; - } else { - return array( - 'key' => $this->_decodeKey($row['arkey']), - 'value' => $this->_decodeValue($row['value']), - ); - } - } - - // }}} - // {{{ get() - - /** - * L[Éηélðæ¾·é - * - * @param string $key - * @param int $lifeTime - * @return string - */ - public function get($key, $lifeTime = null) - { - $stmt = $this->_prepare(self::Q_GET); - $stmt->setFetchMode(PDO::FETCH_ASSOC); - $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); - $stmt->execute(); - $row = $stmt->fetch(); - $stmt->closeCursor(); - if ($row === false) { - return null; - } elseif ($lifeTime !== null && $row['mtime'] < time() - $lifeTime) { - $this->deleteById($row['id']); - return null; - } else { - return $this->_decodeValue($row['value']); - } - } - - // }}} - // {{{ getDetail() - - /** - * L[ÉηéR[hðæ¾·é - * - * @param string $key - * @param int $lifeTime - * @return array - */ - public function getDetail($key, $lifeTime = null) - { - $stmt = $this->_prepare(self::Q_GET); - $stmt->setFetchMode(PDO::FETCH_ASSOC); - $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); - $stmt->execute(); - $row = $stmt->fetch(); - $stmt->closeCursor(); - if ($row === false) { - return null; - } elseif ($lifeTime !== null && $row['mtime'] < time() - $lifeTime) { - $this->deleteById($row['id']); - return null; - } else { - return array( - 'id' => (int)$row['id'], - 'key' => $this->_decodeKey($row['arkey']), - 'value' => $this->_decodeValue($row['value']), - 'mtime' => (int)$row['mtime'], - 'order' => (int)$row['sort_order'], - ); - } - } - - // }}} - // {{{ getAll() - - /** - * SÄÌR[hðAzzñƵÄÔ· - * LøúÀØêÌR[hðOµ½¢êÍOÉgc()µÄ¨±Æ - * - * @param array $orderBy - * @param int $limit - * @param int $offset - * @param bool $getDetails - * @return array - */ - public function getAll(array $orderBy = null, $limit = null, $offset = null, $getDetails = false) - { - $query = self::Q_GETALL - . $this->_buildOrderBy($orderBy) - . $this->_buildLimit($limit, $offset); - $stmt = $this->_prepare($query, true); - $stmt->setFetchMode(PDO::FETCH_ASSOC); - $stmt->execute(); - $values = array(); - if ($getDetails) { - while ($row = $stmt->fetch()) { - $key = $this->_decodeKey($row['arkey']); - $values[$key] = array( - 'id' => (int)$row['id'], - 'key' => $key, - 'value' => $this->_decodeValue($row['value']), - 'mtime' => (int)$row['mtime'], - 'order' => (int)$row['sort_order'], - ); - } - } else { - while ($row = $stmt->fetch()) { - $values[$this->_decodeKey($row['arkey'])] = $this->_decodeValue($row['value']); - } - } - $stmt->closeCursor(); - return $values; - } - - // }}} - // {{{ getIds() - - /** - * SÄÌIDÌzñðÔ· - * åƵÄKeyValueStoreIteratorÅg¤ - * LøúÀØêÌR[hðOµ½¢êÍOÉgc()µÄ¨±Æ - * - * @param array $orderBy - * @param int $limit - * @param int $offset - * @return array - */ - public function getIds(array $orderBy = null, $limit = null, $offset = null) - { - $query = self::Q_GETIDS - . $this->_buildOrderBy($orderBy) - . $this->_buildLimit($limit, $offset); - $stmt = $this->_prepare($query, true); - $stmt->setFetchMode(PDO::FETCH_COLUMN, 0); - $stmt->execute(); - $ids = array(); - while (($id = $stmt->fetch()) !== false) { - $ids[] = (int)$id; - } - $stmt->closeCursor(); - return $ids; - } - - // }}} - // {{{ getKeys() - - /** - * SÄÌL[ÌzñðÔ· - * LøúÀØêÌR[hðOµ½¢êÍOÉgc()µÄ¨±Æ - * - * @param array $orderBy - * @param int $limit - * @param int $offset - * @return array - */ - public function getKeys(array $orderBy = null, $limit = null, $offset = null) - { - $query = self::Q_GETKEYS - . $this->_buildOrderBy($orderBy) - . $this->_buildLimit($limit, $offset); - $stmt = $this->_prepare($query, true); - $stmt->setFetchMode(PDO::FETCH_COLUMN, 0); - $stmt->execute(); - $keys = array(); - while (($key = $stmt->fetch()) !== false) { - $keys[] = $this->_decodeKey($key); - } - $stmt->closeCursor(); - return $keys; - } - - // }}} - // {{{ set() - - /** - * f[^ðÛ¶·é - * - * @param string $key - * @param string $value - * @param int $order - * @return bool - */ - public function set($key, $value, $order = 0) - { - $stmt = $this->_prepare(self::Q_SAVE); - $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); - $stmt->bindValue(':value', $this->_encodeValue($value), PDO::PARAM_STR); - $stmt->bindValue(':order', (int)$order, PDO::PARAM_INT); - if ($stmt->execute()) { - return $stmt->rowCount() == 1; - } else { - return false; - } - } - - // }}} - // {{{ update() - - /** - * f[^ðXV·é - * - * @param string $key - * @param string $value - * @param int $order - * @return bool - */ - public function update($key, $value, $order = 0) - { - $stmt = $this->_prepare(self::Q_UPDATE); - $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); - $stmt->bindValue(':value', $this->_encodeValue($value), PDO::PARAM_STR); - $stmt->bindValue(':order', (int)$order, PDO::PARAM_INT); - if ($stmt->execute()) { - return $stmt->rowCount() == 1; - } else { - return false; - } - } - - // }}} - // {{{ touch() - - /** - * f[^ÌXVúð»ÝÉÝè·é - * - * @param string $key - * @param int $time - * @return bool - */ - public function touch($key, $time = null) - { - $stmt = $this->_prepare(self::Q_TOUCH); - $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); - if ($time === null) { - $stmt->bindValue(':mtime', null, PDO::PARAM_NULL); - } else { - $stmt->bindValue(':mtime', (int)$time, PDO::PARAM_INT); - } - if ($stmt->execute()) { - return $stmt->rowCount() == 1; - } else { - return false; - } - } - - // }}} - // {{{ setOrder() - - /** - * f[^ÌÀÑ (sort_orderJÌl) ðÝè·é - * - * @param string $key - * @param int $order - * @return bool - */ - public function setOrder($key, $order) - { - $stmt = $this->_prepare(self::Q_SETORDER); - $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); - $stmt->bindValue(':order', (int)$order, PDO::PARAM_INT); - if ($stmt->execute()) { - return $stmt->rowCount() == 1; - } else { - return false; - } - } - - // }}} - // {{{ delete() - - /** - * L[ÉηéR[hðí·é - * - * @param string $key - * @return bool - */ - public function delete($key) - { - $stmt = $this->_prepare(self::Q_DELETE); - $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); - if ($stmt->execute()) { - return $stmt->rowCount() == 1; - } else { - return false; - } - } - - // }}} - // {{{ deleteById() - - /** - * IDÉηéR[hðí·é - * - * @param int $id - * @return bool - */ - public function deleteById($id) - { - $stmt = $this->_prepare(self::Q_DELETEBYID); - $stmt->bindValue(':id', (int)$id, PDO::PARAM_INT); - if ($stmt->execute()) { - return $stmt->rowCount() == 1; - } else { - return false; - } - } - - // }}} - // {{{ clean() - - /** - * ·×ÄÌR[hðí·é - * - * @param void - * @return int - */ - public function clean() - { - $stmt = $this->_prepare(self::Q_CLEAN, true); - if ($stmt->execute()) { - return $stmt->rowCount(); - } else { - return false; - } - } - - // }}} - // {{{ gc() - - /** - * úÀØêÌR[hðí·é - * - * @param int $lifeTime - * @return int - */ - public function gc($lifeTime) - { - $stmt = $this->_prepare(self::Q_GC, true); - $stmt->bindValue(':expires', time() - $lifeTime, PDO::PARAM_INT); - if ($stmt->execute()) { - return $stmt->rowCount(); - } else { - return false; - } - } - - // }}} - // {{{ vacuum() - - /** - * ì¬ÏÝvyA[hXe[ggðNAµAVACUUMðs·é - * ¼ÌvZXª¯¶f[^x[XðJ¢Ä¢éÆ«ÉÀs·×«ÅÍÈ¢ - * - * @param void - * @return void - */ - public function vacuum() - { - self::$_objects[$this->_path]['statements'] = array(); - $this->_conn->exec('VACUUM'); - } - - // }}} - // {{{ count() - - /** - * Countable::count() - * - * R[hÌðÔ· - * - * @param void - * @return int - */ - public function count() - { - $stmt = $this->_prepare(self::Q_COUNT); - $stmt->execute(); - $ret = (int)$stmt->fetchColumn(); - $stmt->closeCursor(); - return $ret; - } - - // }}} - // {{{ offsetExists() - - /** - * ArrayAccess::offsetExists() - * - * @param string $offset - * @return string - */ - public function offsetExists($offset) - { - return $this->exists($offset); - } - - // }}} - // {{{ offsetGet() - - /** - * ArrayAccess::offsetGet() - * - * @param string $offset - * @return mixed - */ - public function offsetGet($offset) - { - return $this->get($offset); - } - - // }}} - // {{{ offsetSet() - - /** - * ArrayAccess::offsetSet() - * - * @param string $offset - * @param string $value - * @return void - */ - public function offsetSet($offset, $value) - { - $this->set($offset, $value); - } - - // }}} - // {{{ offsetUnset() - - /** - * ArrayAccess::offsetUnset() - * - * @param string $offset - * @return void - */ - public function offsetUnset($offset) - { - $this->delete($offset); - } - - // }}} - // {{{ getIterator() - - /** - * IteratorAggregate::getIterator() - * - * ½Égetn\bhðÄÎêÄàåävÈæ¤É - * \ßæ¾µ½IDÌXgðg¤Ce[^ðÔ· - * - * @param void - * @return KeyValueStoreIterator - */ - public function getIterator() - { - if (!class_exists('KeyValueStoreIterator', false)) { - include dirname(__FILE__) . '/KeyValueStoreIterator.php'; - } - return new KeyValueStoreIterator($this); - } - - // }}} -} - -// }}} - -/* - * Local Variables: - * mode: php - * coding: cp932 - * tab-width: 4 - * c-basic-offset: 4 - * indent-tabs-mode: nil - * End: - */ -// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Deleted: p2ex/trunk/lib/KeyValueStoreIterator.php =================================================================== --- p2ex/trunk/lib/KeyValueStoreIterator.php 2009-12-29 08:21:27 UTC (rev 745) +++ p2ex/trunk/lib/KeyValueStoreIterator.php 2009-12-30 01:32:31 UTC (rev 746) @@ -1,170 +0,0 @@ -<?php -require_once dirname(__FILE__) . '/KeyValueStore.php'; - -// {{{ KeyValueStoreIterator - -/** - * KeyValueStorepCe[^ - */ -class KeyValueStoreIterator implements Iterator -{ - // {{{ private properties - - /** - * KeyValueStoreÌCX^X - * - * @var KeyValueStore - */ - private $_kvs; - - /** - * KeyValueStore::getIds()ªÔ·IDÌXg - * - * @var array - */ - private $_ids; - - /** - * $_idsÌà|C^ªw·l - * - * @var int - */ - private $_currentId; - - /** - * $_currentIdÉηéL[ - * - * @var string - */ - private $_currentKey; - - /** - * $_currentIdÉηél - * - * @var mixed - */ - private $_currentValue; - - // }}} - // {{{ _fetchCurrent() - - /** - * $_currentKeyÆ$_currentValueðæ¾·é - * - * @param void - * @return void - */ - private function _fetchCurrent() - { - if ($this->_currentId === false || - ($pair = $this->_kvs->findById($this->_currentId)) === null) - { - $this->_currentKey = $this->_currentValue = null; - } else { - $this->_currentKey = $pair['key']; - $this->_currentValue = $pair['value']; - } - } - - // }}} - // {{{ constructor - - /** - * RXgN^ - * - * @param KeyValueStore $kvs - */ - public function __construct(KeyValueStore $kvs) - { - $this->_kvs = $kvs; - $this->_ids = $kvs->getIds(); - $this->_currentId = false; - } - - // }}} - // {{{ current() - - /** - * Iterator::current() - * - * @param void - * @return mixed - */ - public function current() - { - return $this->_currentValue; - } - - // }}} - // {{{ key() - - /** - * Iterator::key() - * - * @param void - * @return string - */ - public function key() - { - return $this->_currentKey; - } - - // }}} - // {{{ next() - - /** - * Iterator::next() - * - * @param void - * @return void - */ - public function next() - { - $this->_currentId = next($this->_ids); - $this->_fetchCurrent(); - } - - // }}} - // {{{ rewind() - - /** - * Iterator::rewind() - * - * @param void - * @return void - */ - public function rewind() - { - $this->_currentId = reset($this->_ids); - $this->_fetchCurrent(); - } - - // }}} - // {{{ valid() - - /** - * Iterator::valid() - * - * @param void - * @return bool - */ - public function valid() - { - return $this->_currentId !== false; - } - - // }}} -} - -// }}} - -/* - * Local Variables: - * mode: php - * coding: cp932 - * tab-width: 4 - * c-basic-offset: 4 - * indent-tabs-mode: nil - * End: - */ -// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Copied: p2ex/trunk/lib/P2KeyValueStore/Binary.php (from rev 745, p2ex/trunk/lib/BinaryStore.php) =================================================================== --- p2ex/trunk/lib/P2KeyValueStore/Binary.php (rev 0) +++ p2ex/trunk/lib/P2KeyValueStore/Binary.php 2009-12-30 01:32:31 UTC (rev 746) @@ -0,0 +1,52 @@ +<?php +require_once dirname(__FILE__) . '/KeyValueStore.php'; + +// {{{ BinaryStore + +/** + * oCif[^ði±»·é + */ +class BinaryStore extends KeyValueStore +{ + // {{{ _encodeValue() + + /** + * f[^ðBase64GR[h·é + * + * @param string $value + * @return string + */ + protected function _encodeValue($value) + { + return base64_encode($value); + } + + // }}} + // {{{ _decodeValue() + + /** + * f[^ðBase64fR[h·é + * + * @param string $value + * @return string + */ + protected function _decodeValue($value) + { + return base64_decode($value); + } + + // }}} +} + +// }}} + +/* + * Local Variables: + * mode: php + * coding: cp932 + * tab-width: 4 + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ +// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Copied: p2ex/trunk/lib/P2KeyValueStore/Compressing.php (from rev 745, p2ex/trunk/lib/CompressingStore.php) =================================================================== --- p2ex/trunk/lib/P2KeyValueStore/Compressing.php (rev 0) +++ p2ex/trunk/lib/P2KeyValueStore/Compressing.php 2009-12-30 01:32:31 UTC (rev 746) @@ -0,0 +1,52 @@ +<?php +require_once dirname(__FILE__) . '/BinaryStore.php'; + +// {{{ CompressingStore + +/** + * TCYÌå«¢f[^ð³kµÄi±»·é + */ +class CompressingStore extends BinaryStore +{ + // {{{ _encodeValue() + + /** + * f[^ð³k·é + * + * @param string $value + * @return string + */ + protected function _encodeValue($value) + { + return parent::_encodeValue(gzdeflate($value, 6)); + } + + // }}} + // {{{ _decodeValue() + + /** + * f[^ðWJ·é + * + * @param string $value + * @return string + */ + protected function _decodeValue($value) + { + return gzinflate(parent::_decodeValue($value)); + } + + // }}} +} + +// }}} + +/* + * Local Variables: + * mode: php + * coding: cp932 + * tab-width: 4 + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ +// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Copied: p2ex/trunk/lib/P2KeyValueStore/Iterator.php (from rev 745, p2ex/trunk/lib/KeyValueStoreIterator.php) =================================================================== --- p2ex/trunk/lib/P2KeyValueStore/Iterator.php (rev 0) +++ p2ex/trunk/lib/P2KeyValueStore/Iterator.php 2009-12-30 01:32:31 UTC (rev 746) @@ -0,0 +1,170 @@ +<?php +require_once dirname(__FILE__) . '/KeyValueStore.php'; + +// {{{ KeyValueStoreIterator + +/** + * KeyValueStorepCe[^ + */ +class KeyValueStoreIterator implements Iterator +{ + // {{{ private properties + + /** + * KeyValueStoreÌCX^X + * + * @var KeyValueStore + */ + private $_kvs; + + /** + * KeyValueStore::getIds()ªÔ·IDÌXg + * + * @var array + */ + private $_ids; + + /** + * $_idsÌà|C^ªw·l + * + * @var int + */ + private $_currentId; + + /** + * $_currentIdÉηéL[ + * + * @var string + */ + private $_currentKey; + + /** + * $_currentIdÉηél + * + * @var mixed + */ + private $_currentValue; + + // }}} + // {{{ _fetchCurrent() + + /** + * $_currentKeyÆ$_currentValueðæ¾·é + * + * @param void + * @return void + */ + private function _fetchCurrent() + { + if ($this->_currentId === false || + ($pair = $this->_kvs->findById($this->_currentId)) === null) + { + $this->_currentKey = $this->_currentValue = null; + } else { + $this->_currentKey = $pair['key']; + $this->_currentValue = $pair['value']; + } + } + + // }}} + // {{{ constructor + + /** + * RXgN^ + * + * @param KeyValueStore $kvs + */ + public function __construct(KeyValueStore $kvs) + { + $this->_kvs = $kvs; + $this->_ids = $kvs->getIds(); + $this->_currentId = false; + } + + // }}} + // {{{ current() + + /** + * Iterator::current() + * + * @param void + * @return mixed + */ + public function current() + { + return $this->_currentValue; + } + + // }}} + // {{{ key() + + /** + * Iterator::key() + * + * @param void + * @return string + */ + public function key() + { + return $this->_currentKey; + } + + // }}} + // {{{ next() + + /** + * Iterator::next() + * + * @param void + * @return void + */ + public function next() + { + $this->_currentId = next($this->_ids); + $this->_fetchCurrent(); + } + + // }}} + // {{{ rewind() + + /** + * Iterator::rewind() + * + * @param void + * @return void + */ + public function rewind() + { + $this->_currentId = reset($this->_ids); + $this->_fetchCurrent(); + } + + // }}} + // {{{ valid() + + /** + * Iterator::valid() + * + * @param void + * @return bool + */ + public function valid() + { + return $this->_currentId !== false; + } + + // }}} +} + +// }}} + +/* + * Local Variables: + * mode: php + * coding: cp932 + * tab-width: 4 + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ +// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Copied: p2ex/trunk/lib/P2KeyValueStore/SJIS.php (from rev 745, p2ex/trunk/lib/SjisStore.php) =================================================================== --- p2ex/trunk/lib/P2KeyValueStore/SJIS.php (rev 0) +++ p2ex/trunk/lib/P2KeyValueStore/SJIS.php 2009-12-30 01:32:31 UTC (rev 746) @@ -0,0 +1,108 @@ +<?php +require_once dirname(__FILE__) . '/KeyValueStore.php'; + +// {{{ SjisStore + +/** + * Shift_JIS̶ñðUTF-8ÉÏ·µÄi±»·é + */ +class SjisStore extends KeyValueStore +{ + // {{{ _encode() + + /** + * Shift_JIS (CP932) ̶ñðUTF-8ÉÏ··é + * + * @param string $str + * @return string + */ + private function _encode($str) + { + return mb_convert_encoding($str, 'UTF-8', 'CP932'); + } + + // }}} + // {{{ _decode() + + /** + * UTF-8̶ñðShift_JIS (CP932) ÉÏ··é + * + * @param string $str + * @return string + */ + private function _decode($str) + { + return mb_convert_encoding($str, 'CP932', 'UTF-8'); + } + + // }}} + // {{{ _encodeKey() + + /** + * L[ðGR[h·é + * + * @param string $key + * @return string + */ + protected function _encodeKey($key) + { + return $this->_encode($key); + } + + // }}} + // {{{ _decodeKey() + + /** + * L[ðfR[h·é + * + * @param string $key + * @return string + */ + protected function _decodeKey($key) + { + return $this->_decode($key); + } + + // }}} + // {{{ _encodeValue() + + /** + * lðGR[h·é + * + * @param string $value + * @return string + */ + protected function _encodeValue($value) + { + return $this->_encode($value); + } + + // }}} + // {{{ _decodeValue() + + /** + * lðfR[h·é + * + * @param string $value + * @return string + */ + protected function _decodeValue($value) + { + return $this->_decode($value); + } + + // }}} +} + +// }}} + +/* + * Local Variables: + * mode: php + * coding: cp932 + * tab-width: 4 + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ +// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Copied: p2ex/trunk/lib/P2KeyValueStore/Serializing.php (from rev 745, p2ex/trunk/lib/SerializingStore.php) =================================================================== --- p2ex/trunk/lib/P2KeyValueStore/Serializing.php (rev 0) +++ p2ex/trunk/lib/P2KeyValueStore/Serializing.php 2009-12-30 01:32:31 UTC (rev 746) @@ -0,0 +1,52 @@ +<?php +require_once dirname(__FILE__) . '/CompressingStore.php'; + +// {{{ SerializingStore + +/** + * lðVACYµÄi±»·é + */ +class SerializingStore extends CompressingStore +{ + // {{{ _encodeValue() + + /** + * lðVACY·é + * + * @param mixed $value + * @return string + */ + protected function _encodeValue($value) + { + return parent::_encodeValue(serialize($value)); + } + + // }}} + // {{{ _decodeValue() + + /** + * lðAVACY·é + * + * @param string $value + * @return mixed + */ + protected function _decodeValue($value) + { + return unserialize(parent::_decodeValue($value)); + } + + // }}} +} + +// }}} + +/* + * Local Variables: + * mode: php + * coding: cp932 + * tab-width: 4 + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ +// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Copied: p2ex/trunk/lib/P2KeyValueStore.php (from rev 745, p2ex/trunk/lib/KeyValueStore.php) =================================================================== --- p2ex/trunk/lib/P2KeyValueStore.php (rev 0) +++ p2ex/trunk/lib/P2KeyValueStore.php 2009-12-30 01:32:31 UTC (rev 746) @@ -0,0 +1,868 @@ +<?php +// {{{ KeyValueStore + +/** + * L[/lÌyAðSQLite3Ìf[^x[XÉÛ¶·é Key-Value Store + */ +class KeyValueStore implements ArrayAccess, Countable, IteratorAggregate +{ + // {{{ constants + + const Q_TABLEEXISTS = 'SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = :table LIMIT 1'; + const Q_CREATETABLE = 'CREATE TABLE $__table ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + arkey TEXT NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT REPLACE, + value TEXT NOT NULL, + mtime INTEGER DEFAULT (strftime(\'%s\',\'now\')), + sort_order INTEGER DEFAULT 0 +)'; + const Q_COUNT = 'SELECT COUNT(*) FROM $__table LIMIT 1'; + const Q_EXSITS = 'SELECT id, mtime FROM $__table WHERE arkey = :key LIMIT 1'; + const Q_FINDBYID = 'SELECT * FROM $__table WHERE id = :id LIMIT 1'; + const Q_GET = 'SELECT * FROM $__table WHERE arkey = :key LIMIT 1'; + const Q_GETALL = 'SELECT * FROM $__table'; + const Q_GETIDS = 'SELECT id FROM $__table'; + const Q_GETKEYS = 'SELECT arkey FROM $__table'; + const Q_SAVE = 'INSERT INTO $__table (arkey, value, sort_order) VALUES (:key, :value, :order)'; + const Q_UPDATE = 'UPDATE $__table SET value = :value, mtime = strftime(\'%s\',\'now\'), sort_order = :order WHERE arkey = :key'; + const Q_TOUCH = 'UPDATE $__table SET mtime = (CASE WHEN :mtime IS NULL THEN strftime(\'%s\',\'now\') ELSE :mtime END) WHERE arkey = :key'; + const Q_SETORDER = 'UPDATE $__table SET sort_order = :order WHERE arkey = :key'; + const Q_DELETE = 'DELETE FROM $__table WHERE arkey = :key'; + const Q_DELETEBYID = 'DELETE FROM $__table WHERE id = :id'; + const Q_CLEAN = 'DELETE FROM $__table'; + const Q_GC = 'DELETE FROM $__table WHERE mtime < :expires'; + + // }}} + // {{{ staric private properties + + /** + * f[^x[XÉêÓÈPDO,PDOStatement,KeyValueStoreÌCX^XðÛ·ézñ + * + * @var array + */ + static private $_objects = array(); + + // }}} + // {{{ private properties + + /** + * PDOÌCX^X + * + * @var PDO + */ + private $_conn; + + /** + * SQLite3f[^x[XÌpX + * + * @var string + */ + private $_path; + + /** + * ¯ÊqƵÄNH[gÏÝÌe[u¼ + * + * @var string + */ + private $_quotedTableName; + + // }}} + // {{{ getStore() + + /** + * VOg\bh + * + * @param string $fileName + * @param string $className + * @param string &$openedPath + * @return KeyValueStore + * @throws InvalidArgumentException, UnexpectedValueException, RuntimeException, PDOException + */ + static public function getStore($fileName, $className = 'KeyValueStore', &$openedPath = null) + { + // øÌ^ð`FbN + if (!is_string($fileName)) { + throw new InvalidArgumentException('Parameter #1 \'$fileName\' should be a string value'); + } + if (!is_string($className)) { + throw new InvalidArgumentException('Parameter #2 \'$className\' should be a string value'); + } + + // NX¼ð`FbN + if (strcasecmp($className, 'KeyValueStore') != 0) { + if (!class_exists($className, false)) { + throw new UnexpectedValueException("Class '{$className}' is not declared"); + } + if (!is_subclass_of($className, 'KeyValueStore')) { + throw new UnexpectedValueException("Class '{$className}' is not a subclass of KeyValueStore"); + } + } + + // f[^x[Xt@Cð`FbN + if ($fileName == ':memory:') { + $path = $fileName; + $createTable = true; + } elseif (file_exists($fileName)) { + if (!is_file($fileName)) { + throw new RuntimeException("'{$fileName}' is not a standard file"); + } + if (!is_writable($fileName)) { + throw new RuntimeException("File '{$fileName}' is not writable"); + } + $path = realpath($fileName); + $createTable = false; + } else { + if (strpos($fileName, '/') !== false || + (strncasecmp(PHP_OS, 'WIN', 3) == 0 && strpos($fileName, '\\') !== false)) + { + $dirName = dirname($fileName); + $baseName = basename($fileName); + } else { + $dirName = getcwd(); + $baseName = $fileName; + } + if (!is_string($dirName) || !is_dir($dirName)) { + throw new RuntimeException("No directory for '{$fileName}'"); + } + if (!is_writable($dirName)) { + throw new RuntimeException("Directory '{$dirName}' is not writable"); + } + $path = realpath($dirName) . DIRECTORY_SEPARATOR . $baseName; + $createTable = true; + } + + $lcname = strtolower($className); + $tableName = 'kvs_' . $lcname; + $openedPath = $path; + + // CX^Xð쬵AÃIÏÉÛ + if (array_key_exists($path, self::$_objects)) { + if (array_key_exists($lcname, self::$_objects[$path]['persisters'])) { + $kvs = self::$_objects[$path]['persisters'][$lcname]; + } else { + $conn = self::$_objects[$path]['connection']; + $kvs = new $className($conn, $path, $tableName); + self::$_objects[$path]['persisters'][$lcname] = $kvs; + } + } else { + $conn = new PDO('sqlite:' . $path); + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $kvs = new $className($conn, $path, $tableName); + self::$_objects[$path] = array( + 'connection' => $conn, + 'statements' => array(), + 'persisters' => array($lcname => $kvs), + ); + } + + return $kvs; + } + + // }}} + // {{{ constructor + + /** + * RXgN^ + * getStore()©çÄÑo³êé + * + * @param PDO $conn + * @param string $path + * @param string $tableName + * @throws PDOException + */ + private function __construct(PDO $conn, $path, $tableName) + { + $this->_conn = $conn; + $this->_path = $path; + $this->_quotedTableName = '"' . str_replace('"', '""', $tableName) . '"'; + + // e[uª¶Ý·é©ð²× + $stmt = $conn->prepare(self::Q_TABLEEXISTS); + $stmt->bindValue(':table', $tableName, PDO::PARAM_STR); + $stmt->execute(); + $exists = $stmt->fetchColumn(); + $stmt->closeCursor(); + unset($stmt); + + // ³¯êÎìé + if (!$exists) { + $conn->exec(str_replace('$__table', $this->_quotedTableName, self::Q_CREATETABLE)); + } + } + + // }}} + // {{{ _prepare() + + /** + * vyA[hXe[ggð쬷é + * + * @param string $query + * @param bool $isTemporary + * @return PDOStatement + * @throws PDOException + */ + private function _prepare($query, $isTemporary = false) + { + $query = str_replace('$__table', $this->_quotedTableName, $query); + + if (!$isTemporary && array_key_exists($query, self::$_objects[$this->_path]['statements'])) { + $stmt = self::$_objects[$this->_path]['statements'][$query]; + } else { + if (strncmp($query, 'SELECT ', 7) == 0) { + $stmt = $this->_conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); + } else { + $stmt = $this->_conn->prepare($query); + } + if (!$isTemporary) { + self::$_objects[$this->_path]['statements'][$query] = $stmt; + } + } + + return $stmt; + } + + // }}} + // {{{ _buildOrderBy() + + /** + * R[hðÜÆßÄæ¾·éÛÌOREDER BYå𶬷é + * + * @param array $orderBy + * @return string + */ + private function _buildOrderBy(array $orderBy = null) + { + if ($orderBy === null) { + return ' ORDER BY sort_order ASC, arkey ASC'; + } + + $terms = array(); + foreach ($orderBy as $column => $ascending) { + $direction = $ascending ? 'ASC' : 'DESC'; + switch ($column) { + case 'id': + $terms[] = 'id ' . $direction; + break; + case 'key': + $terms[] = 'arkey ' . $direction; + break; + case 'value': + $terms[] = 'value ' . $direction; + break; + case 'mtime': + $terms[] = 'mtime ' . $direction; + break; + case 'order': + $terms[] = 'sort_order ' . $direction; + break; + } + } + + if (count($terms)) { + return ' ORDER BY ' . implode(', ', $terms); + } else { + return ''; + } + } + + // }}} + // {{{ _buildLimit() + + /** + * R[hðÜÆßÄæ¾·éÛÌLIMITåÆOFFSETå𶬷é + * + * @param int $limit + * @param int $offset + * @return string + */ + private function _buildLimit($limit = null, $offset = null) + { + if ($limit === null) { + return ''; + } elseif ($offset === null) { + return sprintf(' LIMIT %d', $limit); + } else { + return sprintf(' LIMIT %d OFFSET %d', $limit, $offset); + } + } + + // }}} + // {{{ _encodeKey() + + /** + * L[ðUTF-8 or US-ASCII¶ñÉGR[h·é + * + * @param string $key + * @return string + */ + protected function _encodeKey($key) + { + return (string)$key; + } + + // }}} + // {{{ _decodeKey() + + /** + * L[ðfR[h·é + * + * @param string $key + * @return string + */ + protected function _decodeKey($key) + { + return $key; + } + + // }}} + // {{{ _encodeValue() + + /** + * lðUTF-8 or US-ASCII¶ñÉGR[h·é + * + * @param string $value + * @return string + */ + protected function _encodeValue($value) + { + return (string)$value; + } + + // }}} + // {{{ _decodeValue() + + /** + * lðfR[h·é + * + * @param string $value + * @return string + */ + protected function _decodeValue($value) + { + return $value; + } + + // }}} + // {{{ exists() + + /** + * L[ÉηélªÛ¶³êÄ¢é©ð²×é + * + * @param string $key + * @param int $lifeTime + * @return bool + */ + public function exists($key, $lifeTime = null) + { + $stmt = $this->_prepare(self::Q_EXSITS); + $stmt->setFetchMode(PDO::FETCH_ASSOC); + $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); + $stmt->execute(); + $row = $stmt->fetch(); + $stmt->closeCursor(); + if ($row === false) { + return false; + } elseif ($lifeTime !== null && $row['mtime'] < time() - $lifeTime) { + $this->deleteById($row['id']); + return false; + } else { + return true; + } + } + + // }}} + // {{{ findById() + + /** + * IDÉηéL[ÆlÌyAðæ¾·é + * åƵÄKeyValueStoreIteratorÅg¤ + * + * @param int $id + * @param int $lifeTime + * @return array + */ + public function findById($id, $lifeTime = null) + { + $stmt = $this->_prepare(self::Q_FINDBYID); + $stmt->setFetchMode(PDO::FETCH_ASSOC); + $stmt->bindValue(':id', (int)$id, PDO::PARAM_INT); + $stmt->execute(); + $row = $stmt->fetch(); + $stmt->closeCursor(); + if ($row === false) { + return null; + } elseif ($lifeTime !== null && $row['mtime'] < time() - $lifeTime) { + $this->deleteById($id); + return null; + } else { + return array( + 'key' => $this->_decodeKey($row['arkey']), + 'value' => $this->_decodeValue($row['value']), + ); + } + } + + // }}} + // {{{ get() + + /** + * L[Éηélðæ¾·é + * + * @param string $key + * @param int $lifeTime + * @return string + */ + public function get($key, $lifeTime = null) + { + $stmt = $this->_prepare(self::Q_GET); + $stmt->setFetchMode(PDO::FETCH_ASSOC); + $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); + $stmt->execute(); + $row = $stmt->fetch(); + $stmt->closeCursor(); + if ($row === false) { + return null; + } elseif ($lifeTime !== null && $row['mtime'] < time() - $lifeTime) { + $this->deleteById($row['id']); + return null; + } else { + return $this->_decodeValue($row['value']); + } + } + + // }}} + // {{{ getDetail() + + /** + * L[ÉηéR[hðæ¾·é + * + * @param string $key + * @param int $lifeTime + * @return array + */ + public function getDetail($key, $lifeTime = null) + { + $stmt = $this->_prepare(self::Q_GET); + $stmt->setFetchMode(PDO::FETCH_ASSOC); + $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); + $stmt->execute(); + $row = $stmt->fetch(); + $stmt->closeCursor(); + if ($row === false) { + return null; + } elseif ($lifeTime !== null && $row['mtime'] < time() - $lifeTime) { + $this->deleteById($row['id']); + return null; + } else { + return array( + 'id' => (int)$row['id'], + 'key' => $this->_decodeKey($row['arkey']), + 'value' => $this->_decodeValue($row['value']), + 'mtime' => (int)$row['mtime'], + 'order' => (int)$row['sort_order'], + ); + } + } + + // }}} + // {{{ getAll() + + /** + * SÄÌR[hðAzzñƵÄÔ· + * LøúÀØêÌR[hðOµ½¢êÍOÉgc()µÄ¨±Æ + * + * @param array $orderBy + * @param int $limit + * @param int $offset + * @param bool $getDetails + * @return array + */ + public function getAll(array $orderBy = null, $limit = null, $offset = null, $getDetails = false) + { + $query = self::Q_GETALL + . $this->_buildOrderBy($orderBy) + . $this->_buildLimit($limit, $offset); + $stmt = $this->_prepare($query, true); + $stmt->setFetchMode(PDO::FETCH_ASSOC); + $stmt->execute(); + $values = array(); + if ($getDetails) { + while ($row = $stmt->fetch()) { + $key = $this->_decodeKey($row['arkey']); + $values[$key] = array( + 'id' => (int)$row['id'], + 'key' => $key, + 'value' => $this->_decodeValue($row['value']), + 'mtime' => (int)$row['mtime'], + 'order' => (int)$row['sort_order'], + ); + } + } else { + while ($row = $stmt->fetch()) { + $values[$this->_decodeKey($row['arkey'])] = $this->_decodeValue($row['value']); + } + } + $stmt->closeCursor(); + return $values; + } + + // }}} + // {{{ getIds() + + /** + * SÄÌIDÌzñðÔ· + * åƵÄKeyValueStoreIteratorÅg¤ + * LøúÀØêÌR[hðOµ½¢êÍOÉgc()µÄ¨±Æ + * + * @param array $orderBy + * @param int $limit + * @param int $offset + * @return array + */ + public function getIds(array $orderBy = null, $limit = null, $offset = null) + { + $query = self::Q_GETIDS + . $this->_buildOrderBy($orderBy) + . $this->_buildLimit($limit, $offset); + $stmt = $this->_prepare($query, true); + $stmt->setFetchMode(PDO::FETCH_COLUMN, 0); + $stmt->execute(); + $ids = array(); + while (($id = $stmt->fetch()) !== false) { + $ids[] = (int)$id; + } + $stmt->closeCursor(); + return $ids; + } + + // }}} + // {{{ getKeys() + + /** + * SÄÌL[ÌzñðÔ· + * LøúÀØêÌR[hðOµ½¢êÍOÉgc()µÄ¨±Æ + * + * @param array $orderBy + * @param int $limit + * @param int $offset + * @return array + */ + public function getKeys(array $orderBy = null, $limit = null, $offset = null) + { + $query = self::Q_GETKEYS + . $this->_buildOrderBy($orderBy) + . $this->_buildLimit($limit, $offset); + $stmt = $this->_prepare($query, true); + $stmt->setFetchMode(PDO::FETCH_COLUMN, 0); + $stmt->execute(); + $keys = array(); + while (($key = $stmt->fetch()) !== false) { + $keys[] = $this->_decodeKey($key); + } + $stmt->closeCursor(); + return $keys; + } + + // }}} + // {{{ set() + + /** + * f[^ðÛ¶·é + * + * @param string $key + * @param string $value + * @param int $order + * @return bool + */ + public function set($key, $value, $order = 0) + { + $stmt = $this->_prepare(self::Q_SAVE); + $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); + $stmt->bindValue(':value', $this->_encodeValue($value), PDO::PARAM_STR); + $stmt->bindValue(':order', (int)$order, PDO::PARAM_INT); + if ($stmt->execute()) { + return $stmt->rowCount() == 1; + } else { + return false; + } + } + + // }}} + // {{{ update() + + /** + * f[^ðXV·é + * + * @param string $key + * @param string $value + * @param int $order + * @return bool + */ + public function update($key, $value, $order = 0) + { + $stmt = $this->_prepare(self::Q_UPDATE); + $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); + $stmt->bindValue(':value', $this->_encodeValue($value), PDO::PARAM_STR); + $stmt->bindValue(':order', (int)$order, PDO::PARAM_INT); + if ($stmt->execute()) { + return $stmt->rowCount() == 1; + } else { + return false; + } + } + + // }}} + // {{{ touch() + + /** + * f[^ÌXVúð»ÝÉÝè·é + * + * @param string $key + * @param int $time + * @return bool + */ + public function touch($key, $time = null) + { + $stmt = $this->_prepare(self::Q_TOUCH); + $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); + if ($time === null) { + $stmt->bindValue(':mtime', null, PDO::PARAM_NULL); + } else { + $stmt->bindValue(':mtime', (int)$time, PDO::PARAM_INT); + } + if ($stmt->execute()) { + return $stmt->rowCount() == 1; + } else { + return false; + } + } + + // }}} + // {{{ setOrder() + + /** + * f[^ÌÀÑ (sort_orderJÌl) ðÝè·é + * + * @param string $key + * @param int $order + * @return bool + */ + public function setOrder($key, $order) + { + $stmt = $this->_prepare(self::Q_SETORDER); + $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); + $stmt->bindValue(':order', (int)$order, PDO::PARAM_INT); + if ($stmt->execute()) { + return $stmt->rowCount() == 1; + } else { + return false; + } + } + + // }}} + // {{{ delete() + + /** + * L[ÉηéR[hðí·é + * + * @param string $key + * @return bool + */ + public function delete($key) + { + $stmt = $this->_prepare(self::Q_DELETE); + $stmt->bindValue(':key', $this->_encodeKey($key), PDO::PARAM_STR); + if ($stmt->execute()) { + return $stmt->rowCount() == 1; + } else { + return false; + } + } + + // }}} + // {{{ deleteById() + + /** + * IDÉηéR[hðí·é + * + * @param int $id + * @return bool + */ + public function deleteById($id) + { + $stmt = $this->_prepare(self::Q_DELETEBYID); + $stmt->bindValue(':id', (int)$id, PDO::PARAM_INT); + if ($stmt->execute()) { + return $stmt->rowCount() == 1; + } else { + return false; + } + } + + // }}} + // {{{ clean() + + /** + * ·×ÄÌR[hðí·é + * + * @param void + * @return int + */ + public function clean() + { + $stmt = $this->_prepare(self::Q_CLEAN, true); + if ($stmt->execute()) { + return $stmt->rowCount(); + } else { + return false; + } + } + + // }}} + // {{{ gc() + + /** + * úÀØêÌR[hðí·é + * + * @param int $lifeTime + * @return int + */ + public function gc($lifeTime) + { + $stmt = $this->_prepare(self::Q_GC, true); + $stmt->bindValue(':expires', time() - $lifeTime, PDO::PARAM_INT); + if ($stmt->execute()) { + return $stmt->rowCount(); + } else { + return false; + } + } + + // }}} + // {{{ vacuum() + + /** + * ì¬ÏÝvyA[hXe[ggðNAµAVACUUMðs·é + * ¼ÌvZXª¯¶f[^x[XðJ¢Ä¢éÆ«ÉÀs·×«ÅÍÈ¢ + * + * @param void + * @return void + */ + public function vacuum() + { + self::$_objects[$this->_path]['statements'] = array(); + $this->_conn->exec('VACUUM'); + } + + // }}} + // {{{ count() + + /** + * Countable::count() + * + * R[hÌðÔ· + * + * @param void + * @return int + */ + public function count() + { + $stmt = $this->_prepare(self::Q_COUNT); + $stmt->execute(); + $ret = (int)$stmt->fetchColumn(); + $stmt->closeCursor(); + return $ret; + } + + // }}} + // {{{ offsetExists() + + /** + * ArrayAccess::offsetExists() + * + * @param string $offset + * @return string + */ + public function offsetExists($offset) + { + return $this->exists($offset); + } + + // }}} + // {{{ offsetGet() + + /** + * ArrayAccess::offsetGet() + * + * @param string $offset + * @return mixed + */ + public function offsetGet($offset) + { + return $this->get($offset); + } + + // }}} + // {{{ offsetSet() + + /** + * ArrayAccess::offsetSet() + * + * @param string $offset + * @param string $value + * @return void + */ + public function offsetSet($offset, $value) + { + $this->set($offset, $value); + } + + // }}} + // {{{ offsetUnset() + + /** + * ArrayAccess::offsetUnset() + * + * @param string $offset + * @return void + */ + public function offsetUnset($offset) + { + $this->delete($offset); + } + + // }}} + // {{{ getIterator() + + /** + * IteratorAggregate::getIterator() + * + * ½Égetn\bhðÄÎêÄàåävÈæ¤É + * \ßæ¾µ½IDÌXgðg¤Ce[^ðÔ· + * + * @param void + * @return KeyValueStoreIterator + */ + public function getIterator() + { + if (!class_exists('KeyValueStoreIterator', false)) { + include dirname(__FILE__) . '/KeyValueStoreIterator.php'; + } + return new KeyValueStoreIterator($this); + } + + // }}} +} + +// }}} + +/* + * Local Variables: + * mode: php + * coding: cp932 + * tab-width: 4 + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ +// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Deleted: p2ex/trunk/lib/SerializingStore.php =================================================================== --- p2ex/trunk/lib/SerializingStore.php 2009-12-29 08:21:27 UTC (rev 745) +++ p2ex/trunk/lib/SerializingStore.php 2009-12-30 01:32:31 UTC (rev 746) @@ -1,52 +0,0 @@ -<?php -require_once dirname(__FILE__) . '/CompressingStore.php'; - -// {{{ SerializingStore - -/** - * lðVACYµÄi±»·é - */ -class SerializingStore extends CompressingStore -{ - // {{{ _encodeValue() - - /** - * lðVACY·é - * - * @param mixed $value - * @return string - */ - protected function _encodeValue($value) - { - return parent::_encodeValue(serialize($value)); - } - - // }}} - // {{{ _decodeValue() - - /** - * lðAVACY·é - * - * @param string $value - * @return mixed - */ - protected function _decodeValue($value) - { - return unserialize(parent::_decodeValue($value)); - } - - // }}} -} - -// }}} - -/* - * Local Variables: - * mode: php - * coding: cp932 - * tab-width: 4 - * c-basic-offset: 4 - * indent-tabs-mode: nil - * End: - */ -// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: Deleted: p2ex/trunk/lib/SjisStore.php =================================================================== --- p2ex/trunk/lib/SjisStore.php 2009-12-29 08:21:27 UTC (rev 745) +++ p2ex/trunk/lib/SjisStore.php 2009-12-30 01:32:31 UTC (rev 746) @@ -1,108 +0,0 @@ -<?php -require_once dirname(__FILE__) . '/KeyValueStore.php'; - -// {{{ SjisStore - -/** - * Shift_JIS̶ñðUTF-8ÉÏ·µÄi±»·é - */ -class SjisStore extends KeyValueStore -{ - // {{{ _encode() - - /** - * Shift_JIS (CP932) ̶ñðUTF-8ÉÏ··é - * - * @param string $str - * @return string - */ - private function _encode($str) - { - return mb_convert_encoding($str, 'UTF-8', 'CP932'); - } - - // }}} - // {{{ _decode() - - /** - * UTF-8̶ñðShift_JIS (CP932) ÉÏ··é - * - * @param string $str - * @return string - */ - private function _decode($str) - { - return mb_convert_encoding($str, 'CP932', 'UTF-8'); - } - - // }}} - // {{{ _encodeKey() - - /** - * L[ðGR[h·é - * - * @param string $key - * @return string - */ - protected function _encodeKey($key) - { - return $this->_encode($key); - } - - // }}} - // {{{ _decodeKey() - - /** - * L[ðfR[h·é - * - * @param string $key - * @return string - */ - protected function _decodeKey($key) - { - return $this->_decode($key); - } - - // }}} - // {{{ _encodeValue() - - /** - * lðGR[h·é - * - * @param string $value - * @return string - */ - protected function _encodeValue($value) - { - return $this->_encode($value); - } - - // }}} - // {{{ _decodeValue() - - /** - * lðfR[h·é - * - * @param string $value - * @return string - */ - protected function _decodeValue($value) - { - return $this->_decode($value); - } - - // }}} -} - -// }}} - -/* - * Local Variables: - * mode: php - * coding: cp932 - * tab-width: 4 - * c-basic-offset: 4 - * indent-tabs-mode: nil - * End: - */ -// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: