Skip to content

Commit 9daf789

Browse files
author
Magento CICD
authored
merge magento/2.2-develop into magento-helix/MAGETWO-71434
2 parents 2a5e444 + a4c37be commit 9daf789

File tree

34 files changed

+744
-526
lines changed

34 files changed

+744
-526
lines changed

app/code/Magento/Bundle/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@
160160
<argument name="estimators" xsi:type="array">
161161
<item name="bundle" xsi:type="object">Magento\Catalog\Model\Indexer\Price\CompositeProductBatchSizeManagement</item>
162162
</argument>
163+
<argument name="batchSizeAdjusters" xsi:type="array">
164+
<item name="bundle" xsi:type="object">Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\CompositeProductBatchSizeAdjuster</item>
165+
</argument>
163166
</arguments>
164167
</type>
165168
<type name="Magento\Bundle\Model\ResourceModel\Indexer\Price">

app/code/Magento/Catalog/Model/Indexer/Product/Price/Action/Full.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public function execute($ids = null)
106106
$this->_defaultIndexerResource->getMainTable()
107107
);
108108

109+
// Prepare replica table for indexation.
110+
$this->_defaultIndexerResource->getConnection()->truncateTable($replicaTable);
111+
109112
/** @var \Magento\Catalog\Model\ResourceModel\Product\Indexer\AbstractIndexer $indexer */
110113
foreach ($this->getTypeIndexers() as $indexer) {
111114
$indexer->getTableStrategy()->setUseIdxTable(false);

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
641641
}
642642
unset($this->instances[$product->getSku()]);
643643
unset($this->instancesById[$product->getId()]);
644-
return $this->get($product->getSku());
644+
return $this->get($product->getSku(), false, $product->getStoreId());
645645
}
646646

647647
/**

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Price/BatchSizeCalculator.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,24 @@ class BatchSizeCalculator
2121
*/
2222
private $estimators;
2323

24+
/**
25+
* @var \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\CompositeProductBatchSizeAdjusterInterface[]
26+
* @since 2.2.0
27+
*/
28+
private $batchSizeAdjusters;
29+
2430
/**
2531
* BatchSizeCalculator constructor.
2632
* @param array $batchRowsCount
2733
* @param array $estimators
34+
* @param array $batchSizeAdjusters
35+
* @since 2.2.0
2836
*/
29-
public function __construct(array $batchRowsCount, array $estimators)
37+
public function __construct(array $batchRowsCount, array $estimators, array $batchSizeAdjusters)
3038
{
3139
$this->batchRowsCount = $batchRowsCount;
3240
$this->estimators = $estimators;
41+
$this->batchSizeAdjusters = $batchSizeAdjusters;
3342
}
3443

