rework search api to allow searching on multiple caches at once
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
726d01843e
commit
d2e5f084c8
|
@ -32,6 +32,7 @@ namespace OCA\Files_Sharing;
|
|||
use OC\Files\Cache\FailedCache;
|
||||
use OC\Files\Cache\Wrapper\CacheJail;
|
||||
use OC\Files\Storage\Wrapper\Jail;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\Files\Cache\ICacheEntry;
|
||||
use OCP\Files\StorageNotAvailableException;
|
||||
|
||||
|
@ -182,19 +183,18 @@ class Cache extends CacheJail {
|
|||
// Not a valid action for Shared Cache
|
||||
}
|
||||
|
||||
public function search($pattern) {
|
||||
// Do the normal search on the whole storage for non files
|
||||
public function getQueryFilterForStorage(IQueryBuilder $builder) {
|
||||
// Do the normal jail behavior for non files
|
||||
if ($this->storage->getItemType() !== 'file') {
|
||||
return parent::search($pattern);
|
||||
return parent::getQueryFilterForStorage($builder);
|
||||
}
|
||||
|
||||
$regex = '/' . str_replace('%', '.*', $pattern) . '/i';
|
||||
|
||||
$data = $this->get('');
|
||||
if (preg_match($regex, $data->getName()) === 1) {
|
||||
return [$data];
|
||||
}
|
||||
|
||||
return [];
|
||||
// for single file shares we don't need to do the LIKE
|
||||
return $builder->expr()->andX(
|
||||
parent::getQueryFilterForStorage($builder),
|
||||
$builder->expr()->orX(
|
||||
$builder->expr()->eq('path_hash', $builder->createNamedParameter(md5($this->getGetUnjailedRoot()))),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -989,8 +989,7 @@ class Trashbin {
|
|||
$query = new CacheQueryBuilder(
|
||||
\OC::$server->getDatabaseConnection(),
|
||||
\OC::$server->getSystemConfig(),
|
||||
\OC::$server->getLogger(),
|
||||
$cache
|
||||
\OC::$server->getLogger()
|
||||
);
|
||||
$normalizedParentPath = ltrim(Filesystem::normalizePath(dirname('files_trashbin/versions/'. $filename)), '/');
|
||||
$parentId = $cache->getId($normalizedParentPath);
|
||||
|
@ -999,7 +998,7 @@ class Trashbin {
|
|||
}
|
||||
|
||||
$query->selectFileCache()
|
||||
->whereStorageId()
|
||||
->whereStorageId($cache->getNumericStorageId())
|
||||
->andWhere($query->expr()->eq('parent', $query->createNamedParameter($parentId)))
|
||||
->andWhere($query->expr()->iLike('name', $query->createNamedParameter($pattern)));
|
||||
|
||||
|
|
|
@ -40,7 +40,8 @@
|
|||
namespace OC\Files\Cache;
|
||||
|
||||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
||||
use OCP\DB\IResult;
|
||||
use OC\Files\Search\SearchComparison;
|
||||
use OC\Files\Search\SearchQuery;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\EventDispatcher\IEventDispatcher;
|
||||
use OCP\Files\Cache\CacheEntryInsertedEvent;
|
||||
|
@ -52,6 +53,7 @@ use OCP\Files\Cache\ICache;
|
|||
use OCP\Files\Cache\ICacheEntry;
|
||||
use OCP\Files\FileInfo;
|
||||
use OCP\Files\IMimeTypeLoader;
|
||||
use OCP\Files\Search\ISearchComparison;
|
||||
use OCP\Files\Search\ISearchQuery;
|
||||
use OCP\Files\Storage\IStorage;
|
||||
use OCP\IDBConnection;
|
||||
|
@ -118,15 +120,19 @@ class Cache implements ICache {
|
|||
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
|
||||
$this->connection = \OC::$server->getDatabaseConnection();
|
||||
$this->eventDispatcher = \OC::$server->get(IEventDispatcher::class);
|
||||
$this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader);
|
||||
$this->querySearchHelper = new QuerySearchHelper(
|
||||
$this->mimetypeLoader,
|
||||
$this->connection,
|
||||
\OC::$server->getSystemConfig(),
|
||||
\OC::$server->getLogger()
|
||||
);
|
||||
}
|
||||
|
||||
protected function getQueryBuilder() {
|
||||
return new CacheQueryBuilder(
|
||||
$this->connection,
|
||||
\OC::$server->getSystemConfig(),
|
||||
\OC::$server->getLogger(),
|
||||
$this
|
||||
\OC::$server->getLogger()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -153,7 +159,7 @@ class Cache implements ICache {
|
|||
// normalize file
|
||||
$file = $this->normalize($file);
|
||||
|
||||
$query->whereStorageId()
|
||||
$query->whereStorageId($this->getNumericStorageId())
|
||||
->wherePath($file);
|
||||
} else { //file id
|
||||
$query->whereFileId($file);
|
||||
|
@ -482,7 +488,7 @@ class Cache implements ICache {
|
|||
$query = $this->getQueryBuilder();
|
||||
$query->select('fileid')
|
||||
->from('filecache')
|
||||
->whereStorageId()
|
||||
->whereStorageId($this->getNumericStorageId())
|
||||
->wherePath($file);
|
||||
|
||||
$result = $query->execute();
|
||||
|
@ -718,7 +724,7 @@ class Cache implements ICache {
|
|||
public function clear() {
|
||||
$query = $this->getQueryBuilder();
|
||||
$query->delete('filecache')
|
||||
->whereStorageId();
|
||||
->whereStorageId($this->getNumericStorageId());
|
||||
$query->execute();
|
||||
|
||||
$query = $this->connection->getQueryBuilder();
|
||||
|
@ -746,7 +752,7 @@ class Cache implements ICache {
|
|||
$query = $this->getQueryBuilder();
|
||||
$query->select('size')
|
||||
->from('filecache')
|
||||
->whereStorageId()
|
||||
->whereStorageId($this->getNumericStorageId())
|
||||
->wherePath($file);
|
||||
|
||||
$result = $query->execute();
|
||||
|
@ -775,37 +781,8 @@ class Cache implements ICache {
|
|||
* @return ICacheEntry[] an array of cache entries where the name matches the search pattern
|
||||
*/
|
||||
public function search($pattern) {
|
||||
// normalize pattern
|
||||
$pattern = $this->normalize($pattern);
|
||||
|
||||
if ($pattern === '%%') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$query = $this->getQueryBuilder();
|
||||
$query->selectFileCache()
|
||||
->whereStorageId()
|
||||
->andWhere($query->expr()->iLike('name', $query->createNamedParameter($pattern)));
|
||||
|
||||
$result = $query->execute();
|
||||
$files = $result->fetchAll();
|
||||
$result->closeCursor();
|
||||
|
||||
return array_map(function (array $data) {
|
||||
return self::cacheEntryFromData($data, $this->mimetypeLoader);
|
||||
}, $files);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param IResult $result
|
||||
* @return CacheEntry[]
|
||||
*/
|
||||
private function searchResultToCacheEntries(IResult $result): array {
|
||||
$files = $result->fetchAll();
|
||||
|
||||
return array_map(function (array $data) {
|
||||
return self::cacheEntryFromData($data, $this->mimetypeLoader);
|
||||
}, $files);
|
||||
$operator = new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', $pattern);
|
||||
return $this->searchQuery(new SearchQuery($operator, 0, 0, [], null));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -816,71 +793,16 @@ class Cache implements ICache {
|
|||
* @return ICacheEntry[] an array of cache entries where the mimetype matches the search
|
||||
*/
|
||||
public function searchByMime($mimetype) {
|
||||
$mimeId = $this->mimetypeLoader->getId($mimetype);
|
||||
|
||||
$query = $this->getQueryBuilder();
|
||||
$query->selectFileCache()
|
||||
->whereStorageId();
|
||||
|
||||
if (strpos($mimetype, '/')) {
|
||||
$query->andWhere($query->expr()->eq('mimetype', $query->createNamedParameter($mimeId, IQueryBuilder::PARAM_INT)));
|
||||
if (strpos($mimetype, '/') === false) {
|
||||
$operator = new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%');
|
||||
} else {
|
||||
$query->andWhere($query->expr()->eq('mimepart', $query->createNamedParameter($mimeId, IQueryBuilder::PARAM_INT)));
|
||||
$operator = new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype);
|
||||
}
|
||||
|
||||
$result = $query->execute();
|
||||
$files = $result->fetchAll();
|
||||
$result->closeCursor();
|
||||
|
||||
return array_map(function (array $data) {
|
||||
return self::cacheEntryFromData($data, $this->mimetypeLoader);
|
||||
}, $files);
|
||||
return $this->searchQuery(new SearchQuery($operator, 0, 0, [], null));
|
||||
}
|
||||
|
||||
public function searchQuery(ISearchQuery $searchQuery) {
|
||||
$builder = $this->getQueryBuilder();
|
||||
|
||||
$query = $builder->selectFileCache('file');
|
||||
|
||||
$query->whereStorageId();
|
||||
|
||||
if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) {
|
||||
$user = $searchQuery->getUser();
|
||||
if ($user === null) {
|
||||
throw new \InvalidArgumentException("Searching by tag requires the user to be set in the query");
|
||||
}
|
||||
$query
|
||||
->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid'))
|
||||
->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX(
|
||||
$builder->expr()->eq('tagmap.type', 'tag.type'),
|
||||
$builder->expr()->eq('tagmap.categoryid', 'tag.id')
|
||||
))
|
||||
->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files')))
|
||||
->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($user->getUID())));
|
||||
}
|
||||
|
||||
$searchExpr = $this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation());
|
||||
if ($searchExpr) {
|
||||
$query->andWhere($searchExpr);
|
||||
}
|
||||
|
||||
if ($searchQuery->limitToHome() && ($this instanceof HomeCache)) {
|
||||
$query->andWhere($builder->expr()->like('path', $query->expr()->literal('files/%')));
|
||||
}
|
||||
|
||||
$this->querySearchHelper->addSearchOrdersToQuery($query, $searchQuery->getOrder());
|
||||
|
||||
if ($searchQuery->getLimit()) {
|
||||
$query->setMaxResults($searchQuery->getLimit());
|
||||
}
|
||||
if ($searchQuery->getOffset()) {
|
||||
$query->setFirstResult($searchQuery->getOffset());
|
||||
}
|
||||
|
||||
$result = $query->execute();
|
||||
$cacheEntries = $this->searchResultToCacheEntries($result);
|
||||
$result->closeCursor();
|
||||
return $cacheEntries;
|
||||
return $this->querySearchHelper->searchInCaches($searchQuery, [$this]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -949,7 +871,7 @@ class Cache implements ICache {
|
|||
$query->selectAlias($query->func()->sum('size'), 'f1')
|
||||
->selectAlias($query->func()->min('size'), 'f2')
|
||||
->from('filecache')
|
||||
->whereStorageId()
|
||||
->whereStorageId($this->getNumericStorageId())
|
||||
->whereParent($id);
|
||||
|
||||
$result = $query->execute();
|
||||
|
@ -982,7 +904,7 @@ class Cache implements ICache {
|
|||
$query = $this->getQueryBuilder();
|
||||
$query->select('fileid')
|
||||
->from('filecache')
|
||||
->whereStorageId();
|
||||
->whereStorageId($this->getNumericStorageId());
|
||||
|
||||
$result = $query->execute();
|
||||
$files = $result->fetchAll(\PDO::FETCH_COLUMN);
|
||||
|
@ -1006,7 +928,7 @@ class Cache implements ICache {
|
|||
$query = $this->getQueryBuilder();
|
||||
$query->select('path')
|
||||
->from('filecache')
|
||||
->whereStorageId()
|
||||
->whereStorageId($this->getNumericStorageId())
|
||||
->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
|
||||
->orderBy('fileid', 'DESC')
|
||||
->setMaxResults(1);
|
||||
|
@ -1028,7 +950,7 @@ class Cache implements ICache {
|
|||
$query = $this->getQueryBuilder();
|
||||
$query->select('path')
|
||||
->from('filecache')
|
||||
->whereStorageId()
|
||||
->whereStorageId($this->getNumericStorageId())
|
||||
->whereFileId($id);
|
||||
|
||||
$result = $query->execute();
|
||||
|
@ -1127,4 +1049,16 @@ class Cache implements ICache {
|
|||
'metadata_etag' => $entry->getMetadataEtag(),
|
||||
];
|
||||
}
|
||||
|
||||
public function getQueryFilterForStorage(IQueryBuilder $builder) {
|
||||
return $builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId(), IQueryBuilder::PARAM_INT));
|
||||
}
|
||||
|
||||
public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
|
||||
if ($rawEntry->getStorageId() === $this->getNumericStorageId()) {
|
||||
return $rawEntry;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -125,4 +125,8 @@ class CacheEntry implements ICacheEntry {
|
|||
public function getData() {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function __clone() {
|
||||
$this->data = array_merge([], $this->data);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,13 +36,10 @@ use OCP\ILogger;
|
|||
* Query builder with commonly used helpers for filecache queries
|
||||
*/
|
||||
class CacheQueryBuilder extends QueryBuilder {
|
||||
private $cache;
|
||||
private $alias = null;
|
||||
|
||||
public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger, Cache $cache) {
|
||||
public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) {
|
||||
parent::__construct($connection, $systemConfig, $logger);
|
||||
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
public function selectFileCache(string $alias = null) {
|
||||
|
@ -57,8 +54,8 @@ class CacheQueryBuilder extends QueryBuilder {
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function whereStorageId() {
|
||||
$this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($this->cache->getNumericStorageId(), IQueryBuilder::PARAM_INT)));
|
||||
public function whereStorageId(int $storageId) {
|
||||
$this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
namespace OC\Files\Cache;
|
||||
|
||||
use OCP\Constants;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\Files\Cache\ICache;
|
||||
use OCP\Files\Cache\ICacheEntry;
|
||||
use OCP\Files\Search\ISearchQuery;
|
||||
|
@ -139,4 +140,12 @@ class FailedCache implements ICache {
|
|||
public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int {
|
||||
throw new \Exception("Invalid cache");
|
||||
}
|
||||
|
||||
public function getQueryFilterForStorage(IQueryBuilder $builder) {
|
||||
return 'false';
|
||||
}
|
||||
|
||||
public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,12 +26,17 @@
|
|||
|
||||
namespace OC\Files\Cache;
|
||||
|
||||
use OC\SystemConfig;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\Files\Cache\ICache;
|
||||
use OCP\Files\IMimeTypeLoader;
|
||||
use OCP\Files\Search\ISearchBinaryOperator;
|
||||
use OCP\Files\Search\ISearchComparison;
|
||||
use OCP\Files\Search\ISearchOperator;
|
||||
use OCP\Files\Search\ISearchOrder;
|
||||
use OCP\Files\Search\ISearchQuery;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\ILogger;
|
||||
|
||||
/**
|
||||
* Tools for transforming search queries into database queries
|
||||
|
@ -59,14 +64,23 @@ class QuerySearchHelper {
|
|||
|
||||
/** @var IMimeTypeLoader */
|
||||
private $mimetypeLoader;
|
||||
/** @var IDBConnection */
|
||||
private $connection;
|
||||
/** @var SystemConfig */
|
||||
private $systemConfig;
|
||||
/** @var ILogger */
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* QuerySearchUtil constructor.
|
||||
*
|
||||
* @param IMimeTypeLoader $mimetypeLoader
|
||||
*/
|
||||
public function __construct(IMimeTypeLoader $mimetypeLoader) {
|
||||
public function __construct(
|
||||
IMimeTypeLoader $mimetypeLoader,
|
||||
IDBConnection $connection,
|
||||
SystemConfig $systemConfig,
|
||||
ILogger $logger
|
||||
) {
|
||||
$this->mimetypeLoader = $mimetypeLoader;
|
||||
$this->connection = $connection;
|
||||
$this->systemConfig = $systemConfig;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -228,4 +242,97 @@ class QuerySearchHelper {
|
|||
$query->addOrderBy($order->getField(), $order->getDirection());
|
||||
}
|
||||
}
|
||||
|
||||
protected function getQueryBuilder() {
|
||||
return new CacheQueryBuilder(
|
||||
$this->connection,
|
||||
$this->systemConfig,
|
||||
$this->logger
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ISearchQuery $searchQuery
|
||||
* @param ICache[] $caches
|
||||
* @return CacheEntry[]
|
||||
*/
|
||||
public function searchInCaches(ISearchQuery $searchQuery, array $caches): array {
|
||||
// search in multiple caches at once by creating one query in the following format
|
||||
// SELECT ... FROM oc_filecache WHERE
|
||||
// <filter expressions from the search query>
|
||||
// AND (
|
||||
// <filter expression for storage1> OR
|
||||
// <filter expression for storage2> OR
|
||||
// ...
|
||||
// );
|
||||
//
|
||||
// This gives us all the files matching the search query from all caches
|
||||
//
|
||||
// while the resulting rows don't have a way to tell what storage they came from (multiple storages/caches can share storage_id)
|
||||
// we can just ask every cache if the row belongs to them and give them the cache to do any post processing on the result.
|
||||
|
||||
$builder = $this->getQueryBuilder();
|
||||
|
||||
$query = $builder->selectFileCache('file');
|
||||
|
||||
$storageFilters = array_map(function (ICache $cache) use ($builder) {
|
||||
return $cache->getQueryFilterForStorage($builder);
|
||||
}, $caches);
|
||||
|
||||
$query->andWhere($query->expr()->orX(...$storageFilters));
|
||||
|
||||
if ($this->shouldJoinTags($searchQuery->getSearchOperation())) {
|
||||
$user = $searchQuery->getUser();
|
||||
if ($user === null) {
|
||||
throw new \InvalidArgumentException("Searching by tag requires the user to be set in the query");
|
||||
}
|
||||
$query
|
||||
->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid'))
|
||||
->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX(
|
||||
$builder->expr()->eq('tagmap.type', 'tag.type'),
|
||||
$builder->expr()->eq('tagmap.categoryid', 'tag.id')
|
||||
))
|
||||
->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files')))
|
||||
->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($user->getUID())));
|
||||
}
|
||||
|
||||
$searchExpr = $this->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation());
|
||||
if ($searchExpr) {
|
||||
$query->andWhere($searchExpr);
|
||||
}
|
||||
|
||||
if ($searchQuery->limitToHome() && ($this instanceof HomeCache)) {
|
||||
$query->andWhere($builder->expr()->like('path', $query->expr()->literal('files/%')));
|
||||
}
|
||||
|
||||
$this->addSearchOrdersToQuery($query, $searchQuery->getOrder());
|
||||
|
||||
if ($searchQuery->getLimit()) {
|
||||
$query->setMaxResults($searchQuery->getLimit());
|
||||
}
|
||||
if ($searchQuery->getOffset()) {
|
||||
$query->setFirstResult($searchQuery->getOffset());
|
||||
}
|
||||
|
||||
$result = $query->execute();
|
||||
$files = $result->fetchAll();
|
||||
|
||||
$rawEntries = array_map(function (array $data) {
|
||||
return Cache::cacheEntryFromData($data, $this->mimetypeLoader);
|
||||
}, $files);
|
||||
|
||||
$result->closeCursor();
|
||||
|
||||
// loop trough all caches for each result to see if the result matches that storage
|
||||
$results = [];
|
||||
foreach ($rawEntries as $rawEntry) {
|
||||
foreach ($caches as $cache) {
|
||||
$entry = $cache->getCacheEntryFromSearchResult($rawEntry);
|
||||
if ($entry) {
|
||||
$results[] = $entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,14 +29,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
|
||||
|
@ -108,10 +102,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']);
|
||||
|
@ -230,99 +220,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
|
||||
*
|
||||
|
@ -404,4 +301,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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,6 +31,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;
|
||||
|
@ -46,6 +48,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() {
|
||||
|
@ -216,31 +226,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]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -322,4 +309,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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ namespace OC\Lockdown\Filesystem;
|
|||
|
||||
use OC\Files\Cache\CacheEntry;
|
||||
use OCP\Constants;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\Files\Cache\ICache;
|
||||
use OCP\Files\Cache\ICacheEntry;
|
||||
use OCP\Files\FileInfo;
|
||||
|
@ -127,4 +128,12 @@ class NullCache implements ICache {
|
|||
public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int {
|
||||
throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
|
||||
}
|
||||
|
||||
public function getQueryFilterForStorage(IQueryBuilder $builder) {
|
||||
return 'false';
|
||||
}
|
||||
|
||||
public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
|
||||
namespace OCP\Files\Cache;
|
||||
|
||||
use OCP\DB\QueryBuilder\ICompositeExpression;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\Files\Search\ISearchQuery;
|
||||
|
||||
/**
|
||||
|
@ -265,4 +267,31 @@ interface ICache {
|
|||
* @since 9.0.0
|
||||
*/
|
||||
public function normalize($path);
|
||||
|
||||
/**
|
||||
* Get the query expression required to filter files within this storage.
|
||||
*
|
||||
* In the most basic case this is just `$builder->expr()->eq('storage', $this->getNumericStorageId())`
|
||||
* but storage wrappers can add additional expressions to filter down things further
|
||||
*
|
||||
* @param IQueryBuilder $builder
|
||||
* @return string|ICompositeExpression
|
||||
* @since 22.0.0
|
||||
*/
|
||||
public function getQueryFilterForStorage(IQueryBuilder $builder);
|
||||
|
||||
/**
|
||||
* Construct a cache entry from a search result row *if* the entry belongs to this storage.
|
||||
*
|
||||
* This method will be called for every item in the search results, including results from different storages.
|
||||
* It's the responsibility of this method to return `null` for all results that don't belong to this storage.
|
||||
*
|
||||
* Additionally some implementations might need to further process the resulting entry such as modifying the path
|
||||
* or permissions of the result.
|
||||
*
|
||||
* @param ICacheEntry $rawEntry
|
||||
* @return ICacheEntry|null
|
||||
* @since 22.0.0
|
||||
*/
|
||||
public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry;
|
||||
}
|
||||
|
|
|
@ -363,7 +363,7 @@ class CacheTest extends \Test\TestCase {
|
|||
$this->assertEquals(3, count($results));
|
||||
|
||||
usort($results, function ($value1, $value2) {
|
||||
return $value1['name'] >= $value2['name'];
|
||||
return $value1['name'] <=> $value2['name'];
|
||||
});
|
||||
|
||||
$this->assertEquals('folder', $results[0]['name']);
|
||||
|
@ -376,7 +376,10 @@ class CacheTest extends \Test\TestCase {
|
|||
static::logout();
|
||||
$user = \OC::$server->getUserManager()->get($userId);
|
||||
if ($user !== null) {
|
||||
$user->delete();
|
||||
try {
|
||||
$user->delete();
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,11 +25,14 @@ use OC\DB\QueryBuilder\Literal;
|
|||
use OC\Files\Cache\QuerySearchHelper;
|
||||
use OC\Files\Search\SearchBinaryOperator;
|
||||
use OC\Files\Search\SearchComparison;
|
||||
use OC\SystemConfig;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\Files\IMimeTypeLoader;
|
||||
use OCP\Files\Search\ISearchBinaryOperator;
|
||||
use OCP\Files\Search\ISearchComparison;
|
||||
use OCP\Files\Search\ISearchOperator;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\ILogger;
|
||||
use Test\TestCase;
|
||||
|
||||
/**
|
||||
|
@ -75,7 +78,15 @@ class QuerySearchHelperTest extends TestCase {
|
|||
[6, 'image']
|
||||
]);
|
||||
|
||||
$this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader);
|
||||
$systemConfig = $this->createMock(SystemConfig::class);
|
||||
$logger = $this->createMock(ILogger::class);
|
||||
|
||||
$this->querySearchHelper = new QuerySearchHelper(
|
||||
$this->mimetypeLoader,
|
||||
\OC::$server->get(IDBConnection::class),
|
||||
$systemConfig,
|
||||
$logger
|
||||
);
|
||||
$this->numericStorageId = 10000;
|
||||
|
||||
$this->builder->select(['fileid'])
|
||||
|
|
Loading…
Reference in New Issue