Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ziaratban committed Dec 21, 2023
1 parent 9ef5ba0 commit 1ac9ce5
Showing 1 changed file with 41 additions and 45 deletions.
86 changes: 41 additions & 45 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -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!');
});
}
}

/**
Expand Down Expand Up @@ -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])
Expand All @@ -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!');
});
}
}

/**
Expand Down Expand Up @@ -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])
Expand All @@ -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!');
});
}
}

/**
Expand Down

0 comments on commit 1ac9ce5

Please sign in to comment.