Skip to content

Commit d959d85

Browse files
authored
Merge pull request #1651 from algolia/feat/MAGE-1151-index-builder-interface
MAGE-1151: Index Builders improvements
2 parents 06ad9a8 + 12bf101 commit d959d85

File tree

17 files changed

+358
-157
lines changed

17 files changed

+358
-157
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Api\IndexBuilder;
4+
5+
interface IndexBuilderInterface
6+
{
7+
public function buildIndex(int $storeId, ?array $entityIds, ?array $options): void;
8+
9+
public function buildIndexFull(int $storeId, ?array $options): void;
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Api\IndexBuilder;
4+
5+
interface UpdatableIndexBuilderInterface extends IndexBuilderInterface
6+
{
7+
public function buildIndexList(int $storeId, ?array $entityIds, ?array $options): void;
8+
}

Helper/Data.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public function __construct(
4141
* @throws \Exception
4242
*
4343
* @deprecated
44-
* Use Algolia\AlgoliaSearch\Service\AdditionalSection\IndexBuilder::buildIndex() instead
44+
* Use Algolia\AlgoliaSearch\Service\AdditionalSection\IndexBuilder::buildIndexFull() instead
4545
*/
4646
public function rebuildStoreAdditionalSectionsIndex(int $storeId): void
4747
{
48-
$this->additionalSectionIndexBuilder->buildIndex($storeId);
48+
$this->additionalSectionIndexBuilder->buildIndexFull($storeId);
4949
}
5050

5151
/**
@@ -56,26 +56,27 @@ public function rebuildStoreAdditionalSectionsIndex(int $storeId): void
5656
* @throws NoSuchEntityException
5757
*
5858
* @deprecated
59-
* Use Algolia\AlgoliaSearch\Service\Page\IndexBuilder::buildIndex() instead
59+
* Use Algolia\AlgoliaSearch\Service\Page\IndexBuilder::buildIndexFull() instead
6060
*/
6161
public function rebuildStorePageIndex($storeId, array $pageIds = null): void
6262
{
63-
$this->pageIndexBuilder->buildIndex($storeId, $pageIds);
63+
$this->pageIndexBuilder->buildIndexFull($storeId, ['entityIds' => $pageIds]);
6464
}
6565

6666
/**
6767
* @param $storeId
68-
* @param $categoryIds
68+
* @param null $categoryIds
6969
* @return void
70+
* @throws AlgoliaException
7071
* @throws LocalizedException
7172
* @throws NoSuchEntityException
7273
*
7374
* @deprecated
74-
* Use Algolia\AlgoliaSearch\Service\Category\IndexBuilder::rebuildEntityIds() instead
75+
* Use Algolia\AlgoliaSearch\Service\Category\IndexBuilder::buildIndexList() instead
7576
*/
7677
public function rebuildStoreCategoryIndex($storeId, $categoryIds = null): void
7778
{
78-
$this->categoryIndexBuilder->rebuildEntityIds($storeId, $categoryIds);
79+
$this->categoryIndexBuilder->buildIndexList($storeId, $categoryIds);
7980
}
8081

8182
/**
@@ -86,11 +87,11 @@ public function rebuildStoreCategoryIndex($storeId, $categoryIds = null): void
8687
* @throws NoSuchEntityException
8788
*
8889
* @deprecated
89-
* Use Algolia\AlgoliaSearch\Service\Suggestion:\IndexBuilder:buildIndex() instead
90+
* Use Algolia\AlgoliaSearch\Service\Suggestion:\IndexBuilder:buildIndexFull() instead
9091
*/
9192
public function rebuildStoreSuggestionIndex(int $storeId): void
9293
{
93-
$this->suggestionIndexBuilder->buildIndex($storeId);
94+
$this->suggestionIndexBuilder->buildIndexFull($storeId);
9495
}
9596

9697
/**
@@ -100,11 +101,11 @@ public function rebuildStoreSuggestionIndex(int $storeId): void
100101
* @throws \Exception
101102
*
102103
* @deprecated
103-
* Use Algolia\AlgoliaSearch\Service\Product\IndexBuilder::rebuildEntityIds() instead
104+
* Use Algolia\AlgoliaSearch\Service\Product\IndexBuilder::buildIndexList() instead
104105
*/
105106
public function rebuildStoreProductIndex(int $storeId, array $productIds): void
106107
{
107-
$this->productIndexBuilder->rebuildEntityIds($storeId, $productIds);
108+
$this->productIndexBuilder->buildIndexList($storeId, $productIds);
108109
}
109110

110111
/**
@@ -117,11 +118,19 @@ public function rebuildStoreProductIndex(int $storeId, array $productIds): void
117118
* @throws \Exception
118119
*
119120
* @deprecated
120-
* Use Algolia\AlgoliaSearch\Service\Product\IndexBuilder::buildIndex() instead
121+
* Use Algolia\AlgoliaSearch\Service\Product\IndexBuilder::buildIndexFull() instead
121122
*/
122123
public function rebuildProductIndex(int $storeId, ?array $productIds, int $page, int $pageSize, bool $useTmpIndex): void
123124
{
124-
$this->productIndexBuilder->buildIndex($storeId, $productIds, $page, $pageSize, $useTmpIndex);
125+
$this->productIndexBuilder->buildIndexFull(
126+
$storeId,
127+
[
128+
'productIds' => $productIds,
129+
'page' => $page,
130+
'pageSize' => $pageSize,
131+
'useTmpIndex' => $useTmpIndex
132+
]
133+
);
125134
}
126135

127136
/**
@@ -134,11 +143,11 @@ public function rebuildProductIndex(int $storeId, ?array $productIds, int $page,
134143
* @throws \Exception
135144
*
136145
* @deprecated
137-
* Use Algolia\AlgoliaSearch\Service\Category\IndexBuilder::buildIndex() instead
146+
* Use Algolia\AlgoliaSearch\Service\Category\IndexBuilder::buildIndexFull() instead
138147
*/
139148
public function rebuildCategoryIndex(int $storeId, int $page, int $pageSize): void
140149
{
141-
$this->categoryIndexBuilder->buildIndex($storeId, $page, $pageSize);
150+
$this->categoryIndexBuilder->buildIndexFull($storeId, ['page' => $page, 'pageSize' => $pageSize]);
142151
}
143152

144153
/**

Model/Indexer/AdditionalSection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public function executeFull()
4040
return;
4141
}
4242

43-
/** @uses AdditionalSectionIndexBuilder::buildIndex() */
43+
/** @uses AdditionalSectionIndexBuilder::buildIndexFull() */
4444
$this->queue->addToQueue(
4545
AdditionalSectionIndexBuilder::class,
46-
'buildIndex',
46+
'buildIndexFull',
4747
['storeId' => $storeId],
4848
1
4949
);

Model/Indexer/Category.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ private function rebuildAffectedProducts($storeId)
8181
if ($affectedProductsCount > 0 && $this->configHelper->indexProductOnCategoryProductsUpdate($storeId)) {
8282
$productsPerPage = $this->configHelper->getNumberOfElementByPage();
8383
foreach (array_chunk($affectedProducts, $productsPerPage) as $chunk) {
84-
/** @uses ProductIndexBuilder::rebuildEntityIds() */
84+
/** @uses ProductIndexBuilder::buildIndexList() */
8585
$this->queue->addToQueue(
8686
ProductIndexBuilder::class,
87-
'rebuildEntityIds',
87+
'buildIndexList',
8888
[
8989
'storeId' => $storeId,
90-
'productIds' => $chunk,
90+
'entityIds' => $chunk,
9191
],
9292
count($chunk)
9393
);
@@ -103,13 +103,13 @@ private function rebuildAffectedProducts($storeId)
103103
private function processSpecificCategories($categoryIds, $categoriesPerPage, $storeId)
104104
{
105105
foreach (array_chunk($categoryIds, $categoriesPerPage) as $chunk) {
106-
/** @uses CategoryIndexBuilder::rebuildEntityIds */
106+
/** @uses CategoryIndexBuilder::buildIndexList */
107107
$this->queue->addToQueue(
108108
CategoryIndexBuilder::class,
109-
'rebuildEntityIds',
109+
'buildIndexList',
110110
[
111111
'storeId' => $storeId,
112-
'categoryIds' => $chunk,
112+
'entityIds' => $chunk,
113113
],
114114
count($chunk)
115115
);
@@ -136,12 +136,20 @@ private function processFullReindex($storeId, $categoriesPerPage)
136136
for ($i = 1; $i <= $pages; $i++) {
137137
$data = [
138138
'storeId' => $storeId,
139-
'page' => $i,
140-
'pageSize' => $categoriesPerPage,
139+
'options' => [
140+
'page' => $i,
141+
'pageSize' => $categoriesPerPage,
142+
]
141143
];
142144

143-
/** @uses CategoryIndexBuilder::buildIndex() */
144-
$this->queue->addToQueue(CategoryIndexBuilder::class, 'buildIndex', $data, $categoriesPerPage, true);
145+
/** @uses CategoryIndexBuilder::buildIndexFull() */
146+
$this->queue->addToQueue(
147+
CategoryIndexBuilder::class,
148+
'buildIndexFull',
149+
$data,
150+
$categoriesPerPage,
151+
true
152+
);
145153
}
146154
}
147155
}

Model/Indexer/Page.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ public function execute($ids)
4646
if ($this->isPagesInAdditionalSections($storeId)) {
4747
$data = ['storeId' => $storeId];
4848
if (is_array($ids) && count($ids) > 0) {
49-
$data['pageIds'] = $ids;
49+
$data['options'] = ['entityIds' => $ids];
5050
}
5151

52-
/** @uses PageIndexBuilder::buildIndex() */
52+
/** @uses PageIndexBuilder::buildIndexFull() */
5353
$this->queue->addToQueue(
5454
PageIndexBuilder::class,
55-
'buildIndex',
55+
'buildIndexFull',
5656
$data,
5757
is_array($ids) ? count($ids) : 1
5858
);

Model/Indexer/Product.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ public function execute($productIds)
5858

5959
if (is_array($productIds) && count($productIds) > 0) {
6060
foreach (array_chunk($productIds, $productsPerPage) as $chunk) {
61-
/** @uses ProductIndexBuilder::rebuildEntityIds() */
61+
/** @uses ProductIndexBuilder::buildIndexList() */
6262
$this->queue->addToQueue(
6363
ProductIndexBuilder::class,
64-
'rebuildEntityIds',
65-
['storeId' => $storeId, 'productIds' => $chunk],
64+
'buildIndexList',
65+
['storeId' => $storeId, 'entityIds' => $chunk],
6666
count($chunk)
6767
);
6868
}
@@ -89,16 +89,18 @@ public function execute($productIds)
8989
for ($i = 1; $i <= $pages; $i++) {
9090
$data = [
9191
'storeId' => $storeId,
92-
'productIds' => $productIds,
93-
'page' => $i,
94-
'pageSize' => $productsPerPage,
95-
'useTmpIndex' => $useTmpIndex,
92+
'options' => [
93+
'entityIds' => $productIds,
94+
'page' => $i,
95+
'pageSize' => $productsPerPage,
96+
'useTmpIndex' => $useTmpIndex,
97+
]
9698
];
9799

98-
/** @uses ProductIndexBuilder::buildIndex() */
100+
/** @uses ProductIndexBuilder::buildIndexFull() */
99101
$this->queue->addToQueue(
100102
ProductIndexBuilder::class,
101-
'buildIndex',
103+
'buildIndexFull',
102104
$data,
103105
$productsPerPage,
104106
true

Model/Indexer/Suggestion.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public function executeFull()
4343
return;
4444
}
4545

46-
/** @uses SuggestionIndexBuilder::buildIndex() */
46+
/** @uses SuggestionIndexBuilder::buildIndexFull() */
4747
$this->queue->addToQueue(
4848
SuggestionIndexBuilder::class,
49-
'buildIndex',
49+
'buildIndexFull',
5050
['storeId' => $storeId],
5151
1
5252
);

Model/Job.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,35 +124,47 @@ public function canMerge(Job $job, $maxJobDataSize)
124124

125125
$decodedData = $this->getDecodedData();
126126

127+
// @todo Remove legacy checks on 3.16.0
127128
if ((!isset($decodedData['product_ids']) || count($decodedData['product_ids']) <= 0)
128129
&& (!isset($decodedData['category_ids']) || count($decodedData['category_ids']) < 0)
130+
&& (!isset($decodedData['entity_ids']) || count($decodedData['entity_ids']) < 0)
129131
&& (!isset($decodedData['page_ids']) || count($decodedData['page_ids']) < 0)) {
130132
return false;
131133
}
132134

133135
$candidateDecodedData = $job->getDecodedData();
134136

137+
// @todo Remove legacy checks on 3.16.0
135138
if ((!isset($candidateDecodedData['product_ids']) || count($candidateDecodedData['product_ids']) <= 0)
136139
&& (!isset($candidateDecodedData['category_ids']) || count($candidateDecodedData['category_ids']) < 0)
140+
&& (!isset($candidateDecodedData['entity_ids']) || count($candidateDecodedData['entity_ids']) < 0)
137141
&& (!isset($candidateDecodedData['page_ids']) || count($candidateDecodedData['page_ids']) < 0)) {
138142
return false;
139143
}
140144

145+
// @todo Remove on 3.16.0
141146
if (isset($decodedData['product_ids'])
142147
&& count($decodedData['product_ids']) + count($candidateDecodedData['product_ids']) > $maxJobDataSize) {
143148
return false;
144149
}
145150

151+
// @todo Remove on 3.16.0
146152
if (isset($decodedData['category_ids'])
147153
&& count($decodedData['category_ids']) + count($candidateDecodedData['category_ids']) > $maxJobDataSize) {
148154
return false;
149155
}
150156

157+
// @todo Remove on 3.16.0
151158
if (isset($decodedData['page_ids'])
152159
&& count($decodedData['page_ids']) + count($candidateDecodedData['page_ids']) > $maxJobDataSize) {
153160
return false;
154161
}
155162

163+
if (isset($decodedData['entity_ids'])
164+
&& count($decodedData['entity_ids']) + count($candidateDecodedData['entity_ids']) > $maxJobDataSize) {
165+
return false;
166+
}
167+
156168
return true;
157169
}
158170

@@ -173,6 +185,7 @@ public function merge(Job $mergedJob)
173185

174186
$dataSize = $this->getDataSize();
175187

188+
// @todo Remove useless code on 3.16.0
176189
if (isset($decodedData['product_ids'])) {
177190
$decodedData['product_ids'] = array_unique(array_merge(
178191
$decodedData['product_ids'],
@@ -194,6 +207,13 @@ public function merge(Job $mergedJob)
194207
));
195208

196209
$dataSize = count($decodedData['page_ids']);
210+
} elseif (isset($decodedData['entity_ids'])) {
211+
$decodedData['entity_ids'] = array_unique(array_merge(
212+
$decodedData['entity_ids'],
213+
$mergedJobDecodedData['entity_ids']
214+
));
215+
216+
$dataSize = count($decodedData['entity_ids']);
197217
}
198218

199219
$this->setDecodedData($decodedData);

Model/Source/JobMethods.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class JobMethods implements \Magento\Framework\Data\OptionSourceInterface
77
private $methods = [
88
'saveConfigurationToAlgolia' => 'Save Configuration',
99
'moveIndexWithSetSettings' => 'Move Index',
10-
'buildIndex' => 'Build Full Index',
11-
'rebuildEntityIds' => 'Update Index',
10+
'buildIndexFull' => 'Build Full Index',
11+
'buildIndexList' => 'Update Index',
1212
'deleteInactiveProducts' => 'Delete Inactive Products',
1313
// @deprecated
1414
'deleteObjects' => 'Object deletion (deprecated)',

0 commit comments

Comments
 (0)