From 99415a9f7fa26a990848c2cb63333552c1619729 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 2 Dec 2015 13:36:33 +0100 Subject: [PATCH] multiple minor fies --- .../files/config/mountprovidercollection.php | 4 ++- lib/private/files/config/usermountcache.php | 29 +++++++++++++------ lib/private/files/filesystem.php | 2 +- lib/public/files/config/icachedmountinfo.php | 2 ++ .../files/config/imountprovidercollection.php | 8 +++++ lib/public/files/config/iusermountcache.php | 2 ++ tests/lib/files/config/usermountcache.php | 12 +------- 7 files changed, 37 insertions(+), 22 deletions(-) diff --git a/lib/private/files/config/mountprovidercollection.php b/lib/private/files/config/mountprovidercollection.php index acb106f926..499fa576fb 100644 --- a/lib/private/files/config/mountprovidercollection.php +++ b/lib/private/files/config/mountprovidercollection.php @@ -93,11 +93,13 @@ class MountProviderCollection implements IMountProviderCollection, Emitter { * @param IUser $user * @param IMountPoint[] $mountPoints */ - public function cacheMounts(IUser $user, array $mountPoints) { + public function registerMounts(IUser $user, array $mountPoints) { $this->mountCache->registerMounts($user, $mountPoints); } /** + * Get the mount cache which can be used to search for mounts without setting up the filesystem + * * @return IUserMountCache */ public function getMountCache() { diff --git a/lib/private/files/config/usermountcache.php b/lib/private/files/config/usermountcache.php index 444c59bb60..15eefd4376 100644 --- a/lib/private/files/config/usermountcache.php +++ b/lib/private/files/config/usermountcache.php @@ -31,6 +31,9 @@ use OCP\ILogger; use OCP\IUser; use OCP\IUserManager; +/** + * Cache mounts points per user in the cache so we can easilly look them up + */ class UserMountCache implements IUserMountCache { /** * @var IDBConnection @@ -64,15 +67,16 @@ class UserMountCache implements IUserMountCache { } public function registerMounts(IUser $user, array $mounts) { + // filter out non-proper storages coming from unit tests $mounts = array_filter($mounts, function (IMountPoint $mount) { return $mount->getStorage()->getCache(); }); - $mounts = array_values($mounts); /** @var ICachedMountInfo[] $newMounts */ $newMounts = array_map(function (IMountPoint $mount) use ($user) { $storage = $mount->getStorage(); $rootId = (int)$storage->getCache()->getId(''); $storageId = (int)$storage->getStorageCache()->getNumericId(); + // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) if ($rootId === -1) { return null; } else { @@ -80,9 +84,10 @@ class UserMountCache implements IUserMountCache { } }, $mounts); $newMounts = array_values(array_filter($newMounts)); - $cachedMounts = $this->getMountsForUser($user); + $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(); }; @@ -97,7 +102,8 @@ class UserMountCache implements IUserMountCache { } foreach ($removedMounts as $mount) { $this->removeFromCache($mount); - $this->mountsForUsers[$user->getUID()] = []; + $index = array_search($mount, $this->mountsForUsers[$user->getUID()]); + unset($this->mountsForUsers[$user->getUID()][$index]); } } @@ -121,6 +127,8 @@ class UserMountCache implements IUserMountCache { $query->execute(); } catch (UniqueConstraintViolationException $e) { // seems to mainly happen in tests + // can also happen during concurrent access but we can safely ignore it + // since inserting the same data twice will still result in the correct data being inserted $this->logger->error('Duplicate entry while inserting mount'); $this->logger->logException($e); } @@ -145,14 +153,17 @@ class UserMountCache implements IUserMountCache { * @return ICachedMountInfo[] */ public function getMountsForUser(IUser $user) { - $builder = $this->connection->getQueryBuilder(); - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point') - ->from('mounts') - ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); + if (!isset($this->mountsForUsers[$user->getUID()])) { + $builder = $this->connection->getQueryBuilder(); + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point') + ->from('mounts') + ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); - $rows = $query->execute()->fetchAll(); + $rows = $query->execute()->fetchAll(); - return array_map([$this, 'dbRowToMountInfo'], $rows); + $this->mountsForUsers[$user->getUID()] = array_map([$this, 'dbRowToMountInfo'], $rows); + } + return $this->mountsForUsers[$user->getUID()]; } /** diff --git a/lib/private/files/filesystem.php b/lib/private/files/filesystem.php index 82177ec802..9d4a2c0aa0 100644 --- a/lib/private/files/filesystem.php +++ b/lib/private/files/filesystem.php @@ -428,7 +428,7 @@ class Filesystem { $mounts = $mountConfigManager->getMountsForUser($userObject); array_walk($mounts, array(self::$mounts, 'addMount')); $mounts[] = $mount; - $mountConfigManager->cacheMounts($userObject, $mounts); + $mountConfigManager->registerMounts($userObject, $mounts); } self::listenForNewMountProviders($mountConfigManager, $userManager); diff --git a/lib/public/files/config/icachedmountinfo.php b/lib/public/files/config/icachedmountinfo.php index dc2eb8e0db..a587427f1f 100644 --- a/lib/public/files/config/icachedmountinfo.php +++ b/lib/public/files/config/icachedmountinfo.php @@ -25,6 +25,8 @@ use OCP\Files\Node; use OCP\IUser; /** + * Holds information about a mount for a user + * * @since 9.0.0 */ interface ICachedMountInfo { diff --git a/lib/public/files/config/imountprovidercollection.php b/lib/public/files/config/imountprovidercollection.php index 071a8b2bfc..39da61812a 100644 --- a/lib/public/files/config/imountprovidercollection.php +++ b/lib/public/files/config/imountprovidercollection.php @@ -46,4 +46,12 @@ interface IMountProviderCollection { * @since 8.0.0 */ public function registerProvider(IMountProvider $provider); + + /** + * Get the mount cache which can be used to search for mounts without setting up the filesystem + * + * @return IUserMountCache + * @since 9.0.0 + */ + public function getMountCache(); } diff --git a/lib/public/files/config/iusermountcache.php b/lib/public/files/config/iusermountcache.php index 6756df56c9..156ebbf448 100644 --- a/lib/public/files/config/iusermountcache.php +++ b/lib/public/files/config/iusermountcache.php @@ -25,6 +25,8 @@ use OCP\Files\Mount\IMountPoint; use OCP\IUser; /** + * Cache mounts points per user in the cache so we can easily look them up + * * @since 9.0.0 */ interface IUserMountCache { diff --git a/tests/lib/files/config/usermountcache.php b/tests/lib/files/config/usermountcache.php index cc7686af53..26449b5dd2 100644 --- a/tests/lib/files/config/usermountcache.php +++ b/tests/lib/files/config/usermountcache.php @@ -18,16 +18,6 @@ use OCP\IUserManager; use Test\TestCase; use Test\Util\User\Dummy; -class NullLogger extends Log { - public function __construct($logger = null) { - //disable original constructor - } - - public function log($level, $message, array $context = array()) { - //noop - } -} - /** * @group DB */ @@ -54,7 +44,7 @@ class UserMountCache extends TestCase { $userBackend->createUser('u1', ''); $userBackend->createUser('u2', ''); $this->userManager->registerBackend($userBackend); - $this->cache = new \OC\Files\Config\UserMountCache($this->connection, $this->userManager, new NullLogger()); + $this->cache = new \OC\Files\Config\UserMountCache($this->connection, $this->userManager, $this->getMock('\OC\Log')); } public function tearDown() {