Skip to content

Commit 16683ce

Browse files
committed
Compare to existing doc permissions instead of refetching for all upserts
1 parent 261c025 commit 16683ce

2 files changed

Lines changed: 19 additions & 41 deletions

File tree

src/Database/Adapter/MariaDB.php

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,7 @@ public function createOrUpdateDocuments(
13581358
$documentTenants = [];
13591359

13601360
foreach ($documents as $document) {
1361+
$document = $document['new'];
13611362
$attributes = $document->getAttributes();
13621363
$attributes['_uid'] = $document->getId();
13631364
$attributes['_createdAt'] = $document->getCreatedAt();
@@ -1441,54 +1442,25 @@ public function createOrUpdateDocuments(
14411442
}
14421443

14431444
$stmt->execute();
1444-
1445-
// Fetch existing permissions in bulk after data updates
1446-
$sql = "
1447-
SELECT _document, _type, _permission
1448-
FROM {$this->getSQLTable($name . '_perms')}
1449-
WHERE _document IN (" . \implode(',', \array_map(fn ($index) => ":_key_{$index}", \array_keys($documents))) . ")
1450-
{$this->getTenantQuery($collection, tenantCount: \count($documentTenants))}
1451-
";
1452-
1453-
$stmt = $this->getPDO()->prepare($sql);
1454-
1455-
foreach ($documents as $index => $document) {
1456-
$stmt->bindValue(":_key_{$index}", $document->getId());
1457-
}
1458-
1459-
if ($this->sharedTables) {
1460-
foreach ($documentTenants as $index => $tenant) {
1461-
$stmt->bindValue(":_tenant_{$index}", $tenant);
1462-
}
1463-
}
1464-
1465-
$stmt->execute();
1466-
$existing = $stmt->fetchAll();
14671445
$stmt->closeCursor();
14681446

1469-
// Group permissions by document
1470-
$permissionsByDocument = [];
1471-
foreach ($existing as $row) {
1472-
$permissionsByDocument[$row['_document']][$row['_type']][] = $row['_permission'];
1473-
}
1474-
1475-
foreach ($documentIds as $id) {
1476-
foreach (Database::PERMISSIONS as $type) {
1477-
$permissionsByDocument[$id][$type] = $permissionsByDocument[$id][$type] ?? [];
1478-
}
1479-
}
1480-
14811447
$removeQueries = [];
14821448
$removeBindValues = [];
14831449
$addQueries = [];
14841450
$addBindValues = [];
14851451

14861452
foreach ($documents as $index => $document) {
1487-
$currentPermissions = $permissionsByDocument[$document->getId()] ?? [];
1453+
$old = $document['old'];
1454+
$document = $document['new'];
1455+
1456+
$current = [];
1457+
foreach (Database::PERMISSIONS as $type) {
1458+
$current[$type] = $old->getPermissionsByType($type) ?? [];
1459+
}
14881460

14891461
// Calculate removals
14901462
foreach (Database::PERMISSIONS as $type) {
1491-
$toRemove = \array_diff($currentPermissions[$type] ?? [], $document->getPermissionsByType($type));
1463+
$toRemove = \array_diff($current[$type] ?? [], $document->getPermissionsByType($type));
14921464
if (!empty($toRemove)) {
14931465
$removeQueries[] = "(
14941466
_document = :_uid_{$index}
@@ -1506,7 +1478,8 @@ public function createOrUpdateDocuments(
15061478

15071479
// Calculate additions
15081480
foreach (Database::PERMISSIONS as $type) {
1509-
$toAdd = \array_diff($document->getPermissionsByType($type), $currentPermissions[$type] ?? []);
1481+
$toAdd = \array_diff($document->getPermissionsByType($type), $current[$type] ?? []);
1482+
15101483
foreach ($toAdd as $i => $permission) {
15111484
$addQuery = "(:_uid_{$index}, '{$type}', :add_{$type}_{$index}_{$i}";
15121485

@@ -1552,8 +1525,8 @@ public function createOrUpdateDocuments(
15521525

15531526
$internalIds = $this->getInternalIds($collection, $documentIds, $documentTenants);
15541527
foreach ($documents as $document) {
1555-
if (isset($internalIds[$document->getId()])) {
1556-
$document['$internalId'] = $internalIds[$document->getId()];
1528+
if (isset($internalIds[$document['new']->getId()])) {
1529+
$document['new']['$internalId'] = $internalIds[$document['new']->getId()];
15571530
}
15581531
}
15591532
} catch (PDOException $e) {

src/Database/Database.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4742,7 +4742,10 @@ public function createOrUpdateDocumentsWithIncrease(
47424742
$document = $this->silent(fn () => $this->createDocumentRelationships($collection, $document));
47434743
}
47444744

4745-
$documents[$key] = $document;
4745+
$documents[$key] = [
4746+
'old' => $old,
4747+
'new' => $document
4748+
];
47464749
}
47474750

47484751
$documents = $this->withTransaction(function () use ($collection, $attribute, $documents, $batchSize) {
@@ -4763,6 +4766,8 @@ public function createOrUpdateDocumentsWithIncrease(
47634766
});
47644767

47654768
foreach ($documents as $key => $document) {
4769+
$document = $document['new'];
4770+
47664771
if ($this->resolveRelationships) {
47674772
$document = $this->silent(fn () => $this->populateDocumentRelationships($collection, $document));
47684773
}

0 commit comments

Comments
 (0)