-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transform favorite, tag and systemtag search queries to fileid lists
not as performant but compatible with sharding Signed-off-by: Robin Appelman <[email protected]>
- Loading branch information
1 parent
baca8fa
commit 1c4dbf9
Showing
7 changed files
with
234 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
lib/private/Files/Search/QueryOptimizer/DavTagToFileIds.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* @copyright Copyright (c) 2024 Robin Appelman <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\Files\Search\QueryOptimizer; | ||
|
||
use OC\Files\Search\SearchComparison; | ||
use OCP\Files\Search\ISearchComparison; | ||
use OCP\Files\Search\ISearchOperator; | ||
use OCP\IDBConnection; | ||
use OCP\IGroupManager; | ||
use OCP\IUser; | ||
|
||
class DavTagToFileIds extends ReplacingOptimizerStep { | ||
public function __construct( | ||
private IDBConnection $connection, | ||
private IUser $user, | ||
) { | ||
} | ||
|
||
public function processOperator(ISearchOperator &$operator): bool { | ||
if ($operator instanceof ISearchComparison && $operator->getField() === 'tagname') { | ||
$operator = new SearchComparison(ISearchComparison::COMPARE_IN, 'fileid', $this->getFileIdsForDavTag($operator)); | ||
return true; | ||
} else { | ||
return parent::processOperator($operator); | ||
} | ||
} | ||
|
||
|
||
private function getFileIdsForDavTag(ISearchComparison $comparison): array { | ||
$query = $this->connection->getQueryBuilder(); | ||
|
||
$query->select('tagmap.objid') | ||
->from('vcategory_to_object', 'tagmap') | ||
->leftJoin('tagmap', 'vcategory', 'tag', $query->expr()->andX( | ||
$query->expr()->eq('tagmap.type', 'tag.type'), | ||
$query->expr()->eq('tagmap.categoryid', 'tag.id') | ||
)) | ||
->where($query->expr()->eq('tag.type', $query->createNamedParameter('files'))) | ||
->andWhere($query->expr()->eq('tag.uid', $query->createNamedParameter($this->user->getUID()))); | ||
if ($comparison->getType() === ISearchComparison::COMPARE_EQUAL) { | ||
$query->andWhere($query->expr()->eq('tag.category', $query->createNamedParameter($comparison->getValue()))); | ||
} elseif ($comparison->getType() === ISearchComparison::COMPARE_LIKE) { | ||
$query->andWhere($query->expr()->like('tag.category', $query->createNamedParameter($comparison->getValue()))); | ||
} else { | ||
throw new \InvalidArgumentException('Unsupported comparison for field ' . $comparison->getField() . ': ' . $comparison->getType()); | ||
} | ||
return $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* @copyright Copyright (c) 2024 Robin Appelman <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\Files\Search\QueryOptimizer; | ||
|
||
use OC\Files\Search\SearchComparison; | ||
use OCP\Files\Search\ISearchComparison; | ||
use OCP\Files\Search\ISearchOperator; | ||
use OCP\IDBConnection; | ||
use OCP\IGroupManager; | ||
use OCP\IUser; | ||
|
||
class FavoriteToTag extends ReplacingOptimizerStep { | ||
public const TAG_FAVORITE = '_$!<Favorite>!$_'; | ||
|
||
public function processOperator(ISearchOperator &$operator): bool { | ||
if ($operator instanceof ISearchComparison && $operator->getField() === 'favorite' && $operator->getType() === ISearchComparison::COMPARE_EQUAL) { | ||
$operator = new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', self::TAG_FAVORITE); | ||
return true; | ||
} else { | ||
return parent::processOperator($operator); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
lib/private/Files/Search/QueryOptimizer/SystemTagToFileIds.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* @copyright Copyright (c) 2024 Robin Appelman <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\Files\Search\QueryOptimizer; | ||
|
||
use OC\Files\Search\SearchComparison; | ||
use OCP\Files\Search\ISearchComparison; | ||
use OCP\Files\Search\ISearchOperator; | ||
use OCP\IDBConnection; | ||
use OCP\IGroupManager; | ||
use OCP\IUser; | ||
|
||
class SystemTagToFileIds extends ReplacingOptimizerStep { | ||
public function __construct( | ||
private IDBConnection $connection, | ||
private IUser $user, | ||
private IGroupManager $groupManager, | ||
) { | ||
} | ||
|
||
public function processOperator(ISearchOperator &$operator): bool { | ||
if ($operator instanceof ISearchComparison && $operator->getField() === 'systemtag') { | ||
$operator = new SearchComparison(ISearchComparison::COMPARE_IN, 'fileid', $this->getFileIdsForSystemTag($operator)); | ||
return true; | ||
} else { | ||
return parent::processOperator($operator); | ||
} | ||
} | ||
|
||
|
||
private function getFileIdsForSystemTag(ISearchComparison $comparison): array { | ||
$query = $this->connection->getQueryBuilder(); | ||
|
||
$on = $query->expr()->andX($query->expr()->eq('systemtag.id', 'systemtagmap.systemtagid')); | ||
if (!$this->groupManager->isAdmin($this->user->getUID())) { | ||
$on->add($query->expr()->eq('systemtag.visibility', $query->createNamedParameter(true))); | ||
} | ||
|
||
$query->select('systemtagmap.objectid') | ||
->from('systemtag_object_mapping', 'systemtagmap') | ||
->leftJoin('systemtagmap', 'systemtag', 'systemtag', $on) | ||
->where($query->expr()->eq('systemtagmap.objecttype', $query->createNamedParameter('files'))) | ||
->andWhere($query->expr()->eq('systemtagmap.objecttype', $query->createNamedParameter('files'))); | ||
if ($comparison->getType() === ISearchComparison::COMPARE_EQUAL) { | ||
$query->andWhere($query->expr()->eq('systemtag.name', $query->createNamedParameter($comparison->getValue()))); | ||
} elseif ($comparison->getType() === ISearchComparison::COMPARE_LIKE) { | ||
$query->andWhere($query->expr()->like('systemtag.name', $query->createNamedParameter($comparison->getValue()))); | ||
} else { | ||
throw new \InvalidArgumentException('Unsupported comparison for field ' . $comparison->getField() . ': ' . $comparison->getType()); | ||
} | ||
return $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters