Skip to content

Commit

Permalink
Performance improvements Duplication Manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabordemooij committed Dec 28, 2012
1 parent 10234e2 commit fc91645
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 22 deletions.
28 changes: 24 additions & 4 deletions RedBean/DuplicationManager.php
Expand Up @@ -175,12 +175,13 @@ public function dup($bean,$trail=array(),$pid=false) {
* @return array $copiedBean the duplicated bean
*/
protected function duplicate($bean,$trail=array(),$pid=false) {
$type = $bean->getMeta('type');
$type = $bean->getMeta('type');
$key = $type.$bean->getID();
if (isset($trail[$key])) return $bean;
$trail[$key]=$bean;
$copy =$this->redbean->dispense($type);
$copy->import( $bean->getProperties() );
$copy->importFrom($bean);
$copy->id = 0;
$tables = $this->tables;
foreach($tables as $table) {
Expand Down Expand Up @@ -225,13 +226,32 @@ protected function duplicate($bean,$trail=array(),$pid=false) {
* @return array $array exported structure
*/
public function exportAll($beans,$parents=false,$filters=array()) {
$array = array();
$array = array(); $copies = array(); $parentTypes = array();
if (!is_array($beans)) $beans = array($beans);
foreach($beans as $bean) {
$this->setFilters($filters);
$f = $this->dup($bean,array(),true);
$array[] = $f->export(false,$parents,false,$filters);
$copies[] = $f; //$f->export(false,$parents,false,$filters);
}
if ($parents) {
$firstCopy = reset($copies);
$properties = $firstCopy->getProperties();
foreach($properties as $property=>$value) {
if (strpos($property,'_id')!==false) {
$parentTypes[] = substr($property,0,-3);
}
}
//Try to preload as much parents as possible by analyzing first bean
$this->redbean->preload($copies,implode(',',$parentTypes));
foreach($copies as $bean) {
$array[] = $bean->export(false,true,false,$filters);
}
}
else {
foreach($copies as $bean) {
$array[] = $bean->export(false,false,false,$filters);
}
}
return $array;
}
}
}
48 changes: 31 additions & 17 deletions RedBean/OODBBean.php
Expand Up @@ -24,11 +24,11 @@ class RedBean_OODBBean implements IteratorAggregate, ArrayAccess, Countable {
*/
private static $flagKeyedExport = false;

/**
* Reference to NULL property for magic getter.
* @var Null $null
*/
private $null = null;
/**
* Reference to NULL property for magic getter.
* @var Null $null
*/
private $null = null;


/**
Expand All @@ -43,7 +43,7 @@ class RedBean_OODBBean implements IteratorAggregate, ArrayAccess, Countable {
* Meta information gets stored.
* @var array
*/
private $__info = NULL;
private $__info = array();

/**
* Contains a BeanHelper to access service objects like
Expand Down Expand Up @@ -158,6 +158,20 @@ public function import( $arr, $selection=false, $notrim=false ) {
}
return $this;
}

/**
* A Quick way to import bean data from a source bean.
*
* @param RedBean_OODBBean $sourceBean the source bean to take properties from
*
* @return RedBean_OODBBean $self
*/
public function importFrom(RedBean_OODBBean $sourceBean) {
$this->__info['tainted'] = true;
$array = $sourceBean->properties;
$this->properties = $array;
return $this;
}

/**
* Injects the properties of another bean but keeps the original ID.
Expand Down Expand Up @@ -554,8 +568,8 @@ public function __toString() {
* @return void
*/
public function offsetSet($offset, $value) {
$this->__set($offset, $value);
}
$this->__set($offset, $value);
}

/**
* Implementation of Array Access Interface, you can access bean objects
Expand All @@ -565,9 +579,9 @@ public function offsetSet($offset, $value) {
*
* @return
*/
public function offsetExists($offset) {
return isset($this->properties[$offset]);
}
public function offsetExists($offset) {
return isset($this->properties[$offset]);
}

/**
* Implementation of Array Access Interface, you can access bean objects
Expand All @@ -578,9 +592,9 @@ public function offsetExists($offset) {
*
* @return
*/
public function offsetUnset($offset) {
unset($this->properties[$offset]);
}
public function offsetUnset($offset) {
unset($this->properties[$offset]);
}

/**
* Implementation of Array Access Interface, you can access bean objects
Expand All @@ -591,9 +605,9 @@ public function offsetUnset($offset) {
*
* @return
*/
public function offsetGet($offset) {
return $this->__get($offset);
}
public function offsetGet($offset) {
return $this->__get($offset);
}

/**
* Chainable method to cast a certain ID to a bean; for instance:
Expand Down
19 changes: 18 additions & 1 deletion testing/RedUNIT/Blackhole/Import.php
Expand Up @@ -22,6 +22,23 @@ class RedUNIT_Blackhole_Import extends RedUNIT_Blackhole {
* @return void
*/
public function run() {
testpack('Test importFrom() and Tainted');
$bean = R::dispense('bean');
R::store($bean);
$bean->name = 'abc';
asrt($bean->getMeta('tainted'),true);
R::store($bean);
asrt($bean->getMeta('tainted'),false);
$copy = R::dispense('bean');
R::store($copy);
$copy = R::load('bean',$copy->id);
asrt($copy->getMeta('tainted'),false);
$copy->import(array('name'=>'xyz'));
asrt($copy->getMeta('tainted'),true);
$copy->setMeta('tainted',false);
asrt($copy->getMeta('tainted'),false);
$copy->importFrom($bean);
asrt($copy->getMeta('tainted'),true);
testpack('Test basic import() feature.');
$bean = new RedBean_OODBBean;
$bean->import(array("a"=>1,"b"=>2));
Expand All @@ -46,4 +63,4 @@ public function run() {
asrt($cup->id, 0);//id should not be transferred
}

}
}

0 comments on commit fc91645

Please sign in to comment.