Skip to content

Commit

Permalink
Merge pull request #485 from utopia-php/feat-change-bulk-signatures
Browse files Browse the repository at this point in the history
Make bulk methods always return array of documents
  • Loading branch information
abnegate authored Nov 21, 2024
2 parents 1f7cbd4 + 4590937 commit b5bd17e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 102 deletions.
73 changes: 31 additions & 42 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3970,15 +3970,15 @@ public function updateDocument(string $collection, string $id, Document $documen
* @param array<Query> $queries
* @param int $batchSize
*
* @return int
* @return array<Document>
*
* @throws AuthorizationException
* @throws DatabaseException
*/
public function updateDocuments(string $collection, Document $updates, array $queries = [], int $batchSize = self::INSERT_BATCH_SIZE): int
public function updateDocuments(string $collection, Document $updates, array $queries = [], int $batchSize = self::INSERT_BATCH_SIZE): array
{
if ($updates->isEmpty()) {
return 0;
return [];
}

$collection = $this->silent(fn () => $this->getCollection($collection));
Expand Down Expand Up @@ -4033,11 +4033,9 @@ public function updateDocuments(string $collection, Document $updates, array $qu
throw new StructureException($validator->getDescription());
}

$affectedDocumentIds = [];

$affected = $this->withTransaction(function () use ($collection, $queries, $batchSize, $updates, $limit, $cursor, $affectedDocumentIds) {
$documents = $this->withTransaction(function () use ($collection, $queries, $batchSize, $updates, $limit, $cursor) {
$lastDocument = null;
$totalModified = 0;
$documents = [];

$documentSecurity = $collection->getAttribute('documentSecurity', false);

Expand Down Expand Up @@ -4075,12 +4073,10 @@ public function updateDocuments(string $collection, Document $updates, array $qu

foreach ($affectedDocuments as $document) {
if ($this->resolveRelationships) {
$newDocument = array_merge($document->getArrayCopy(), $updates->getArrayCopy());

$this->silent(fn () => $this->updateDocumentRelationships($collection, $document, new Document($newDocument)));
$newDocument = new Document(array_merge($document->getArrayCopy(), $updates->getArrayCopy()));
$this->silent(fn () => $this->updateDocumentRelationships($collection, $document, $newDocument));
$documents[] = $newDocument;
}

$affectedDocumentIds[] = $document->getId();
}

$getResults = fn () => $this->adapter->updateDocuments(
Expand All @@ -4089,33 +4085,27 @@ public function updateDocuments(string $collection, Document $updates, array $qu
$affectedDocuments
);

$result = $skipAuth ? $authorization->skip($getResults) : $getResults();

$totalModified += $result;
$skipAuth ? $authorization->skip($getResults) : $getResults();

if (count($affectedDocuments) < $batchSize) {
break;
} elseif ($originalLimit && count($affectedDocumentIds) == $originalLimit) {
} elseif ($originalLimit && count($documents) == $originalLimit) {
break;
}

$lastDocument = end($affectedDocuments);
}

$this->trigger(self::EVENT_DOCUMENTS_UPDATE, new Document([
'$collection' => $collection->getId(),
'modified' => $affectedDocumentIds,
]));

foreach ($affectedDocumentIds as $id) {
$this->purgeRelatedDocuments($collection, $id);
$this->purgeCachedDocument($collection->getId(), $id);
foreach ($documents as $document) {
$this->purgeRelatedDocuments($collection, $document->getId());
$this->purgeCachedDocument($collection->getId(), $document->getId());
$this->trigger(self::EVENT_DOCUMENT_UPDATE, $document);
}

return $totalModified;
return $documents;
});

return $affected;
return $documents;
}

/**
Expand Down Expand Up @@ -5150,13 +5140,13 @@ private function deleteCascade(Document $collection, Document $relatedCollection
* @param array<Query> $queries
* @param int $batchSize
*
* @return int
* @return array<Document>
*
* @throws AuthorizationException
* @throws DatabaseException
* @throws RestrictedException
*/
public function deleteDocuments(string $collection, array $queries = [], int $batchSize = self::DELETE_BATCH_SIZE): int
public function deleteDocuments(string $collection, array $queries = [], int $batchSize = self::DELETE_BATCH_SIZE): array
{
if ($this->adapter->getSharedTables() && empty($this->adapter->getTenant())) {
throw new DatabaseException('Missing tenant. Tenant must be set when table sharing is enabled.');
Expand Down Expand Up @@ -5193,12 +5183,11 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
throw new DatabaseException("cursor Document must be from the same Collection.");
}

$affectedDocumentIds = [];

$deleted = $this->withTransaction(function () use ($collection, $queries, $batchSize, $affectedDocumentIds, $limit, $cursor) {
$documents = $this->withTransaction(function () use ($collection, $queries, $batchSize, $limit, $cursor) {
$documentSecurity = $collection->getAttribute('documentSecurity', false);
$authorization = new Authorization(self::PERMISSION_DELETE);
$skipAuth = $authorization->isValid($collection->getDelete());
$documents = [];

if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
throw new AuthorizationException($authorization->getDescription());
Expand Down Expand Up @@ -5228,7 +5217,7 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
break;
}

$affectedDocumentIds = array_merge($affectedDocumentIds, array_map(fn ($document) => $document->getId(), $affectedDocuments));
$documents = array_merge($affectedDocuments, $documents);

foreach ($affectedDocuments as $document) {
// Delete Relationships
Expand All @@ -5242,27 +5231,27 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba

if (count($affectedDocuments) < $batchSize) {
break;
} elseif ($originalLimit && count($affectedDocumentIds) == $originalLimit) {
} elseif ($originalLimit && count($documents) == $originalLimit) {
break;
}

$lastDocument = end($affectedDocuments);
}

if (empty($affectedDocumentIds)) {
return 0;
if (empty($documents)) {
return [];
}

$this->trigger(self::EVENT_DOCUMENTS_DELETE, new Document([
'$collection' => $collection->getId(),
'modified' => $affectedDocumentIds
]));
foreach ($documents as $document) {
$this->trigger(self::EVENT_DOCUMENT_DELETE, $document);
}

$this->adapter->deleteDocuments($collection->getId(), array_map(fn ($document) => $document->getId(), $documents));

// Mass delete using adapter with query
return $this->adapter->deleteDocuments($collection->getId(), $affectedDocumentIds);
return $documents;
});

return $deleted;
return $documents;
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/Database/Mirror.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,19 +662,19 @@ public function updateDocuments(
Document $updates,
array $queries = [],
int $batchSize = self::INSERT_BATCH_SIZE
): int {
$count = $this->source->updateDocuments($collection, $updates, $queries, $batchSize);
): array {
$documents = $this->source->updateDocuments($collection, $updates, $queries, $batchSize);

if (
\in_array($collection, self::SOURCE_ONLY_COLLECTIONS)
|| $this->destination === null
) {
return $count;
return $documents;
}

$upgrade = $this->silent(fn () => $this->getUpgradeStatus($collection));
if ($upgrade === null || $upgrade->getAttribute('status', '') !== 'upgraded') {
return $count;
return $documents;
}

try {
Expand Down Expand Up @@ -707,7 +707,7 @@ public function updateDocuments(
$this->logError('updateDocuments', $err);
}

return $count;
return $documents;
}

public function deleteDocument(string $collection, string $id): bool
Expand Down Expand Up @@ -753,20 +753,20 @@ public function deleteDocument(string $collection, string $id): bool
return $result;
}

public function deleteDocuments(string $collection, array $queries = [], int $batchSize = self::DELETE_BATCH_SIZE): int
public function deleteDocuments(string $collection, array $queries = [], int $batchSize = self::DELETE_BATCH_SIZE): array
{
$count = $this->source->deleteDocuments($collection, $queries, $batchSize);
$documents = $this->source->deleteDocuments($collection, $queries, $batchSize);

if (
\in_array($collection, self::SOURCE_ONLY_COLLECTIONS)
|| $this->destination === null
) {
return $count;
return $documents;
}

$upgrade = $this->silent(fn () => $this->getUpgradeStatus($collection));
if ($upgrade === null || $upgrade->getAttribute('status', '') !== 'upgraded') {
return $count;
return $documents;
}

try {
Expand All @@ -793,7 +793,7 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
$this->logError('deleteDocuments', $err);
}

return $count;
return $documents;
}

public function updateAttributeRequired(string $collection, string $id, bool $required): Document
Expand Down
Loading

0 comments on commit b5bd17e

Please sign in to comment.