diff options
author | Morris Jobke <hey@morrisjobke.de> | 2018-09-27 17:20:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-27 17:20:13 +0200 |
commit | 065ec741734861923b5a8d5c61c10d3b6ecde378 (patch) | |
tree | 0db59a9635d909e9e1fbd69c8886191069bd4825 /lib | |
parent | 9c0a9a4d4692e42443aee2ae1b84e5559bb30da0 (diff) | |
parent | 6dae2b92508696c6b47216099abdd3f3fcd8497e (diff) | |
download | nextcloud-server-065ec741734861923b5a8d5c61c10d3b6ecde378.tar.gz nextcloud-server-065ec741734861923b5a8d5c61c10d3b6ecde378.zip |
Merge pull request #10884 from nextcloud/large-share-count-performance-13
[13] Improve performance when dealing with large numbers of shares
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Files/Config/UserMountCache.php | 51 | ||||
-rw-r--r-- | lib/private/Files/Mount/Manager.php | 55 | ||||
-rw-r--r-- | lib/private/Files/View.php | 20 | ||||
-rw-r--r-- | lib/private/Share20/Manager.php | 4 |
4 files changed, 81 insertions, 49 deletions
diff --git a/lib/private/Files/Config/UserMountCache.php b/lib/private/Files/Config/UserMountCache.php index 255314c4e83..07c6e1f2d62 100644 --- a/lib/private/Files/Config/UserMountCache.php +++ b/lib/private/Files/Config/UserMountCache.php @@ -102,17 +102,31 @@ class UserMountCache implements IUserMountCache { } }, $mounts); $newMounts = array_values(array_filter($newMounts)); + $newMountRootIds = array_map(function (ICachedMountInfo $mount) { + return $mount->getRootId(); + }, $newMounts); + $newMounts = array_combine($newMountRootIds, $newMounts); $cachedMounts = $this->getMountsForUser($user); - $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) { - // since we are only looking for mounts for a specific user comparing on root id is enough - return $mount1->getRootId() - $mount2->getRootId(); - }; + $cachedMountRootIds = array_map(function (ICachedMountInfo $mount) { + return $mount->getRootId(); + }, $cachedMounts); + $cachedMounts = array_combine($cachedMountRootIds, $cachedMounts); - /** @var ICachedMountInfo[] $addedMounts */ - $addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff); - /** @var ICachedMountInfo[] $removedMounts */ - $removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff); + $addedMounts = []; + $removedMounts = []; + + foreach ($newMounts as $rootId => $newMount) { + if (!isset($cachedMounts[$rootId])) { + $addedMounts[] = $newMount; + } + } + + foreach ($cachedMounts as $rootId => $cachedMount) { + if (!isset($newMounts[$rootId])) { + $removedMounts[] = $cachedMount; + } + } $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts); @@ -136,16 +150,19 @@ class UserMountCache implements IUserMountCache { * @return ICachedMountInfo[] */ private function findChangedMounts(array $newMounts, array $cachedMounts) { + $new = []; + foreach ($newMounts as $mount) { + $new[$mount->getRootId()] = $mount; + } $changed = []; - foreach ($newMounts as $newMount) { - foreach ($cachedMounts as $cachedMount) { + foreach ($cachedMounts as $cachedMount) { + $rootId = $cachedMount->getRootId(); + if (isset($new[$rootId])) { + $newMount = $new[$rootId]; if ( - $newMount->getRootId() === $cachedMount->getRootId() && - ( - $newMount->getMountPoint() !== $cachedMount->getMountPoint() || - $newMount->getStorageId() !== $cachedMount->getStorageId() || - $newMount->getMountId() !== $cachedMount->getMountId() - ) + $newMount->getMountPoint() !== $cachedMount->getMountPoint() || + $newMount->getStorageId() !== $cachedMount->getStorageId() || + $newMount->getMountId() !== $cachedMount->getMountId() ) { $changed[] = $newMount; } @@ -198,7 +215,7 @@ class UserMountCache implements IUserMountCache { } $mount_id = $row['mount_id']; if (!is_null($mount_id)) { - $mount_id = (int) $mount_id; + $mount_id = (int)$mount_id; } return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $mount_id, isset($row['path']) ? $row['path'] : ''); } diff --git a/lib/private/Files/Mount/Manager.php b/lib/private/Files/Mount/Manager.php index 7bd888a6389..ee524366535 100644 --- a/lib/private/Files/Mount/Manager.php +++ b/lib/private/Files/Mount/Manager.php @@ -25,21 +25,28 @@ namespace OC\Files\Mount; +use OC\Cache\CappedMemoryCache; use \OC\Files\Filesystem; use OCP\Files\Mount\IMountManager; use OCP\Files\Mount\IMountPoint; class Manager implements IMountManager { - /** - * @var MountPoint[] - */ - private $mounts = array(); + /** @var MountPoint[] */ + private $mounts = []; + + /** @var CappedMemoryCache */ + private $inPathCache; + + public function __construct() { + $this->inPathCache = new CappedMemoryCache(); + } /** * @param IMountPoint $mount */ public function addMount(IMountPoint $mount) { $this->mounts[$mount->getMountPoint()] = $mount; + $this->inPathCache->clear(); } /** @@ -51,15 +58,17 @@ class Manager implements IMountManager { $mountPoint .= '/'; } unset($this->mounts[$mountPoint]); + $this->inPathCache->clear(); } /** * @param string $mountPoint * @param string $target */ - public function moveMount($mountPoint, $target){ + public function moveMount($mountPoint, $target) { $this->mounts[$target] = $this->mounts[$mountPoint]; unset($this->mounts[$mountPoint]); + $this->inPathCache->clear(); } /** @@ -70,23 +79,24 @@ class Manager implements IMountManager { */ public function find($path) { \OC_Util::setupFS(); - $path = $this->formatPath($path); - if (isset($this->mounts[$path])) { - return $this->mounts[$path]; - } + $path = Filesystem::normalizePath($path); - \OC_Hook::emit('OC_Filesystem', 'get_mountpoint', array('path' => $path)); - $foundMountPoint = ''; - $mountPoints = array_keys($this->mounts); - foreach ($mountPoints as $mountpoint) { - if (strpos($path, $mountpoint) === 0 and strlen($mountpoint) > strlen($foundMountPoint)) { - $foundMountPoint = $mountpoint; + $current = $path; + while (true) { + $mountPoint = $current . '/'; + if (isset($this->mounts[$mountPoint])) { + $this->pathCache[$path] = $this->mounts[$mountPoint]; + return $this->mounts[$mountPoint]; + } + + if ($current === '') { + return null; + } + + $current = dirname($current); + if ($current === '.' || $current === '/') { + $current = ''; } - } - if (isset($this->mounts[$foundMountPoint])) { - return $this->mounts[$foundMountPoint]; - } else { - return null; } } @@ -107,11 +117,14 @@ class Manager implements IMountManager { $result[] = $this->mounts[$mountPoint]; } } + + $this->inPathCache[$path] = $result; return $result; } public function clear() { - $this->mounts = array(); + $this->mounts = []; + $this->inPathCache->clear(); } /** diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 73219635e89..cd1219fd77e 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -1431,16 +1431,21 @@ class View { $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); + + $fileNames = array_map(function(ICacheEntry $content) { + return $content->getName(); + }, $contents); /** - * @var \OC\Files\FileInfo[] $files + * @var \OC\Files\FileInfo[] $fileInfos */ - $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { + $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { if ($sharingDisabled) { $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; } $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); }, $contents); + $files = array_combine($fileNames, $fileInfos); //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders $mounts = Filesystem::getMountManager()->findIn($path); @@ -1495,13 +1500,6 @@ class View { $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); } - //remove any existing entry with the same name - foreach ($files as $i => $file) { - if ($file['name'] === $rootEntry['name']) { - unset($files[$i]); - break; - } - } $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ // if sharing was disabled for the user we remove the share permissions @@ -1510,7 +1508,7 @@ class View { } $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); - $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); + $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); } } } @@ -1526,7 +1524,7 @@ class View { }); } - return $files; + return array_values($files); } else { return []; } diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index b8131425b4a..69a3d749a99 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -1165,6 +1165,10 @@ class Manager implements IManager { * @throws ShareNotFound */ public function getShareByToken($token) { + // tokens can't be valid local user names + if ($this->userManager->userExists($token)) { + throw new ShareNotFound(); + } $share = null; try { if($this->shareApiAllowLinks()) { |