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 7ffb199 commit e05e46d
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions lib/internal/Magento/Framework/Mview/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,26 +274,11 @@ public function update()
$lastVersionId = (int) $this->getState()->getVersionId();
$action = $this->actionFactory->get($this->getActionClass());

$this->executeAction($action, $lastVersionId, $currentVersionId);

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 ($versionFrom = $lastVersionId; $versionFrom < $currentVersionId; $versionFrom += $versionBatchSize) {
// Don't go past the current version for atomicy.
$versionTo = min($currentVersionId, $versionFrom + $versionBatchSize);
$ids = array_map('intval', $this->getChangelog()->getList($versionFrom, $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->getState()->loadByView($this->getId());
$statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED
? View\StateInterface::STATUS_SUSPENDED
Expand All @@ -317,6 +302,35 @@ 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 ($versionFrom = $lastVersionId; $versionFrom < $currentVersionId; $versionFrom += $versionBatchSize) {
// Don't go past the current version for atomicy.
$versionTo = min($currentVersionId, $versionFrom + $versionBatchSize);
$ids = array_map('intval', $this->getChangelog()->getList($versionFrom, $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 e05e46d

Please sign in to comment.