Skip to content

Commit 3df2993

Browse files
committed
delete chunks
1 parent 01da5dd commit 3df2993

File tree

4 files changed

+78
-63
lines changed

4 files changed

+78
-63
lines changed

src/Database/Adapter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,12 @@ abstract public function deleteDocument(string $collection, string $id): bool;
671671
* Delete Documents
672672
*
673673
* @param string $collection
674-
* @param array<string> $ids
674+
* @param array<string> $internalIds
675+
* @param array<string> $permissionIds
675676
*
676677
* @return int
677678
*/
678-
abstract public function deleteDocuments(string $collection, array $ids): int;
679+
abstract public function deleteDocuments(string $collection, array $internalIds, array $permissionIds): int;
679680

680681
/**
681682
* Find Documents

src/Database/Adapter/MariaDB.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,13 +1969,14 @@ public function deleteDocument(string $collection, string $id): bool
19691969
* Delete Documents
19701970
*
19711971
* @param string $collection
1972-
* @param array<string> $ids
1972+
* @param array<string> $internalIds
1973+
* @param array<string> $permissionIds
19731974
*
19741975
* @return int
19751976
*/
1976-
public function deleteDocuments(string $collection, array $ids): int
1977+
public function deleteDocuments(string $collection, array $internalIds, array $permissionIds): int
19771978
{
1978-
if (empty($ids)) {
1979+
if (empty($internalIds)) {
19791980
return 0;
19801981
}
19811982

@@ -1987,49 +1988,51 @@ public function deleteDocuments(string $collection, array $ids): int
19871988
$where[] = "_tenant = :_tenant";
19881989
}
19891990

1990-
$where[] = "_uid IN (" . \implode(', ', \array_map(fn ($index) => ":_id_{$index}", \array_keys($ids))) . ")";
1991+
$where[] = "_id IN (" . \implode(', ', \array_map(fn ($index) => ":_id_{$index}", \array_keys($internalIds))) . ")";
19911992

19921993
$sql = "DELETE FROM {$this->getSQLTable($name)} WHERE " . \implode(' AND ', $where);
19931994

19941995
$sql = $this->trigger(Database::EVENT_DOCUMENTS_DELETE, $sql);
19951996

19961997
$stmt = $this->getPDO()->prepare($sql);
19971998

1998-
foreach ($ids as $id => $value) {
1999+
foreach ($internalIds as $id => $value) {
19992000
$stmt->bindValue(":_id_{$id}", $value);
20002001
}
20012002

20022003
if ($this->sharedTables) {
20032004
$stmt->bindValue(':_tenant', $this->tenant);
20042005
}
20052006

2006-
$sql = "
2007+
if (!$stmt->execute()) {
2008+
throw new DatabaseException('Failed to delete documents');
2009+
}
2010+
2011+
if(!empty($permissionIds)){
2012+
$sql = "
20072013
DELETE FROM {$this->getSQLTable($name . '_perms')}
2008-
WHERE _document IN (" . \implode(', ', \array_map(fn ($index) => ":_id_{$index}", \array_keys($ids))) . ")
2014+
WHERE _document IN (" . \implode(', ', \array_map(fn ($index) => ":_id_{$index}", \array_keys($permissionIds))) . ")
20092015
";
20102016

2011-
if ($this->sharedTables) {
2012-
$sql .= ' AND _tenant = :_tenant';
2013-
}
2014-
2015-
$sql = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $sql);
2017+
if ($this->sharedTables) {
2018+
$sql .= ' AND _tenant = :_tenant';
2019+
}
20162020

2017-
$stmtPermissions = $this->getPDO()->prepare($sql);
2021+
$sql = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $sql);
20182022

2019-
foreach ($ids as $id => $value) {
2020-
$stmtPermissions->bindValue(":_id_{$id}", $value);
2021-
}
2023+
$stmtPermissions = $this->getPDO()->prepare($sql);
20222024

2023-
if ($this->sharedTables) {
2024-
$stmtPermissions->bindValue(':_tenant', $this->tenant);
2025-
}
2025+
foreach ($permissionIds as $id => $value) {
2026+
$stmtPermissions->bindValue(":_id_{$id}", $value);
2027+
}
20262028

2027-
if (!$stmt->execute()) {
2028-
throw new DatabaseException('Failed to delete documents');
2029-
}
2029+
if ($this->sharedTables) {
2030+
$stmtPermissions->bindValue(':_tenant', $this->tenant);
2031+
}
20302032

2031-
if (!$stmtPermissions->execute()) {
2032-
throw new DatabaseException('Failed to delete permissions');
2033+
if (!$stmtPermissions->execute()) {
2034+
throw new DatabaseException('Failed to delete permissions');
2035+
}
20332036
}
20342037
} catch (\Throwable $e) {
20352038
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);

