From 37e8b698f71481fe420a8eb248a5f604cd732cbd Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 14 Jun 2017 18:11:55 +0200 Subject: [PATCH] cache nodes from search results Signed-off-by: Robin Appelman --- apps/dav/lib/Connector/Sabre/CachingTree.php | 39 ++++++++++++++++++++ apps/dav/lib/Connector/Sabre/ObjectTree.php | 6 +-- apps/dav/lib/Connector/Sabre/Server.php | 1 + apps/dav/lib/Files/FileSearchBackend.php | 15 +++++--- apps/dav/lib/Server.php | 3 +- 5 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 apps/dav/lib/Connector/Sabre/CachingTree.php diff --git a/apps/dav/lib/Connector/Sabre/CachingTree.php b/apps/dav/lib/Connector/Sabre/CachingTree.php new file mode 100644 index 0000000000..80c8311fed --- /dev/null +++ b/apps/dav/lib/Connector/Sabre/CachingTree.php @@ -0,0 +1,39 @@ + + * + * @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 . + * + */ + +namespace OCA\DAV\Connector\Sabre; + +use Sabre\DAV\Tree; + +class CachingTree extends Tree { + /** + * Store a node in the cache + * + * @param Node $node + * @param null|string $path + */ + public function cacheNode(Node $node, $path = null) { + if (is_null($path)) { + $path = $node->getPath(); + } + $this->cache[trim($path, '/')] = $node; + } +} diff --git a/apps/dav/lib/Connector/Sabre/ObjectTree.php b/apps/dav/lib/Connector/Sabre/ObjectTree.php index 3371c655f2..a44b25c24c 100644 --- a/apps/dav/lib/Connector/Sabre/ObjectTree.php +++ b/apps/dav/lib/Connector/Sabre/ObjectTree.php @@ -39,7 +39,7 @@ use OCP\Files\StorageInvalidException; use OCP\Files\StorageNotAvailableException; use OCP\Lock\LockedException; -class ObjectTree extends \Sabre\DAV\Tree { +class ObjectTree extends CachingTree { /** * @var \OC\Files\View @@ -97,10 +97,6 @@ class ObjectTree extends \Sabre\DAV\Tree { return $path; } - public function cacheNode(Node $node) { - $this->cache[trim($node->getPath(), '/')] = $node; - } - /** * Returns the INode object for the requested path * diff --git a/apps/dav/lib/Connector/Sabre/Server.php b/apps/dav/lib/Connector/Sabre/Server.php index 9915ffdba0..686b0ea760 100644 --- a/apps/dav/lib/Connector/Sabre/Server.php +++ b/apps/dav/lib/Connector/Sabre/Server.php @@ -33,6 +33,7 @@ namespace OCA\DAV\Connector\Sabre; * @see \Sabre\DAV\Server */ class Server extends \Sabre\DAV\Server { + /** @var CachingTree $tree */ /** * @see \Sabre\DAV\Server diff --git a/apps/dav/lib/Files/FileSearchBackend.php b/apps/dav/lib/Files/FileSearchBackend.php index 0d837807fd..515e7ed012 100644 --- a/apps/dav/lib/Files/FileSearchBackend.php +++ b/apps/dav/lib/Files/FileSearchBackend.php @@ -26,6 +26,7 @@ use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchOrder; use OC\Files\Search\SearchQuery; use OC\Files\View; +use OCA\DAV\Connector\Sabre\CachingTree; use OCA\DAV\Connector\Sabre\Directory; use OCA\DAV\Connector\Sabre\FilesPlugin; use OCA\DAV\Connector\Sabre\TagsPlugin; @@ -39,7 +40,6 @@ use OCP\Files\Search\ISearchQuery; use OCP\IUser; use OCP\Share\IManager; use Sabre\DAV\Exception\NotFound; -use Sabre\DAV\Tree; use SearchDAV\Backend\ISearchBackend; use SearchDAV\Backend\SearchPropertyDefinition; use SearchDAV\Backend\SearchResult; @@ -49,7 +49,7 @@ use SearchDAV\XML\Operator; use SearchDAV\XML\Order; class FileSearchBackend implements ISearchBackend { - /** @var Tree */ + /** @var CachingTree */ private $tree; /** @var IUser */ @@ -67,14 +67,14 @@ class FileSearchBackend implements ISearchBackend { /** * FileSearchBackend constructor. * - * @param Tree $tree + * @param CachingTree $tree * @param IUser $user * @param IRootFolder $rootFolder * @param IManager $shareManager * @param View $view * @internal param IRootFolder $rootFolder */ - public function __construct(Tree $tree, IUser $user, IRootFolder $rootFolder, IManager $shareManager, View $view) { + public function __construct(CachingTree $tree, IUser $user, IRootFolder $rootFolder, IManager $shareManager, View $view) { $this->tree = $tree; $this->user = $user; $this->rootFolder = $rootFolder; @@ -157,10 +157,13 @@ class FileSearchBackend implements ISearchBackend { return array_map(function (Node $node) { if ($node instanceof Folder) { - return new SearchResult(new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager), $this->getHrefForNode($node)); + $davNode = new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager); } else { - return new SearchResult(new \OCA\DAV\Connector\Sabre\File($this->view, $node, $this->shareManager), $this->getHrefForNode($node)); + $davNode = new \OCA\DAV\Connector\Sabre\File($this->view, $node, $this->shareManager); } + $path = $this->getHrefForNode($node); + $this->tree->cacheNode($davNode, $path); + return new SearchResult($davNode, $path); }, $results); } diff --git a/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index 994ac04033..407e7e28e4 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -35,6 +35,7 @@ use OCA\DAV\Comments\CommentsPlugin; use OCA\DAV\Connector\Sabre\Auth; use OCA\DAV\Connector\Sabre\BearerAuth; use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin; +use OCA\DAV\Connector\Sabre\CachingTree; use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin; use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin; use OCA\DAV\Connector\Sabre\DavAclPlugin; @@ -75,7 +76,7 @@ class Server { $dispatcher = \OC::$server->getEventDispatcher(); $root = new RootCollection(); - $this->server = new \OCA\DAV\Connector\Sabre\Server($root); + $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root)); // Add maintenance plugin $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig()));