Skip to content

Commit

Permalink
fix(TreeMapper#getSoftDeletedRootItems): Avoid O(n^2) algorithm
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
  • Loading branch information
marcelklehr committed Aug 7, 2024
1 parent 24e35c3 commit ebeca91
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions lib/Db/TreeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -934,33 +934,21 @@ public function getSoftDeletedRootItems(string $userId, string $type): array {
if ($type === TreeMapper::TYPE_FOLDER || $type === TreeMapper::TYPE_SHARE) {
$qb = $this->selectFromType($type);
$qb
->join('i', 'bookmarks_tree', 't', $qb->expr()->eq('t.id', 'i.id'))
->innerJoin('i', 'bookmarks_tree', 't', $qb->expr()->eq('i.id', 't.id'))
->leftJoin('t', 'bookmarks_tree', 't2', $qb->expr()->eq('t.parent_folder', 't2.id'))
->leftJoin('t', 'bookmarks_root_folders', 'r', $qb->expr()->eq('t.parent_folder', 'r.folder_id'))
->where($qb->expr()->isNotNull('t.soft_deleted_at'))
->andWhere($qb->expr()->eq('t.type', $qb->createPositionalParameter($type, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq('i.user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)));
$items = $this->findEntitiesWithType($qb, $type);

if ($type === TreeMapper::TYPE_SHARE) {
return $items;
}

$topmostFolders = [];
foreach ($items as $folder) {
$topmostFolders[$folder->getId()] = $folder;
}

foreach ($items as $folder1) {
foreach ($items as $folder2) {
if ($folder1->getId() !== $folder2->getId() && $this->hasDescendant($folder1->getId(), TreeMapper::TYPE_FOLDER, $folder2->getId())) {
$topmostFolders[$folder2->getId()] = false;
}
}
}

return array_values(array_filter(array_values($topmostFolders), fn ($value) => $value !== false));
->andWhere($qb->expr()->eq('i.user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->orX(
$qb->expr()->isNull('t2.soft_deleted_at'),
$qb->expr()->isNotNull('r.folder_id'),
));
return $this->findEntitiesWithType($qb, $type);
}
if ($type === TreeMapper::TYPE_BOOKMARK) {
$params = new QueryParameters();
$params->setLimit(-1);
$params->setSoftDeleted(true);
$params->setSoftDeletedFolders(false);
return $this->bookmarkMapper->findAll($userId, $params);
Expand Down

0 comments on commit ebeca91

Please sign in to comment.