Skip to content

Commit 99e9487

Browse files
authored
[5.x] chunk on number of tags rather than number of entries (#1644)
* chunk on number of tags rather than number of entries * docblock * style * rename param
1 parent 357f075 commit 99e9487

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

src/Storage/DatabaseEntriesRepository.php

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class DatabaseEntriesRepository implements Contract, ClearableRepository, Prunab
4141
* Create a new database repository.
4242
*
4343
* @param string $connection
44-
* @param int $chunkSize
44+
* @param int|null $chunkSize
4545
* @return void
4646
*/
4747
public function __construct(string $connection, ?int $chunkSize = null)
@@ -126,7 +126,7 @@ protected function countExceptionOccurences(IncomingEntry $exception)
126126
/**
127127
* Store the given array of entries.
128128
*
129-
* @param \Illuminate\Support\Collection|\Laravel\Telescope\IncomingEntry[] $entries
129+
* @param \Illuminate\Support\Collection<int, \Laravel\Telescope\IncomingEntry> $entries
130130
* @return void
131131
*/
132132
public function store(Collection $entries)
@@ -155,7 +155,7 @@ public function store(Collection $entries)
155155
/**
156156
* Store the given array of exception entries.
157157
*
158-
* @param \Illuminate\Support\Collection|\Laravel\Telescope\IncomingEntry[] $exceptions
158+
* @param \Illuminate\Support\Collection<int, \Laravel\Telescope\IncomingEntry> $exceptions
159159
* @return void
160160
*/
161161
protected function storeExceptions(Collection $exceptions)
@@ -185,25 +185,45 @@ protected function storeExceptions(Collection $exceptions)
185185
/**
186186
* Store the tags for the given entries.
187187
*
188-
* @param \Illuminate\Support\Collection $results
188+
* @param \Illuminate\Support\Collection<string, array<array-key, mixed>> $results
189189
* @return void
190190
*/
191191
protected function storeTags(Collection $results)
192192
{
193-
$results->chunk($this->chunkSize)->each(function ($chunked) {
194-
try {
195-
$this->table('telescope_entries_tags')->insert($chunked->flatMap(function ($tags, $uuid) {
196-
return collect($tags)->map(function ($tag) use ($uuid) {
197-
return [
198-
'entry_uuid' => $uuid,
199-
'tag' => $tag,
200-
];
201-
});
202-
})->all());
203-
} catch (UniqueConstraintViolationException $e) {
204-
// Ignore tags that already exist...
193+
$toInsert = [];
194+
195+
foreach ($results as $uuid => $tags) {
196+
foreach ($tags as $tag) {
197+
$toInsert[] = [
198+
'entry_uuid' => $uuid,
199+
'tag' => $tag,
200+
];
201+
202+
if (count($toInsert) >= $this->chunkSize) {
203+
$this->insertChunkOfTags($toInsert);
204+
$toInsert = [];
205+
}
205206
}
206-
});
207+
}
208+
209+
if ($toInsert !== []) {
210+
$this->insertChunkOfTags($toInsert);
211+
}
212+
}
213+
214+
/**
215+
* Insert a chunk of tags, ignoring unique constraint violations.
216+
*
217+
* @param array<int, array{entry_uuid: string, tag: string}> $tags
218+
* @return void
219+
*/
220+
protected function insertChunkOfTags($tags)
221+
{
222+
try {
223+
$this->table('telescope_entries_tags')->insert($tags);
224+
} catch (UniqueConstraintViolationException $e) {
225+
// Ignore tags that already exist...
226+
}
207227
}
208228

209229
/**

0 commit comments

Comments
 (0)