From c80710a023a1ff6ca3c13f2cc3ba2a867e482638 Mon Sep 17 00:00:00 2001
From: Luke Rodgers <lukerodgers90@gmail.com>
Date: Sat, 11 Nov 2017 13:03:32 +0000
Subject: [PATCH] Add mview getListSize command

---
 .../Command/IndexerStatusMviewCommand.php     |  2 +-
 .../Command/IndexerStatusMviewCommandTest.php |  9 ++---
 .../Framework/Mview/View/Changelog.php        | 36 ++++++++++++++++---
 .../Mview/View/ChangelogInterface.php         |  9 +++++
 4 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php
index 0efeef4a71b..37caabc613e 100644
--- a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php
+++ b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php
@@ -61,7 +61,7 @@ class IndexerStatusMviewCommand extends Command
                     continue;
                 }
 
-                $pendingCount = count($changelog->getList($state->getVersionId(), $currentVersionId));
+                $pendingCount = $changelog->getListSize($state->getVersionId(), $currentVersionId);
 
                 $pendingString = "<error>$pendingCount</error>";
                 if ($pendingCount <= 0) {
diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php
index 292adb55c55..4ae3ca83870 100644
--- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php
+++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php
@@ -176,14 +176,11 @@ class IndexerStatusMviewCommandTest extends \PHPUnit\Framework\TestCase
             ->disableOriginalConstructor()
             ->getMock();
 
-        $list = [];
-        if ($changelogData['version_id'] !== $stateData['version_id']) {
-            $list = range($stateData['version_id']+1, $changelogData['version_id']);
-        }
+        $listSize = $changelogData['version_id'] - $stateData['version_id'];
 
         $changelog->expects($this->any())
-            ->method('getList')
-            ->willReturn($list);
+            ->method('getListSize')
+            ->willReturn($listSize);
 
         $changelog->expects($this->any())
             ->method('getVersion')
diff --git a/lib/internal/Magento/Framework/Mview/View/Changelog.php b/lib/internal/Magento/Framework/Mview/View/Changelog.php
index 91caf662283..4f648d6b7d6 100644
--- a/lib/internal/Magento/Framework/Mview/View/Changelog.php
+++ b/lib/internal/Magento/Framework/Mview/View/Changelog.php
@@ -127,14 +127,12 @@ class Changelog implements ChangelogInterface
     }
 
     /**
-     * Retrieve entity ids by range [$fromVersionId..$toVersionId]
-     *
      * @param int $fromVersionId
      * @param int $toVersionId
-     * @return int[]
+     * @return \Magento\Framework\DB\Select
      * @throws ChangelogTableNotExistsException
      */
-    public function getList($fromVersionId, $toVersionId)
+    protected function getListSelect($fromVersionId, $toVersionId)
     {
         $changelogTableName = $this->resource->getTableName($this->getName());
         if (!$this->connection->isTableExists($changelogTableName)) {
@@ -154,9 +152,39 @@ class Changelog implements ChangelogInterface
             (int)$toVersionId
         );
 
+        return $select;
+    }
+
+    /**
+     * Retrieve entity ids by range [$fromVersionId..$toVersionId]
+     *
+     * @param int $fromVersionId
+     * @param int $toVersionId
+     * @return int[]
+     * @throws ChangelogTableNotExistsException
+     */
+    public function getList($fromVersionId, $toVersionId)
+    {
+        $select = $this->getListSelect($fromVersionId, $toVersionId);
         return $this->connection->fetchCol($select);
     }
 
+    /**
+     * Retrieve the count of entity ids in the range [$fromVersionId..$toVersionId]
+     *
+     * @param int $fromVersionId
+     * @param int $toVersionId
+     * @return int[]
+     * @throws ChangelogTableNotExistsException
+     */
+    public function getListSize($fromVersionId, $toVersionId)
+    {
+        $countSelect = $this->getListSelect($fromVersionId, $toVersionId);
+        $countSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
+        $countSelect->columns(new \Zend_Db_Expr(("COUNT(DISTINCT " . $this->getColumnName() . ")")));
+        return $this->connection->fetchOne($countSelect);
+    }
+
     /**
      * Get maximum version_id from changelog
      * @return int
diff --git a/lib/internal/Magento/Framework/Mview/View/ChangelogInterface.php b/lib/internal/Magento/Framework/Mview/View/ChangelogInterface.php
index b00c1ca3a2e..da115ecdb83 100644
--- a/lib/internal/Magento/Framework/Mview/View/ChangelogInterface.php
+++ b/lib/internal/Magento/Framework/Mview/View/ChangelogInterface.php
@@ -42,6 +42,15 @@ interface ChangelogInterface
      */
     public function getList($fromVersionId, $toVersionId);
 
+    /**
+     * Retrieve the count of entity ids in the range [$fromVersionId..$toVersionId]
+     *
+     * @param $fromVersionId
+     * @param $toVersionId
+     * @return mixed
+     */
+    public function getListSize($fromVersionId, $toVersionId);
+
     /**
      * Get maximum version_id from changelog
      *
-- 
GitLab