[Phpfreechat-svn] SF.net SVN: phpfreechat: [902] trunk/src
Status: Beta
Brought to you by:
kerphi
From: <ke...@us...> - 2006-12-19 17:56:06
|
Revision: 902 http://svn.sourceforge.net/phpfreechat/?rev=902&view=rev Author: kerphi Date: 2006-12-19 09:56:01 -0800 (Tue, 19 Dec 2006) Log Message: ----------- Container optimization : add a memory cache (work in progress: do not update this revision !!!) Modified Paths: -------------- trunk/src/containers/file.class.php trunk/src/pfccontainer.class.php trunk/src/pfcglobalconfig.class.php Added Paths: ----------- trunk/src/pfccontainerinterface.class.php Modified: trunk/src/containers/file.class.php =================================================================== --- trunk/src/containers/file.class.php 2006-12-18 09:16:01 UTC (rev 901) +++ trunk/src/containers/file.class.php 2006-12-19 17:56:01 UTC (rev 902) @@ -20,24 +20,19 @@ * Boston, MA 02110-1301 USA */ -require_once dirname(__FILE__)."/../pfccontainer.class.php"; +require_once dirname(__FILE__)."/../pfccontainerinterface.class.php"; /** * pfcContainer_File is a concret container which stock data into files * * @author Stephane Gully <ste...@gm...> */ -class pfcContainer_File extends pfcContainer +class pfcContainer_File extends pfcContainerInterface { var $_users = array("nickid" => array(), "timestamp" => array()); var $_meta = array(); - function pfcContainer_File(&$config) - { - pfcContainer::pfcContainer($config); - } - function loadPaths() { $c =& $this->c; @@ -91,7 +86,7 @@ { if (file_exists($leaffilename) && filesize($leaffilename)>0) unlink($leaffilename); - touch($leaffilename); + touch($leaffilename); } else { @@ -122,7 +117,6 @@ $dir_base = $c->container_cfg_server_dir; $dir = $dir_base.'/'.$group; - if ($subgroup == NULL) { if (is_dir($dir)) @@ -154,6 +148,7 @@ } closedir($dh); } + return $ret; } Modified: trunk/src/pfccontainer.class.php =================================================================== --- trunk/src/pfccontainer.class.php 2006-12-18 09:16:01 UTC (rev 901) +++ trunk/src/pfccontainer.class.php 2006-12-19 17:56:01 UTC (rev 902) @@ -20,6 +20,8 @@ * Boston, MA 02110-1301 USA */ + require_once dirname(__FILE__)."/pfccontainerinterface.class.php"; + /** * pfcContainer is an abstract class which define interface * to be implemented by concrete container (example: File) @@ -27,13 +29,20 @@ * @author Stephane Gully <ste...@gm...> * @abstract */ -class pfcContainer +class pfcContainer extends pfcContainerInterface { - var $c; - function pfcContainer(&$config) { $this->c =& $config; } - function getDefaultConfig() { return array(); } - function init() { return array(); } + var $_container = null; // contains the concrete container instance + + function pfcContainer(&$c, $type = 'File') + { + pfcContainerInterface::pfcContainerInterface($c); + // create the concrete container instance + require_once dirname(__FILE__)."/containers/".strtolower($type).".class.php"; + $container_classname = "pfcContainer_".$type; + $this->_container =& new $container_classname($this->c); + } + /** * Create (connect/join) the nickname into the server or the channel locations * Notice: the caller must take care to update all channels the users joined (use stored channel list into metadata) @@ -280,7 +289,7 @@ { if ($chan == NULL) $chan = 'SERVER'; - $ret = $this->getMeta("channelid-to-nickid", $this->encode($chan)); + $ret = $this->getMeta("channelid-to-nickid", $this->encode($chan)); for($i = 0; $i<count($ret['timestamp']); $i++) { if ($ret['value'][$i] == $nickid) return $i; @@ -411,7 +420,6 @@ return $lastmsgid; } - /** * Remove all created data for this server (identified by serverid) * Notice: for the default File container, it's just a recursive directory remove @@ -421,25 +429,6 @@ $this->rmMeta(NULL); } - /** - * In the default File container: used to encode UTF8 strings to ASCII filenames - * This method can be overridden by the concrete container - */ - function encode($str) - { - return $str; - } - - /** - * In the default File container: used to decode ASCII filenames to UTF8 strings - * This method can be overridden by the concrete container - */ - function decode($str) - { - return $str; - } - - function getAllUserMeta($nickid) { $result = array(); @@ -483,7 +472,9 @@ $ret = $this->setMeta("channelid-to-metadata", $this->encode($chan), $key, $value); return $ret; } - + + var $_cache = array(); + /** * Write a meta data value identified by a group / subgroup / leaf [with a value] * As an example in the default file container this arborescent structure is modelised by simple directories @@ -499,8 +490,24 @@ * @return 1 if the old leaf has been overwritten, 0 if a new leaf has been created */ function setMeta($group, $subgroup, $leaf, $leafvalue = NULL) - { die(_pfc("%s must be implemented", get_class($this)."::".__FUNCTION__)); } + { + $ret = $this->_container->setMeta($group, $subgroup, $leaf, $leafvalue); + echo "setMeta($group, $subgroup, $leaf, $leafvalue)\n"; + //print_r($ret); + + + // @todo creer la bonne hierarchie du cache + + //$this->_cache['group'][$group] = array_merge($this->_cache['group'][$group],array($subgroup)); + //$this->_cache[$group][$subgroup][$leaf] = $leafvalue; + + //$this->_cache['subgroup'][$group][$subgroup] = $ret; + //$this->_cache['leaf'][$group][$subgroup][$leaf] = ($withleafvalue ? ; + + return $ret; + } + /** * Read meta data identified by a group [/ subgroup [/ leaf]] @@ -511,7 +518,73 @@ * @return array which contains two subarray 'timestamp' and 'value' */ function getMeta($group, $subgroup = null, $leaf = null, $withleafvalue = false) - { die(_pfc("%s must be implemented", get_class($this)."::".__FUNCTION__)); } + { + echo "getMeta($group, $subgroup, $leaf, $withleafvalue)\n"; + + // check in the cache + $ret = array('timestamp' => array(), + 'value' => array()); + if ($subgroup == null && + isset($this->_cache[$group]['value'])) + { + $ret['timestamp'] = $this->_cache[$group]['timestamp']; + $ret['value'] = $this->_cache[$group]['value']; + echo "getMeta->incache\n"; + return $ret; + } + else if ($leaf == null && + isset($this->_cache[$group][$subgroup]['value'])) + { + $ret['timestamp'] = $this->_cache[$group][$subgroup]['timestamp']; + $ret['value'] = $this->_cache[$group][$subgroup]['value']; + echo "getMeta->incache\n"; + return $ret; + } + else + { + if ($withleafvalue) + { + if (isset($this->_cache[$group][$subgroup][$leaf]['value'])) + { + echo "getMeta->incache\n"; + return $this->_cache[$group][$subgroup][$leaf]; + } + } + else + { + if (isset($this->_cache[$group][$subgroup][$leaf]['timestamp'])) + { + echo "getMeta->incache\n"; + return $this->_cache[$group][$subgroup][$leaf]; + } + } + } + + echo "getMeta->notincache\n"; + + // get the fresh data + $ret = $this->_container->getMeta($group, $subgroup, $leaf, $withleafvalue); + + // store in the cache + if ($subgroup == null) + { + $this->_cache[$group]['value'] = $ret['value']; + $this->_cache[$group]['timestamp'] = $ret['timestamp']; + } + else if ($leaf == null) + { + $this->_cache[$group][$subgroup]['value'] = $ret['value']; + $this->_cache[$group][$subgroup]['timestamp'] = $ret['timestamp']; + } + else + { + if ($withleafvalue) + $this->_cache[$group][$subgroup][$leaf]['value'] = $ret['value']; + $this->_cache[$group][$subgroup][$leaf]['timestamp'] = $ret['timestamp']; + } + + return $ret; + } /** @@ -522,8 +595,43 @@ * @return true on success, false on error */ function rmMeta($group, $subgroup = null, $leaf = null) - { die(_pfc("%s must be implemented", get_class($this)."::".__FUNCTION__)); } + { + echo "rmMeta($group, $subgroup, $leaf)\n"; + + print_r($this->_cache); + // remove from the cache + if ($group == null) + $this->_cache = array(); + else if ($subgroup == null) + unset($this->_cache[$group]); + else if ($leaf == null) + unset($this->_cache[$group][$subgroup]); + else + unset($this->_cache[$group][$subgroup][$leaf]); + + print_r($this->_cache); + + return $this->_container->rmMeta($group, $subgroup, $leaf); + } + + /** + * In the default File container: used to encode UTF8 strings to ASCII filenames + * This method can be overridden by the concrete container + */ + function encode($str) + { + return $this->_container->encode($str); + } + + /** + * In the default File container: used to decode ASCII filenames to UTF8 strings + * This method can be overridden by the concrete container + */ + function decode($str) + { + return $this->_container->decode($str); + } } ?> \ No newline at end of file Added: trunk/src/pfccontainerinterface.class.php =================================================================== --- trunk/src/pfccontainerinterface.class.php (rev 0) +++ trunk/src/pfccontainerinterface.class.php 2006-12-19 17:56:01 UTC (rev 902) @@ -0,0 +1,96 @@ +<?php +/** + * pfccontainerinterface.class.php + * + * Copyright © 2006 Stephane Gully <ste...@gm...> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +/** + * pfcContainerInterface is an interface implemented by pfcContainer and each pfcContainer concrete isntances (File,Mysql...) + * + * @author Stephane Gully <ste...@gm...> + * @abstract + */ +class pfcContainerInterface +{ + var $c; + function pfcContainerInterface(&$config) { $this->c =& $config; } + function getDefaultConfig() { return array(); } + function init() { return array(); } + + /** + * Write a meta data value identified by a group / subgroup / leaf [with a value] + * As an example in the default file container this arborescent structure is modelised by simple directories + * group1/subgroup1/leaf1 + * /leaf2 + * /subgroup2/... + * Each leaf can contain or not a value. + * However each leaf and each group/subgroup must store the lastmodified time (timestamp). + * @param $group root arborescent element + * @param $subgroup is the root first child which contains leafs + * @param $leaf is the only element which can contain values + * @param $leafvalue NULL means the leaf will not contain any values + * @return 1 if the old leaf has been overwritten, 0 if a new leaf has been created + */ + function setMeta($group, $subgroup, $leaf, $leafvalue = NULL) + { die(_pfc("%s must be implemented", get_class($this)."::".__FUNCTION__)); } + + + /** + * Read meta data identified by a group [/ subgroup [/ leaf]] + * @param $group is mandatory, it's the arborescence's root + * @param $subgroup if null then the subgroup list names are returned + * @param $leaf if null then the leaf names are returned + * @param $withleafvalue if set to true the leaf value will be returned + * @return array which contains two subarray 'timestamp' and 'value' + */ + function getMeta($group, $subgroup = null, $leaf = null, $withleafvalue = false) + { die(_pfc("%s must be implemented", get_class($this)."::".__FUNCTION__)); } + + + /** + * Remove a meta data or a group of metadata + * @param $group if null then it will remove all the possible groups (all the created metadata) + * @param $subgroup if null then it will remove the $group's childs (all the subgroup contained by $group) + * @param $leaf if null then it will remove all the $subgroup's childs (all the leafs contained by $subgroup) + * @return true on success, false on error + */ + function rmMeta($group, $subgroup = null, $leaf = null) + { die(_pfc("%s must be implemented", get_class($this)."::".__FUNCTION__)); } + + + /** + * In the default File container: used to encode UTF8 strings to ASCII filenames + * This method can be overridden by the concrete container + */ + function encode($str) + { + return $str; + } + + /** + * In the default File container: used to decode ASCII filenames to UTF8 strings + * This method can be overridden by the concrete container + */ + function decode($str) + { + return $str; + } +} + +?> \ No newline at end of file Modified: trunk/src/pfcglobalconfig.class.php =================================================================== --- trunk/src/pfcglobalconfig.class.php 2006-12-18 09:16:01 UTC (rev 901) +++ trunk/src/pfcglobalconfig.class.php 2006-12-19 17:56:01 UTC (rev 902) @@ -235,6 +235,7 @@ */ function &getContainerInstance() { + // bug in php4: cant make a static pfcContainer instance because // it make problems with pfcGlobalConfig references (not updated) // it works well in php5, maybe there is a workeround but I don't have time to debug this @@ -243,11 +244,10 @@ // static $container; // if (!isset($container)) // { - $container_classname = "pfcContainer_".$this->container_type; - require_once dirname(__FILE__)."/containers/".strtolower($this->container_type).".class.php"; - $container =& new $container_classname($this); + require_once dirname(__FILE__).'/pfccontainer.class.php'; + $container =& new pfcContainer($this,$this->container_type); + return $container; // } - return $container; } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |