Skip to content

Commit 59aab83

Browse files
committed
Skip relationship writes for duplicates in ignore mode, chunk find queries by maxQueryValues
When ignore=true, pre-fetch existing document IDs so createDocumentRelationships() is skipped for duplicates that will be silently ignored by the adapter. Also chunk the batch find() in upsertDocumentsWithIncrease by maxQueryValues to avoid exceeding query parameter limits.
1 parent eb564c3 commit 59aab83

1 file changed

Lines changed: 39 additions & 12 deletions

File tree

src/Database/Database.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5655,6 +5655,29 @@ public function createDocuments(
56555655
$time = DateTime::now();
56565656
$modified = 0;
56575657

5658+
// When ignore mode is on and relationships are being resolved,
5659+
// pre-fetch existing document IDs so we skip relationship writes for duplicates
5660+
$preExistingIds = [];
5661+
if ($ignore && $this->resolveRelationships) {
5662+
$inputIds = \array_values(\array_unique(\array_filter(
5663+
\array_map(fn (Document $doc) => $doc->getId(), $documents)
5664+
)));
5665+
5666+
foreach (\array_chunk($inputIds, $this->maxQueryValues) as $idChunk) {
5667+
$existing = $this->authorization->skip(fn () => $this->silent(fn () => $this->find(
5668+
$collection->getId(),
5669+
[
5670+
Query::equal('$id', $idChunk),
5671+
Query::select(['$id']),
5672+
Query::limit(\count($idChunk)),
5673+
]
5674+
)));
5675+
foreach ($existing as $doc) {
5676+
$preExistingIds[$doc->getId()] = true;
5677+
}
5678+
}
5679+
}
5680+
56585681
foreach ($documents as $document) {
56595682
$createdAt = $document->getCreatedAt();
56605683
$updatedAt = $document->getUpdatedAt();
@@ -5694,7 +5717,7 @@ public function createDocuments(
56945717
}
56955718
}
56965719

5697-
if ($this->resolveRelationships) {
5720+
if ($this->resolveRelationships && !isset($preExistingIds[$document->getId()])) {
56985721
$document = $this->silent(fn () => $this->createDocumentRelationships($collection, $document));
56995722
}
57005723

@@ -7135,22 +7158,26 @@ public function upsertDocumentsWithIncrease(
71357158
}
71367159
foreach ($idsByTenant as $tenant => $tenantIds) {
71377160
$tenantIds = \array_values(\array_unique($tenantIds));
7138-
$fetched = $this->authorization->skip(fn () => $this->withTenant($tenant, fn () => $this->silent(fn () => $this->find(
7161+
foreach (\array_chunk($tenantIds, $this->maxQueryValues) as $idChunk) {
7162+
$fetched = $this->authorization->skip(fn () => $this->withTenant($tenant, fn () => $this->silent(fn () => $this->find(
7163+
$collection->getId(),
7164+
[Query::equal('$id', $idChunk), Query::limit(\count($idChunk))],
7165+
))));
7166+
foreach ($fetched as $doc) {
7167+
$existingDocs[$doc->getId()] = $doc;
7168+
}
7169+
}
7170+
}
7171+
} else {
7172+
foreach (\array_chunk($uniqueIds, $this->maxQueryValues) as $idChunk) {
7173+
$fetched = $this->authorization->skip(fn () => $this->silent(fn () => $this->find(
71397174
$collection->getId(),
7140-
[Query::equal('$id', $tenantIds), Query::limit(\count($tenantIds))],
7141-
))));
7175+
[Query::equal('$id', $idChunk), Query::limit(\count($idChunk))],
7176+
)));
71427177
foreach ($fetched as $doc) {
71437178
$existingDocs[$doc->getId()] = $doc;
71447179
}
71457180
}
7146-
} else {
7147-
$fetched = $this->authorization->skip(fn () => $this->silent(fn () => $this->find(
7148-
$collection->getId(),
7149-
[Query::equal('$id', $uniqueIds), Query::limit(\count($uniqueIds))],
7150-
)));
7151-
foreach ($fetched as $doc) {
7152-
$existingDocs[$doc->getId()] = $doc;
7153-
}
71547181
}
71557182
}
71567183

0 commit comments

Comments
 (0)