3544
/**
@@ -52,6 +61,10 @@ public function estimateBatchSize(\Magento\Framework\DB\Adapter\AdapterInterface
5261
? $this->estimators[$indexerTypeId]
5362
: $this->estimators['default'];
5463

64+
$batchRowsCount = isset($this->batchSizeAdjusters[$indexerTypeId])
65+
? $this->batchSizeAdjusters[$indexerTypeId]->adjust($batchRowsCount)
66+
: $batchRowsCount;
67+
5568
$calculator->ensureBatchSize($connection, $batchRowsCount);
5669

5770
return $batchRowsCount;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\ResourceModel\Product\Indexer\Price;
8+
9+
/**
10+
* Correct batch size according to number of composite related items.
11+
*/
12+
class CompositeProductBatchSizeAdjuster implements CompositeProductBatchSizeAdjusterInterface
13+
{
14+
/**
15+
* @var CompositeProductRelationsCalculator
16+
*/
17+
private $compositeProductRelationsCalculator;
18+
19+
/**
20+
* @param CompositeProductRelationsCalculator $compositeProductRelationsCalculator
21+
*/
22+
public function __construct(CompositeProductRelationsCalculator $compositeProductRelationsCalculator)
23+
{
24+
$this->compositeProductRelationsCalculator = $compositeProductRelationsCalculator;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function adjust($batchSize)
31+
{
32+
$maxRelationsCount = $this->compositeProductRelationsCalculator->getMaxRelationsCount();
33+
return $maxRelationsCount > 0 ? ceil($batchSize / $maxRelationsCount) : $batchSize;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\ResourceModel\Product\Indexer\Price;
8+
9+
/**
10+
* Correct batch size according to number of composite related items.
11+
* @api
12+
*/
13+
interface CompositeProductBatchSizeAdjusterInterface
14+
{
15+
/**
16+
* Correct batch size according to number of composite related items.
17+
*
18+
* @param int $batchSize
19+
* @return int
20+
*/
21+
public function adjust($batchSize);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\ResourceModel\Product\Indexer\Price;
8+
9+
/**
10+
* Class calculates composite product relations.
11+
*/
12+
class CompositeProductRelationsCalculator
13+
{
14+
/**
15+
* @param DefaultPrice $indexerResource
16+
*/
17+
public function __construct(DefaultPrice $indexerResource)
18+
{
19+
$this->indexerResource = $indexerResource;
20+
}
21+
22+
/**
23+
* Returns maximum number of composite related products.
24+
*
25+
* @return int
26+
*/
27+
public function getMaxRelationsCount()
28+
{
29+
$connection = $this->indexerResource->getConnection();
30+
$relationSelect = $connection->select();
31+
$relationSelect->from(
32+
['relation' => $this->indexerResource->getTable('catalog_product_relation')],
33+
['count' => new \Zend_Db_Expr('count(relation.child_id)')]
34+
);
35+
$relationSelect->group('parent_id');
36+
37+
$maxSelect = $connection->select();
38+
$maxSelect->from(
39+
['max_value' => $relationSelect],
40+
['count' => new \Zend_Db_Expr('MAX(count)')]
41+
);
42+
return $connection->fetchOne($maxSelect);
43+
}
44+
}

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Price/CompositeProductRowSizeEstimator.php

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ class CompositeProductRowSizeEstimator implements IndexTableRowSizeEstimatorInte
2020
*/
2121
const MEMORY_SIZE_FOR_ONE_ROW = 200;
2222

23-
/**
24-
* @var DefaultPrice
25-
*/
26-
private $indexerResource;
27-
2823
/**
2924
* @var WebsiteManagementInterface
3025
*/
@@ -36,18 +31,25 @@ class CompositeProductRowSizeEstimator implements IndexTableRowSizeEstimatorInte
3631
private $collectionFactory;
3732

3833
/**
39-
* @param DefaultPrice $indexerResource
34+
* @var CompositeProductRelationsCalculator
35+
* @since 2.2.0
36+
*/
37+
private $compositeProductRelationsCalculator;
38+
39+
/**
4040
* @param WebsiteManagementInterface $websiteManagement
4141
* @param CollectionFactory $collectionFactory
42+
* @param CompositeProductRelationsCalculator $compositeProductRelationsCalculator
43+
* @since 2.2.0
4244
*/
4345
public function __construct(
44-
DefaultPrice $indexerResource,
4546
WebsiteManagementInterface $websiteManagement,
46-
CollectionFactory $collectionFactory
47+
CollectionFactory $collectionFactory,
48+
CompositeProductRelationsCalculator $compositeProductRelationsCalculator
4749
) {
48-
$this->indexerResource = $indexerResource;
4950
$this->websiteManagement = $websiteManagement;
5051
$this->collectionFactory = $collectionFactory;
52+
$this->compositeProductRelationsCalculator = $compositeProductRelationsCalculator;
5153
}
5254

5355
/**
@@ -59,21 +61,7 @@ public function estimateRowSize()
5961
{
6062
$websitesCount = $this->websiteManagement->getCount();
6163
$customerGroupCount = $this->collectionFactory->create()->getSize();
62-
63-
$connection = $this->indexerResource->getConnection();
64-
$relationSelect = $connection->select();
65-
$relationSelect->from(
66-
['relation' => $this->indexerResource->getTable('catalog_product_relation')],
67-
['count' => new \Zend_Db_Expr('count(relation.child_id)')]
68-
);
69-
$relationSelect->group('parent_id');
70-
71-
$maxSelect = $connection->select();
72-
$maxSelect->from(
73-
['max_value' => $relationSelect],
74-
['count' => new \Zend_Db_Expr('MAX(count)')]
75-
);
76-
$maxRelatedProductCount = $connection->fetchOne($maxSelect);
64+
$maxRelatedProductCount = $this->compositeProductRelationsCalculator->getMaxRelationsCount();
7765

7866
/**
7967
* Calculate memory size for largest composite product in database.

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ protected function setUp()
193193
'validate',
194194
'save',
195195
'getMediaGalleryEntries',
196-
'setData'
196+
'setData',
197+
'getStoreId'
197198
]);
198199
$this->initializedProductMock->expects($this->any())
199200
->method('hasGalleryAttribute')

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Indexer/Price/BatchSizeCalculatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ protected function setUp()
2929
$this->batchRowsCount = 200;
3030
$this->model = new \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator(
3131
['default' => $this->batchRowsCount],
32-
['default' => $this->estimatorMock]
32+
['default' => $this->estimatorMock],
33+
[]
3334
);
3435
}
3536

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product\Indexer\Price;
8+
9+
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\CompositeProductBatchSizeAdjuster;
10+
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\CompositeProductRelationsCalculator;
11+
12+
class CompositeProductBatchSizeAdjusterTest extends \PHPUnit\Framework\TestCase
13+
{
14+
/**
15+
* @var CompositeProductBatchSizeAdjuster
16+
*/
17+
private $model;
18+
19+
/**
20+
* @var \PHPUnit_Framework_MockObject_MockObject|CompositeProductRelationsCalculator
21+
*/
22+
private $relationsCalculatorMock;
23+
24+
protected function setUp()
25+
{
26+
$this->relationsCalculatorMock = $this->getMockBuilder(CompositeProductRelationsCalculator::class)
27+
->disableOriginalConstructor()
28+
->getMock();
29+
$this->model = new CompositeProductBatchSizeAdjuster($this->relationsCalculatorMock);
30+
}
31+
32+
public function testAdjust()
33+
{
34+
$this->relationsCalculatorMock->expects($this->once())
35+
->method('getMaxRelationsCount')
36+
->willReturn(200);
37+
$this->assertEquals(25, $this->model->adjust(5000));
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product\Indexer\Price;
8+
9+
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice;
10+
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\CompositeProductRelationsCalculator;
11+
12+
class CompositeProductRelationsCalculatorTest extends \PHPUnit\Framework\TestCase
13+
{
14+
/**
15+
* @var \PHPUnit_Framework_MockObject_MockObject|DefaultPrice
16+
*/
17+
private $defaultPriceMock;
18+
19+
/**
20+
* @var CompositeProductRelationsCalculator
21+
*/
22+
private $model;
23+
24+
protected function setUp()
25+
{
26+
$this->defaultPriceMock = $this->getMockBuilder(DefaultPrice::class)->disableOriginalConstructor()->getMock();
27+
$this->model = new CompositeProductRelationsCalculator($this->defaultPriceMock);
28+
}
29+
30+
public function testGetMaxRelationsCount()
31+
{
32+
$tableName = 'catalog_product_relation';
33+
$maxRelatedProductCount = 200;
34+
35+
$connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)->getMock();
36+
$this->defaultPriceMock->expects($this->once())->method('getConnection')->willReturn($connectionMock);
37+
$this->defaultPriceMock->expects($this->once())->method('getTable')->with($tableName)->willReturn($tableName);
38+
39+
$relationSelectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
40+
->disableOriginalConstructor()
41+
->getMock();
42+
$relationSelectMock->expects($this->once())
43+
->method('from')
44+
->with(
45+
['relation' => $tableName],
46+
['count' => 'count(relation.child_id)']
47+
)
48+
->willReturnSelf();
49+
$relationSelectMock->expects($this->once())->method('group')->with('parent_id')->willReturnSelf();
50+
$connectionMock->expects($this->at(0))->method('select')->willReturn($relationSelectMock);
51+
52+
$maxSelectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
53+
->disableOriginalConstructor()
54+
->getMock();
55+
$maxSelectMock->expects($this->once())
56+
->method('from')
57+
->with(
58+
['max_value' => $relationSelectMock],
59+
['count' => 'MAX(count)']
60+
)
61+
->willReturnSelf();
62+
$connectionMock->expects($this->at(1))->method('select')->willReturn($maxSelectMock);
63+
64+
$connectionMock->expects($this->at(2))
65+
->method('fetchOne')
66+
->with($maxSelectMock)
67+
->willReturn($maxRelatedProductCount);
68+
69+
$this->assertEquals($maxRelatedProductCount, $this->model->getMaxRelationsCount());
70+
}
71+
}

0 commit comments

Comments
 (0)