Permalink
Browse files

Performance improvements Duplication Manager.

  • Loading branch information...
1 parent 10234e2 commit fc9164579fb9ad9fd42b0d2cb99269a42c9d817b @gabordemooij committed Dec 28, 2012
Showing with 73 additions and 22 deletions.
  1. +24 −4 RedBean/DuplicationManager.php
  2. +31 −17 RedBean/OODBBean.php
  3. +18 −1 testing/RedUNIT/Blackhole/Import.php
@@ -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) {
@@ -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;
}
-}
+}
View
@@ -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;
/**
@@ -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
@@ -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.
@@ -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
@@ -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
@@ -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
@@ -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:
@@ -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));
@@ -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.