summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2018-08-24 17:10:17 +0200
committerGitHub <noreply@github.com>2018-08-24 17:10:17 +0200
commite8817ed3ea3a1bd74945029b01664abf37d3911e (patch)
tree822aabee8563357cda5cd07dc7978c355256222c /lib
parent6099786c8dbd86c0848909108eb2a5572ad7a4bb (diff)
parent67f3d6f0a6b87290cba0d6cf1f5e03e4e97e1f78 (diff)
downloadnextcloud-server-e8817ed3ea3a1bd74945029b01664abf37d3911e.tar.gz
nextcloud-server-e8817ed3ea3a1bd74945029b01664abf37d3911e.zip
Merge pull request #10724 from nextcloud/large-share-count-performance
Improve performance when dealing with large numbers of shares
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Files/Config/UserMountCache.php51
-rw-r--r--lib/private/Files/Mount/Manager.php50
-rw-r--r--lib/private/Files/View.php20
-rw-r--r--lib/private/Share20/Manager.php4
4 files changed, 78 insertions, 47 deletions
diff --git a/lib/private/Files/Config/UserMountCache.php b/lib/private/Files/Config/UserMountCache.php
index a6741652308..63abdf5fdeb 100644
--- a/lib/private/Files/Config/UserMountCache.php
+++ b/lib/private/Files/Config/UserMountCache.php
@@ -101,17 +101,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);
@@ -135,16 +149,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;
}
@@ -197,7 +214,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 019dda03a40..1293b8549a5 100644
--- a/lib/private/Files/Mount/Manager.php
+++ b/lib/private/Files/Mount/Manager.php
@@ -38,8 +38,12 @@ class Manager implements IMountManager {
/** @var CappedMemoryCache */
private $pathCache;
+ /** @var CappedMemoryCache */
+ private $inPathCache;
+
public function __construct() {
$this->pathCache = new CappedMemoryCache();
+ $this->inPathCache = new CappedMemoryCache();
}
/**
@@ -48,6 +52,7 @@ class Manager implements IMountManager {
public function addMount(IMountPoint $mount) {
$this->mounts[$mount->getMountPoint()] = $mount;
$this->pathCache->clear();
+ $this->inPathCache->clear();
}
/**
@@ -60,16 +65,18 @@ class Manager implements IMountManager {
}
unset($this->mounts[$mountPoint]);
$this->pathCache->clear();
+ $this->inPathCache->clear();
}
/**
* @param string $mountPoint
* @param string $target
*/
- public function moveMount(string $mountPoint, string $target){
+ public function moveMount(string $mountPoint, string $target) {
$this->mounts[$target] = $this->mounts[$mountPoint];
unset($this->mounts[$mountPoint]);
$this->pathCache->clear();
+ $this->inPathCache->clear();
}
/**
@@ -80,32 +87,29 @@ class Manager implements IMountManager {
*/
public function find(string $path) {
\OC_Util::setupFS();
- $path = $this->formatPath($path);
- if (isset($this->mounts[$path])) {
- return $this->mounts[$path];
- }
+ $path = Filesystem::normalizePath($path);
if (isset($this->pathCache[$path])) {
return $this->pathCache[$path];
}
- \OC_Hook::emit('OC_Filesystem', 'get_mountpoint', ['path' => $path]);
- $foundMountPoint = '';
- $mountPoints = array_keys($this->mounts);
- $foundMountPointLength = 0;
- foreach ($mountPoints as $mountpoint) {
- if (\strlen($mountpoint) > $foundMountPointLength && strpos($path, $mountpoint) === 0) {
- $foundMountPoint = $mountpoint;
- $foundMountPointLength = \strlen($foundMountPoint);
+ $current = $path;
+ while (true) {
+ $mountPoint = $current . '/';
+ if (isset($this->mounts[$mountPoint])) {
+ $this->pathCache[$path] = $this->mounts[$mountPoint];
+ return $this->mounts[$mountPoint];
}
- }
- if (isset($this->mounts[$foundMountPoint])) {
- $this->pathCache[$path] = $this->mounts[$foundMountPoint];
- return $this->mounts[$foundMountPoint];
- }
+ if ($current === '') {
+ return null;
+ }
- return null;
+ $current = dirname($current);
+ if ($current === '.' || $current === '/') {
+ $current = '';
+ }
+ }
}
/**
@@ -117,6 +121,11 @@ class Manager implements IMountManager {
public function findIn(string $path): array {
\OC_Util::setupFS();
$path = $this->formatPath($path);
+
+ if (isset($this->inPathCache[$path])) {
+ return $this->inPathCache[$path];
+ }
+
$result = [];
$pathLength = \strlen($path);
$mountPoints = array_keys($this->mounts);
@@ -125,12 +134,15 @@ class Manager implements IMountManager {
$result[] = $this->mounts[$mountPoint];
}
}
+
+ $this->inPathCache[$path] = $result;
return $result;
}
public function clear() {
$this->mounts = [];
$this->pathCache->clear();
+ $this->inPathCache->clear();
}
/**
diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php
index c6429a89942..19e38717803 100644
--- a/lib/private/Files/View.php
+++ b/lib/private/Files/View.php
@@ -1433,16 +1433,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);
@@ -1496,13 +1501,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
@@ -1511,7 +1509,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);
}
}
}
@@ -1527,7 +1525,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 037ea53048a..9497b2c2637 100644
--- a/lib/private/Share20/Manager.php
+++ b/lib/private/Share20/Manager.php
@@ -1208,6 +1208,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()) {