Skip to content

Commit d2c93d6

Browse files
committed
6775 Add new command line options for splitting up indexing into slices
1 parent 90a80f3 commit d2c93d6

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

src/Solr/Implementor/ProductRepository.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ interface ProductRepository
2323
public function getProductsInChunks($storeId, ProductIdChunks $chunks);
2424

2525
/**
26+
* @param null|int $sliceId
27+
* @param null|int $totalNumberSlices
2628
* @return int[]
2729
*/
28-
public function getAllProductIds();
30+
public function getAllProductIds($sliceId = null, $totalNumberSlices = null);
2931

3032
/**
3133
* @param null|int[] $productIds

src/Solr/Indexer/ProductIndexer.php

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,15 @@ protected function _getStoreConfig($storeId = null)
9393
* @param array|null $productIds Restrict to given Products if this is set
9494
* @param boolean|string $emptyIndex Whether to truncate the index before refilling it
9595
* @param null|int[] $restrictToStoreIds
96+
* @param null|int $sliceId
97+
* @param null|int $totalNumberSlices
9698
* @throws \Exception
9799
* @throws \IntegerNet\Solr\Exception
98100
*/
99-
public function reindex($productIds = null, $emptyIndex = false, $restrictToStoreIds = null)
101+
public function reindex($productIds = null, $emptyIndex = false, $restrictToStoreIds = null, $sliceId = null, $totalNumberSlices = null)
100102
{
101-
if (is_null($productIds)) {
102-
$this->_getResource()->checkSwapCoresConfiguration($restrictToStoreIds);
103+
if (is_null($productIds) && is_null($sliceId)) {
104+
$this->checkSwapCoresConfiguration($restrictToStoreIds);
103105
}
104106

105107
foreach($this->_config as $storeId => $storeConfig) {
@@ -114,40 +116,44 @@ public function reindex($productIds = null, $emptyIndex = false, $restrictToStor
114116
$this->storeEmulation->start($storeId);
115117
try {
116118

117-
if (is_null($productIds) && $storeConfig->getIndexingConfig()->isSwapCores()) {
118-
$this->_getResource()->setUseSwapIndex();
119+
if (is_null($productIds) && is_null($sliceId) && $storeConfig->getIndexingConfig()->isSwapCores()) {
120+
$this->activateSwapCore();
119121
}
120122

121123
if (
122-
($emptyIndex && $storeConfig->getIndexingConfig()->isDeleteDocumentsBeforeIndexing())
124+
($emptyIndex && is_null($sliceId) && $storeConfig->getIndexingConfig()->isDeleteDocumentsBeforeIndexing())
123125
|| $emptyIndex === 'force'
124126
) {
125-
$this->_getResource()->deleteAllDocuments($storeId, self::CONTENT_TYPE);
127+
$this->clearIndex($storeId);
126128
}
127129

128130
$pageSize = intval($storeConfig->getIndexingConfig()->getPagesize());
129131
if ($pageSize <= 0) {
130132
$pageSize = 100;
131133
}
132134

135+
if ($productIds == null) {
136+
$productIds = $this->productRepository->getAllProductIds($sliceId, $totalNumberSlices);
137+
}
138+
133139
$associations = $this->productRepository->getProductAssociations($productIds);
134140
$chunks = ProductIdChunks::withAssociationsTogether(
135-
$productIds == null ? $this->productRepository->getAllProductIds() : $productIds,
141+
$productIds,
136142
$associations,
137143
$pageSize);
138144
$productIterator = $this->productRepository->getProductsInChunks($storeId, $chunks);
139145
$this->_indexProductCollection($emptyIndex, $productIterator, $storeId, $productIds, $associations);
140146

141-
$this->_getResource()->setUseSwapIndex(false);
147+
$this->deactivateSwapCore();
142148
} catch (\Exception $e) {
143149
$this->storeEmulation->stop();
144150
throw $e;
145151
}
146152
$this->storeEmulation->stop();
147153
}
148154

149-
if (is_null($productIds)) {
150-
$this->_getResource()->swapCores($restrictToStoreIds);
155+
if (is_null($productIds) && is_null($sliceId)) {
156+
$this->swapCores($restrictToStoreIds);
151157
}
152158
}
153159

@@ -514,5 +520,39 @@ protected function _indexProductCollection($emptyIndex, PagedProductIterator $pr
514520
return $storeId;
515521
}
516522

523+
/**
524+
* @param $storeId
525+
*/
526+
public function clearIndex($storeId)
527+
{
528+
$this->_getResource()->deleteAllDocuments($storeId, self::CONTENT_TYPE);
529+
}
530+
531+
public function activateSwapCore()
532+
{
533+
$this->_getResource()->setUseSwapIndex();
534+
}
535+
536+
public function deactivateSwapCore()
537+
{
538+
$this->_getResource()->setUseSwapIndex(false);
539+
}
540+
541+
/**
542+
* @param null|int[] $restrictToStoreIds
543+
*/
544+
public function swapCores($restrictToStoreIds)
545+
{
546+
$this->_getResource()->swapCores($restrictToStoreIds);
547+
}
548+
549+
/**
550+
* @param $restrictToStoreIds
551+
*/
552+
public function checkSwapCoresConfiguration($restrictToStoreIds)
553+
{
554+
return $this->_getResource()->checkSwapCoresConfiguration($restrictToStoreIds);
555+
}
556+
517557

518558
}

0 commit comments

Comments
 (0)