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

Optimize Node::getParent and usage in previews #33602

Merged
merged 3 commits into from
Aug 25, 2022
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
14 changes: 8 additions & 6 deletions lib/private/Files/Node/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public function getDirectoryListing() {

return array_map(function (FileInfo $info) {
if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) {
return new Folder($this->root, $this->view, $info->getPath(), $info);
return new Folder($this->root, $this->view, $info->getPath(), $info, $this);
} else {
return new File($this->root, $this->view, $info->getPath(), $info);
return new File($this->root, $this->view, $info->getPath(), $info, $this);
}
}, $folderContent);
}
Expand All @@ -119,10 +119,11 @@ protected function createNode($path, FileInfo $info = null) {
} else {
$isDir = $info->getType() === FileInfo::TYPE_FOLDER;
}
$parent = dirname($path) === $this->getPath() ? $this : null;
if ($isDir) {
return new Folder($this->root, $this->view, $path, $info);
return new Folder($this->root, $this->view, $path, $info, $parent);
} else {
return new File($this->root, $this->view, $path, $info);
return new File($this->root, $this->view, $path, $info, $parent);
}
}

Expand Down Expand Up @@ -163,7 +164,8 @@ public function newFolder($path) {
if (!$this->view->mkdir($fullPath)) {
throw new NotPermittedException('Could not create folder');
}
$node = new Folder($this->root, $this->view, $fullPath);
$parent = dirname($fullPath) === $this->getPath() ? $this : null;
$node = new Folder($this->root, $this->view, $fullPath, null, $parent);
$this->sendHooks(['postWrite', 'postCreate'], [$node]);
return $node;
} else {
Expand Down Expand Up @@ -193,7 +195,7 @@ public function newFile($path, $content = null) {
if ($result === false) {
throw new NotPermittedException('Could not create path');
}
$node = new File($this->root, $this->view, $fullPath);
$node = new File($this->root, $this->view, $fullPath, null, $this);
$this->sendHooks(['postWrite', 'postCreate'], [$node]);
return $node;
}
Expand Down
21 changes: 16 additions & 5 deletions lib/private/Files/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,23 @@ class Node implements \OCP\Files\Node {
*/
protected $fileInfo;

/**
* @var Node|null
*/
protected $parent;

/**
* @param \OC\Files\View $view
* @param \OCP\Files\IRootFolder $root
* @param string $path
* @param FileInfo $fileInfo
*/
public function __construct($root, $view, $path, $fileInfo = null) {
public function __construct($root, $view, $path, $fileInfo = null, ?Node $parent = null) {
$this->view = $view;
$this->root = $root;
$this->path = $path;
$this->fileInfo = $fileInfo;
$this->parent = $parent;
Fixed Show fixed Hide fixed
}

/**
Expand Down Expand Up @@ -278,11 +284,16 @@ public function isCreatable() {
* @return Node
*/
public function getParent() {
$newPath = dirname($this->path);
if ($newPath === '' || $newPath === '.' || $newPath === '/') {
return $this->root;
if ($this->parent === null) {
$newPath = dirname($this->path);
if ($newPath === '' || $newPath === '.' || $newPath === '/') {
return $this->root;
}

$this->parent = $this->root->get($newPath);
Fixed Show fixed Hide fixed
}
return $this->root->get($newPath);

return $this->parent;
Fixed Show fixed Hide fixed
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Preview/ProviderV1Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
}

private function getViewAndPath(File $file) {
$view = new View($file->getParent()->getPath());
$view = new View(dirname($file->getPath()));
$path = $file->getName();

return [$view, $path];
Expand Down
40 changes: 35 additions & 5 deletions tests/lib/Files/Node/FolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OC\Files\FileInfo;
use OC\Files\Mount\Manager;
use OC\Files\Mount\MountPoint;
use OC\Files\Node\File;
use OC\Files\Node\Folder;
use OC\Files\Node\Node;
use OC\Files\Node\Root;
Expand Down Expand Up @@ -105,11 +106,13 @@ public function testGet() {
->method('getUser')
->willReturn($this->user);

$node = new File($root, $view, '/bar/foo/asd');
$root->method('get')
->with('/bar/foo/asd');
->with('/bar/foo/asd')
->willReturn($node);

$node = new Folder($root, $view, '/bar/foo');
$node->get('asd');
$parentNode = new Folder($root, $view, '/bar/foo');
self::assertEquals($node, $parentNode->get('asd'));
}

public function testNodeExists() {
Expand Down Expand Up @@ -178,11 +181,38 @@ public function testNewFolder() {
->willReturn(true);

$node = new Folder($root, $view, '/bar/foo');
$child = new Folder($root, $view, '/bar/foo/asd');
$child = new Folder($root, $view, '/bar/foo/asd', null, $node);
$result = $node->newFolder('asd');
$this->assertEquals($child, $result);
}

public function testNewFolderDeepParent() {
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
$view = $this->createMock(View::class);
$root = $this->getMockBuilder(Root::class)
->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher])
->getMock();
$root->expects($this->any())
->method('getUser')
->willReturn($this->user);

$view->method('getFileInfo')
->with('/foobar')
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));

$view->method('mkdir')
->with('/foobar/asd/sdf')
->willReturn(true);

$node = new Folder($root, $view, '/foobar');
$child = new Folder($root, $view, '/foobar/asd/sdf', null, null);
$result = $node->newFolder('asd/sdf');
$this->assertEquals($child, $result);
}


public function testNewFolderNotPermitted() {
$this->expectException(\OCP\Files\NotPermittedException::class);
Expand Down Expand Up @@ -228,7 +258,7 @@ public function testNewFile() {
->willReturn(true);

$node = new Folder($root, $view, '/bar/foo');
$child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd');
$child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd', null, $node);
$result = $node->newFile('asd');
$this->assertEquals($child, $result);
}
Expand Down