Skip to content

Commit

Permalink
Refactor View::update
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Farizon committed Jul 15, 2019
1 parent d91a3c9 commit ba4dff7
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions lib/internal/Magento/Framework/Mview/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Framework\Mview;

use InvalidArgumentException;
Expand Down Expand Up @@ -254,23 +256,7 @@ public function update()
try {
$this->getState()->setStatus(View\StateInterface::STATUS_WORKING)->save();

$versionBatchSize = self::$maxVersionQueryBatch;
$batchSize = isset($this->changelogBatchSize[$this->getChangelog()->getViewId()])
? $this->changelogBatchSize[$this->getChangelog()->getViewId()]
: self::DEFAULT_BATCH_SIZE;

for ($vsFrom = $lastVersionId; $vsFrom < $currentVersionId; $vsFrom += $versionBatchSize) {
// Don't go past the current version for atomicy.
$versionTo = min($currentVersionId, $vsFrom + $versionBatchSize);
$ids = $this->getChangelog()->getList($vsFrom, $versionTo);

// We run the actual indexer in batches.
// Chunked AFTER loading to avoid duplicates in separate chunks.
$chunks = array_chunk($ids, $batchSize);
foreach ($chunks as $ids) {
$action->execute($ids);
}
}
$this->executeAction($action, $lastVersionId, $currentVersionId);

$this->getState()->loadByView($this->getId());
$statusToRestore = $this->getState()->getStatus() === View\StateInterface::STATUS_SUSPENDED
Expand All @@ -294,6 +280,36 @@ public function update()
}
}

/**
* Execute action from last version to current version, by batches
*
* @param ActionInterface $action
* @param int $lastVersionId
* @param int $currentVersionId
* @return void
* @throws \Exception
*/
private function executeAction(ActionInterface $action, int $lastVersionId, int $currentVersionId)
{
$versionBatchSize = self::$maxVersionQueryBatch;
$batchSize = isset($this->changelogBatchSize[$this->getChangelog()->getViewId()])
? $this->changelogBatchSize[$this->getChangelog()->getViewId()]
: self::DEFAULT_BATCH_SIZE;

for ($vsFrom = $lastVersionId; $vsFrom < $currentVersionId; $vsFrom += $versionBatchSize) {
// Don't go past the current version for atomicity.
$versionTo = min($currentVersionId, $vsFrom + $versionBatchSize);
$ids = $this->getChangelog()->getList($vsFrom, $versionTo);

// We run the actual indexer in batches.
// Chunked AFTER loading to avoid duplicates in separate chunks.
$chunks = array_chunk($ids, $batchSize);
foreach ($chunks as $ids) {
$action->execute($ids);
}
}
}

/**
* Suspend view updates and set version ID to changelog's end
*
Expand Down

0 comments on commit ba4dff7

Please sign in to comment.