Skip to content

Commit

Permalink
perf: optimized rebuilding of items (via chunks)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasstislav committed Sep 13, 2023
1 parent 7e12851 commit 1bc2e4e
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,33 @@ public function rebuild(): void
Versioned::set_stage(Versioned::LIVE);
}

foreach ($this->getDataList() as $record) {
/** @var DataObject|FluentVersionedExtension|FluentExtension $record */
if (
($record->hasExtension(FluentVersionedExtension::class) && !$record->isPublishedInLocale())
|| ($record->hasExtension(FluentExtension::class) && !$record->existsInLocale())
) {
continue;
$chunkSize = 500;
$currentChunk = 0;

// Keep looping until we run out of chunks
while ($chunk = $this->getDataList()->limit($chunkSize, $chunkSize * $currentChunk)->getIterator()) {
// Loop over all the item in our chunk
foreach ($chunk as $record) {
/** @var DataObject|FluentVersionedExtension|FluentExtension $record */
if (
($record->hasExtension(FluentVersionedExtension::class) && !$record->isPublishedInLocale())
|| ($record->hasExtension(FluentExtension::class) && !$record->existsInLocale())
) {
continue;
}

$this->add(Document::create($record), false);
}

$this->add(Document::create($record), false);
}
$this->commit();

$this->commit();
if ($chunk->count() < $chunkSize) {
// If our last chunk had less item than our chunkSize, we've reach the end.
break;
}

$currentChunk++;
}

if (
ClassInfo::exists(Versioned::class)
Expand Down

0 comments on commit 1bc2e4e

Please sign in to comment.