diff options
author | godzilla80@gmx.net <> | 2010-02-13 19:04:45 +0000 |
---|---|---|
committer | godzilla80@gmx.net <> | 2010-02-13 19:04:45 +0000 |
commit | 966fd66f217911d079c4bd6a87b09f4a0c5c4736 (patch) | |
tree | 26d4cda965ed5a6ddf2aeb805fcef42877584fd3 /framework/Testing/Data/Distributed/TDistributedDataSourceConfig.php | |
parent | 879cced5e01d43378065c938483b55a35ff10834 (diff) |
NEW: Add Beta of master/slave senario solution
- add package System.Testing.Data.Analysis
- add package System.Testing.Data.Distributed
- add sub package System.Testing.Data.Distributed.MasterSlave
- add unittest for System.Testing.Data.Analysis
Diffstat (limited to 'framework/Testing/Data/Distributed/TDistributedDataSourceConfig.php')
-rw-r--r-- | framework/Testing/Data/Distributed/TDistributedDataSourceConfig.php | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/framework/Testing/Data/Distributed/TDistributedDataSourceConfig.php b/framework/Testing/Data/Distributed/TDistributedDataSourceConfig.php new file mode 100644 index 00000000..b8c4e5a9 --- /dev/null +++ b/framework/Testing/Data/Distributed/TDistributedDataSourceConfig.php @@ -0,0 +1,181 @@ +<?php +/** + * TDistributedDataSourceConfig class file. + * + * @author Yves Berkholz <godzilla80[at]gmx[dot]net> + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2010 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Testing.Data.Distributed + */ + + Prado::using('System.Data.TDataSourceConfig'); + Prado::using('System.Testing.Data.Distributed.TDistributedDbConnection'); + + /** + * IDistributedDataSourceConfig module interface provides <module> configuration for database connections. + * + * @author Yves Berkholz <godzilla80[at]gmx[dot]net> + * @version $Id$ + * @package System.Testing.Data.Distributed + * @since 4.0 + */ + interface IDistributedDataSourceConfig /*extends IDataSourceConfig*/ { + /** + * @return string Database connection class name to be created for child connection. + */ + public function getDistributedConnectionClass(); + + /** + * @param string Database connection class name to be created for child connection. + */ + public function setDistributedConnectionClass($value); + } + + /** + * TDistributedDataSourceConfig module class provides <module> configuration for database connections. + * + * @author Yves Berkholz <godzilla80[at]gmx[dot]net> + * @version $Id$ + * @package System.Testing.Data.Distributed + * @since 4.0 + */ + class TDistributedDataSourceConfig extends TDataSourceConfig implements IDistributedDataSourceConfig + { + /** + * @var array + */ + private $_childConnectionAttributes = array(); + + /** + * @var string + */ + private $_connDistributedClass = 'System.Data.TDbConnection'; + + /** + * @var IDistributedDbConnection + */ + private $_connDistributed = null; + + /** + * @var boolean + */ + protected $bInitialized = false; + + /** + * @var boolean + */ + private $_hasDistributedConnectionData = false; + + /** + * Initalize the database connection properties from attributes in <database> tag. + * @param TXmlDocument xml configuration. + */ + public function init($xml) + { + parent::init($xml); + $this->initChildConnectionData($xml); + $this->bInitialized = true; + } + + /** + * Initalize the database connection properties from attributes in $tagName tag. + * @param TXmlDocument xml configuration. + * @param string Tagnames to parse. Defaults to 'child' + */ + protected function initChildConnectionData($xml, $tagName='child') + { + $c = 0; + foreach($xml->getElementsByTagName($tagName) as $item) + { + ++$c; + $this->_childConnectionAttributes[] = $item->getAttributes(); + } + + if($c===0) + throw new TConfigurationException('distributeddatasource_child_required', get_class($this), $tagName); + } + + /** + * @return string Database connection class name to be created for child connection. + */ + public function getDistributedConnectionClass() + { + return $this->_connDistributedClass; + } + + /** + * @param string Database connection class name to be created for child connection. + */ + public function setDistributedConnectionClass($value) + { + $this->_connDistributedClass=$value; + } + + /** + * @return IDistributedDbConnection + */ + public function getDistributedDbConnection() + { + $this->_hasDistributedConnectionData = false; + if($this->_connDistributed===null) + $this->_connDistributed = Prado::createComponent($this->getDistributedConnectionClass()); + + if($this->_hasDistributedConnectionData) + return $this->_connDistributed; + + $attribs = $this->getDistributedDbConnectionAttributes(); + + if($attribs===null) + return $this->_connDistributed; + + foreach($attribs as $name => $value) + $this->_connDistributed->setSubproperty($name, $value); + + $this->_hasDistributedConnectionData = true; + + return $this->_connDistributed; + } + + /** + * @return TMap + */ + protected function getDistributedDbConnectionAttributes() + { + $index = 0; + $c = count($this->_childConnectionAttributes); + + if($c > 1) { + $aSrc = array(); + $aTmp = array(); + + foreach($this->_childConnectionAttributes as $k => $item) + { + $weight = 1; + if( isset($item['Weight']) ) + $weight = $item['Weight']; + $aSrc[$k] = $weight; + } + + asort($aSrc); + + foreach($aSrc as $idx => $weight) + $aTmp = array_merge($aTmp, array_pad(array(), $weight*5, $idx)); + + $min = 0; + $max = count($aTmp)-1; + $factor = array_sum($aSrc) / count($aSrc); + $wrand = round($min + (pow(rand(0, $max) / $max, $factor) * ($max - $min))); + $index = $aTmp[$wrand]; + } + + $result = $this->_childConnectionAttributes[$index]; + + if( isset($result['Weight']) ) + unset($result['Weight']); + + return $result; + } + } +?>
\ No newline at end of file |