From 131e9658a5ef644e3d422f554d1ae0e5601d561c Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Wed, 19 Jul 2023 14:04:35 +0530 Subject: [PATCH 1/5] change to use findOne instead of iterating index array --- app/controllers/api/databases.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 4459322baf..eb37bea9f0 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2535,20 +2535,18 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') throw new Exception(Exception::COLLECTION_NOT_FOUND); } - $indexes = $collection->getAttribute('indexes'); - - // Search for index - $indexIndex = array_search($key, array_map(fn($idx) => $idx['key'], $indexes)); - - if ($indexIndex === false) { + $index = $dbForProject->findOne('indexes', [ + Query::equal('$id',[$database->getInternalId().'_'.$collection->getInternalId().'_'.$key]) + ]); + + if ($index->isEmpty()) { throw new Exception(Exception::INDEX_NOT_FOUND); } - $index = $indexes[$indexIndex]; $index->setAttribute('collectionId', $database->getInternalId() . '_' . $collectionId); $response->dynamic($index, Response::MODEL_INDEX); - }); + });; App::delete('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') ->alias('/v1/database/collections/:collectionId/indexes/:key', ['databaseId' => 'default']) From 8328dac86bb3c6d3010eedaa7063c8cab77af565 Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Wed, 19 Jul 2023 14:08:02 +0530 Subject: [PATCH 2/5] lint issues --- app/controllers/api/databases.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index eb37bea9f0..37694fcb65 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2536,9 +2536,9 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') } $index = $dbForProject->findOne('indexes', [ - Query::equal('$id',[$database->getInternalId().'_'.$collection->getInternalId().'_'.$key]) + Query::equal('$id', [$database->getInternalId() . '_' . $collection->getInternalId() . '_' . $key]) ]); - + if ($index->isEmpty()) { throw new Exception(Exception::INDEX_NOT_FOUND); } @@ -2546,7 +2546,8 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') $index->setAttribute('collectionId', $database->getInternalId() . '_' . $collectionId); $response->dynamic($index, Response::MODEL_INDEX); - });; + }); +; App::delete('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') ->alias('/v1/database/collections/:collectionId/indexes/:key', ['databaseId' => 'default']) From b700336d45c64a386939db34cc8db93119290c12 Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Thu, 20 Jul 2023 14:52:05 +0530 Subject: [PATCH 3/5] fix get index route to use find --- app/controllers/api/databases.php | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 37694fcb65..d16bd57f4c 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2524,28 +2524,13 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') ->inject('dbForProject') ->action(function (string $databaseId, string $collectionId, string $key, Response $response, Database $dbForProject) { - $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); + $index = $dbForProject->find('indexes', [Query::equal('collectionId', [$collectionId]), Query::equal('key', [$key]), Query::equal('databaseId', [$databaseId]), Query::limit(1)]); - if ($database->isEmpty()) { - throw new Exception(Exception::DATABASE_NOT_FOUND); - } - $collection = $dbForProject->getDocument('database_' . $database->getInternalId(), $collectionId); - - if ($collection->isEmpty()) { - throw new Exception(Exception::COLLECTION_NOT_FOUND); - } - - $index = $dbForProject->findOne('indexes', [ - Query::equal('$id', [$database->getInternalId() . '_' . $collection->getInternalId() . '_' . $key]) - ]); - - if ($index->isEmpty()) { + if (empty($index)) { throw new Exception(Exception::INDEX_NOT_FOUND); } - $index->setAttribute('collectionId', $database->getInternalId() . '_' . $collectionId); - - $response->dynamic($index, Response::MODEL_INDEX); + $response->dynamic($index[0], Response::MODEL_INDEX); }); ; From 7d412b4741899addb72f73fd34098609f023853b Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Mon, 24 Jul 2023 12:02:28 +0530 Subject: [PATCH 4/5] adds get db call and get collection call --- app/controllers/api/databases.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 6bfc08731f..4f2f42c849 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2524,7 +2524,18 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') ->inject('dbForProject') ->action(function (string $databaseId, string $collectionId, string $key, Response $response, Database $dbForProject) { - $index = $dbForProject->find('indexes', [Query::equal('collectionId', [$collectionId]), Query::equal('key', [$key]), Query::equal('databaseId', [$databaseId]), Query::limit(1)]); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); + + if ($database->isEmpty()) { + throw new Exception(Exception::DATABASE_NOT_FOUND); + } + $collection = $dbForProject->getDocument('database_' . $database->getInternalId(), $collectionId); + + if ($collection->isEmpty()) { + throw new Exception(Exception::COLLECTION_NOT_FOUND); + } + + $index = $dbForProject->find('indexes', [Query::equal('$id', [$database->getInternalId() . '_' . $collection->getInternalId() . '_' . $key]), Query::limit(1)]); if (empty($index)) { throw new Exception(Exception::INDEX_NOT_FOUND); @@ -2532,7 +2543,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') $response->dynamic($index[0], Response::MODEL_INDEX); }); -; + App::delete('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') ->alias('/v1/database/collections/:collectionId/indexes/:key', ['databaseId' => 'default']) From f7c02d9308039aed8c540c31dc9e68fd4bbc0ae3 Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Mon, 24 Jul 2023 23:41:49 +0530 Subject: [PATCH 5/5] removes extra db call and uses document find method --- app/controllers/api/databases.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 4f2f42c849..c815954747 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2535,13 +2535,12 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') throw new Exception(Exception::COLLECTION_NOT_FOUND); } - $index = $dbForProject->find('indexes', [Query::equal('$id', [$database->getInternalId() . '_' . $collection->getInternalId() . '_' . $key]), Query::limit(1)]); - + $index = $collection->find('key', $key, 'indexes'); if (empty($index)) { throw new Exception(Exception::INDEX_NOT_FOUND); } - $response->dynamic($index[0], Response::MODEL_INDEX); + $response->dynamic($index, Response::MODEL_INDEX); });