tag: get rid of windowing

old-stable24
Varun Patil 2022-10-26 09:29:32 -07:00
parent 352f8d3d54
commit 6d8f06c885
3 changed files with 97 additions and 101 deletions

View File

@ -248,28 +248,7 @@ class ApiController extends Controller
);
// Preload all tag previews
$previews = $this->timelineQuery->getTagPreviews($folder);
// Convert to map with key as systemtagid
$previews_map = [];
foreach ($previews as &$preview) {
$key = $preview['systemtagid'];
if (!\array_key_exists($key, $previews_map)) {
$previews_map[$key] = [];
}
unset($preview['systemtagid']);
$previews_map[$key][] = $preview;
}
// Add previews to list
foreach ($list as &$tag) {
$key = $tag['id'];
if (\array_key_exists($key, $previews_map)) {
$tag['previews'] = $previews_map[$key];
} else {
$tag['previews'] = [];
}
}
$this->timelineQuery->getTagPreviews($list, $folder);
return new JSONResponse($list, Http::STATUS_OK);
}

View File

@ -159,15 +159,14 @@ trait TimelineQueryDays
return $day;
}
/** Get the query for oc_filecache join */
private function getFilecacheJoinQuery(
IQueryBuilder &$query,
/**
* Get all folders inside a top folder
*/
private function getSubfolderIdsRecursive(
IDBConnection &$conn,
Folder &$folder,
bool $recursive,
bool $archive
) {
$pathQuery = null;
if ($recursive) {
// CTE to get all folders recursively in the given top folder
$cte =
'WITH RECURSIVE cte_folders(fileid) AS (
@ -216,12 +215,38 @@ trait TimelineQueryDays
$topFolderId = $archiveFolder ? $archiveFolder->getId() : -1;
}
// Join with CTE
$pathQuery = $query->expr()->in('f.parent', $query->createFunction($cte));
return array_column($conn->executeQuery($cte, [
'topFolderId' => $topFolderId,
'excludedFolderIds' => $excludedFolderIds,
])->fetchAll(), 'fileid');
}
// Set query parameters
$query->setParameter('topFolderId', $topFolderId, IQueryBuilder::PARAM_INT);
$query->setParameter('excludedFolderIds', $excludedFolderIds, IQueryBuilder::PARAM_INT_ARRAY);
/**
* Get the query for oc_filecache join
*
* @param IQueryBuilder $query Query builder
* @param Folder|array $folder Either the top folder or array of folder Ids
* @param bool $recursive Whether to get the days recursively
* @param bool $archive Whether to get the days only from the archive folder
*/
private function getFilecacheJoinQuery(
IQueryBuilder &$query,
&$folder,
bool $recursive,
bool $archive
) {
$pathQuery = null;
if ($recursive) {
// Get all subfolder Ids recursively
$folderIds = [];
if ($folder instanceof Folder) {
$folderIds = $this->getSubfolderIdsRecursive($query->getConnection(), $folder, $archive);
} else {
$folderIds = $folder;
}
// Join with folder IDs
$pathQuery = $query->expr()->in('f.parent', $query->createNamedParameter($folderIds, IQueryBuilder::PARAM_INT_ARRAY));
} else {
// If getting non-recursively folder only check for parent
$pathQuery = $query->expr()->eq('f.parent', $query->createNamedParameter($folder->getId(), IQueryBuilder::PARAM_INT));

View File

@ -77,47 +77,39 @@ trait TimelineQueryTags
return $tags;
}
public function getTagPreviews(Folder $folder)
public function getTagPreviews(array &$tags, Folder &$folder)
{
// Cache subfolder ids to prevent duplicate requests
$folderIds = $this->getSubfolderIdsRecursive($this->connection, $folder, false);
foreach ($tags as &$tag) {
$query = $this->connection->getQueryBuilder();
// Windowing
$rowNumber = $query->createFunction('ROW_NUMBER() OVER (PARTITION BY stom.systemtagid) as n');
// SELECT all photos with this tag
$query->select('f.fileid', 'f.etag', 'stom.systemtagid', $rowNumber)->from(
$query->select('f.fileid', 'f.etag')->from(
'systemtag_object_mapping',
'stom'
)->where(
$query->expr()->eq('stom.objecttype', $query->createNamedParameter('files')),
$query->expr()->eq('stom.systemtagid', $query->createNamedParameter($tag['id'])),
);
// WHERE these items are memories indexed photos
$query->innerJoin('stom', 'memories', 'm', $query->expr()->eq('m.fileid', 'stom.objectid'));
// WHERE these photos are in the user's requested folder recursively
$query->innerJoin('m', 'filecache', 'f', $this->getFilecacheJoinQuery($query, $folder, true, false));
$query->innerJoin('m', 'filecache', 'f', $this->getFilecacheJoinQuery($query, $folderIds, true, false));
// Make this a sub query
$fun = $query->createFunction('('.$query->getSQL().')');
// MAX 4
$query->setMaxResults(4);
// Create outer query
$outerQuery = $this->connection->getQueryBuilder();
$outerQuery->setParameters($query->getParameters());
$outerQuery->select('*')->from($fun, 't');
$outerQuery->where($query->expr()->lte('t.n', $outerQuery->createParameter('nc')));
$outerQuery->setParameter('nc', 4, IQueryBuilder::PARAM_INT);
// FETCH all tag previews
$previews = $outerQuery->executeQuery()->fetchAll();
// FETCH tag previews
$tag['previews'] = $query->executeQuery()->fetchAll();
// Post-process
foreach ($previews as &$row) {
foreach ($tag['previews'] as &$row) {
$row['fileid'] = (int) $row['fileid'];
$row['systemtagid'] = (int) $row['systemtagid'];
unset($row['n']);
}
return $previews;
}
}
}