Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ class Data extends \Magento\ImportExport\Model\ResourceModel\Import\Data
*
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param \Magento\Framework\Json\Helper\Data $coreHelper
* @param \Magento\ImportExport\Model\ResourceModel\Import\Data\IteratorFactory $iteratorFactory,
* @param string $connectionName
* @param array $arguments
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Magento\Framework\Json\Helper\Data $jsonHelper,
\Magento\ImportExport\Model\ResourceModel\Import\Data\IteratorFactory $iteratorFactory,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$connectionName = null,
array $arguments = []
) {
parent::__construct($context, $jsonHelper, $connectionName);
parent::__construct($context, $jsonHelper, $iteratorFactory, $connectionName);

if (isset($arguments['entity_type'])) {
$this->_entityType = $arguments['entity_type'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,20 @@ class DataTest extends \PHPUnit\Framework\TestCase
*/
protected function _getDependencies($entityType, $bunchData)
{
/** @var $statementMock \Magento\Framework\DB\Statement\Pdo\Mysql */
$statementMock = $this->createPartialMock(
\Magento\Framework\DB\Statement\Pdo\Mysql::class,
['setFetchMode', 'getIterator']
);
$statementMock->expects(
/** @var $iteratorFactoryMock \Magento\ImportExport\Model\ResourceModel\Import\Data\IteratorFactory */
$iteratorFactoryMock = $this
->getMockBuilder(\Magento\ImportExport\Model\ResourceModel\Import\Data\IteratorFactory::class)
->getMock();

$iteratorFactoryMock->expects(
$this->any()
)->method(
'getIterator'
'create'
)->will(
$this->returnValue(new \ArrayIterator($bunchData))
);

/** @var $selectMock \Magento\Framework\DB\Select */
$selectMock = $this->createPartialMock(\Magento\Framework\DB\Select::class, ['from', 'order']);
$selectMock->expects($this->any())->method('from')->will($this->returnSelf());
$selectMock->expects($this->any())->method('order')->will($this->returnSelf());

/** @var $connectionMock \Magento\Framework\DB\Adapter\AdapterInterface */
$connectionMock = $this->createPartialMock(
\Magento\Framework\DB\Adapter\Pdo\Mysql::class,
['select', 'from', 'order', 'query']
);
$connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock));
$connectionMock->expects($this->any())->method('query')->will($this->returnValue($statementMock));

/** @var $resourceModelMock \Magento\Framework\App\ResourceConnection */
$resourceModelMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
$resourceModelMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock));

$data = ['resource' => $resourceModelMock, 'entity_type' => $entityType];
$data = ['iterator_factory' => $iteratorFactoryMock, 'entity_type' => $entityType];

if ($entityType == CustomerComposite::COMPONENT_ENTITY_ADDRESS) {
$data['customer_attributes'] = $this->_customerAttributes;
Expand All @@ -86,7 +69,6 @@ public function testGetNextBunch($entityType, $bunchData, $expectedData)
{
$dependencies = $this->_getDependencies($entityType, [[$bunchData]]);

$resource = $dependencies['resource'];
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$jsonDecoderMock = $this->getMockBuilder(\Magento\Framework\Json\DecoderInterface::class)
->disableOriginalConstructor()
Expand All @@ -100,20 +82,24 @@ public function testGetNextBunch($entityType, $bunchData, $expectedData)
'jsonDecoder' => $jsonDecoderMock,
]
);
unset($dependencies['resource'], $dependencies['json_helper']);

$iteratorFactory = $dependencies['iterator_factory'];

unset($dependencies['json_helper'], $dependencies['iterator_factory']);

$contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
$contextMock->expects($this->once())->method('getResources')->willReturn($resource);

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$object = $objectManager->getObject(
\Magento\CustomerImportExport\Model\ResourceModel\Import\CustomerComposite\Data::class,
[
'context' => $contextMock,
'jsonHelper' => $jsonHelper,
'arguments' => $dependencies,
'iteratorFactory' => $iteratorFactory,
'arguments' => $dependencies
]
);

$this->assertEquals($expectedData, $object->getNextBunch());
}

Expand Down
23 changes: 9 additions & 14 deletions app/code/Magento/ImportExport/Model/ResourceModel/Import/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class Data extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implemen
*/
protected $jsonHelper;

/**
* @var Data\IteratorFactory
*/
protected $iteratorFactory;

/**
* Class constructor
*
Expand All @@ -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;
}

/**
Expand All @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterator factory should produce \Iterator objects accodring to the method docblock

}

/**
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
}
Loading