From 5ac58bfb6db92bb074f309c91bd9106c40cb06cf Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Wed, 4 Mar 2020 14:12:21 +0330 Subject: [PATCH 01/21] adding flexible batch operation in ActiveRecord class --- src/ActiveRecord.php | 141 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index c720d94b2..0dba2dea8 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -25,6 +25,25 @@ */ abstract class ActiveRecord extends BaseActiveRecord { + + private static $batchInsertCommand; + private static $batchInsertQueue = 0; + private static $batchInsertDocuments = []; + private static $batchInsertInit = false; + public static $batchInsertSize = 500; + + private static $batchUpdateCommand; + private static $batchUpdateQueue = 0; + private static $batchUpdateDocuments = []; + private static $batchUpdateInit = false; + public static $batchUpdateSize = 500; + + private static $batchDeleteCommand; + private static $batchDeleteQueue = 0; + private static $batchDeleteDocuments = []; + private static $batchDeleteInit = false; + public static $batchDeleteSize = 500; + /** * Returns the Mongo connection used by this AR class. * By default, the "mongodb" application component is used as the Mongo connection. @@ -411,4 +430,126 @@ private function dumpBsonObject(Type $object) } return ArrayHelper::toArray($object); } + + public function batchSave():void{ + if($this->getIsNewRecord()) + return $this->batchInsert(); + return $this->batchUpdate(); + } + + public static function hasBatchInsert(){ + return self::$batchInsertQueue > 0; + } + + private static function batchInsertInit():void{ + if(self::$batchInsertInit) + return; + self::$batchInsertInit = true; + $db = static::getDb(); + self::$batchInsertCommand = ($db ? $db : yii::$app->mongodb)->createCommand(); + register_shutdown_function(function(){ + if(self::hasBatchInsert()) + yii::warning(self::className().' : batch insert mode not completed!'); + }); + } + + public function batchInsert():void{ + self::batchInsertInit(); + $values = $this->getDirtyAttributes($attributes); + if (empty($values)) { + $currentAttributes = $this->getAttributes(); + foreach ($this->primaryKey() as $key) { + if (isset($currentAttributes[$key])) { + $values[$key] = $currentAttributes[$key]; + } + } + } + self::$batchInsertCommand->AddInsert($values); + self::$batchInsertQueue++; + if(self::$batchInsertQueue >= self::$batchInsertSize) + self::flushBatchInsert(); + } + + public static function flushBatchInsert(){ + if(self::$batchInsertQueue === 0) + return; + self::$batchInsertQueue = 0; + $result = self::$batchInsertCommand->executeBatch(self::collectionName()); + self::$batchInsertCommand->document = []; + return $result; + } + + public static function hasBatchUpdate(){ + return self::$batchUpdateQueue > 0; + } + + private static function batchUpdateInit():void{ + if(self::$batchUpdateInit) + return; + self::$batchUpdateInit = true; + $db = static::getDb(); + self::$batchUpdateCommand = ($db ? $db : yii::$app->mongodb)->createCommand(); + register_shutdown_function(function(){ + if(self::hasBatchUpdate()) + yii::warning(self::className().' : batch update mode not completed!'); + }); + } + + public function batchUpdate():void{ + self::batchUpdateInit(); + $values = $this->getDirtyAttributes($attributes); + if (empty($values)) { + $this->afterSave(false, $values); + return 0; + } + $condition = $this->getOldPrimaryKey(true); + self::$batchUpdateCommand->AddUpdate($condition, $values); + self::$batchUpdateQueue++; + if(self::$batchUpdateQueue >= self::$batchUpdateSize) + self::flushBatchUpdate(); + } + + public static function flushBatchUpdate(){ + if(self::$batchUpdateQueue === 0) + return; + self::$batchUpdateQueue = 0; + $result = self::$batchUpdateCommand->executeBatch(self::collectionName()); + self::$batchUpdateCommand->document = []; + return $result; + } + + public static function hasBatchDelete(){ + return self::$batchDeleteQueue > 0; + } + + private static function batchDeleteInit():void{ + if(self::$batchDeleteInit) + return; + self::$batchDeleteInit = true; + $db = static::getDb(); + self::$batchDeleteCommand = ($db ? $db : yii::$app->mongodb)->createCommand(); + register_shutdown_function(function(){ + if(self::hasBatchDelete()) + yii::warning(self::className().' : batch update mode not completed!'); + }); + } + + public function batchDelete():void{ + self::batchDeleteInit(); + $condition = $this->getOldPrimaryKey(true); + self::$batchDeleteCommand->AddDelete($condition); + self::$batchDeleteQueue++; + if(self::$batchDeleteQueue >= self::$batchDeleteSize) + self::flushBatchDelete(); + } + + public static function flushBatchDelete(){ + if(self::$batchDeleteQueue === 0) + return; + self::$batchDeleteQueue = 0; + $result = self::$batchDeleteCommand->executeBatch(self::collectionName()); + self::$batchDeleteCommand->document = []; + return $result; + } + } From d97e1a9e4615cf58767fa16eef399c006065fdc5 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Wed, 4 Mar 2020 14:17:30 +0330 Subject: [PATCH 02/21] Update ActiveRecord.php --- src/ActiveRecord.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 0dba2dea8..24c7543c2 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -498,10 +498,8 @@ private static function batchUpdateInit():void{ public function batchUpdate():void{ self::batchUpdateInit(); $values = $this->getDirtyAttributes($attributes); - if (empty($values)) { - $this->afterSave(false, $values); - return 0; - } + if (empty($values)) + return; $condition = $this->getOldPrimaryKey(true); self::$batchUpdateCommand->AddUpdate($condition, $values); self::$batchUpdateQueue++; @@ -536,8 +534,7 @@ private static function batchDeleteInit():void{ public function batchDelete():void{ self::batchDeleteInit(); - $condition = $this->getOldPrimaryKey(true); - self::$batchDeleteCommand->AddDelete($condition); + self::$batchDeleteCommand->AddDelete($this->getOldPrimaryKey(true)); self::$batchDeleteQueue++; if(self::$batchDeleteQueue >= self::$batchDeleteSize) self::flushBatchDelete(); From 37a539bb7b5c82ca2d8909c819834f567178fc32 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Wed, 4 Mar 2020 15:13:31 +0330 Subject: [PATCH 03/21] Update ActiveRecord.php --- src/ActiveRecord.php | 70 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 24c7543c2..6105713e9 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -431,28 +431,40 @@ private function dumpBsonObject(Type $object) return ArrayHelper::toArray($object); } - public function batchSave():void{ + /** + * invoke batchInsert() or batchUpdate() base on getIsNewRecord() + */ + public function batchSave(){ if($this->getIsNewRecord()) return $this->batchInsert(); return $this->batchUpdate(); } + /** + * checking if current ActiveRecord class has documents in queue for insert + * @return bool + */ public static function hasBatchInsert(){ return self::$batchInsertQueue > 0; } - private static function batchInsertInit():void{ + /** + * this method is invoked in first call of batchInsert() method for once + */ + private static function batchInsertInit(){ if(self::$batchInsertInit) return; self::$batchInsertInit = true; - $db = static::getDb(); - self::$batchInsertCommand = ($db ? $db : yii::$app->mongodb)->createCommand(); + self::$batchInsertCommand = static::getDb()->createCommand(); register_shutdown_function(function(){ if(self::hasBatchInsert()) yii::warning(self::className().' : batch insert mode not completed!'); }); } + /** + * adding insert operation to queue base on current instance data + */ public function batchInsert():void{ self::batchInsertInit(); $values = $this->getDirtyAttributes($attributes); @@ -470,6 +482,11 @@ public function batchInsert():void{ self::flushBatchInsert(); } + /** + * execute batch insert operations in queue and reset anything + * this method is not continue when not exists any insert operations in queue + * @return see docs of Command::executeBatch() + */ public static function flushBatchInsert(){ if(self::$batchInsertQueue === 0) return; @@ -479,23 +496,32 @@ public static function flushBatchInsert(){ return $result; } + /** + * checking if current ActiveRecord class has documents in queue for update + * @return bool + */ public static function hasBatchUpdate(){ return self::$batchUpdateQueue > 0; } - private static function batchUpdateInit():void{ + /** + * this method is invoked in first call of batchUpdate() method for once + */ + private static function batchUpdateInit(){ if(self::$batchUpdateInit) return; self::$batchUpdateInit = true; - $db = static::getDb(); - self::$batchUpdateCommand = ($db ? $db : yii::$app->mongodb)->createCommand(); + self::$batchUpdateCommand = static::getDb()->createCommand(); register_shutdown_function(function(){ if(self::hasBatchUpdate()) yii::warning(self::className().' : batch update mode not completed!'); }); } - public function batchUpdate():void{ + /** + * adding update operation to queue base on current instance data + */ + public function batchUpdate(){ self::batchUpdateInit(); $values = $this->getDirtyAttributes($attributes); if (empty($values)) @@ -507,6 +533,11 @@ public function batchUpdate():void{ self::flushBatchUpdate(); } + /** + * execute batch update operations in queue and reset anything + * this method is not continue when not exists any update operations in queue + * @return see docs of Command::executeBatch() + */ public static function flushBatchUpdate(){ if(self::$batchUpdateQueue === 0) return; @@ -516,22 +547,31 @@ public static function flushBatchUpdate(){ return $result; } + /** + * checking if current ActiveRecord class has documents in queue for delete + * @return bool + */ public static function hasBatchDelete(){ return self::$batchDeleteQueue > 0; } - private static function batchDeleteInit():void{ + /** + * this method is invoked in first call of batchDelete() method for once + */ + private static function batchDeleteInit(){ if(self::$batchDeleteInit) return; self::$batchDeleteInit = true; - $db = static::getDb(); - self::$batchDeleteCommand = ($db ? $db : yii::$app->mongodb)->createCommand(); + self::$batchDeleteCommand = static::getDb()->createCommand(); register_shutdown_function(function(){ if(self::hasBatchDelete()) - yii::warning(self::className().' : batch update mode not completed!'); + yii::warning(self::className().' : batch delete mode not completed!'); }); } + /** + * adding delete operation to queue base on current instance data + */ public function batchDelete():void{ self::batchDeleteInit(); self::$batchDeleteCommand->AddDelete($this->getOldPrimaryKey(true)); @@ -540,6 +580,11 @@ public function batchDelete():void{ self::flushBatchDelete(); } + /** + * execute batch delete operations in queue and reset anything + * this method is not continue when not exists any delete operations in queue + * @return see docs of Command::executeBatch() + */ public static function flushBatchDelete(){ if(self::$batchDeleteQueue === 0) return; @@ -548,5 +593,4 @@ public static function flushBatchDelete(){ self::$batchDeleteCommand->document = []; return $result; } - } From ec3ed695780526a37d3843c2c915884dd4aef39e Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Wed, 4 Mar 2020 19:17:38 +0330 Subject: [PATCH 04/21] Update ActiveRecord.php --- src/ActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 6105713e9..dbba837d7 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -465,7 +465,7 @@ private static function batchInsertInit(){ /** * adding insert operation to queue base on current instance data */ - public function batchInsert():void{ + public function batchInsert(){ self::batchInsertInit(); $values = $this->getDirtyAttributes($attributes); if (empty($values)) { From fbd9a720319c0ecc465f76709afbb0c2a38e3413 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Wed, 4 Mar 2020 19:18:55 +0330 Subject: [PATCH 05/21] Update ActiveRecord.php --- src/ActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index dbba837d7..0ac5269f7 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -572,7 +572,7 @@ private static function batchDeleteInit(){ /** * adding delete operation to queue base on current instance data */ - public function batchDelete():void{ + public function batchDelete(){ self::batchDeleteInit(); self::$batchDeleteCommand->AddDelete($this->getOldPrimaryKey(true)); self::$batchDeleteQueue++; From dd0f5d0476608a2b5171bfa1ed1c83bdd61a5b20 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Wed, 4 Mar 2020 20:58:37 +0330 Subject: [PATCH 06/21] Update ActiveRecord.php --- src/ActiveRecord.php | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 0ac5269f7..6f128ca55 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -26,22 +26,67 @@ abstract class ActiveRecord extends BaseActiveRecord { + /* + * @var Command instance of Command class for batch insert. + */ private static $batchInsertCommand; + /* + * @var integer count of insert operation in queue + */ private static $batchInsertQueue = 0; + /* + * @var array array of document for insert + */ private static $batchInsertDocuments = []; + /* + * @var boolean a boolean flag for detect first initialize + */ private static $batchInsertInit = false; + /* + * @var int size of batch for insert operations + */ public static $batchInsertSize = 500; + /* + * @var Command instance of Command class for batch update. + */ private static $batchUpdateCommand; + /* + * @var integer count of update operation in queue + */ private static $batchUpdateQueue = 0; + /* + * @var array array of document for update + */ private static $batchUpdateDocuments = []; + /* + * @var boolean a boolean flag for detect first initialize + */ private static $batchUpdateInit = false; + /* + * @var int size of batch for update operations + */ public static $batchUpdateSize = 500; + /* + * @var Command instance of Command class for batch delete. + */ private static $batchDeleteCommand; + /* + * @var integer count of delete operation in queue + */ private static $batchDeleteQueue = 0; + /* + * @var array array of document for delete + */ private static $batchDeleteDocuments = []; + /* + * @var boolean a boolean flag for detect first initialize + */ private static $batchDeleteInit = false; + /* + * @var int size of batch for delete operations + */ public static $batchDeleteSize = 500; /** From e1315f8aa2a60fcc22fe9f5c23134fdb49b893e1 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Sat, 7 Mar 2020 10:34:40 +0330 Subject: [PATCH 07/21] Update ActiveRecord.php --- src/ActiveRecord.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 6f128ca55..2c7088e88 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -509,8 +509,10 @@ private static function batchInsertInit(){ /** * adding insert operation to queue base on current instance data + * @param array $attributes list of attributes that need to be inserted. Defaults to null, + * meaning all attributes that are loaded will be inserted. */ - public function batchInsert(){ + public function batchInsert($attributes = null){ self::batchInsertInit(); $values = $this->getDirtyAttributes($attributes); if (empty($values)) { @@ -565,8 +567,10 @@ private static function batchUpdateInit(){ /** * adding update operation to queue base on current instance data + * @param array $attributes list of attribute names that need to be updated. Defaults to null, + * meaning all attributes that are loaded from DB will be updated. */ - public function batchUpdate(){ + public function batchUpdate($attributes = null){ self::batchUpdateInit(); $values = $this->getDirtyAttributes($attributes); if (empty($values)) From 76e3fdb4f0a65e2c4e0aa8ef19ff2e52cf629204 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Sat, 7 Mar 2020 10:46:56 +0330 Subject: [PATCH 08/21] Update ActiveRecord.php --- src/ActiveRecord.php | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 2c7088e88..31614d967 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -478,11 +478,13 @@ private function dumpBsonObject(Type $object) /** * invoke batchInsert() or batchUpdate() base on getIsNewRecord() + * @param array $attributes list of attributes that need to be inserted or updated. Defaults to null, + * meaning all attributes that are loaded will be inserted or updated. */ - public function batchSave(){ + public function batchSave($attributes = null){ if($this->getIsNewRecord()) - return $this->batchInsert(); - return $this->batchUpdate(); + return $this->batchInsert($attributes); + return $this->batchUpdate($attributes); } /** @@ -503,7 +505,7 @@ private static function batchInsertInit(){ self::$batchInsertCommand = static::getDb()->createCommand(); register_shutdown_function(function(){ if(self::hasBatchInsert()) - yii::warning(self::className().' : batch insert mode not completed!'); + yii::warning(static::className().' : batch insert mode not completed!'); }); } @@ -525,7 +527,7 @@ public function batchInsert($attributes = null){ } self::$batchInsertCommand->AddInsert($values); self::$batchInsertQueue++; - if(self::$batchInsertQueue >= self::$batchInsertSize) + if(self::$batchInsertQueue >= static::$batchInsertSize) self::flushBatchInsert(); } @@ -538,7 +540,7 @@ public static function flushBatchInsert(){ if(self::$batchInsertQueue === 0) return; self::$batchInsertQueue = 0; - $result = self::$batchInsertCommand->executeBatch(self::collectionName()); + $result = self::$batchInsertCommand->executeBatch(static::collectionName()); self::$batchInsertCommand->document = []; return $result; } @@ -561,7 +563,7 @@ private static function batchUpdateInit(){ self::$batchUpdateCommand = static::getDb()->createCommand(); register_shutdown_function(function(){ if(self::hasBatchUpdate()) - yii::warning(self::className().' : batch update mode not completed!'); + yii::warning(static::className().' : batch update mode not completed!'); }); } @@ -578,7 +580,7 @@ public function batchUpdate($attributes = null){ $condition = $this->getOldPrimaryKey(true); self::$batchUpdateCommand->AddUpdate($condition, $values); self::$batchUpdateQueue++; - if(self::$batchUpdateQueue >= self::$batchUpdateSize) + if(self::$batchUpdateQueue >= static::$batchUpdateSize) self::flushBatchUpdate(); } @@ -591,7 +593,7 @@ public static function flushBatchUpdate(){ if(self::$batchUpdateQueue === 0) return; self::$batchUpdateQueue = 0; - $result = self::$batchUpdateCommand->executeBatch(self::collectionName()); + $result = self::$batchUpdateCommand->executeBatch(static::collectionName()); self::$batchUpdateCommand->document = []; return $result; } @@ -614,7 +616,7 @@ private static function batchDeleteInit(){ self::$batchDeleteCommand = static::getDb()->createCommand(); register_shutdown_function(function(){ if(self::hasBatchDelete()) - yii::warning(self::className().' : batch delete mode not completed!'); + yii::warning(static::className().' : batch delete mode not completed!'); }); } @@ -625,7 +627,7 @@ public function batchDelete(){ self::batchDeleteInit(); self::$batchDeleteCommand->AddDelete($this->getOldPrimaryKey(true)); self::$batchDeleteQueue++; - if(self::$batchDeleteQueue >= self::$batchDeleteSize) + if(self::$batchDeleteQueue >= static::$batchDeleteSize) self::flushBatchDelete(); } @@ -638,7 +640,7 @@ public static function flushBatchDelete(){ if(self::$batchDeleteQueue === 0) return; self::$batchDeleteQueue = 0; - $result = self::$batchDeleteCommand->executeBatch(self::collectionName()); + $result = self::$batchDeleteCommand->executeBatch(static::collectionName()); self::$batchDeleteCommand->document = []; return $result; } From 805d40b91e736319682657194b76445fd2ecb569 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Wed, 11 Mar 2020 16:14:20 +0330 Subject: [PATCH 09/21] adding batchUpdateAll and batchDeleteAll static method --- src/ActiveRecord.php | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 31614d967..c505d8f62 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -578,7 +578,23 @@ public function batchUpdate($attributes = null){ if (empty($values)) return; $condition = $this->getOldPrimaryKey(true); - self::$batchUpdateCommand->AddUpdate($condition, $values); + self::$batchUpdateCommand->addUpdate($condition, $values); + self::$batchUpdateQueue++; + if(self::$batchUpdateQueue >= static::$batchUpdateSize) + self::flushBatchUpdate(); + } + + /** + * adding update operation to queue + * @param array $attributes list of attribute names that need to be updated. + * @param array $condition Description of the objects to update. + * Please refer to Query::where() on how to specify this parameter. + * @param array $options List of options in format: optionName => optionValue. + * Please refer to Command::addUpdate() on how to specify this parameter. + */ + public static function batchUpdateAll($attributes, $condition = [], $options = []){ + self::batchUpdateInit(); + self::$batchUpdateCommand->addUpdate($condition, $attributes, $options); self::$batchUpdateQueue++; if(self::$batchUpdateQueue >= static::$batchUpdateSize) self::flushBatchUpdate(); @@ -631,6 +647,21 @@ public function batchDelete(){ self::flushBatchDelete(); } + /** + * adding delete operation to queue + * @param array $condition Description of the objects to delete. + * Please refer to Query::where() on how to specify this parameter. + * @param array $options List of options in format: optionName => optionValue. + * Please refer to Command::AddDelete() on how to specify this parameter. + */ + public function batchDeleteAll($condition = [], $options = []){ + self::batchDeleteInit(); + self::$batchDeleteCommand->AddDelete($condition, $options); + self::$batchDeleteQueue++; + if(self::$batchDeleteQueue >= static::$batchDeleteSize) + self::flushBatchDelete(); + } + /** * execute batch delete operations in queue and reset anything * this method is not continue when not exists any delete operations in queue From ee35183eb34c29601cd67abfd91b97bf453a3f2b Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Sat, 11 Apr 2020 14:54:55 +0430 Subject: [PATCH 10/21] adding return --- src/ActiveRecord.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index c505d8f62..0d573f0bd 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -480,6 +480,7 @@ private function dumpBsonObject(Type $object) * invoke batchInsert() or batchUpdate() base on getIsNewRecord() * @param array $attributes list of attributes that need to be inserted or updated. Defaults to null, * meaning all attributes that are loaded will be inserted or updated. + * @return see ActiveRecord::batchInsert() */ public function batchSave($attributes = null){ if($this->getIsNewRecord()) @@ -513,6 +514,7 @@ private static function batchInsertInit(){ * adding insert operation to queue base on current instance data * @param array $attributes list of attributes that need to be inserted. Defaults to null, * meaning all attributes that are loaded will be inserted. + * @return null|array return null if no execute command or return result of Command::executeBatch() */ public function batchInsert($attributes = null){ self::batchInsertInit(); @@ -528,7 +530,7 @@ public function batchInsert($attributes = null){ self::$batchInsertCommand->AddInsert($values); self::$batchInsertQueue++; if(self::$batchInsertQueue >= static::$batchInsertSize) - self::flushBatchInsert(); + return self::flushBatchInsert(); } /** @@ -571,6 +573,7 @@ private static function batchUpdateInit(){ * adding update operation to queue base on current instance data * @param array $attributes list of attribute names that need to be updated. Defaults to null, * meaning all attributes that are loaded from DB will be updated. + * @return null|array return null if no execute command or return result of Command::executeBatch() */ public function batchUpdate($attributes = null){ self::batchUpdateInit(); @@ -581,7 +584,7 @@ public function batchUpdate($attributes = null){ self::$batchUpdateCommand->addUpdate($condition, $values); self::$batchUpdateQueue++; if(self::$batchUpdateQueue >= static::$batchUpdateSize) - self::flushBatchUpdate(); + return self::flushBatchUpdate(); } /** @@ -591,13 +594,14 @@ public function batchUpdate($attributes = null){ * Please refer to Query::where() on how to specify this parameter. * @param array $options List of options in format: optionName => optionValue. * Please refer to Command::addUpdate() on how to specify this parameter. + * @return null|array return null if no execute command or return result of Command::executeBatch() */ public static function batchUpdateAll($attributes, $condition = [], $options = []){ self::batchUpdateInit(); self::$batchUpdateCommand->addUpdate($condition, $attributes, $options); self::$batchUpdateQueue++; if(self::$batchUpdateQueue >= static::$batchUpdateSize) - self::flushBatchUpdate(); + return self::flushBatchUpdate(); } /** @@ -638,13 +642,14 @@ private static function batchDeleteInit(){ /** * adding delete operation to queue base on current instance data + * @return null|array return null if no execute command or return result of Command::executeBatch() */ public function batchDelete(){ self::batchDeleteInit(); self::$batchDeleteCommand->AddDelete($this->getOldPrimaryKey(true)); self::$batchDeleteQueue++; if(self::$batchDeleteQueue >= static::$batchDeleteSize) - self::flushBatchDelete(); + return self::flushBatchDelete(); } /** @@ -653,13 +658,14 @@ public function batchDelete(){ * Please refer to Query::where() on how to specify this parameter. * @param array $options List of options in format: optionName => optionValue. * Please refer to Command::AddDelete() on how to specify this parameter. + * @return null|array return null if no execute command or return result of Command::executeBatch() */ public function batchDeleteAll($condition = [], $options = []){ self::batchDeleteInit(); self::$batchDeleteCommand->AddDelete($condition, $options); self::$batchDeleteQueue++; if(self::$batchDeleteQueue >= static::$batchDeleteSize) - self::flushBatchDelete(); + return self::flushBatchDelete(); } /** From a19225576971d3ab808e9d296043b2f225dcdf2e Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Sat, 11 Apr 2020 15:56:11 +0430 Subject: [PATCH 11/21] fixbug --- src/ActiveRecord.php | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 0d573f0bd..019a312f6 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -541,12 +541,20 @@ public function batchInsert($attributes = null){ public static function flushBatchInsert(){ if(self::$batchInsertQueue === 0) return; - self::$batchInsertQueue = 0; $result = self::$batchInsertCommand->executeBatch(static::collectionName()); + self::$batchInsertQueue = 0; self::$batchInsertCommand->document = []; return $result; } + /** + * resetting queue + */ + public static function resetBatchInsert(){ + self::$batchInsertQueue = 0; + self::$batchInsertCommand->document = []; + } + /** * checking if current ActiveRecord class has documents in queue for update * @return bool @@ -612,12 +620,20 @@ public static function batchUpdateAll($attributes, $condition = [], $options = [ public static function flushBatchUpdate(){ if(self::$batchUpdateQueue === 0) return; - self::$batchUpdateQueue = 0; $result = self::$batchUpdateCommand->executeBatch(static::collectionName()); + self::$batchUpdateQueue = 0; self::$batchUpdateCommand->document = []; return $result; } + /** + * resetting queue + */ + public static function resetBatchUpdate(){ + self::$batchUpdateQueue = 0; + self::$batchUpdateCommand->document = []; + } + /** * checking if current ActiveRecord class has documents in queue for delete * @return bool @@ -676,9 +692,17 @@ public function batchDeleteAll($condition = [], $options = []){ public static function flushBatchDelete(){ if(self::$batchDeleteQueue === 0) return; - self::$batchDeleteQueue = 0; $result = self::$batchDeleteCommand->executeBatch(static::collectionName()); + self::$batchDeleteQueue = 0; self::$batchDeleteCommand->document = []; return $result; } + + /** + * resetting queue + */ + public static function resetBatchDelete(){ + self::$batchDeleteQueue = 0; + self::$batchDeleteCommand->document = []; + } } From c9e7926c2760beb98846f3d52387fcf13ea75e46 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Sun, 12 Apr 2020 15:30:20 +0430 Subject: [PATCH 12/21] resetting methods --- src/ActiveRecord.php | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 019a312f6..8b7b684b5 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -533,6 +533,14 @@ public function batchInsert($attributes = null){ return self::flushBatchInsert(); } + /** + * resetting batch insert + */ + public static function resetBatchInsert(){ + self::$batchInsertQueue = 0; + self::$batchInsertCommand->document = []; + } + /** * execute batch insert operations in queue and reset anything * this method is not continue when not exists any insert operations in queue @@ -542,19 +550,10 @@ public static function flushBatchInsert(){ if(self::$batchInsertQueue === 0) return; $result = self::$batchInsertCommand->executeBatch(static::collectionName()); - self::$batchInsertQueue = 0; - self::$batchInsertCommand->document = []; + self::resetBatchInsert(); return $result; } - /** - * resetting queue - */ - public static function resetBatchInsert(){ - self::$batchInsertQueue = 0; - self::$batchInsertCommand->document = []; - } - /** * checking if current ActiveRecord class has documents in queue for update * @return bool @@ -612,6 +611,14 @@ public static function batchUpdateAll($attributes, $condition = [], $options = [ return self::flushBatchUpdate(); } + /** + * resetting batch update + */ + public static function resetBatchUpdate(){ + self::$batchUpdateQueue = 0; + self::$batchUpdateCommand->document = []; + } + /** * execute batch update operations in queue and reset anything * this method is not continue when not exists any update operations in queue @@ -621,8 +628,7 @@ public static function flushBatchUpdate(){ if(self::$batchUpdateQueue === 0) return; $result = self::$batchUpdateCommand->executeBatch(static::collectionName()); - self::$batchUpdateQueue = 0; - self::$batchUpdateCommand->document = []; + self::resetBatchUpdate(); return $result; } @@ -684,6 +690,14 @@ public function batchDeleteAll($condition = [], $options = []){ return self::flushBatchDelete(); } + /** + * resetting batch delete + */ + public static function resetBatchDelete(){ + self::$batchDeleteQueue = 0; + self::$batchDeleteCommand->document = []; + } + /** * execute batch delete operations in queue and reset anything * this method is not continue when not exists any delete operations in queue @@ -693,8 +707,7 @@ public static function flushBatchDelete(){ if(self::$batchDeleteQueue === 0) return; $result = self::$batchDeleteCommand->executeBatch(static::collectionName()); - self::$batchDeleteQueue = 0; - self::$batchDeleteCommand->document = []; + self::resetBatchDelete(); return $result; } From c6808c7dbb4cf0ae06a0ef06adfe751fedcfd94c Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Sun, 12 Apr 2020 15:36:33 +0430 Subject: [PATCH 13/21] Update ActiveRecord.php --- src/ActiveRecord.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 8b7b684b5..61b76baf2 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -632,14 +632,6 @@ public static function flushBatchUpdate(){ return $result; } - /** - * resetting queue - */ - public static function resetBatchUpdate(){ - self::$batchUpdateQueue = 0; - self::$batchUpdateCommand->document = []; - } - /** * checking if current ActiveRecord class has documents in queue for delete * @return bool @@ -710,12 +702,4 @@ public static function flushBatchDelete(){ self::resetBatchDelete(); return $result; } - - /** - * resetting queue - */ - public static function resetBatchDelete(){ - self::$batchDeleteQueue = 0; - self::$batchDeleteCommand->document = []; - } } From a4fd97af75c6adb1c5b059e0ffaa4e03620a6d43 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Thu, 9 Jul 2020 12:30:17 +0430 Subject: [PATCH 14/21] fix enableLogging base on : https://github.com/yiisoft/yii2-mongodb/pull/314 --- src/ActiveRecord.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 61b76baf2..6d7328a21 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -504,10 +504,11 @@ private static function batchInsertInit(){ return; self::$batchInsertInit = true; self::$batchInsertCommand = static::getDb()->createCommand(); - register_shutdown_function(function(){ - if(self::hasBatchInsert()) - yii::warning(static::className().' : batch insert mode not completed!'); - }); + if(self::$batchInsertCommand->db->enableLogging) + register_shutdown_function(function(){ + if(self::hasBatchInsert()) + yii::warning(static::className().' : batch insert mode not completed!'); + }); } /** @@ -570,10 +571,11 @@ private static function batchUpdateInit(){ return; self::$batchUpdateInit = true; self::$batchUpdateCommand = static::getDb()->createCommand(); - register_shutdown_function(function(){ - if(self::hasBatchUpdate()) - yii::warning(static::className().' : batch update mode not completed!'); - }); + if(self::$batchUpdateCommand->db->enableLogging) + register_shutdown_function(function(){ + if(self::hasBatchUpdate()) + yii::warning(static::className().' : batch update mode not completed!'); + }); } /** @@ -648,10 +650,11 @@ private static function batchDeleteInit(){ return; self::$batchDeleteInit = true; self::$batchDeleteCommand = static::getDb()->createCommand(); - register_shutdown_function(function(){ - if(self::hasBatchDelete()) - yii::warning(static::className().' : batch delete mode not completed!'); - }); + if(self::$batchDeleteCommand->db->enableLogging) + register_shutdown_function(function(){ + if(self::hasBatchDelete()) + yii::warning(static::className().' : batch delete mode not completed!'); + }); } /** From af70d1997d8e7cb030a101230988c417bdaf07ea Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Tue, 29 Sep 2020 17:10:04 +0330 Subject: [PATCH 15/21] fix bug --- src/ActiveRecord.php | 139 ++++++++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 61 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 6d7328a21..2b9816b38 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -29,19 +29,15 @@ abstract class ActiveRecord extends BaseActiveRecord /* * @var Command instance of Command class for batch insert. */ - private static $batchInsertCommand; + private static $batchInsertCommand = []; /* * @var integer count of insert operation in queue */ - private static $batchInsertQueue = 0; + private static $batchInsertQueue = []; /* * @var array array of document for insert */ private static $batchInsertDocuments = []; - /* - * @var boolean a boolean flag for detect first initialize - */ - private static $batchInsertInit = false; /* * @var int size of batch for insert operations */ @@ -50,19 +46,15 @@ abstract class ActiveRecord extends BaseActiveRecord /* * @var Command instance of Command class for batch update. */ - private static $batchUpdateCommand; + private static $batchUpdateCommand = []; /* * @var integer count of update operation in queue */ - private static $batchUpdateQueue = 0; + private static $batchUpdateQueue = []; /* * @var array array of document for update */ private static $batchUpdateDocuments = []; - /* - * @var boolean a boolean flag for detect first initialize - */ - private static $batchUpdateInit = false; /* * @var int size of batch for update operations */ @@ -71,19 +63,15 @@ abstract class ActiveRecord extends BaseActiveRecord /* * @var Command instance of Command class for batch delete. */ - private static $batchDeleteCommand; + private static $batchDeleteCommand = []; /* * @var integer count of delete operation in queue */ - private static $batchDeleteQueue = 0; + private static $batchDeleteQueue = []; /* * @var array array of document for delete */ private static $batchDeleteDocuments = []; - /* - * @var boolean a boolean flag for detect first initialize - */ - private static $batchDeleteInit = false; /* * @var int size of batch for delete operations */ @@ -493,18 +481,22 @@ public function batchSave($attributes = null){ * @return bool */ public static function hasBatchInsert(){ - return self::$batchInsertQueue > 0; + $className = static::className(); + return array_key_exists($className,self::$batchInsertQueue) && self::$batchInsertQueue[$className] > 0; } /** * this method is invoked in first call of batchInsert() method for once */ private static function batchInsertInit(){ - if(self::$batchInsertInit) + $className = static::className(); + + if(array_key_exists($className,self::$batchInsertCommand)) return; - self::$batchInsertInit = true; - self::$batchInsertCommand = static::getDb()->createCommand(); - if(self::$batchInsertCommand->db->enableLogging) + + self::$batchInsertQueue[$className] = 0; + self::$batchInsertCommand[$className] = static::getDb()->createCommand(); + if(self::$batchInsertCommand[$className]->db->enableLogging) register_shutdown_function(function(){ if(self::hasBatchInsert()) yii::warning(static::className().' : batch insert mode not completed!'); @@ -528,9 +520,10 @@ public function batchInsert($attributes = null){ } } } - self::$batchInsertCommand->AddInsert($values); - self::$batchInsertQueue++; - if(self::$batchInsertQueue >= static::$batchInsertSize) + $className = static::className(); + self::$batchInsertCommand[$className]->AddInsert($values); + self::$batchInsertQueue[$className]++; + if(self::$batchInsertQueue[$className] >= static::$batchInsertSize) return self::flushBatchInsert(); } @@ -538,8 +531,11 @@ public function batchInsert($attributes = null){ * resetting batch insert */ public static function resetBatchInsert(){ - self::$batchInsertQueue = 0; - self::$batchInsertCommand->document = []; + $className = static::className(); + if(!array_key_exists($className,self::$batchInsertCommand)) + return; + self::$batchInsertQueue[$className] = 0; + self::$batchInsertCommand[$className]->document = []; } /** @@ -548,9 +544,10 @@ public static function resetBatchInsert(){ * @return see docs of Command::executeBatch() */ public static function flushBatchInsert(){ - if(self::$batchInsertQueue === 0) + $className = static::className(); + if(!array_key_exists($className,self::$batchInsertQueue) || self::$batchInsertQueue[$className] === 0) return; - $result = self::$batchInsertCommand->executeBatch(static::collectionName()); + $result = self::$batchInsertCommand[$className]->executeBatch(static::collectionName()); self::resetBatchInsert(); return $result; } @@ -560,18 +557,22 @@ public static function flushBatchInsert(){ * @return bool */ public static function hasBatchUpdate(){ - return self::$batchUpdateQueue > 0; + $className = static::className(); + return array_key_exists($className,self::$batchUpdateQueue) && self::$batchUpdateQueue[$className] > 0; } /** * this method is invoked in first call of batchUpdate() method for once */ private static function batchUpdateInit(){ - if(self::$batchUpdateInit) + $className = static::className(); + + if(array_key_exists($className,self::$batchUpdateCommand)) return; - self::$batchUpdateInit = true; - self::$batchUpdateCommand = static::getDb()->createCommand(); - if(self::$batchUpdateCommand->db->enableLogging) + + self::$batchUpdateQueue[$className] = 0; + self::$batchUpdateCommand[$className] = static::getDb()->createCommand(); + if(self::$batchUpdateCommand[$className]->db->enableLogging) register_shutdown_function(function(){ if(self::hasBatchUpdate()) yii::warning(static::className().' : batch update mode not completed!'); @@ -590,9 +591,10 @@ public function batchUpdate($attributes = null){ if (empty($values)) return; $condition = $this->getOldPrimaryKey(true); - self::$batchUpdateCommand->addUpdate($condition, $values); - self::$batchUpdateQueue++; - if(self::$batchUpdateQueue >= static::$batchUpdateSize) + $className = static::className(); + self::$batchUpdateCommand[$className]->addUpdate($condition, $values); + self::$batchUpdateQueue[$className]++; + if(self::$batchUpdateQueue[$className] >= static::$batchUpdateSize) return self::flushBatchUpdate(); } @@ -607,9 +609,10 @@ public function batchUpdate($attributes = null){ */ public static function batchUpdateAll($attributes, $condition = [], $options = []){ self::batchUpdateInit(); - self::$batchUpdateCommand->addUpdate($condition, $attributes, $options); - self::$batchUpdateQueue++; - if(self::$batchUpdateQueue >= static::$batchUpdateSize) + $className = static::className(); + self::$batchUpdateCommand[$className]->addUpdate($condition, $attributes, $options); + self::$batchUpdateQueue[$className]++; + if(self::$batchUpdateQueue[$className] >= static::$batchUpdateSize) return self::flushBatchUpdate(); } @@ -617,8 +620,11 @@ public static function batchUpdateAll($attributes, $condition = [], $options = [ * resetting batch update */ public static function resetBatchUpdate(){ - self::$batchUpdateQueue = 0; - self::$batchUpdateCommand->document = []; + $className = static::className(); + if(!array_key_exists($className,self::$batchUpdateCommand)) + return; + self::$batchUpdateQueue[$className] = 0; + self::$batchUpdateCommand[$className]->document = []; } /** @@ -627,9 +633,10 @@ public static function resetBatchUpdate(){ * @return see docs of Command::executeBatch() */ public static function flushBatchUpdate(){ - if(self::$batchUpdateQueue === 0) + $className = static::className(); + if(!array_key_exists($className,self::$batchUpdateQueue) || self::$batchUpdateQueue[$className] === 0) return; - $result = self::$batchUpdateCommand->executeBatch(static::collectionName()); + $result = self::$batchUpdateCommand[$className]->executeBatch(static::collectionName()); self::resetBatchUpdate(); return $result; } @@ -639,18 +646,22 @@ public static function flushBatchUpdate(){ * @return bool */ public static function hasBatchDelete(){ - return self::$batchDeleteQueue > 0; + $className = static::className(); + return array_key_exists($className,self::$batchDeleteQueue) && self::$batchDeleteQueue[$className] > 0; } /** * this method is invoked in first call of batchDelete() method for once */ private static function batchDeleteInit(){ - if(self::$batchDeleteInit) + $className = static::className(); + + if(array_key_exists($className,self::$batchDeleteCommand)) return; - self::$batchDeleteInit = true; - self::$batchDeleteCommand = static::getDb()->createCommand(); - if(self::$batchDeleteCommand->db->enableLogging) + + self::$batchDeleteQueue[$className] = 0; + self::$batchDeleteCommand[$className] = static::getDb()->createCommand(); + if(self::$batchDeleteCommand[$className]->db->enableLogging) register_shutdown_function(function(){ if(self::hasBatchDelete()) yii::warning(static::className().' : batch delete mode not completed!'); @@ -663,9 +674,10 @@ private static function batchDeleteInit(){ */ public function batchDelete(){ self::batchDeleteInit(); - self::$batchDeleteCommand->AddDelete($this->getOldPrimaryKey(true)); - self::$batchDeleteQueue++; - if(self::$batchDeleteQueue >= static::$batchDeleteSize) + $className = static::className(); + self::$batchDeleteCommand[$className]->AddDelete($this->getOldPrimaryKey(true)); + self::$batchDeleteQueue[$className]++; + if(self::$batchDeleteQueue[$className] >= static::$batchDeleteSize) return self::flushBatchDelete(); } @@ -679,9 +691,10 @@ public function batchDelete(){ */ public function batchDeleteAll($condition = [], $options = []){ self::batchDeleteInit(); - self::$batchDeleteCommand->AddDelete($condition, $options); - self::$batchDeleteQueue++; - if(self::$batchDeleteQueue >= static::$batchDeleteSize) + $className = static::className(); + self::$batchDeleteCommand[$className]->AddDelete($condition, $options); + self::$batchDeleteQueue[$className]++; + if(self::$batchDeleteQueue[$className] >= static::$batchDeleteSize) return self::flushBatchDelete(); } @@ -689,8 +702,11 @@ public function batchDeleteAll($condition = [], $options = []){ * resetting batch delete */ public static function resetBatchDelete(){ - self::$batchDeleteQueue = 0; - self::$batchDeleteCommand->document = []; + $className = static::className(); + if(!array_key_exists($className,self::$batchDeleteCommand)) + return; + self::$batchDeleteQueue[$className] = 0; + self::$batchDeleteCommand[$className]->document = []; } /** @@ -699,10 +715,11 @@ public static function resetBatchDelete(){ * @return see docs of Command::executeBatch() */ public static function flushBatchDelete(){ - if(self::$batchDeleteQueue === 0) + $className = static::className(); + if(!array_key_exists($className,self::$batchDeleteQueue) || self::$batchDeleteQueue[$className] === 0) return; - $result = self::$batchDeleteCommand->executeBatch(static::collectionName()); + $result = self::$batchDeleteCommand[$className]->executeBatch(static::collectionName()); self::resetBatchDelete(); return $result; } -} +} \ No newline at end of file From 5f73d15dc74a291dbc412920f578340494eba48a Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Wed, 23 Dec 2020 20:16:48 +0330 Subject: [PATCH 16/21] fix a bug --- src/ActiveRecord.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 2b9816b38..940c2872e 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -68,10 +68,6 @@ abstract class ActiveRecord extends BaseActiveRecord * @var integer count of delete operation in queue */ private static $batchDeleteQueue = []; - /* - * @var array array of document for delete - */ - private static $batchDeleteDocuments = []; /* * @var int size of batch for delete operations */ @@ -689,7 +685,7 @@ public function batchDelete(){ * Please refer to Command::AddDelete() on how to specify this parameter. * @return null|array return null if no execute command or return result of Command::executeBatch() */ - public function batchDeleteAll($condition = [], $options = []){ + public static function batchDeleteAll($condition = [], $options = []){ self::batchDeleteInit(); $className = static::className(); self::$batchDeleteCommand[$className]->AddDelete($condition, $options); From 2caf17b321ba13169c8cdca22f21572819e66d6d Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Wed, 13 Dec 2023 17:46:15 +0330 Subject: [PATCH 17/21] add scope to batch --- src/ActiveRecord.php | 173 +++++++++++++++++++++++-------------------- 1 file changed, 91 insertions(+), 82 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 940c2872e..48da803ff 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -466,35 +466,38 @@ private function dumpBsonObject(Type $object) * meaning all attributes that are loaded will be inserted or updated. * @return see ActiveRecord::batchInsert() */ - public function batchSave($attributes = null){ + public function batchSave($attributes = null, $scope = ''){ if($this->getIsNewRecord()) - return $this->batchInsert($attributes); - return $this->batchUpdate($attributes); + return $this->batchInsert($attributes,$scope); + return $this->batchUpdate($attributes,$scope); } /** * checking if current ActiveRecord class has documents in queue for insert * @return bool */ - public static function hasBatchInsert(){ + public static function hasBatchInsert($scope = ''){ $className = static::className(); - return array_key_exists($className,self::$batchInsertQueue) && self::$batchInsertQueue[$className] > 0; + return @self::$batchInsertCommand[$className][$scope] ? true : false; } /** * this method is invoked in first call of batchInsert() method for once */ - private static function batchInsertInit(){ + private static function batchInsertInit($scope = ''){ $className = static::className(); - if(array_key_exists($className,self::$batchInsertCommand)) + if(@self::$batchInsertCommand[$className][$scope]) return; - self::$batchInsertQueue[$className] = 0; - self::$batchInsertCommand[$className] = static::getDb()->createCommand(); - if(self::$batchInsertCommand[$className]->db->enableLogging) - register_shutdown_function(function(){ - if(self::hasBatchInsert()) + if(!@self::$batchInsertQueue[$className]) + self::$batchInsertQueue[$className] = []; + + self::$batchInsertQueue[$className][$scope] = 0; + self::$batchInsertCommand[$className][$scope] = static::getDb()->createCommand(); + if(self::$batchInsertCommand[$className][$scope]->db->enableLogging) + register_shutdown_function(function()use($scope){ + if(self::hasBatchInsert($scope)) yii::warning(static::className().' : batch insert mode not completed!'); }); } @@ -505,8 +508,8 @@ private static function batchInsertInit(){ * meaning all attributes that are loaded will be inserted. * @return null|array return null if no execute command or return result of Command::executeBatch() */ - public function batchInsert($attributes = null){ - self::batchInsertInit(); + public function batchInsert($attributes = null, $scope = ''){ + self::batchInsertInit($scope); $values = $this->getDirtyAttributes($attributes); if (empty($values)) { $currentAttributes = $this->getAttributes(); @@ -517,21 +520,21 @@ public function batchInsert($attributes = null){ } } $className = static::className(); - self::$batchInsertCommand[$className]->AddInsert($values); - self::$batchInsertQueue[$className]++; - if(self::$batchInsertQueue[$className] >= static::$batchInsertSize) - return self::flushBatchInsert(); + self::$batchInsertCommand[$className][$scope]->AddInsert($values); + self::$batchInsertQueue[$className][$scope]++; + if(self::$batchInsertQueue[$className][$scope] >= static::$batchInsertSize) + return self::flushBatchInsert($scope); } /** * resetting batch insert */ - public static function resetBatchInsert(){ + public static function resetBatchInsert($scope = ''){ $className = static::className(); - if(!array_key_exists($className,self::$batchInsertCommand)) + if(!@self::$batchInsertCommand[$className][$scope]) return; - self::$batchInsertQueue[$className] = 0; - self::$batchInsertCommand[$className]->document = []; + self::$batchInsertQueue[$className][$scope] = 0; + self::$batchInsertCommand[$className][$scope]->document = []; } /** @@ -539,12 +542,12 @@ public static function resetBatchInsert(){ * this method is not continue when not exists any insert operations in queue * @return see docs of Command::executeBatch() */ - public static function flushBatchInsert(){ + public static function flushBatchInsert($scope = ''){ $className = static::className(); - if(!array_key_exists($className,self::$batchInsertQueue) || self::$batchInsertQueue[$className] === 0) + if(!@self::$batchInsertQueue[$className][$scope]) return; - $result = self::$batchInsertCommand[$className]->executeBatch(static::collectionName()); - self::resetBatchInsert(); + $result = self::$batchInsertCommand[$className][$scope]->executeBatch(static::collectionName()); + self::resetBatchInsert($scope); return $result; } @@ -552,25 +555,28 @@ public static function flushBatchInsert(){ * checking if current ActiveRecord class has documents in queue for update * @return bool */ - public static function hasBatchUpdate(){ + public static function hasBatchUpdate($scope = ''){ $className = static::className(); - return array_key_exists($className,self::$batchUpdateQueue) && self::$batchUpdateQueue[$className] > 0; + return @self::$batchUpdateQueue[$className][$scope] ? true : false; } /** * this method is invoked in first call of batchUpdate() method for once */ - private static function batchUpdateInit(){ + private static function batchUpdateInit($scope = ''){ $className = static::className(); - if(array_key_exists($className,self::$batchUpdateCommand)) + if(@self::$batchUpdateCommand[$className][$scope]) return; - self::$batchUpdateQueue[$className] = 0; - self::$batchUpdateCommand[$className] = static::getDb()->createCommand(); - if(self::$batchUpdateCommand[$className]->db->enableLogging) - register_shutdown_function(function(){ - if(self::hasBatchUpdate()) + if(!@self::$batchUpdateQueue[$className]) + self::$batchUpdateQueue[$className] = []; + + self::$batchUpdateQueue[$className][$scope] = 0; + self::$batchUpdateCommand[$className][$scope] = static::getDb()->createCommand(); + if(self::$batchUpdateCommand[$className][$scope]->db->enableLogging) + register_shutdown_function(function()use($scope){ + if(self::hasBatchUpdate($scope)) yii::warning(static::className().' : batch update mode not completed!'); }); } @@ -581,17 +587,17 @@ private static function batchUpdateInit(){ * meaning all attributes that are loaded from DB will be updated. * @return null|array return null if no execute command or return result of Command::executeBatch() */ - public function batchUpdate($attributes = null){ - self::batchUpdateInit(); + public function batchUpdate($attributes = null, $scope = ''){ + self::batchUpdateInit($scope); $values = $this->getDirtyAttributes($attributes); if (empty($values)) return; $condition = $this->getOldPrimaryKey(true); $className = static::className(); - self::$batchUpdateCommand[$className]->addUpdate($condition, $values); - self::$batchUpdateQueue[$className]++; - if(self::$batchUpdateQueue[$className] >= static::$batchUpdateSize) - return self::flushBatchUpdate(); + self::$batchUpdateCommand[$className][$scope]->addUpdate($condition, $values); + self::$batchUpdateQueue[$className][$scope]++; + if(self::$batchUpdateQueue[$className][$scope] >= static::$batchUpdateSize) + return self::flushBatchUpdate($scope); } /** @@ -603,24 +609,24 @@ public function batchUpdate($attributes = null){ * Please refer to Command::addUpdate() on how to specify this parameter. * @return null|array return null if no execute command or return result of Command::executeBatch() */ - public static function batchUpdateAll($attributes, $condition = [], $options = []){ - self::batchUpdateInit(); + public static function batchUpdateAll($attributes, $condition = [], $options = [], $scope = ''){ + self::batchUpdateInit($scope); $className = static::className(); - self::$batchUpdateCommand[$className]->addUpdate($condition, $attributes, $options); - self::$batchUpdateQueue[$className]++; - if(self::$batchUpdateQueue[$className] >= static::$batchUpdateSize) + self::$batchUpdateCommand[$className][$scope]->addUpdate($condition, $attributes, $options); + self::$batchUpdateQueue[$className][$scope]++; + if(self::$batchUpdateQueue[$className][$scope] >= static::$batchUpdateSize) return self::flushBatchUpdate(); } /** * resetting batch update */ - public static function resetBatchUpdate(){ + public static function resetBatchUpdate($scope = ''){ $className = static::className(); - if(!array_key_exists($className,self::$batchUpdateCommand)) + if(!@self::$batchUpdateCommand[$className][$scope]) return; - self::$batchUpdateQueue[$className] = 0; - self::$batchUpdateCommand[$className]->document = []; + self::$batchUpdateQueue[$className][$scope] = 0; + self::$batchUpdateCommand[$className][$scope]->document = []; } /** @@ -628,11 +634,11 @@ public static function resetBatchUpdate(){ * this method is not continue when not exists any update operations in queue * @return see docs of Command::executeBatch() */ - public static function flushBatchUpdate(){ + public static function flushBatchUpdate($scope = ''){ $className = static::className(); - if(!array_key_exists($className,self::$batchUpdateQueue) || self::$batchUpdateQueue[$className] === 0) + if(!@self::$batchUpdateQueue[$className][$scope]) return; - $result = self::$batchUpdateCommand[$className]->executeBatch(static::collectionName()); + $result = self::$batchUpdateCommand[$className][$scope]->executeBatch(static::collectionName()); self::resetBatchUpdate(); return $result; } @@ -641,25 +647,28 @@ public static function flushBatchUpdate(){ * checking if current ActiveRecord class has documents in queue for delete * @return bool */ - public static function hasBatchDelete(){ + public static function hasBatchDelete($scope = ''){ $className = static::className(); - return array_key_exists($className,self::$batchDeleteQueue) && self::$batchDeleteQueue[$className] > 0; + return @self::$batchDeleteQueue[$className][$scope] ? true : false; } /** * this method is invoked in first call of batchDelete() method for once */ - private static function batchDeleteInit(){ + private static function batchDeleteInit($scope = ''){ $className = static::className(); - if(array_key_exists($className,self::$batchDeleteCommand)) + if(@self::$batchDeleteCommand[$className][$scope]) return; - self::$batchDeleteQueue[$className] = 0; - self::$batchDeleteCommand[$className] = static::getDb()->createCommand(); - if(self::$batchDeleteCommand[$className]->db->enableLogging) - register_shutdown_function(function(){ - if(self::hasBatchDelete()) + if(!@self::$batchDeleteQueue[$className]) + self::$batchDeleteQueue[$className] = []; + + self::$batchDeleteQueue[$className][$scope] = 0; + self::$batchDeleteCommand[$className][$scope] = static::getDb()->createCommand(); + if(self::$batchDeleteCommand[$className][$scope]->db->enableLogging) + register_shutdown_function(function()use($scope){ + if(self::hasBatchDelete($scope)) yii::warning(static::className().' : batch delete mode not completed!'); }); } @@ -668,13 +677,13 @@ private static function batchDeleteInit(){ * adding delete operation to queue base on current instance data * @return null|array return null if no execute command or return result of Command::executeBatch() */ - public function batchDelete(){ - self::batchDeleteInit(); + public function batchDelete($scope = ''){ + self::batchDeleteInit($scope); $className = static::className(); - self::$batchDeleteCommand[$className]->AddDelete($this->getOldPrimaryKey(true)); - self::$batchDeleteQueue[$className]++; - if(self::$batchDeleteQueue[$className] >= static::$batchDeleteSize) - return self::flushBatchDelete(); + self::$batchDeleteCommand[$className][$scope]->AddDelete($this->getOldPrimaryKey(true)); + self::$batchDeleteQueue[$className][$scope]++; + if(self::$batchDeleteQueue[$className][$scope] >= static::$batchDeleteSize) + return self::flushBatchDelete($scope); } /** @@ -685,24 +694,24 @@ public function batchDelete(){ * Please refer to Command::AddDelete() on how to specify this parameter. * @return null|array return null if no execute command or return result of Command::executeBatch() */ - public static function batchDeleteAll($condition = [], $options = []){ - self::batchDeleteInit(); + public static function batchDeleteAll($condition = [], $options = [], $scope = ''){ + self::batchDeleteInit($scope); $className = static::className(); - self::$batchDeleteCommand[$className]->AddDelete($condition, $options); - self::$batchDeleteQueue[$className]++; - if(self::$batchDeleteQueue[$className] >= static::$batchDeleteSize) - return self::flushBatchDelete(); + self::$batchDeleteCommand[$className][$scope]->AddDelete($condition, $options); + self::$batchDeleteQueue[$className][$scope]++; + if(self::$batchDeleteQueue[$className][$scope] >= static::$batchDeleteSize) + return self::flushBatchDelete($scope); } /** * resetting batch delete */ - public static function resetBatchDelete(){ + public static function resetBatchDelete($scope = ''){ $className = static::className(); - if(!array_key_exists($className,self::$batchDeleteCommand)) + if(!@self::$batchDeleteCommand[$className][$scope]) return; - self::$batchDeleteQueue[$className] = 0; - self::$batchDeleteCommand[$className]->document = []; + self::$batchDeleteQueue[$className][$scope] = 0; + self::$batchDeleteCommand[$className][$scope]->document = []; } /** @@ -710,12 +719,12 @@ public static function resetBatchDelete(){ * this method is not continue when not exists any delete operations in queue * @return see docs of Command::executeBatch() */ - public static function flushBatchDelete(){ + public static function flushBatchDelete($scope = ''){ $className = static::className(); - if(!array_key_exists($className,self::$batchDeleteQueue) || self::$batchDeleteQueue[$className] === 0) + if(!@self::$batchDeleteQueue[$className][$scope]) return; - $result = self::$batchDeleteCommand[$className]->executeBatch(static::collectionName()); - self::resetBatchDelete(); + $result = self::$batchDeleteCommand[$className][$scope]->executeBatch(static::collectionName()); + self::resetBatchDelete($scope); return $result; } } \ No newline at end of file From c15495227aed777dd0df72764095ffdb337c8219 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Wed, 20 Dec 2023 19:10:24 +0330 Subject: [PATCH 18/21] dev --- src/ActiveRecord.php | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 48da803ff..902941581 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -34,10 +34,6 @@ abstract class ActiveRecord extends BaseActiveRecord * @var integer count of insert operation in queue */ private static $batchInsertQueue = []; - /* - * @var array array of document for insert - */ - private static $batchInsertDocuments = []; /* * @var int size of batch for insert operations */ @@ -51,10 +47,6 @@ abstract class ActiveRecord extends BaseActiveRecord * @var integer count of update operation in queue */ private static $batchUpdateQueue = []; - /* - * @var array array of document for update - */ - private static $batchUpdateDocuments = []; /* * @var int size of batch for update operations */ @@ -533,8 +525,10 @@ public static function resetBatchInsert($scope = ''){ $className = static::className(); if(!@self::$batchInsertCommand[$className][$scope]) return; - self::$batchInsertQueue[$className][$scope] = 0; - self::$batchInsertCommand[$className][$scope]->document = []; + unset( + self::$batchInsertQueue[$className][$scope], + self::$batchInsertCommand[$className][$scope] + ); } /** @@ -625,8 +619,10 @@ public static function resetBatchUpdate($scope = ''){ $className = static::className(); if(!@self::$batchUpdateCommand[$className][$scope]) return; - self::$batchUpdateQueue[$className][$scope] = 0; - self::$batchUpdateCommand[$className][$scope]->document = []; + unset( + self::$batchUpdateQueue[$className][$scope], + self::$batchUpdateCommand[$className][$scope] + ); } /** @@ -710,8 +706,10 @@ public static function resetBatchDelete($scope = ''){ $className = static::className(); if(!@self::$batchDeleteCommand[$className][$scope]) return; - self::$batchDeleteQueue[$className][$scope] = 0; - self::$batchDeleteCommand[$className][$scope]->document = []; + unset( + self::$batchDeleteQueue[$className][$scope], + self::$batchDeleteCommand[$className][$scope] + ); } /** From 595514a7940c039d2f03aa7d10d04404fd161383 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Thu, 21 Dec 2023 08:55:51 +0330 Subject: [PATCH 19/21] fix bug --- src/ActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 902941581..23ad270e9 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -470,7 +470,7 @@ public function batchSave($attributes = null, $scope = ''){ */ public static function hasBatchInsert($scope = ''){ $className = static::className(); - return @self::$batchInsertCommand[$className][$scope] ? true : false; + return @self::$batchInsertQueue[$className][$scope] ? true : false; } /** From 9ef5ba09c1b9b0a5e9c6a6f8d2a8138eb771ac99 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Thu, 21 Dec 2023 11:41:33 +0330 Subject: [PATCH 20/21] fix --- src/ActiveRecord.php | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 23ad270e9..2d24e6ccb 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -38,6 +38,10 @@ abstract class ActiveRecord extends BaseActiveRecord * @var int size of batch for insert operations */ public static $batchInsertSize = 500; + /* + * @var bool register_shutdown_function is set? + */ + public static $batchInsertShutdownFunction = false; /* * @var Command instance of Command class for batch update. @@ -51,6 +55,10 @@ abstract class ActiveRecord extends BaseActiveRecord * @var int size of batch for update operations */ public static $batchUpdateSize = 500; + /* + * @var bool register_shutdown_function is set? + */ + public static $batchUpdateShutdownFunction = false; /* * @var Command instance of Command class for batch delete. @@ -64,6 +72,10 @@ abstract class ActiveRecord extends BaseActiveRecord * @var int size of batch for delete operations */ public static $batchDeleteSize = 500; + /* + * @var bool register_shutdown_function is set? + */ + public static $batchDeleteShutdownFunction = false; /** * Returns the Mongo connection used by this AR class. @@ -487,11 +499,17 @@ private static function batchInsertInit($scope = ''){ self::$batchInsertQueue[$className][$scope] = 0; self::$batchInsertCommand[$className][$scope] = static::getDb()->createCommand(); - if(self::$batchInsertCommand[$className][$scope]->db->enableLogging) + if( + !self::$batchInsertShutdownFunction && + self::$batchInsertCommand[$className][$scope]->db->enableLogging + ) + { + self::$batchInsertShutdownFunction = true; register_shutdown_function(function()use($scope){ if(self::hasBatchInsert($scope)) yii::warning(static::className().' : batch insert mode not completed!'); }); + } } /** @@ -568,11 +586,17 @@ private static function batchUpdateInit($scope = ''){ self::$batchUpdateQueue[$className][$scope] = 0; self::$batchUpdateCommand[$className][$scope] = static::getDb()->createCommand(); - if(self::$batchUpdateCommand[$className][$scope]->db->enableLogging) + if( + !self::$batchUpdateShutdownFunction && + self::$batchUpdateCommand[$className][$scope]->db->enableLogging + ) + { + self::$batchUpdateShutdownFunction = true; register_shutdown_function(function()use($scope){ if(self::hasBatchUpdate($scope)) yii::warning(static::className().' : batch update mode not completed!'); }); + } } /** @@ -662,11 +686,17 @@ private static function batchDeleteInit($scope = ''){ self::$batchDeleteQueue[$className][$scope] = 0; self::$batchDeleteCommand[$className][$scope] = static::getDb()->createCommand(); - if(self::$batchDeleteCommand[$className][$scope]->db->enableLogging) + if( + !self::$batchDeleteShutdownFunction && + self::$batchDeleteCommand[$className][$scope]->db->enableLogging + ) + { + self::$batchDeleteShutdownFunction = true; register_shutdown_function(function()use($scope){ if(self::hasBatchDelete($scope)) yii::warning(static::className().' : batch delete mode not completed!'); }); + } } /** From 1ac9ce522a479e33ea7bd20596942e108584a917 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Thu, 21 Dec 2023 12:28:45 +0330 Subject: [PATCH 21/21] dev --- src/ActiveRecord.php | 86 +++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 45 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 2d24e6ccb..8bee14b44 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -25,6 +25,10 @@ */ abstract class ActiveRecord extends BaseActiveRecord { + /* + * @var bool register_shutdown_function is set? + */ + public static $batchShutdownFunction = false; /* * @var Command instance of Command class for batch insert. @@ -38,10 +42,6 @@ abstract class ActiveRecord extends BaseActiveRecord * @var int size of batch for insert operations */ public static $batchInsertSize = 500; - /* - * @var bool register_shutdown_function is set? - */ - public static $batchInsertShutdownFunction = false; /* * @var Command instance of Command class for batch update. @@ -55,10 +55,6 @@ abstract class ActiveRecord extends BaseActiveRecord * @var int size of batch for update operations */ public static $batchUpdateSize = 500; - /* - * @var bool register_shutdown_function is set? - */ - public static $batchUpdateShutdownFunction = false; /* * @var Command instance of Command class for batch delete. @@ -72,10 +68,6 @@ abstract class ActiveRecord extends BaseActiveRecord * @var int size of batch for delete operations */ public static $batchDeleteSize = 500; - /* - * @var bool register_shutdown_function is set? - */ - public static $batchDeleteShutdownFunction = false; /** * Returns the Mongo connection used by this AR class. @@ -476,6 +468,34 @@ public function batchSave($attributes = null, $scope = ''){ return $this->batchUpdate($attributes,$scope); } + private static function batchInit(){ + + if(self::$batchShutdownFunction) + return; + + self::$batchShutdownFunction = true; + + register_shutdown_function(function(){ + + $className = static::className(); + + foreach(self::$batchInsertQueue as $scope => $_) + if(self::hasBatchInsert($scope)) + if(self::$batchInsertCommand[$className][$scope]->db->enableLogging) + yii::warning($className.' : batch insert mode not completed!'); + + foreach(self::$batchUpdateQueue as $scope => $_) + if(self::hasBatchUpdate($scope)) + if(self::$batchUpdateCommand[$className][$scope]->db->enableLogging) + yii::warning($className.' : batch update mode not completed!'); + + foreach(self::$batchDeleteQueue as $scope => $_) + if(self::hasBatchDelete($scope)) + if(self::$batchDeleteCommand[$className][$scope]->db->enableLogging) + yii::warning($className.' : batch delete mode not completed!'); + }); + } + /** * checking if current ActiveRecord class has documents in queue for insert * @return bool @@ -489,6 +509,9 @@ public static function hasBatchInsert($scope = ''){ * this method is invoked in first call of batchInsert() method for once */ private static function batchInsertInit($scope = ''){ + + self::batchInit(); + $className = static::className(); if(@self::$batchInsertCommand[$className][$scope]) @@ -499,17 +522,6 @@ private static function batchInsertInit($scope = ''){ self::$batchInsertQueue[$className][$scope] = 0; self::$batchInsertCommand[$className][$scope] = static::getDb()->createCommand(); - if( - !self::$batchInsertShutdownFunction && - self::$batchInsertCommand[$className][$scope]->db->enableLogging - ) - { - self::$batchInsertShutdownFunction = true; - register_shutdown_function(function()use($scope){ - if(self::hasBatchInsert($scope)) - yii::warning(static::className().' : batch insert mode not completed!'); - }); - } } /** @@ -576,6 +588,9 @@ public static function hasBatchUpdate($scope = ''){ * this method is invoked in first call of batchUpdate() method for once */ private static function batchUpdateInit($scope = ''){ + + self::batchInit(); + $className = static::className(); if(@self::$batchUpdateCommand[$className][$scope]) @@ -586,17 +601,6 @@ private static function batchUpdateInit($scope = ''){ self::$batchUpdateQueue[$className][$scope] = 0; self::$batchUpdateCommand[$className][$scope] = static::getDb()->createCommand(); - if( - !self::$batchUpdateShutdownFunction && - self::$batchUpdateCommand[$className][$scope]->db->enableLogging - ) - { - self::$batchUpdateShutdownFunction = true; - register_shutdown_function(function()use($scope){ - if(self::hasBatchUpdate($scope)) - yii::warning(static::className().' : batch update mode not completed!'); - }); - } } /** @@ -676,6 +680,9 @@ public static function hasBatchDelete($scope = ''){ * this method is invoked in first call of batchDelete() method for once */ private static function batchDeleteInit($scope = ''){ + + self::batchInit(); + $className = static::className(); if(@self::$batchDeleteCommand[$className][$scope]) @@ -686,17 +693,6 @@ private static function batchDeleteInit($scope = ''){ self::$batchDeleteQueue[$className][$scope] = 0; self::$batchDeleteCommand[$className][$scope] = static::getDb()->createCommand(); - if( - !self::$batchDeleteShutdownFunction && - self::$batchDeleteCommand[$className][$scope]->db->enableLogging - ) - { - self::$batchDeleteShutdownFunction = true; - register_shutdown_function(function()use($scope){ - if(self::hasBatchDelete($scope)) - yii::warning(static::className().' : batch delete mode not completed!'); - }); - } } /**