src/Database/Adapter/Postgres.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,13 +1743,14 @@ public function deleteDocument(string $collection, string $id): bool
17431743
* Delete Documents
17441744
*
17451745
* @param string $collection
1746-
* @param array<string> $ids
1746+
* @param array<string> $internalIds
1747+
* @param array<string> $permissionIds
17471748
*
17481749
* @return int
17491750
*/
1750-
public function deleteDocuments(string $collection, array $ids): int
1751+
public function deleteDocuments(string $collection, array $internalIds, array $permissionIds): int
17511752
{
1752-
if (empty($ids)) {
1753+
if (empty($internalIds)) {
17531754
return 0;
17541755
}
17551756

@@ -1761,49 +1762,51 @@ public function deleteDocuments(string $collection, array $ids): int
17611762
$where[] = "_tenant = :_tenant";
17621763
}
17631764

1764-
$where[] = "_uid IN (" . \implode(', ', \array_map(fn ($index) => ":_id_{$index}", \array_keys($ids))) . ")";
1765+
$where[] = "_id IN (" . \implode(', ', \array_map(fn ($index) => ":_id_{$index}", \array_keys($internalIds))) . ")";
17651766

17661767
$sql = "DELETE FROM {$this->getSQLTable($name)} WHERE " . \implode(' AND ', $where);
17671768

17681769
$sql = $this->trigger(Database::EVENT_DOCUMENTS_DELETE, $sql);
17691770

17701771
$stmt = $this->getPDO()->prepare($sql);
17711772

1772-
foreach ($ids as $id => $value) {
1773+
foreach ($internalIds as $id => $value) {
17731774
$stmt->bindValue(":_id_{$id}", $value);
17741775
}
17751776

17761777
if ($this->sharedTables) {
17771778
$stmt->bindValue(':_tenant', $this->tenant);
17781779
}
17791780

1780-
$sql = "
1781+
if (!$stmt->execute()) {
1782+
throw new DatabaseException('Failed to delete documents');
1783+
}
1784+
1785+
if(!empty($permissionIds)){
1786+
$sql = "
17811787
DELETE FROM {$this->getSQLTable($name . '_perms')}
1782-
WHERE _document IN (" . \implode(', ', \array_map(fn ($id) => ":_id_{$id}", \array_keys($ids))) . ")
1788+
WHERE _document IN (" . \implode(', ', \array_map(fn ($index) => ":_id_{$index}", \array_keys($permissionIds))) . ")
17831789
";
17841790

1785-
if ($this->sharedTables) {
1786-
$sql .= ' AND _tenant = :_tenant';
1787-
}
1788-
1789-
$sql = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $sql);
1791+
if ($this->sharedTables) {
1792+
$sql .= ' AND _tenant = :_tenant';
1793+
}
17901794

1791-
$stmtPermissions = $this->getPDO()->prepare($sql);
1795+
$sql = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $sql);
17921796

1793-
foreach ($ids as $id => $value) {
1794-
$stmtPermissions->bindValue(":_id_{$id}", $value);
1795-
}
1797+
$stmtPermissions = $this->getPDO()->prepare($sql);
17961798

1797-
if ($this->sharedTables) {
1798-
$stmtPermissions->bindValue(':_tenant', $this->tenant);
1799-
}
1799+
foreach ($permissionIds as $id => $value) {
1800+
$stmtPermissions->bindValue(":_id_{$id}", $value);
1801+
}
18001802

1801-
if (!$stmt->execute()) {
1802-
throw new DatabaseException('Failed to delete documents');
1803-
}
1803+
if ($this->sharedTables) {
1804+
$stmtPermissions->bindValue(':_tenant', $this->tenant);
1805+
}
18041806

1805-
if (!$stmtPermissions->execute()) {
1806-
throw new DatabaseException('Failed to delete permissions');
1807+
if (!$stmtPermissions->execute()) {
1808+
throw new DatabaseException('Failed to delete permissions');
1809+
}
18071810
}
18081811
} catch (\Throwable $e) {
18091812
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);

src/Database/Database.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5437,6 +5437,9 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
54375437
$new[] = Query::cursorAfter($lastDocument);
54385438
}
54395439

5440+
/**
5441+
* @var array<Document> $affectedDocuments
5442+
*/
54405443
$affectedDocuments = $this->silent(fn () => $this->find(
54415444
$collection->getId(),
54425445
array_merge($new, $queries),
@@ -5447,9 +5450,15 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
54475450
break;
54485451
}
54495452

5450-
$documents = \array_merge($affectedDocuments, $documents);
5451-
5453+
$internalId = [];
5454+
$permIds = [];
54525455
foreach ($affectedDocuments as $document) {
5456+
$documents[] = $document;
5457+
$internalId[] = $document->getInternalId();
5458+
if(!empty($document->getPermissions())){
5459+
$permIds[] = $document->getId();
5460+
}
5461+
54535462
if ($this->resolveRelationships) {
54545463
$document = $this->silent(fn () => $this->deleteDocumentRelationships(
54555464
$collection,
@@ -5469,6 +5478,16 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
54695478
}
54705479
}
54715480

5481+
$this->withTransaction(function () use ($affectedDocuments, $collection, $batchSize, $skipAuth, $authorization, $internalId, $permIds) {
5482+
$getResults = fn () => $this->adapter->deleteDocuments(
5483+
$collection->getId(),
5484+
$internalId,
5485+
$permIds
5486+
);
5487+
5488+
$skipAuth ? $authorization->skip($getResults) : $getResults();
5489+
});
5490+
54725491
if (count($affectedDocuments) < $batchSize) {
54735492
break;
54745493
} elseif ($originalLimit && count($documents) == $originalLimit) {
@@ -5482,17 +5501,6 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
54825501
return [];
54835502
}
54845503

5485-
$this->withTransaction(function () use ($documents, $collection, $batchSize, $skipAuth, $authorization) {
5486-
foreach (\array_chunk($documents, $batchSize) as $chunk) {
5487-
$getResults = fn () => $this->adapter->deleteDocuments(
5488-
$collection->getId(),
5489-
array_map(fn ($document) => $document->getId(), $chunk)
5490-
);
5491-
5492-
$skipAuth ? $authorization->skip($getResults) : $getResults();
5493-
}
5494-
});
5495-
54965504
foreach ($documents as $document) {
54975505
$this->purgeCachedDocument($collection->getId(), $document->getId());
54985506
}

0 commit comments

Comments
 (0)