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

Build parent path without queries #445

Merged
merged 3 commits into from
Jan 5, 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
6 changes: 4 additions & 2 deletions lib/Model/PageInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,18 @@ public function jsonSerialize(): array {
public function fromFile(File $file, int $parentId, ?string $lastUserId = null, ?string $lastUserDisplayName = null, ?string $emoji = null, ?string $subpageOrder = null): void {
$this->setId($file->getId());
// Set folder name as title for all index pages except the collective landing page
$dirName = dirname($file->getInternalPath());
$dirName = $dirName === '.' ? '' : $dirName;
if ($parentId !== 0 && 0 === strcmp($file->getName(), self::INDEX_PAGE_TITLE . self::SUFFIX)) {
$this->setTitle($file->getParent()->getName());
$this->setTitle(basename($dirName));
} else {
$this->setTitle(basename($file->getName(), self::SUFFIX));
}
$this->setFilePath($dirName);
$this->setTimestamp($file->getMTime());
$this->setSize($file->getSize());
$this->setFileName($file->getName());
$this->setCollectivePath(rtrim(explode('/', $file->getMountPoint()->getMountPoint(), 4)[3], '/'));
$this->setFilePath($file->getParent()->getInternalPath());
if (null !== $lastUserId) {
$this->setLastUserId($lastUserId);
}
Expand Down
19 changes: 13 additions & 6 deletions lib/Mount/CollectiveFolderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class CollectiveFolderManager {
private IRequest $request;
private ?string $rootPath = null;

/** @var int|null */
private $rootFolderStorageId = null;

/**
* CollectiveFolderManager constructor.
*
Expand Down Expand Up @@ -185,14 +188,18 @@ private function getJailPath(int $folderId): string {
* @throws NotFoundException
*/
private function getRootFolderStorageId(): int {
$qb = $this->connection->getQueryBuilder();
if ($this->rootFolderStorageId === null) {
$qb = $this->connection->getQueryBuilder();

$qb->select('fileid')
->from('filecache')
->where($qb->expr()->eq('storage', $qb->createNamedParameter($this->getRootFolder()->getStorage()->getCache()->getNumericStorageId())))
->andWhere($qb->expr()->eq('path_hash', $qb->createNamedParameter(md5($this->getRootPath()))));
$qb->select('fileid')
->from('filecache')
->where($qb->expr()->eq('storage', $qb->createNamedParameter($this->getRootFolder()->getStorage()->getCache()->getNumericStorageId())))
->andWhere($qb->expr()->eq('path_hash', $qb->createNamedParameter(md5($this->getRootPath()))));

$this->rootFolderStorageId = (int)$qb->execute()->fetchColumn();
}

return (int)$qb->execute()->fetchColumn();
return $this->rootFolderStorageId;
}

/**
Expand Down
20 changes: 12 additions & 8 deletions lib/Service/PageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\InvalidPathException;
use OCP\Files\Node;
use OCP\Files\NotPermittedException as FilesNotPermittedException;
use OCP\Files\NotFoundException as FilesNotFoundException;
use OCP\IConfig;
Expand Down Expand Up @@ -136,35 +137,38 @@ public function getFolder(int $collectiveId, int $fileId, string $userId): Folde

/**
* @param File $file
* @param Node|null $parent
*
* @return int
* @throws NotFoundException
*/
public function getParentPageId(File $file): int {
private function getParentPageId(File $file, ?Node $parent = null): int {
try {
if (self::isLandingPage($file)) {
// Return `0` for landing page
return 0;
}

$parent = $parent ?? $file->getParent();

if (self::isIndexPage($file)) {
// Go down two levels if index page but not landing page
return $this->getIndexPageFile($file->getParent()->getParent())->getId();
return $this->getIndexPageFile($parent->getParent())->getId();
}

return $this->getIndexPageFile($file->getParent())->getId();
return $this->getIndexPageFile($parent)->getId();
} catch (InvalidPathException | FilesNotFoundException $e) {
throw new NotFoundException($e->getMessage(), 0, $e);
}
}

/**
* @param File $file
*
* @param Node|null $parent
* @return PageInfo
* @throws NotFoundException
*/
private function getPageByFile(File $file): PageInfo {
private function getPageByFile(File $file, ?Node $parent = null): PageInfo {
$pageInfo = new PageInfo();
try {
$page = $this->pageMapper->findByFileId($file->getId());
Expand All @@ -176,7 +180,7 @@ private function getPageByFile(File $file): PageInfo {
$subpageOrder = ($page !== null) ? $page->getSubpageOrder() : null;
try {
$pageInfo->fromFile($file,
$this->getParentPageId($file),
$this->getParentPageId($file, $parent),
$lastUserId,
$lastUserId ? $this->userManager->get($lastUserId)->getDisplayName() : null,
$emoji,
Expand Down Expand Up @@ -439,7 +443,7 @@ public static function folderHasSubPage(Folder $folder, string $title): int {
public function recurseFolder(Folder $folder, string $userId): array {
// Find index page or create it if we have subpages, but it doesn't exist
try {
$indexPage = $this->getPageByFile($this->getIndexPageFile($folder));
$indexPage = $this->getPageByFile($this->getIndexPageFile($folder), $folder);
} catch (NotFoundException $e) {
if (!self::folderHasSubPages($folder)) {
return [];
Expand All @@ -451,7 +455,7 @@ public function recurseFolder(Folder $folder, string $userId): array {
// Add subpages and recurse over subfolders
foreach ($folder->getDirectoryListing() as $node) {
if ($node instanceof File && self::isPage($node) && !self::isIndexPage($node)) {
$pageInfos[] = $this->getPageByFile($node);
$pageInfos[] = $this->getPageByFile($node, $folder);
} elseif ($node instanceof Folder) {
array_push($pageInfos, ...$this->recurseFolder($node, $userId));
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Model/PageInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function testFromFile(): void {
$fileMountPoint = '/files/user/Collectives/collective/';
$fileCollectivePath = 'Collectives/collective';
$parentInternalPath = 'path/to/file';
$internalPath = $parentInternalPath . '/' . $fileName;
$userId = 'jane';

$mountPoint = $this->getMockBuilder(MountPoint::class)
Expand All @@ -39,6 +40,7 @@ public function testFromFile(): void {
$file->method('getName')->willReturn($fileName);
$file->method('getMountPoint')->willReturn($mountPoint);
$file->method('getParent')->willReturn($parent);
$file->method('getInternalPath')->willReturn($internalPath);

$pageInfo = new PageInfo();
$pageInfo->fromFile($file, 1, $userId);
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Service/PageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ public function testRecurseFolder(): void {
->willReturn($folder);
$indexFile->method('getMountPoint')
->willReturn($mountPoint);
$indexFile->method('getInternalPath')
->willReturn('Collectives/testfolder/Readme.md');
$indexPage = new Page();
$this->pageMapper->method('findByFileId')
->willReturn($indexPage);
Expand All @@ -268,6 +270,9 @@ public function testRecurseFolder(): void {
->willReturn($folder);
$file->method('getMountPoint')
->willReturn($mountPoint);
$file->method('getInternalPath')
->willReturn('Collectives/testfolder/Readme.md');

$filesNotJustMd[] = $file;

// Only add markdown files to $filesJustMd
Expand Down