Skip to content

Commit 6f2340c

Browse files
authored
Merge pull request #4 from integer-net/slices
Slices
2 parents 90a80f3 + f2c3270 commit 6f2340c

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
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: 57 additions & 13 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,48 @@ 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

133-
$associations = $this->productRepository->getProductAssociations($productIds);
135+
if ($productIds == null) {
136+
$productIdsToIndex = $this->productRepository->getAllProductIds($sliceId, $totalNumberSlices);
137+
} else {
138+
$productIdsToIndex = $productIds;
139+
}
140+
141+
$associations = $this->productRepository->getProductAssociations($productIdsToIndex);
134142
$chunks = ProductIdChunks::withAssociationsTogether(
135-
$productIds == null ? $this->productRepository->getAllProductIds() : $productIds,
143+
$productIdsToIndex,
136144
$associations,
137145
$pageSize);
138146
$productIterator = $this->productRepository->getProductsInChunks($storeId, $chunks);
139-
$this->_indexProductCollection($emptyIndex, $productIterator, $storeId, $productIds, $associations);
147+
$this->_indexProductCollection($emptyIndex, $productIterator, $storeId, $productIdsToIndex, $associations);
140148

141-
$this->_getResource()->setUseSwapIndex(false);
149+
if (is_null($productIds) && is_null($sliceId) && $storeConfig->getIndexingConfig()->isSwapCores()) {
150+
$this->deactivateSwapCore();
151+
}
142152
} catch (\Exception $e) {
143153
$this->storeEmulation->stop();
144154
throw $e;
145155
}
146156
$this->storeEmulation->stop();
147157
}
148158

149-
if (is_null($productIds)) {
150-
$this->_getResource()->swapCores($restrictToStoreIds);
159+
if (is_null($productIds) && is_null($sliceId)) {
160+
$this->swapCores($restrictToStoreIds);
151161
}
152162
}
153163

@@ -514,5 +524,39 @@ protected function _indexProductCollection($emptyIndex, PagedProductIterator $pr
514524
return $storeId;
515525
}
516526

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

518562
}

0 commit comments

Comments
 (0)