address comments.

This commit is contained in:
Darshan
2025-06-18 12:00:19 +05:30
parent 16a8753509
commit d6bf748a07
7 changed files with 69 additions and 60 deletions
@@ -200,39 +200,31 @@ abstract class Action extends UtopiaAction
/**
* Resolves relationships in a document and attaches metadata.
*/
final protected function resolveDocumentRelations(Document $document, Document $collection, array &$context): bool
{
/* @type Document $database */
$database = $context['database'];
final protected function processDocument(
/* database */
Document $database,
Document $collection,
Document $document,
Database $dbForProject,
/* @type Database $dbForProject */
$dbForProject = $context['dbForProject'];
/* options */
array &$collectionsCache,
?int &$operations = null,
): bool {
/* remove `$collection` if needed */
$removeCollection = $context['removeCollection'] ?? false;
/* count operations inside loop */
$trackOperations = array_key_exists('trackOperations', $context);
if (!$trackOperations) {
$context['operations'] ??= 0;
} elseif ($document->isEmpty()) {
if ($operations !== null && $document->isEmpty()) {
return false;
}
$operations = &$context['operations'];
$collectionsCache = &$context['collectionsCache'];
if ($operations !== null) {
$operations++;
}
$operations++;
$collectionId = $collection->getId();
$document->removeAttribute('$collection');
$document->setAttribute('$databaseId', $database->getId());
$document->setAttribute('$collectionId', $collectionId);
if ($removeCollection) {
$document->removeAttribute('$collection');
}
$relationships = $collectionsCache[$collectionId] ??= \array_filter(
$collection->getAttribute('attributes', []),
fn ($attr) => $attr->getAttribute('type') === Database::VAR_RELATIONSHIP
@@ -243,7 +235,7 @@ abstract class Action extends UtopiaAction
$related = $document->getAttribute($key);
if (empty($related)) {
if (\in_array(\gettype($related), ['array', 'object'])) {
if (\in_array(\gettype($related), ['array', 'object']) && $operations !== null) {
$operations++;
}
continue;
@@ -266,14 +258,21 @@ abstract class Action extends UtopiaAction
);
}
foreach ($relations as $index => $relation) {
foreach ($relations as $relation) {
if ($relation instanceof Document) {
$relatedCollection = new Document([
'$id' => $relatedCollectionId,
'attributes' => $collectionsCache[$relatedCollectionId],
]);
$this->resolveDocumentRelations(document: $relation, collection: $relatedCollection, context: $context);
$this->processDocument(
database: $database,
collection: $relatedCollection,
document: $relation,
dbForProject: $dbForProject,
collectionsCache: $collectionsCache,
operations: $operations
);
}
}
@@ -374,12 +374,14 @@ class Create extends Action
->setContext($this->getCollectionsEventsContext(), $collection);
$collectionsCache = [];
$removeCollection = true;
$context = compact('database', 'dbForProject', 'collectionsCache', 'removeCollection');
foreach ($documents as $document) {
// Add $collectionId and $databaseId for all documents
$this->resolveDocumentRelations(document: $document, collection: $collection, context: $context);
$this->processDocument(
database: $database,
collection: $collection,
document: $document,
dbForProject: $dbForProject,
collectionsCache: $collectionsCache,
);
}
$queueForStatsUsage
@@ -115,10 +115,13 @@ class Delete extends Action
}
$collectionsCache = [];
$context = compact('database', 'dbForProject', 'collectionsCache');
// Add $collection and $databaseId for all documents
$this->resolveDocumentRelations(document: $document, collection: $collection, context: $context);
$this->processDocument(
database: $database,
collection: $collection,
document: $document,
dbForProject: $dbForProject,
collectionsCache: $collectionsCache,
);
$queueForStatsUsage
->addMetric(METRIC_DATABASES_OPERATIONS_WRITES, 1)
@@ -101,14 +101,14 @@ class Get extends Action
$operations = 0;
$collectionsCache = [];
$trackOperations = true;
$context = compact('database', 'dbForProject', 'operations', 'trackOperations', 'collectionsCache');
// Add $collectionId and $databaseId for all documents
$this->resolveDocumentRelations(document: $document, collection: $collection, context: $context);
// get updated from the context
$operations = $context['operations'];
$this->processDocument(
database: $database,
collection: $collection,
document: $document,
dbForProject: $dbForProject,
collectionsCache: $collectionsCache,
operations: $operations
);
$queueForStatsUsage
->addMetric(METRIC_DATABASES_OPERATIONS_READS, max($operations, 1))
@@ -248,10 +248,13 @@ class Update extends Action
}
$collectionsCache = [];
$context = compact('database', 'dbForProject', 'collectionsCache');
// Add $collectionId and $databaseId for all documents
$this->resolveDocumentRelations(document: $document, collection: $collection, context: $context);
$this->processDocument(
database: $database,
collection: $collection,
document: $document,
dbForProject: $dbForProject,
collectionsCache: $collectionsCache,
);
$response->dynamic($document, $this->getResponseModel());
@@ -237,10 +237,13 @@ class Upsert extends Action
$collectionsCache = [];
$document = $upserted[0];
$context = compact('database', 'dbForProject', 'collectionsCache');
// Add $collectionId and $databaseId for all documents
$this->resolveDocumentRelations(document: $document, collection: $collection, context: $context);
$this->processDocument(
database: $database,
collection: $collection,
document: $document,
dbForProject: $dbForProject,
collectionsCache: $collectionsCache,
);
$relationships = \array_map(
fn ($document) => $document->getAttribute('key'),
@@ -131,18 +131,17 @@ class XList extends Action
$operations = 0;
$collectionsCache = [];
$trackOperations = true;
$removeCollection = true;
$context = compact('database', 'dbForProject', 'operations', 'collectionsCache', 'removeCollection', 'trackOperations');
// Add $collectionId and $databaseId for all documents
foreach ($documents as $document) {
$this->resolveDocumentRelations(document: $document, collection: $collection, context: $context);
$this->processDocument(
database: $database,
collection: $collection,
document: $document,
dbForProject: $dbForProject,
collectionsCache: $collectionsCache,
operations: $operations,
);
}
// get updated from the context
$operations = $context['operations'];
$queueForStatsUsage
->addMetric(METRIC_DATABASES_OPERATIONS_READS, max($operations, 1))
->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), METRIC_DATABASE_ID_OPERATIONS_READS), $operations);