Skip to content

Commit 06a00ff

Browse files
committed
Optimize counts for catalogs (not working on first level catalog)
1 parent 1f532f8 commit 06a00ff

File tree

3 files changed

+77
-61
lines changed

3 files changed

+77
-61
lines changed

app/resto/core/RestoCollections.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ public function toArray()
259259
array(
260260
'rel' => 'items',
261261
'title' => 'All collections',
262-
'matched' => 0,
263262
'type' => RestoUtil::$contentTypes['geojson'],
264263
'href' => $this->context->core['baseUrl'] . RestoRouter::ROUTE_TO_STAC_SEARCH
265264
)

app/resto/core/RestoConstants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class RestoConstants
2020
// [IMPORTANT] Starting resto 7.x, default routes are defined in RestoRouter class
2121

2222
// resto version
23-
const VERSION = '9.0.0-RC17';
23+
const VERSION = '9.0.0-RC18';
2424

2525
/* ============================================================
2626
* NEVER EVER TOUCH THESE VALUES

app/resto/core/dbfunctions/CatalogsFunctions.php

Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,17 @@ public function getCatalogs($params, $baseUrl, $withChilds)
142142
try {
143143
$results = $this->dbDriver->pQuery('SELECT id, title, description, level, counters, owner, links, visibility, rtype, to_iso8601(created) as created FROM ' . $this->dbDriver->targetSchema . '.catalog' . ( empty($where) ? '' : ' WHERE ' . join(' AND ', $where) . ' ORDER BY id ASC'), $values);
144144
while ($result = pg_fetch_assoc($results)) {
145-
146-
/*
147-
* Recursively add child collection counters to catalog counters
148-
*/
149-
$result = $this->onTheFlyUpdateCountersWithCollection($result, $baseUrl);
150-
151145
$catalogs[] = CatalogsFunctions::format($result);
152146
}
153147
} catch (Exception $e) {
154148
RestoLogUtil::httpError(500, $e->getMessage());
155149
}
156150

157-
return $catalogs;
151+
/*
152+
* Recursively add child collection counters to catalog counters
153+
*/
154+
return $this->onTheFlyUpdateCountersWithCollection($catalogs, $params['id'] ?? null, $baseUrl);
155+
158156
}
159157

