Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add INodeByPath interface to allow optimizing getNodeForPath #1060

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/DAV/INodeByPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Sabre\DAV;

/**
* INodeByPath.
*
* This interface adds a tiny bit of functionality to collections.
*
* Getting a node that is deep in the tree normally requires going trough each parent node
* which can cause a significant performance overhead.
*
* Implementing this interface allows solving this overhead by directly jumping to the target node.
*
* @copyright Copyright (C) Robin Appelman (https://icewind.nl/)
* @author Robin Appelman (https://icewind.nl/)
* @license http://sabre.io/license/ Modified BSD License
*/
interface INodeByPath
{
/**
* Returns the INode object for the requested path.
*
* In case where this collection can not retrieve the requested node
* but also can not determine that the node does not exists,
* null should be returned to signal that the caller should fallback
* to walking the directory tree.
*
* @param string $path
*
* @return INode|null
*/
public function getNodeForPath($path);
}
30 changes: 18 additions & 12 deletions lib/DAV/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class Tree
class Tree implements INodeByPath
{
/**
* The root node.
Expand Down Expand Up @@ -62,20 +62,26 @@ public function getNodeForPath($path)
return $this->rootNode;
}

// Attempting to fetch its parent
list($parentName, $baseName) = Uri\split($path);
$parts = explode('/', $path);
$node = $this->rootNode;

// If there was no parent, we must simply ask it from the root node.
if ('' === $parentName) {
$node = $this->rootNode->getChild($baseName);
} else {
// Otherwise, we recursively grab the parent and ask him/her.
$parent = $this->getNodeForPath($parentName);

if (!($parent instanceof ICollection)) {
while (count($parts)) {
if (!($node instanceof ICollection)) {
throw new Exception\NotFound('Could not find node at path: '.$path);
}
$node = $parent->getChild($baseName);

if ($node instanceof INodeByPath) {
$targetNode = $node->getNodeForPath(implode('/', $parts));
if ($targetNode instanceof Node) {
$node = $targetNode;
break;
}
}

$part = array_shift($parts);
if ('' !== $part) {
$node = $node->getChild($part);
}
}

$this->cache[$path] = $node;
Expand Down
25 changes: 25 additions & 0 deletions tests/Sabre/DAV/TreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public function testGetMultipleNodes2()
self::assertArrayHasKey('multi/1', $result);
self::assertArrayHasKey('multi/2', $result);
}

public function testGetSubTreeNode()
{
$tree = new TreeMock();
$this->assertInstanceOf(INode::class, $tree->getNodeForPath('subtree/sub/1'));
$this->assertInstanceOf(INode::class, $tree->getNodeForPath('subtree/2/3'));
}
}

class TreeMock extends Tree
Expand All @@ -126,6 +133,12 @@ public function __construct()
new TreeDirectoryTester('1', [
new TreeDirectoryTester('2'),
]),
new NodeByPathTester('subtree', [
new TreeFileTester('sub/1'),
new TreeDirectoryTester('2', [
new TreeFileTester('3'),
]),
]),
])
);
}
Expand Down Expand Up @@ -177,6 +190,18 @@ public function setName($name)
}
}

class NodeByPathTester extends SimpleCollection implements INodeByPath
{
public function getNodeForPath($path)
{
if (isset($this->children[$path])) {
return $this->children[$path];
} else {
return null;
}
}
}

class TreeFileTester extends File implements IProperties
{
public $name;
Expand Down