-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Created memory efficient iterator for importexport_importdata table #10918
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,11 @@ class Data extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implemen | |
*/ | ||
protected $jsonHelper; | ||
|
||
/** | ||
* @var Data\IteratorFactory | ||
*/ | ||
protected $iteratorFactory; | ||
|
||
/** | ||
* Class constructor | ||
* | ||
|
@@ -35,10 +40,13 @@ class Data extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implemen | |
public function __construct( | ||
\Magento\Framework\Model\ResourceModel\Db\Context $context, | ||
\Magento\Framework\Json\Helper\Data $jsonHelper, | ||
\Magento\ImportExport\Model\ResourceModel\Import\Data\IteratorFactory $iteratorFactory, | ||
$connectionName = null | ||
) { | ||
parent::__construct($context, $connectionName); | ||
|
||
$this->jsonHelper = $jsonHelper; | ||
$this->iteratorFactory = $iteratorFactory; | ||
} | ||
|
||
/** | ||
|
@@ -58,20 +66,7 @@ protected function _construct() | |
*/ | ||
public function getIterator() | ||
{ | ||
$connection = $this->getConnection(); | ||
$select = $connection->select()->from($this->getMainTable(), ['data'])->order('id ASC'); | ||
$stmt = $connection->query($select); | ||
|
||
$stmt->setFetchMode(\Zend_Db::FETCH_NUM); | ||
if ($stmt instanceof \IteratorAggregate) { | ||
$iterator = $stmt->getIterator(); | ||
} else { | ||
// Statement doesn't support iterating, so fetch all records and create iterator ourself | ||
$rows = $stmt->fetchAll(); | ||
$iterator = new \ArrayIterator($rows); | ||
} | ||
|
||
return $iterator; | ||
return $this->iteratorFactory->create(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Iterator factory should produce |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\ImportExport\Model\ResourceModel\Import\Data; | ||
|
||
/** | ||
* DataProvider for import data | ||
*/ | ||
class DataProvider extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please name classes properly, not a word-per-folder. |
||
{ | ||
/** | ||
* Ids of rows in table | ||
* @var array | ||
*/ | ||
protected $rowsIds = []; | ||
|
||
/** | ||
* DataProvider constructor. | ||
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context | ||
* @param string $connectionName | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\Model\ResourceModel\Db\Context $context, | ||
$connectionName = null | ||
) { | ||
parent::__construct($context, $connectionName); | ||
} | ||
|
||
/** | ||
* Resource initialization | ||
*/ | ||
protected function _construct() | ||
{ | ||
$this->_init('importexport_importdata', 'id'); | ||
} | ||
|
||
/** | ||
* Get all existing rows ids to use them later for efficient data retrieval | ||
* @return array | ||
*/ | ||
protected function getRowsIds() | ||
{ | ||
if(empty($this->rowsIds)) { | ||
$connection = $this->getConnection(); | ||
|
||
$select = $connection | ||
->select() | ||
->from($this->getMainTable(), ['id']) | ||
->order('id ASC'); | ||
|
||
$this->rowsIds = $connection->fetchCol($select); | ||
} | ||
|
||
return $this->rowsIds; | ||
} | ||
|
||
/** | ||
* Returns import data row by $index | ||
* | ||
* @param $index int | ||
* @return array | ||
*/ | ||
public function getImportDataRow($index) | ||
{ | ||
$rowsIds = $this->getRowsIds(); | ||
|
||
if(!isset($rowsIds[$index])) { | ||
return null; | ||
} | ||
|
||
$connection = $this->getConnection(); | ||
|
||
$select = $connection | ||
->select() | ||
->from($this->getMainTable(), ['data']) | ||
->order('id ASC') | ||
->where('id = ?', $rowsIds[$index]); | ||
|
||
$statement = $connection->query($select); | ||
|
||
$row = $statement->fetch(); | ||
|
||
return [$row['data']]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\ImportExport\Model\ResourceModel\Import\Data; | ||
|
||
/** | ||
* Import data iterator | ||
*/ | ||
class Iterator implements \Iterator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please name classes properly, not a word-per-folder. |
||
{ | ||
/** | ||
* Current iterator index | ||
* @var int | ||
*/ | ||
protected $currentIndex = 0; | ||
|
||
/** | ||
* @var DataProvider | ||
*/ | ||
protected $importDataProvider = null; | ||
|
||
/** | ||
* @var DataProviderFactory | ||
*/ | ||
protected $importDataProviderFactory; | ||
|
||
/** | ||
* Iterator constructor. | ||
* @param DataProvider $importDataProvider | ||
*/ | ||
public function __construct(DataProviderFactory $importDataProviderFactory) { | ||
$this->importDataProviderFactory = $importDataProviderFactory; | ||
} | ||
|
||
/** | ||
* Returns current row | ||
* | ||
* @return array | ||
*/ | ||
public function current() | ||
{ | ||
if($this->importDataProvider == null) { | ||
$this->importDataProvider = $this->importDataProviderFactory->create(); | ||
} | ||
|
||
return $this->importDataProvider->getImportDataRow($this->currentIndex); | ||
} | ||
|
||
/** | ||
* Moves iterator to next row | ||
*/ | ||
public function next() | ||
{ | ||
$this->currentIndex++; | ||
} | ||
|
||
/** | ||
* Gets current row key | ||
* @return int | ||
*/ | ||
public function key() | ||
{ | ||
return $this->currentIndex; | ||
} | ||
|
||
/** | ||
* Returns if current row is valid | ||
* @return int | ||
*/ | ||
public function valid() | ||
{ | ||
return $this->current() !== null; | ||
} | ||
|
||
/** | ||
* Rewinds iterator to first row | ||
*/ | ||
public function rewind() | ||
{ | ||
$this->currentIndex = 0; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See http://devdocs.magento.com/guides/v2.0/contributor-guide/backward-compatible-development/#adding-a-constructor-parameter for a correct way to add new dependency.