diff options
author | Andy Scherzinger <info@andy-scherzinger.de> | 2023-09-08 09:56:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-08 09:56:50 +0200 |
commit | 9cccc03d7496da8a7612536c9ce92edcc1618d73 (patch) | |
tree | d77c30e15ac5d5a7d089643b1612acce73b997f4 | |
parent | b75c0fe3063ad0f7b820d51c22edd1c27ef33263 (diff) | |
parent | ee8c1a5c609743ba1d73d5f317bd6db050827329 (diff) | |
download | nextcloud-server-9cccc03d7496da8a7612536c9ce92edcc1618d73.tar.gz nextcloud-server-9cccc03d7496da8a7612536c9ce92edcc1618d73.zip |
Merge pull request #39890 from nextcloud/less-container-queries-27
[stable27] Reduce the number of container queries
-rw-r--r-- | apps/dav/lib/Connector/Sabre/Directory.php | 14 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Storage.php | 32 | ||||
-rw-r--r-- | lib/private/Files/Cache/Wrapper/CacheWrapper.php | 14 | ||||
-rw-r--r-- | lib/private/Files/SetupManager.php | 5 | ||||
-rw-r--r-- | lib/private/Files/Storage/Wrapper/Quota.php | 5 | ||||
-rw-r--r-- | lib/private/legacy/OC_Helper.php | 19 | ||||
-rw-r--r-- | tests/lib/Files/ViewTest.php | 3 | ||||
-rw-r--r-- | tests/lib/HelperStorageTest.php | 31 |
8 files changed, 80 insertions, 43 deletions
diff --git a/apps/dav/lib/Connector/Sabre/Directory.php b/apps/dav/lib/Connector/Sabre/Directory.php index c29070fe921..49354deb778 100644 --- a/apps/dav/lib/Connector/Sabre/Directory.php +++ b/apps/dav/lib/Connector/Sabre/Directory.php @@ -315,20 +315,22 @@ class Directory extends \OCA\DAV\Connector\Sabre\Node implements \Sabre\DAV\ICol } } + private function getLogger(): LoggerInterface { + return \OC::$server->get(LoggerInterface::class); + } + /** * Returns available diskspace information * * @return array */ public function getQuotaInfo() { - /** @var LoggerInterface $logger */ - $logger = \OC::$server->get(LoggerInterface::class); if ($this->quotaInfo) { return $this->quotaInfo; } $relativePath = $this->fileView->getRelativePath($this->info->getPath()); if ($relativePath === null) { - $logger->warning("error while getting quota as the relative path cannot be found"); + $this->getLogger()->warning("error while getting quota as the relative path cannot be found"); return [0, 0]; } @@ -345,13 +347,13 @@ class Directory extends \OCA\DAV\Connector\Sabre\Node implements \Sabre\DAV\ICol ]; return $this->quotaInfo; } catch (\OCP\Files\NotFoundException $e) { - $logger->warning("error while getting quota into", ['exception' => $e]); + $this->getLogger()->warning("error while getting quota into", ['exception' => $e]); return [0, 0]; } catch (\OCP\Files\StorageNotAvailableException $e) { - $logger->warning("error while getting quota into", ['exception' => $e]); + $this->getLogger()->warning("error while getting quota into", ['exception' => $e]); return [0, 0]; } catch (NotPermittedException $e) { - $logger->warning("error while getting quota into", ['exception' => $e]); + $this->getLogger()->warning("error while getting quota into", ['exception' => $e]); return [0, 0]; } } diff --git a/apps/files_trashbin/lib/Storage.php b/apps/files_trashbin/lib/Storage.php index f08d8d25a55..09b0dcf0a1a 100644 --- a/apps/files_trashbin/lib/Storage.php +++ b/apps/files_trashbin/lib/Storage.php @@ -66,7 +66,7 @@ class Storage extends Wrapper { * Storage constructor. * * @param array $parameters - * @param ITrashManager $trashManager + * @param ITrashManager|null $trashManager * @param IUserManager|null $userManager * @param ILogger|null $logger * @param EventDispatcherInterface|null $eventDispatcher @@ -208,19 +208,27 @@ class Storage extends Wrapper { } /** - * Setup the storate wrapper callback + * Setup the storage wrapper callback */ public static function setupStorage() { - \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) { - return new \OCA\Files_Trashbin\Storage( - ['storage' => $storage, 'mountPoint' => $mountPoint], - \OC::$server->query(ITrashManager::class), - \OC::$server->getUserManager(), - \OC::$server->getLogger(), - \OC::$server->getEventDispatcher(), - \OC::$server->getLazyRootFolder() - ); - }, 1); + $trashManager = \OC::$server->get(ITrashManager::class); + $userManager = \OC::$server->get(IUserManager::class); + $logger = \OC::$server->get(ILogger::class); + $eventDispatcher = \OC::$server->get(EventDispatcherInterface::class); + $rootFolder = \OC::$server->get(IRootFolder::class); + Filesystem::addStorageWrapper( + 'oc_trashbin', + function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder) { + return new Storage( + ['storage' => $storage, 'mountPoint' => $mountPoint], + $trashManager, + $userManager, + $logger, + $eventDispatcher, + $rootFolder, + ); + }, + 1); } public function getMountPoint() { diff --git a/lib/private/Files/Cache/Wrapper/CacheWrapper.php b/lib/private/Files/Cache/Wrapper/CacheWrapper.php index 6479ea793b0..39a78f31343 100644 --- a/lib/private/Files/Cache/Wrapper/CacheWrapper.php +++ b/lib/private/Files/Cache/Wrapper/CacheWrapper.php @@ -33,8 +33,10 @@ use OC\Files\Cache\Cache; use OC\Files\Cache\QuerySearchHelper; use OCP\Files\Cache\ICache; use OCP\Files\Cache\ICacheEntry; +use OCP\Files\IMimeTypeLoader; use OCP\Files\Search\ISearchOperator; use OCP\Files\Search\ISearchQuery; +use OCP\IDBConnection; class CacheWrapper extends Cache { /** @@ -47,9 +49,15 @@ class CacheWrapper extends Cache { */ public function __construct($cache) { $this->cache = $cache; - $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); - $this->connection = \OC::$server->getDatabaseConnection(); - $this->querySearchHelper = \OC::$server->get(QuerySearchHelper::class); + if ($cache instanceof Cache) { + $this->mimetypeLoader = $cache->mimetypeLoader; + $this->connection = $cache->connection; + $this->querySearchHelper = $cache->querySearchHelper; + } else { + $this->mimetypeLoader = \OC::$server->get(IMimeTypeLoader::class); + $this->connection = \OC::$server->get(IDBConnection::class); + $this->querySearchHelper = \OC::$server->get(QuerySearchHelper::class); + } } protected function getCache() { diff --git a/lib/private/Files/SetupManager.php b/lib/private/Files/SetupManager.php index dfc496524a8..916fcfb0a7e 100644 --- a/lib/private/Files/SetupManager.php +++ b/lib/private/Files/SetupManager.php @@ -156,7 +156,8 @@ class SetupManager { return $storage; }); - Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) { + $quotaIncludeExternal = $this->config->getSystemValue('quota_include_external_storage', false); + Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) use ($quotaIncludeExternal) { // set up quota for home storages, even for other users // which can happen when using sharing @@ -168,7 +169,7 @@ class SetupManager { $user = $storage->getUser(); return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) { return OC_Util::getUserQuota($user); - }, 'root' => 'files']); + }, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]); } } diff --git a/lib/private/Files/Storage/Wrapper/Quota.php b/lib/private/Files/Storage/Wrapper/Quota.php index cfb2c455a2c..b13aa6f9f04 100644 --- a/lib/private/Files/Storage/Wrapper/Quota.php +++ b/lib/private/Files/Storage/Wrapper/Quota.php @@ -45,6 +45,7 @@ class Quota extends Wrapper { protected int|float|null $quota; protected string $sizeRoot; private SystemConfig $config; + private bool $quotaIncludeExternalStorage; /** * @param array $parameters @@ -54,7 +55,7 @@ class Quota extends Wrapper { $this->quota = $parameters['quota'] ?? null; $this->quotaCallback = $parameters['quotaCallback'] ?? null; $this->sizeRoot = $parameters['root'] ?? ''; - $this->config = \OC::$server->get(SystemConfig::class); + $this->quotaIncludeExternalStorage = $parameters['include_external_storage'] ?? false; } /** @@ -82,7 +83,7 @@ class Quota extends Wrapper { * @return int|float */ protected function getSize($path, $storage = null) { - if ($this->config->getValue('quota_include_external_storage', false)) { + if ($this->quotaIncludeExternalStorage) { $rootInfo = Filesystem::getFileInfo('', 'ext'); if ($rootInfo) { return $rootInfo->getSize(true); diff --git a/lib/private/legacy/OC_Helper.php b/lib/private/legacy/OC_Helper.php index 07d81933d00..39672dc6a72 100644 --- a/lib/private/legacy/OC_Helper.php +++ b/lib/private/legacy/OC_Helper.php @@ -57,6 +57,8 @@ use Psr\Log\LoggerInterface; */ class OC_Helper { private static $templateManager; + private static ?ICacheFactory $cacheFactory = null; + private static ?bool $quotaIncludeExternalStorage = null; /** * Make a human file size @@ -462,12 +464,15 @@ class OC_Helper { * @throws \OCP\Files\NotFoundException */ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoints = true, $useCache = true) { - /** @var ICacheFactory $cacheFactory */ - $cacheFactory = \OC::$server->get(ICacheFactory::class); - $memcache = $cacheFactory->createLocal('storage_info'); + if (!self::$cacheFactory) { + self::$cacheFactory = \OC::$server->get(ICacheFactory::class); + } + $memcache = self::$cacheFactory->createLocal('storage_info'); // return storage info without adding mount points - $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); + if (self::$quotaIncludeExternalStorage === null) { + self::$quotaIncludeExternalStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); + } $view = Filesystem::getView(); if (!$view) { @@ -484,7 +489,7 @@ class OC_Helper { } if (!$rootInfo) { - $rootInfo = \OC\Files\Filesystem::getFileInfo($path, $includeExtStorage ? 'ext' : false); + $rootInfo = \OC\Files\Filesystem::getFileInfo($path, self::$quotaIncludeExternalStorage ? 'ext' : false); } if (!$rootInfo instanceof \OCP\Files\FileInfo) { throw new \OCP\Files\NotFoundException(); @@ -498,9 +503,9 @@ class OC_Helper { $storage = $mount->getStorage(); $sourceStorage = $storage; if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { - $includeExtStorage = false; + self::$quotaIncludeExternalStorage = false; } - if ($includeExtStorage) { + if (self::$quotaIncludeExternalStorage) { if ($storage->instanceOfStorage('\OC\Files\Storage\Home') || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') ) { diff --git a/tests/lib/Files/ViewTest.php b/tests/lib/Files/ViewTest.php index d8c9331fa45..7d111547032 100644 --- a/tests/lib/Files/ViewTest.php +++ b/tests/lib/Files/ViewTest.php @@ -23,6 +23,7 @@ use OCP\Files\FileInfo; use OCP\Files\GenericFileException; use OCP\Files\Mount\IMountManager; use OCP\Files\Storage\IStorage; +use OCP\IDBConnection; use OCP\Lock\ILockingProvider; use OCP\Lock\LockedException; use OCP\Share\IShare; @@ -1591,7 +1592,7 @@ class ViewTest extends \Test\TestCase { ->getMock(); $storage->method('getId')->willReturn('non-null-id'); $storage->method('getStorageCache')->willReturnCallback(function () use ($storage) { - return new \OC\Files\Cache\Storage($storage); + return new \OC\Files\Cache\Storage($storage, true, \OC::$server->get(IDBConnection::class)); }); $mounts[] = $this->getMockBuilder(TestMoveableMountPoint::class) diff --git a/tests/lib/HelperStorageTest.php b/tests/lib/HelperStorageTest.php index d3f480502b2..0643c4b68a4 100644 --- a/tests/lib/HelperStorageTest.php +++ b/tests/lib/HelperStorageTest.php @@ -10,6 +10,7 @@ namespace Test; use OC\Files\Storage\Temporary; use OCP\Files\Mount\IMountManager; +use OCP\IConfig; use Test\Traits\UserTrait; /** @@ -26,12 +27,14 @@ class HelperStorageTest extends \Test\TestCase { private $storageMock; /** @var \OC\Files\Storage\Storage */ private $storage; + private bool $savedQuotaIncludeExternalStorage; protected function setUp(): void { parent::setUp(); $this->user = $this->getUniqueID('user_'); $this->createUser($this->user, $this->user); + $this->savedQuotaIncludeExternalStorage = $this->getIncludeExternalStorage(); \OC\Files\Filesystem::tearDown(); \OC_User::setUserId($this->user); @@ -45,6 +48,7 @@ class HelperStorageTest extends \Test\TestCase { } protected function tearDown(): void { + $this->setIncludeExternalStorage($this->savedQuotaIncludeExternalStorage); $this->user = null; if ($this->storageMock) { @@ -91,6 +95,19 @@ class HelperStorageTest extends \Test\TestCase { $this->assertEquals(5, $storageInfo['used']); $this->assertEquals(17, $storageInfo['total']); } + private function getIncludeExternalStorage(): bool { + $class = new \ReflectionClass(\OC_Helper::class); + $prop = $class->getProperty('quotaIncludeExternalStorage'); + $prop->setAccessible(true); + return $prop->getValue(null) ?? false; + } + + private function setIncludeExternalStorage(bool $include) { + $class = new \ReflectionClass(\OC_Helper::class); + $prop = $class->getProperty('quotaIncludeExternalStorage'); + $prop->setAccessible(true); + $prop->setValue(null, $include); + } /** * Test getting the storage info, ignoring extra mount points @@ -104,8 +121,7 @@ class HelperStorageTest extends \Test\TestCase { $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); $extStorage->getScanner()->scan(''); // update root size - $config = \OC::$server->getConfig(); - $config->setSystemValue('quota_include_external_storage', false); + $this->setIncludeExternalStorage(false); \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); @@ -129,10 +145,9 @@ class HelperStorageTest extends \Test\TestCase { \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); - $config = \OC::$server->getConfig(); - $oldConfig = $config->getSystemValue('quota_include_external_storage', false); - $config->setSystemValue('quota_include_external_storage', 'true'); + $this->setIncludeExternalStorage(true); + $config = \OC::$server->get(IConfig::class); $config->setUserValue($this->user, 'files', 'quota', '25'); $storageInfo = \OC_Helper::getStorageInfo(''); @@ -140,7 +155,6 @@ class HelperStorageTest extends \Test\TestCase { $this->assertEquals(22, $storageInfo['used']); $this->assertEquals(25, $storageInfo['total']); - $config->setSystemValue('quota_include_external_storage', $oldConfig); $config->setUserValue($this->user, 'files', 'quota', 'default'); } @@ -161,15 +175,12 @@ class HelperStorageTest extends \Test\TestCase { \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); $config = \OC::$server->getConfig(); - $oldConfig = $config->getSystemValue('quota_include_external_storage', false); - $config->setSystemValue('quota_include_external_storage', 'true'); + $this->setIncludeExternalStorage(true); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(12, $storageInfo['free'], '12 bytes free in home storage'); $this->assertEquals(22, $storageInfo['used'], '5 bytes of home storage and 17 bytes of the temporary storage are used'); $this->assertEquals(34, $storageInfo['total'], '5 bytes used and 12 bytes free in home storage as well as 17 bytes used in temporary storage'); - - $config->setSystemValue('quota_include_external_storage', $oldConfig); } |