diff options
author | Robin Appelman <robin@icewind.nl> | 2021-05-04 19:06:02 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2021-06-14 16:11:22 +0200 |
commit | e198dc1b200f3ade93498e0ea7b468c87d46748a (patch) | |
tree | 235c30d6035bec46bef5ee2f2b134333dfb65409 /lib/private/Files/Cache/Wrapper | |
parent | dfbac05f7ba00c78ac15df61a425317a890b08d1 (diff) | |
download | nextcloud-server-e198dc1b200f3ade93498e0ea7b468c87d46748a.tar.gz nextcloud-server-e198dc1b200f3ade93498e0ea7b468c87d46748a.zip |
rework search api to allow searching on multiple caches at once
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private/Files/Cache/Wrapper')
-rw-r--r-- | lib/private/Files/Cache/Wrapper/CacheJail.php | 127 | ||||
-rw-r--r-- | lib/private/Files/Cache/Wrapper/CacheWrapper.php | 50 |
2 files changed, 49 insertions, 128 deletions
diff --git a/lib/private/Files/Cache/Wrapper/CacheJail.php b/lib/private/Files/Cache/Wrapper/CacheJail.php index 9b88591e1d1..9b85370a111 100644 --- a/lib/private/Files/Cache/Wrapper/CacheJail.php +++ b/lib/private/Files/Cache/Wrapper/CacheJail.php @@ -28,14 +28,8 @@ namespace OC\Files\Cache\Wrapper; use OC\Files\Cache\Cache; -use OC\Files\Search\SearchBinaryOperator; -use OC\Files\Search\SearchComparison; -use OC\Files\Search\SearchQuery; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Files\Cache\ICacheEntry; -use OCP\Files\Search\ISearchBinaryOperator; -use OCP\Files\Search\ISearchComparison; -use OCP\Files\Search\ISearchQuery; /** * Jail to a subdirectory of the wrapped cache @@ -107,10 +101,6 @@ class CacheJail extends CacheWrapper { } } - /** - * @param ICacheEntry|array $entry - * @return array - */ protected function formatCacheEntry($entry) { if (isset($entry['path'])) { $entry['path'] = $this->getJailedPath($entry['path']); @@ -229,99 +219,6 @@ class CacheJail extends CacheWrapper { return $this->getCache()->getStatus($this->getSourcePath($file)); } - private function formatSearchResults($results) { - return array_map(function ($entry) { - $entry['path'] = $this->getJailedPath($entry['path'], $this->getGetUnjailedRoot()); - return $entry; - }, $results); - } - - /** - * search for files matching $pattern - * - * @param string $pattern - * @return array an array of file data - */ - public function search($pattern) { - // normalize pattern - $pattern = $this->normalize($pattern); - - if ($pattern === '%%') { - return []; - } - - $query = $this->getQueryBuilder(); - $query->selectFileCache() - ->whereStorageId() - ->andWhere($query->expr()->orX( - $query->expr()->like('path', $query->createNamedParameter($this->getGetUnjailedRoot() . '/%')), - $query->expr()->eq('path_hash', $query->createNamedParameter(md5($this->getGetUnjailedRoot()))), - )) - ->andWhere($query->expr()->iLike('name', $query->createNamedParameter($pattern))); - - $result = $query->execute(); - $files = $result->fetchAll(); - $result->closeCursor(); - - $results = array_map(function (array $data) { - return self::cacheEntryFromData($data, $this->mimetypeLoader); - }, $files); - return $this->formatSearchResults($results); - } - - /** - * search for files by mimetype - * - * @param string $mimetype - * @return array - */ - public function searchByMime($mimetype) { - $mimeId = $this->mimetypeLoader->getId($mimetype); - - $query = $this->getQueryBuilder(); - $query->selectFileCache() - ->whereStorageId() - ->andWhere($query->expr()->orX( - $query->expr()->like('path', $query->createNamedParameter($this->getGetUnjailedRoot() . '/%')), - $query->expr()->eq('path_hash', $query->createNamedParameter(md5($this->getGetUnjailedRoot()))), - )); - - if (strpos($mimetype, '/')) { - $query->andWhere($query->expr()->eq('mimetype', $query->createNamedParameter($mimeId, IQueryBuilder::PARAM_INT))); - } else { - $query->andWhere($query->expr()->eq('mimepart', $query->createNamedParameter($mimeId, IQueryBuilder::PARAM_INT))); - } - - $result = $query->execute(); - $files = $result->fetchAll(); - $result->closeCursor(); - - $results = array_map(function (array $data) { - return self::cacheEntryFromData($data, $this->mimetypeLoader); - }, $files); - return $this->formatSearchResults($results); - } - - public function searchQuery(ISearchQuery $query) { - $prefixFilter = new SearchComparison( - ISearchComparison::COMPARE_LIKE, - 'path', - $this->getGetUnjailedRoot() . '/%' - ); - $rootFilter = new SearchComparison( - ISearchComparison::COMPARE_EQUAL, - 'path', - $this->getGetUnjailedRoot() - ); - $operation = new SearchBinaryOperator( - ISearchBinaryOperator::OPERATOR_AND, - [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, [$prefixFilter, $rootFilter]) , $query->getSearchOperation()] - ); - $simpleQuery = new SearchQuery($operation, $query->getLimit(), $query->getOffset(), $query->getOrder(), $query->getUser()); - $results = $this->getCache()->searchQuery($simpleQuery); - return $this->formatSearchResults($results); - } - /** * update the folder size and the size of all parent folders * @@ -403,4 +300,28 @@ class CacheJail extends CacheWrapper { } return $this->getCache()->moveFromCache($sourceCache, $sourcePath, $this->getSourcePath($targetPath)); } + + public function getQueryFilterForStorage(IQueryBuilder $builder) { + $escapedRoot = $builder->getConnection()->escapeLikeParameter($this->getGetUnjailedRoot()); + + return $builder->expr()->andX( + $this->getCache()->getQueryFilterForStorage($builder), + $builder->expr()->orX( + $builder->expr()->eq('path_hash', $builder->createNamedParameter(md5($this->getGetUnjailedRoot()))), + $builder->expr()->like('path', $builder->createNamedParameter($escapedRoot . '/%')), + ) + ); + } + + public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry { + $rawEntry = $this->getCache()->getCacheEntryFromSearchResult($rawEntry); + if ($rawEntry) { + $jailedPath = $this->getJailedPath($rawEntry->getPath()); + if ($jailedPath !== null) { + return $this->formatCacheEntry(clone $rawEntry); + } + } + + return null; + } } diff --git a/lib/private/Files/Cache/Wrapper/CacheWrapper.php b/lib/private/Files/Cache/Wrapper/CacheWrapper.php index 01fbdd3bf42..edda332af67 100644 --- a/lib/private/Files/Cache/Wrapper/CacheWrapper.php +++ b/lib/private/Files/Cache/Wrapper/CacheWrapper.php @@ -30,6 +30,8 @@ namespace OC\Files\Cache\Wrapper; use OC\Files\Cache\Cache; +use OC\Files\Cache\QuerySearchHelper; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Files\Cache\ICache; use OCP\Files\Cache\ICacheEntry; use OCP\Files\Search\ISearchQuery; @@ -45,6 +47,14 @@ class CacheWrapper extends Cache { */ public function __construct($cache) { $this->cache = $cache; + $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); + $this->connection = \OC::$server->getDatabaseConnection(); + $this->querySearchHelper = new QuerySearchHelper( + $this->mimetypeLoader, + $this->connection, + \OC::$server->getSystemConfig(), + \OC::$server->getLogger() + ); } protected function getCache() { @@ -215,31 +225,8 @@ class CacheWrapper extends Cache { return $this->getCache()->getStatus($file); } - /** - * search for files matching $pattern - * - * @param string $pattern - * @return ICacheEntry[] an array of file data - */ - public function search($pattern) { - $results = $this->getCache()->search($pattern); - return array_map([$this, 'formatCacheEntry'], $results); - } - - /** - * search for files by mimetype - * - * @param string $mimetype - * @return ICacheEntry[] - */ - public function searchByMime($mimetype) { - $results = $this->getCache()->searchByMime($mimetype); - return array_map([$this, 'formatCacheEntry'], $results); - } - - public function searchQuery(ISearchQuery $query) { - $results = $this->getCache()->searchQuery($query); - return array_map([$this, 'formatCacheEntry'], $results); + public function searchQuery(ISearchQuery $searchQuery) { + return $this->querySearchHelper->searchInCaches($searchQuery, [$this]); } /** @@ -321,4 +308,17 @@ class CacheWrapper extends Cache { public static function getById($id) { return parent::getById($id); } + + public function getQueryFilterForStorage(IQueryBuilder $builder) { + return $this->getCache()->getQueryFilterForStorage($builder); + } + + public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry { + $rawEntry = $this->getCache()->getCacheEntryFromSearchResult($rawEntry); + if ($rawEntry) { + return $this->formatCacheEntry(clone $rawEntry); + } + + return null; + } } |