From 33b971a2c166c7ba7978e384b1b51e28f9546934 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 1 Apr 2026 18:56:14 +1300 Subject: [PATCH] (fix): pre-compute collection mapping in getDatabasesDB, remove all queries from Metadata MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the collection ID mapping query from the Metadata decorator to getDatabasesDB where it runs once during resource init. The mapping includes both relative keys (collection_N) and full keys (database_M_collection_N) for dedicated mode compatibility. The Metadata decorator is now purely stateless — no database queries, no dbForProject dependency. It only uses the pre-set map from setCollectionId(). Co-Authored-By: Claude Opus 4.6 (1M context) --- app/init/resources.php | 40 ++++++- .../Utopia/Database/Hooks/Metadata.php | 103 +----------------- 2 files changed, 36 insertions(+), 107 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index 664a604f63..5a9d4e6d26 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -769,6 +769,39 @@ Http::setResource('getDatabasesDB', function (Group $pools, Database $dbForProje $databaseIdCollectionIdDocumentsMetric = $databaseType . '.' . $databaseIdCollectionIdDocumentsMetric; } + // Pre-compute collection ID mapping (internal name → user-facing ID) + $collectionIdMap = []; + try { + $databaseKey = 'database_' . $originalDatabase->getSequence(); + $dbPrefix = $databaseKey . '_'; + $collections = $authorization->skip( + fn () => $dbForProject->silent( + fn () => $dbForProject->find($databaseKey, [ + \Utopia\Database\Query::select(['$id', '$sequence']), + \Utopia\Database\Query::limit(5000), + ]) + ) + ); + foreach ($collections as $col) { + $seq = $col->getSequence(); + if ($seq !== null) { + $key = 'collection_' . $seq; + $collectionIdMap[$key] = $col->getId(); + $collectionIdMap[$dbPrefix . $key] = $col->getId(); + } + } + } catch (\Throwable) { + // Database may not have collections yet + } + + $metadata = new Metadata( + database: $originalDatabase, + context: $context, + ); + foreach ($collectionIdMap as $internal => $external) { + $metadata->setCollectionId($internal, $external); + } + $database ->addHook(new DocumentUsage( $usage, @@ -778,12 +811,7 @@ Http::setResource('getDatabasesDB', function (Group $pools, Database $dbForProje )) ->addHook(new Permissions()) ->addHook(new Relationships($database)) - ->addHook(new Metadata( - database: $originalDatabase, - context: $context, - dbForProject: $dbForProject, - authorization: $authorization, - )); + ->addHook($metadata); if ($database->getSharedTables() && ($database->getTenant() !== null)) { $database->addHook(new Tenancy($database->getTenant())); diff --git a/src/Appwrite/Utopia/Database/Hooks/Metadata.php b/src/Appwrite/Utopia/Database/Hooks/Metadata.php index de486ebf83..b901d718f6 100644 --- a/src/Appwrite/Utopia/Database/Hooks/Metadata.php +++ b/src/Appwrite/Utopia/Database/Hooks/Metadata.php @@ -6,14 +6,11 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Event; use Utopia\Database\Hook\Decorator; -use Utopia\Database\Validator\Authorization; use Utopia\Query\Schema\ColumnType; /** * Stamps database/collection metadata onto every document returned from the database, * and recursively decorates nested relationship documents. - * - * Replaces the manual processDocument() loop that previously ran in every endpoint. */ class Metadata implements Decorator { @@ -23,15 +20,11 @@ class Metadata implements Decorator /** @var array internal collection name → user-facing collection ID */ private array $collectionIdMap = []; - private bool $mapLoaded = false; - private int $operations = 0; public function __construct( private Document $database, private string $context = 'collection', - private ?Database $dbForProject = null, - private ?Authorization $authorization = null, ) { } @@ -51,7 +44,7 @@ class Metadata implements Decorator $this->operations++; - $collectionId = $this->resolveCollectionId($collection->getId()); + $collectionId = $this->collectionIdMap[$collection->getId()] ?? $collection->getId(); $document->setAttribute('$databaseId', $this->database->getId()); $document->setAttribute('$' . $this->context . 'Id', $collectionId); @@ -70,68 +63,6 @@ class Metadata implements Decorator $this->operations = 0; } - /** - * Resolve internal collection name to user-facing ID. - * Loads mapping lazily on first miss, caches for lifetime. - */ - private function resolveCollectionId(string $internalName): string - { - if (isset($this->collectionIdMap[$internalName])) { - return $this->collectionIdMap[$internalName]; - } - - // Strip database prefix if present (e.g., 'database_2_collection_15' → 'collection_15') - $databasePrefix = 'database_' . $this->database->getSequence() . '_'; - $relativeName = \str_starts_with($internalName, $databasePrefix) - ? \substr($internalName, \strlen($databasePrefix)) - : $internalName; - - if (isset($this->collectionIdMap[$relativeName])) { - $this->collectionIdMap[$internalName] = $this->collectionIdMap[$relativeName]; - return $this->collectionIdMap[$relativeName]; - } - - if (!$this->mapLoaded && $this->dbForProject !== null && $this->authorization !== null) { - $this->mapLoaded = true; - $this->loadCollectionMap(); - if (isset($this->collectionIdMap[$relativeName])) { - $this->collectionIdMap[$internalName] = $this->collectionIdMap[$relativeName]; - return $this->collectionIdMap[$relativeName]; - } - } - - return $internalName; - } - - /** - * Load all collection ID mappings in one query. - * Uses skipValidation to avoid triggering hooks/events. - */ - private function loadCollectionMap(): void - { - $databaseKey = 'database_' . $this->database->getSequence(); - - try { - $collections = $this->authorization->skip( - fn () => $this->dbForProject->silent( - fn () => $this->dbForProject->find($databaseKey, [ - \Utopia\Database\Query::select(['$id', '$sequence']), - \Utopia\Database\Query::limit(5000), - ]) - ) - ); - - foreach ($collections as $col) { - $seq = $col->getSequence(); - if ($seq !== null) { - $this->collectionIdMap['collection_' . $seq] = $col->getId(); - } - } - } catch (\Throwable) { - // Silently fail — fall back to internal names - } - } - private function decorateRelationships(Document $collection, Document $document, int $depth = 0): void { if ($depth >= Database::RELATION_MAX_DEPTH) { @@ -156,16 +87,13 @@ class Metadata implements Decorator $options = $relationship->getAttribute('options', []); $relatedInternalName = (\is_array($options) ? ($options['relatedCollection'] ?? null) : null) ?? $relationship->getAttribute('relatedCollection'); - $relatedExternalId = $this->resolveCollectionId($relatedInternalName); + $relatedExternalId = $this->collectionIdMap[$relatedInternalName] ?? $relatedInternalName; foreach ($relations as $relation) { if ($relation instanceof Document) { $this->operations++; $relation->setAttribute('$databaseId', $this->database->getId()); $relation->setAttribute('$' . $this->context . 'Id', $relatedExternalId); - - $relatedCollection = $this->getRelatedCollection($relatedInternalName); - $this->decorateRelationships($relatedCollection, $relation, $depth + 1); } } } @@ -185,31 +113,4 @@ class Metadata implements Decorator return $this->relationshipCache[$collectionId]; } - - private function getRelatedCollection(string $internalName): Document - { - if (!isset($this->relationshipCache[$internalName]) && $this->dbForProject !== null && $this->authorization !== null) { - $relatedExternalId = $this->resolveCollectionId($internalName); - try { - $relatedCollection = $this->authorization->skip( - fn () => $this->dbForProject->getDocument( - 'database_' . $this->database->getSequence(), - $relatedExternalId - ) - ); - - $this->relationshipCache[$internalName] = \array_filter( - $relatedCollection->getAttribute('attributes', []), - fn ($attr) => $attr->getAttribute('type') === ColumnType::Relationship->value - ); - } catch (\Throwable) { - $this->relationshipCache[$internalName] = []; - } - } - - return new Document([ - '$id' => $internalName, - 'attributes' => $this->relationshipCache[$internalName] ?? [], - ]); - } }