160158
/**
@@ -591,7 +589,7 @@ public function getSummaries($types, $baseUrl)
591589
'collection' => array(
592590
array(
593591
'const' => $_collectionId,
594-
'count' => $catalogs[$i]['counters']['collections'][$_collectionId]
592+
'count' => $catalogs[$i]['counters']['collections'][$_collectionId] ?? 0
595593
)
596594
)
597595
);
@@ -789,72 +787,91 @@ private function getCleanLinks($catalog, $userid, $context) {
789787
* Return an update links array and counters object by adding child collection counters
790788
* to the input catalog
791789
*
792-
* @param Object $catalog
790+
* @param array $catalogs
791+
* @param string $catalogId
792+
* @param string $baseUrl
793793
*/
794-
private function onTheFlyUpdateCountersWithCollection($catalog, $baseUrl)
794+
private function onTheFlyUpdateCountersWithCollection($catalogs, $catalogId, $baseUrl)
795795
{
796796

797-
$counters = json_decode($catalog['counters'], true);
798-
$originalLinks = json_decode($catalog['links'], true);
799-
800797
$collections = array();
801-
802-
$results = $this->dbDriver->pQuery('SELECT id, counters, links FROM ' . $this->dbDriver->targetSchema . '.catalog WHERE lower(id) = lower($1) OR lower(id) LIKE lower($2) ORDER BY id ASC', array(
803-
$catalog['id'],
804-
$catalog['id'] . '/%'
805-
));
806-
while ($result = pg_fetch_assoc($results)) {
807798

808-
if ($catalog['id'] !== $result['id']) {
809-
$catalogCounters = json_decode($result['counters'], true);
810-
$counters['total'] = $counters['total'] + $catalogCounters['total'];
799+
// First get collections counts
800+
try {
801+
$results = $this->dbDriver->query('SELECT id, counters, title, description FROM ' . $this->dbDriver->targetSchema . '.catalog WHERE lower(id) LIKE lower(\'collections/%\')');
802+
while ($result = pg_fetch_assoc($results)) {
803+
$collections[$result['id']] = array(
804+
'counters' => json_decode($result['counters'], true),
805+
'title' => $result['title'] ?? null,
806+
'description' => $result['description'] ?? null,
807+
808+
);
811809
}
810+
} catch (Exception $e) {
811+
RestoLogUtil::httpError(500, $e->getMessage());
812+
}
813+
814+
$catalogsUpdated = array();
815+
for ($i = 0, $ii = count($catalogs); $i < $ii; $i++)
816+
{
817+
$catalogsUpdated[] = $this->computeCountersSum($catalogs[$i], $catalogs, $collections);
818+
}
819+
820+
return $catalogsUpdated;
821+
}
822+
823+
/**
824+
* Calculate the total counter for a given path and its children
825+
*
826+
* @param array $parentCatalog
827+
* @param array $catalogs
828+
* @param array $collections
829+
*/
830+
private function computeCountersSum($parentCatalog, $catalogs, $collections) {
831+
832+
$parentCatalogId = $parentCatalog['id'] . '/';
833+
834+
// Iterate over all catalog entries
835+
foreach ($catalogs as $catalog) {
812836

813-
// Process collection
814-
if ( isset($result['links']) ) {
815-
$links = json_decode($result['links'], true);
816-
for ($i = 0, $ii = count($links); $i < $ii; $i++) {
817-
if ($links[$i]['rel'] === 'child') {
818-
$exploded = explode('/', substr($links[$i]['href'], strlen($baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS) + 1));
819-
if (count($exploded) === 1) {
820-
$collections[$exploded[0]] = $links[$i]['href'];
821-
}
822-
}
823-
}
837+
// Check if the catalog's path starts with the parent path
838+
if ( !str_starts_with($catalog['id'], $parentCatalogId) ) {
839+
continue;
824840
}
825-
}
826841

827-
// Now process links
828-
foreach (array_keys($collections) as $collectionId) {
829-
$results = $this->dbDriver->pQuery('SELECT title, description, counters FROM ' . $this->dbDriver->targetSchema . '.catalog WHERE lower(id) = lower($1) ORDER BY id ASC', array(
830-
'collections/' . $collectionId
831-
));
832-
while ($result = pg_fetch_assoc($results)) {
833-
$collectionCounters = json_decode($result['counters'], true);
834-
$counters['total'] = $counters['total'] + $collectionCounters['total'];
835-
$counters['collections'][$collectionId] = $collectionCounters['total'];
836-
for ($i = 0, $ii = count($originalLinks); $i < $ii; $i++) {
837-
if ($originalLinks[$i]['rel'] === 'child') {
838-
$exploded = explode('/', substr($originalLinks[$i]['href'], strlen($baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS) + 1));
839-
if (count($exploded) === 1 && $exploded[0] === $collectionId) {
840-
$originalLinks[$i]['matched'] = $collectionCounters['total'];
841-
if ( isset($result['title']) ) {
842-
$originalLinks[$i]['title'] = $result['title'];
843-
}
844-
if ( isset($result['description']) ) {
845-
$originalLinks[$i]['description'] = $result['description'];
842+
$parentCatalog['counters']['total'] = $parentCatalog['counters']['total'] + $catalog['counters']['total'];
843+
844+
// Process collection
845+
if ( isset($catalog['links']) ) {
846+
for ($i = 0, $ii = count($catalog['links']); $i < $ii; $i++) {
847+
if ($catalog['links'][$i]['rel'] === 'child') {
848+
$exploded = explode('/', substr($catalog['links'][$i]['href'], strlen($baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS) + 1));
849+
if ( count($exploded) === 1 && isset($collections[$exploded[0]]) ) {
850+
$total = $total + $collections[$exploded[0]]['counters']['total'];
851+
$parentCatalog['counters']['collections'][$exploded[0]] = $collections[$exploded[0]]['counters']['total'];
852+
853+
for ($j = 0, $jj = count($parentCatalog['links']); $j < $jj; $j++) {
854+
if ($parentCatalog['links'][$j]['rel'] === 'child') {
855+
$exploded2 = explode('/', substr($parentCatalog['links'][$j]['href'], strlen($baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS) + 1));
856+
if (count($exploded2) === 1 && $exploded2[0] === $exploded[0]) {
857+
$parentCatalog['links'][$j]['matched'] = $collectionCounters[$exploded[0]];
858+
if ( isset($collections[$exploded[0]]['title']) ) {
859+
$parentCatalog['links'][$i]['title'] = $collections[$exploded[0]]['title'];
860+
}
861+
if ( isset($collections[$exploded[0]]['description']) ) {
862+
$parentCatalog['links'][$i]['description'] = $collections[$exploded[0]]['description'];
863+
}
864+
}
865+
}
846866
}
847867
}
848868
}
849-
}
869+
}
850870
}
851-
}
852871

853-
$catalog['counters'] = json_encode($counters, JSON_UNESCAPED_SLASHES);
854-
$catalog['links'] = json_encode($originalLinks, JSON_UNESCAPED_SLASHES);
855-
856-
return $catalog;
872+
}
857873

874+
return $parentCatalog;
858875
}
859876

860877
}

0 commit comments

Comments
 (0)