Permalink
Browse files

Dup-cache.

  • Loading branch information...
1 parent 3d01ab1 commit 7a88b5439843c90d7132bcabc58196338e174fe5 @gabordemooij committed Oct 11, 2012
Showing with 82 additions and 11 deletions.
  1. +65 −11 RedBean/DuplicationManager.php
  2. +17 −0 testing/RedUNIT/Base/Dup.php
@@ -35,6 +35,7 @@ class RedBean_DuplicationManager {
protected $redbean;
protected $tables = array();
+ protected $columns = array();
protected $filters = array();
protected $cacheTables = false;
/**
@@ -56,11 +57,28 @@ public function __construct( RedBean_Toolbox $toolbox ) {
* @param array $tables
*/
public function setTables($tables) {
- $this->tables = $tables;
+ foreach($tables as $key=>$value) {
+ if (is_numeric($key)) {
+ $this->tables[] = $value;
+ }
+ else {
+ $this->tables[] = $key;
+ $this->columns[$key] = $value;
+ }
+ }
$this->cacheTables = true;
}
/**
+ * Returns a schema array for cache.
+ *
+ * @return array
+ */
+ public function getSchema() {
+ return $this->columns;
+ }
+
+ /**
* Indicates whether you want the duplication manager to cache the database schema.
* If this flag is set to TRUE the duplication manager will query the database schema
* only once. Otherwise the duplicationmanager will, by default, query the schema
@@ -85,6 +103,34 @@ public function setFilters($filters) {
$this->filters = $filters;
}
+ /**
+ * Determines whether the bean has an own list based on
+ * schema inspection from realtime schema or cache.
+ *
+ * @param string $type bean type
+ * @param string $target type of list you want to detect
+ *
+ * @return boolean
+ */
+ protected function hasOwnList($type,$target) {
+ return (isset($this->columns[$target][$type.'_id']));
+ }
+
+ /**
+ * Determines whether the bea has a shared list based on
+ * schema inspection from realtime schema or cache.
+ *
+ * @param string $type bean type
+ * @param string $target type of list you are looking for
+ *
+ * @return boolean
+ */
+ protected function hasSharedList($type,$target) {
+ $linkType = array($type,$target);
+ sort($linkType);
+ $linkType = implode('_',$linkType);
+ return (in_array($linkType,$this->tables));
+ }
/**
* Makes a copy of a bean. This method makes a deep copy
@@ -113,9 +159,13 @@ public function setFilters($filters) {
*/
public function dup($bean,$trail=array(),$pid=false) {
if (!count($this->tables)) $this->tables = $this->toolbox->getWriter()->getTables();
+ if (!count($this->columns)) foreach($this->tables as $table) $this->columns[$table] = $this->toolbox->getWriter()->getColumns($table);
$beanCopy = clone($bean);
$rs = $this->duplicate($beanCopy,$trail,$pid);
- if (!$this->cacheTables) $this->tables = array();
+ if (!$this->cacheTables) {
+ $this->tables = array();
+ $this->columns = array();
+ }
return $rs;
}
@@ -154,17 +204,21 @@ protected function duplicate($bean,$trail=array(),$pid=false) {
if (strpos($table,'_')!==false || $table==$type) continue;
$owned = 'own'.ucfirst($table);
$shared = 'shared'.ucfirst($table);
- if ($beans = $bean->$owned) {
- $copy->$owned = array();
- foreach($beans as $subBean) {
- array_push($copy->$owned,$this->duplicate($subBean,$trail,$pid));
+ if ($this->hasOwnList($type,$table)) {
+ if ($beans = $bean->$owned) {
+ $copy->$owned = array();
+ foreach($beans as $subBean) {
+ array_push($copy->$owned,$this->duplicate($subBean,$trail,$pid));
+ }
}
+ $copy->setMeta('sys.shadow.'.$owned,null);
}
- $copy->setMeta('sys.shadow.'.$owned,null);
- if ($beans = $bean->$shared) {
- $copy->$shared = array();
- foreach($beans as $subBean) {
- array_push($copy->$shared,$subBean);
+ if ($this->hasSharedList($type, $table)) {
+ if ($beans = $bean->$shared) {
+ $copy->$shared = array();
+ foreach($beans as $subBean) {
+ array_push($copy->$shared,$subBean);
+ }
}
}
$copy->setMeta('sys.shadow.'.$shared,null);
@@ -25,6 +25,23 @@ class RedUNIT_Base_Dup extends RedUNIT_Base {
*/
public function run() {
+ testpack('Dup() and Cache');
+ R::nuke();
+ $can = R::dispense('can')->setAttr('size', 3);
+ $can->ownCoffee[] = R::dispense('coffee')->setAttr('color', 'black');
+ $can->sharedTag[] = R::dispense('tag')->setAttr('name', 'cool');
+ $can = R::load('can', R::store($can));
+ $d = new RedBean_DuplicationManager(R::$toolbox);
+ $cache = '{"book":{"id":"INTEGER","title":"TEXT"},"bean":{"id":"INTEGER","prop":"INTEGER"},"pessoa":{"id":"INTEGER","nome":"TEXT","nome_meio":"TEXT","sobrenome":"TEXT","nascimento":"NUMERIC","reg_owner":"TEXT"},"documento":{"id":"INTEGER","nome_documento":"TEXT","numero_documento":"TEXT","reg_owner":"TEXT","ownPessoa_id":"INTEGER"},"can":{"id":"INTEGER","size":"INTEGER"},"coffee":{"id":"INTEGER","color":"TEXT","can_id":"INTEGER"},"tag":{"id":"INTEGER","name":"TEXT"},"can_tag":{"id":"INTEGER","tag_id":"INTEGER","can_id":"INTEGER"}}';
+ $d->setTables(json_decode($cache, 1));
+ $x = $d->dup($can);
+ asrt(isset($x->ownCoffee),true);
+ asrt(count($x->ownCoffee),1);
+ asrt(isset($x->sharedTag),true);
+ asrt(count($x->sharedTag),1);
+ asrt($cache,json_encode($d->getSchema()));
+
+
testpack('Dup() and Export() should not taint beans');
R::nuke();

0 comments on commit 7a88b54

Please sign in to comment.