Use return value for write ops count

This commit is contained in:
Jake Barnby 2025-10-10 15:51:26 +13:00
parent 276b135799
commit cdac840071
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C

View file

@ -167,8 +167,10 @@ class Update extends Action
} }
} }
$totalOperations++; if (!\in_array($action, ['bulkCreate', 'bulkUpdate', 'bulkUpsert', 'bulkDelete'])) {
$databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + 1; $totalOperations++;
$databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + 1;
}
if ($data instanceof Document) { if ($data instanceof Document) {
$data = $data->getArrayCopy(); $data = $data->getArrayCopy();
@ -194,16 +196,24 @@ class Update extends Action
$this->handleDecrementOperation($dbForProject, $collectionId, $documentId, $data, $createdAt, $state); $this->handleDecrementOperation($dbForProject, $collectionId, $documentId, $data, $createdAt, $state);
break; break;
case 'bulkCreate': case 'bulkCreate':
$this->handleBulkCreateOperation($dbForProject, $collectionId, $data, $createdAt, $state); $count = $this->handleBulkCreateOperation($dbForProject, $collectionId, $data, $createdAt, $state);
$totalOperations += $count;
$databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + $count;
break; break;
case 'bulkUpdate': case 'bulkUpdate':
$this->handleBulkUpdateOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state); $count = $this->handleBulkUpdateOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state);
$totalOperations += $count;
$databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + $count;
break; break;
case 'bulkUpsert': case 'bulkUpsert':
$this->handleBulkUpsertOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state); $count = $this->handleBulkUpsertOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state);
$totalOperations += $count;
$databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + $count;
break; break;
case 'bulkDelete': case 'bulkDelete':
$this->handleBulkDeleteOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state); $count = $this->handleBulkDeleteOperation($dbForProject, $transactionState, $collectionId, $data, $createdAt, $state);
$totalOperations += $count;
$databaseOperations[$databaseInternalId] = ($databaseOperations[$databaseInternalId] ?? 0) + $count;
break; break;
} }
} }
@ -645,7 +655,7 @@ class Update extends Action
* @param array $data * @param array $data
* @param \DateTime $createdAt * @param \DateTime $createdAt
* @param array &$state * @param array &$state
* @return void * @return int Number of documents created
* @throws \Utopia\Database\Exception * @throws \Utopia\Database\Exception
*/ */
private function handleBulkCreateOperation( private function handleBulkCreateOperation(
@ -654,13 +664,14 @@ class Update extends Action
array $data, array $data,
\DateTime $createdAt, \DateTime $createdAt,
array &$state array &$state
): void { ): int {
$dbForProject->withRequestTimestamp($createdAt, function () use ($dbForProject, $collectionId, $data, &$state) { $count = 0;
$dbForProject->withRequestTimestamp($createdAt, function () use ($dbForProject, $collectionId, $data, &$state, &$count) {
$documents = \array_map(function ($doc) { $documents = \array_map(function ($doc) {
return $doc instanceof Document ? $doc : new Document($doc); return $doc instanceof Document ? $doc : new Document($doc);
}, $data); }, $data);
$dbForProject->createDocuments( $count = $dbForProject->createDocuments(
$collectionId, $collectionId,
$documents, $documents,
onNext: function (Document $document) use (&$state, $collectionId) { onNext: function (Document $document) use (&$state, $collectionId) {
@ -668,6 +679,7 @@ class Update extends Action
} }
); );
}); });
return $count;
} }
/** /**
@ -679,7 +691,7 @@ class Update extends Action
* @param array $data * @param array $data
* @param \DateTime $createdAt * @param \DateTime $createdAt
* @param array &$state * @param array &$state
* @return void * @return int Number of documents updated
* @throws \Utopia\Database\Exception * @throws \Utopia\Database\Exception
* @throws \Utopia\Database\Exception\Query * @throws \Utopia\Database\Exception\Query
* @throws ConflictException * @throws ConflictException
@ -691,7 +703,7 @@ class Update extends Action
array $data, array $data,
\DateTime $createdAt, \DateTime $createdAt,
array &$state array &$state
): void { ): int {
$queries = Query::parseQueries($data['queries'] ?? []); $queries = Query::parseQueries($data['queries'] ?? []);
$updateData = new Document($data['data']); $updateData = new Document($data['data']);
@ -701,7 +713,7 @@ class Update extends Action
// Clone the document before passing to updateDocuments to prevent mutation // Clone the document before passing to updateDocuments to prevent mutation
// The database layer mutates the input document, which would corrupt transaction state // The database layer mutates the input document, which would corrupt transaction state
$dbForProject->updateDocuments( $count = $dbForProject->updateDocuments(
$collectionId, $collectionId,
clone $updateData, clone $updateData,
$queries, $queries,
@ -739,6 +751,8 @@ class Update extends Action
); );
} }
} }
return $count;
} }
/** /**
@ -750,7 +764,7 @@ class Update extends Action
* @param array $data * @param array $data
* @param \DateTime $createdAt * @param \DateTime $createdAt
* @param array &$state * @param array &$state
* @return void * @return int Number of documents upserted
* @throws ConflictException * @throws ConflictException
* @throws \Utopia\Database\Exception * @throws \Utopia\Database\Exception
*/ */
@ -761,14 +775,14 @@ class Update extends Action
array $data, array $data,
\DateTime $createdAt, \DateTime $createdAt,
array &$state array &$state
): void { ): int {
$documents = \array_map(function ($doc) { $documents = \array_map(function ($doc) {
return $doc instanceof Document ? $doc : new Document($doc); return $doc instanceof Document ? $doc : new Document($doc);
}, $data); }, $data);
$mergedDocuments = $transactionState->applyBulkUpsertToState($collectionId, $documents, $state); $mergedDocuments = $transactionState->applyBulkUpsertToState($collectionId, $documents, $state);
$dbForProject->upsertDocuments( $count = $dbForProject->upsertDocuments(
$collectionId, $collectionId,
$mergedDocuments, $mergedDocuments,
onNext: function (Document $upserted, ?Document $old) use (&$state, $collectionId, $createdAt) { onNext: function (Document $upserted, ?Document $old) use (&$state, $collectionId, $createdAt) {
@ -786,6 +800,8 @@ class Update extends Action
$state[$collectionId][$upserted->getId()] = $upserted; $state[$collectionId][$upserted->getId()] = $upserted;
} }
); );
return $count;
} }
/** /**
@ -797,7 +813,7 @@ class Update extends Action
* @param array $data * @param array $data
* @param \DateTime $createdAt * @param \DateTime $createdAt
* @param array &$state * @param array &$state
* @return void * @return int Number of documents deleted
* @throws \Utopia\Database\Exception\Query * @throws \Utopia\Database\Exception\Query
* @throws ConflictException * @throws ConflictException
* @throws \Utopia\Database\Exception * @throws \Utopia\Database\Exception
@ -809,10 +825,10 @@ class Update extends Action
array $data, array $data,
\DateTime $createdAt, \DateTime $createdAt,
array &$state array &$state
): void { ): int {
$queries = Query::parseQueries($data['queries'] ?? []); $queries = Query::parseQueries($data['queries'] ?? []);
$dbForProject->deleteDocuments( $count = $dbForProject->deleteDocuments(
$collectionId, $collectionId,
$queries, $queries,
onNext: function (Document $deleted, Document $old) use (&$state, $collectionId, $createdAt) { onNext: function (Document $deleted, Document $old) use (&$state, $collectionId, $createdAt) {
@ -832,5 +848,7 @@ class Update extends Action
); );
$transactionState->applyBulkDeleteToState($collectionId, $queries, $state); $transactionState->applyBulkDeleteToState($collectionId, $queries, $state);
return $count;
} }
} }