Merge pull request #5415 from nextcloud/search-cache-node

cache nodes from search results
This commit is contained in:
Morris Jobke 2017-09-06 10:16:01 +02:00 committed by GitHub
commit 1724fed8c5
6 changed files with 55 additions and 15 deletions

View File

@ -0,0 +1,39 @@
<?php
/**
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
*
* @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 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;
}
}

View File

@ -39,7 +39,7 @@ use OCP\Files\StorageInvalidException;
use OCP\Files\StorageNotAvailableException; use OCP\Files\StorageNotAvailableException;
use OCP\Lock\LockedException; use OCP\Lock\LockedException;
class ObjectTree extends \Sabre\DAV\Tree { class ObjectTree extends CachingTree {
/** /**
* @var \OC\Files\View * @var \OC\Files\View
@ -97,10 +97,6 @@ class ObjectTree extends \Sabre\DAV\Tree {
return $path; return $path;
} }
public function cacheNode(Node $node) {
$this->cache[trim($node->getPath(), '/')] = $node;
}
/** /**
* Returns the INode object for the requested path * Returns the INode object for the requested path
* *

View File

@ -33,6 +33,7 @@ namespace OCA\DAV\Connector\Sabre;
* @see \Sabre\DAV\Server * @see \Sabre\DAV\Server
*/ */
class Server extends \Sabre\DAV\Server { class Server extends \Sabre\DAV\Server {
/** @var CachingTree $tree */
/** /**
* @see \Sabre\DAV\Server * @see \Sabre\DAV\Server

View File

@ -26,6 +26,7 @@ use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchOrder; use OC\Files\Search\SearchOrder;
use OC\Files\Search\SearchQuery; use OC\Files\Search\SearchQuery;
use OC\Files\View; use OC\Files\View;
use OCA\DAV\Connector\Sabre\CachingTree;
use OCA\DAV\Connector\Sabre\Directory; use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\FilesPlugin; use OCA\DAV\Connector\Sabre\FilesPlugin;
use OCA\DAV\Connector\Sabre\TagsPlugin; use OCA\DAV\Connector\Sabre\TagsPlugin;
@ -39,7 +40,6 @@ use OCP\Files\Search\ISearchQuery;
use OCP\IUser; use OCP\IUser;
use OCP\Share\IManager; use OCP\Share\IManager;
use Sabre\DAV\Exception\NotFound; use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\Tree;
use SearchDAV\Backend\ISearchBackend; use SearchDAV\Backend\ISearchBackend;
use SearchDAV\Backend\SearchPropertyDefinition; use SearchDAV\Backend\SearchPropertyDefinition;
use SearchDAV\Backend\SearchResult; use SearchDAV\Backend\SearchResult;
@ -49,7 +49,7 @@ use SearchDAV\XML\Operator;
use SearchDAV\XML\Order; use SearchDAV\XML\Order;
class FileSearchBackend implements ISearchBackend { class FileSearchBackend implements ISearchBackend {
/** @var Tree */ /** @var CachingTree */
private $tree; private $tree;
/** @var IUser */ /** @var IUser */
@ -67,14 +67,14 @@ class FileSearchBackend implements ISearchBackend {
/** /**
* FileSearchBackend constructor. * FileSearchBackend constructor.
* *
* @param Tree $tree * @param CachingTree $tree
* @param IUser $user * @param IUser $user
* @param IRootFolder $rootFolder * @param IRootFolder $rootFolder
* @param IManager $shareManager * @param IManager $shareManager
* @param View $view * @param View $view
* @internal param IRootFolder $rootFolder * @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->tree = $tree;
$this->user = $user; $this->user = $user;
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
@ -157,10 +157,13 @@ class FileSearchBackend implements ISearchBackend {
return array_map(function (Node $node) { return array_map(function (Node $node) {
if ($node instanceof Folder) { 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 { } 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); }, $results);
} }

View File

@ -35,6 +35,7 @@ use OCA\DAV\Comments\CommentsPlugin;
use OCA\DAV\Connector\Sabre\Auth; use OCA\DAV\Connector\Sabre\Auth;
use OCA\DAV\Connector\Sabre\BearerAuth; use OCA\DAV\Connector\Sabre\BearerAuth;
use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin; use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
use OCA\DAV\Connector\Sabre\CachingTree;
use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin; use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin;
use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin; use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
use OCA\DAV\Connector\Sabre\DavAclPlugin; use OCA\DAV\Connector\Sabre\DavAclPlugin;
@ -75,7 +76,7 @@ class Server {
$dispatcher = \OC::$server->getEventDispatcher(); $dispatcher = \OC::$server->getEventDispatcher();
$root = new RootCollection(); $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 // Add maintenance plugin
$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig())); $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig()));

View File

@ -24,6 +24,7 @@ namespace OCA\DAV\Tests\Files;
use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery; use OC\Files\Search\SearchQuery;
use OC\Files\View; use OC\Files\View;
use OCA\DAV\Connector\Sabre\CachingTree;
use OCA\DAV\Connector\Sabre\Directory; use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\File; use OCA\DAV\Connector\Sabre\File;
use OCA\DAV\Connector\Sabre\FilesPlugin; use OCA\DAV\Connector\Sabre\FilesPlugin;
@ -34,7 +35,6 @@ use OCP\Files\IRootFolder;
use OCP\Files\Search\ISearchComparison; use OCP\Files\Search\ISearchComparison;
use OCP\IUser; use OCP\IUser;
use OCP\Share\IManager; use OCP\Share\IManager;
use Sabre\DAV\Tree;
use SearchDAV\XML\BasicSearch; use SearchDAV\XML\BasicSearch;
use SearchDAV\XML\Literal; use SearchDAV\XML\Literal;
use SearchDAV\XML\Operator; use SearchDAV\XML\Operator;
@ -42,7 +42,7 @@ use SearchDAV\XML\Scope;
use Test\TestCase; use Test\TestCase;
class FileSearchBackendTest extends TestCase { class FileSearchBackendTest extends TestCase {
/** @var Tree|\PHPUnit_Framework_MockObject_MockObject */ /** @var CachingTree|\PHPUnit_Framework_MockObject_MockObject */
private $tree; private $tree;
/** @var IUser */ /** @var IUser */
@ -74,7 +74,7 @@ class FileSearchBackendTest extends TestCase {
->method('getUID') ->method('getUID')
->willReturn('test'); ->willReturn('test');
$this->tree = $this->getMockBuilder(Tree::class) $this->tree = $this->getMockBuilder(CachingTree::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();