Allow preloading of properties in one go

Make it possible to not be affected by a N+1 issue when doing a search

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
This commit is contained in:
Carl Schwan 2022-04-08 15:57:51 +02:00
commit 9c1e52272c
2 changed files with 12 additions and 0 deletions

View file

@ -21,6 +21,7 @@
namespace SearchDAV\Backend;
use Sabre\DAV\INode;
use SearchDAV\Query\Query;
interface ISearchBackend {
@ -81,4 +82,10 @@ interface ISearchBackend {
* @return SearchResult[]
*/
public function search(Query $query);
/**
* @param INode[] $nodes
* @param string[] $requestProperties
*/
public function preloadPropertyFor(array $nodes, array $requestProperties): void;
}

View file

@ -22,6 +22,7 @@
namespace SearchDAV\DAV;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
use Sabre\HTTP\ResponseInterface;
@ -170,6 +171,10 @@ class SearchHandler {
private function getPropertiesIteratorResults($results, $propertyNames = [], $depth = 0): \Iterator {
$propFindType = $propertyNames ? PropFind::NORMAL : PropFind::ALLPROPS;
$this->searchBackend->preloadPropertyFor(array_map(function (SearchResult $result): INode {
return $result->node;
}, $results), $propertyNames);
foreach ($results as $result) {
$node = $result->node;
$propFind = new PropFind($result->href, (array)$propertyNames, $depth, $propFindType);