Skip to content

Commit

Permalink
Merge pull request #1132 from oat-sa/feat/ADF-1686/improve-resources-…
Browse files Browse the repository at this point in the history
…query-performance

feat: do a single query to get all resource ids instead of a recursiv…
  • Loading branch information
gabrielfs7 authored Oct 4, 2024
2 parents e9cd24f + 6c2c76a commit e5480a8
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions core/kernel/persistence/smoothsql/class.Class.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ public function getInstances(core_kernel_classes_Class $resource, $recursive = f
{
$returnValue = [];

if (empty($params)) {
$resourceIds = $this->getInstanceUris($resource->getUri(), $recursive);

foreach ($resourceIds as $resourceId) {
$returnValue[$resourceId] = $this->getModel()->getResource($resourceId);
}

return $returnValue;
}

$params = array_merge($params, ['like' => false, 'recursive' => $recursive]);

$query = $this->getFilteredQuery($resource, [], $params);
Expand Down Expand Up @@ -630,4 +640,47 @@ public function updateUri(core_kernel_classes_Class $resource, string $newUri):
]
);
}

public function getInstanceUris(string $classUri, bool $recursive = false): array
{
if (!$recursive) {
$query = 'SELECT subject FROM statements WHERE predicate = ? AND object = ?';
$params = [
OntologyRdf::RDF_TYPE,
$classUri,
];
} else {
$query = <<<'SQL'
WITH RECURSIVE statements_tree AS (
SELECT
r.subject,
r.predicate
FROM statements r
WHERE r.subject = ?
AND r.predicate IN (?, ?)
UNION ALL
SELECT
s.subject,
s.predicate
FROM statements s
JOIN statements_tree st
ON s.object = st.subject
WHERE s.predicate IN (?, ?)
)
SELECT subject FROM statements_tree WHERE predicate = ?;
SQL;
$params = [
$classUri,
OntologyRdfs::RDFS_SUBCLASSOF,
OntologyRdf::RDF_TYPE,
OntologyRdfs::RDFS_SUBCLASSOF,
OntologyRdf::RDF_TYPE,
OntologyRdf::RDF_TYPE,
];
}

$statement = $this->getPersistence()->query($query, $params);

return array_column($statement->fetchAll(), 'subject');
}
}

0 comments on commit e5480a8

Please sign in to comment.