Skip to content

Commit f3a3ece

Browse files
Merge pull request #39888 from nextcloud/less-container-queries
Reduce the number of container queries
2 parents b82331e + b6c3507 commit f3a3ece

File tree

9 files changed

+92
-65
lines changed

9 files changed

+92
-65
lines changed

apps/dav/lib/Connector/Sabre/Directory.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,20 +315,22 @@ public function delete() {
315315
}
316316
}
317317

318+
private function getLogger(): LoggerInterface {
319+
return \OC::$server->get(LoggerInterface::class);
320+
}
321+
318322
/**
319323
* Returns available diskspace information
320324
*
321325
* @return array
322326
*/
323327
public function getQuotaInfo() {
324-
/** @var LoggerInterface $logger */
325-
$logger = \OC::$server->get(LoggerInterface::class);
326328
if ($this->quotaInfo) {
327329
return $this->quotaInfo;
328330
}
329331
$relativePath = $this->fileView->getRelativePath($this->info->getPath());
330332
if ($relativePath === null) {
331-
$logger->warning("error while getting quota as the relative path cannot be found");
333+
$this->getLogger()->warning("error while getting quota as the relative path cannot be found");
332334
return [0, 0];
333335
}
334336

@@ -345,13 +347,13 @@ public function getQuotaInfo() {
345347
];
346348
return $this->quotaInfo;
347349
} catch (\OCP\Files\NotFoundException $e) {
348-
$logger->warning("error while getting quota into", ['exception' => $e]);
350+
$this->getLogger()->warning("error while getting quota into", ['exception' => $e]);
349351
return [0, 0];
350352
} catch (\OCP\Files\StorageNotAvailableException $e) {
351-
$logger->warning("error while getting quota into", ['exception' => $e]);
353+
$this->getLogger()->warning("error while getting quota into", ['exception' => $e]);
352354
return [0, 0];
353355
} catch (NotPermittedException $e) {
354-
$logger->warning("error while getting quota into", ['exception' => $e]);
356+
$this->getLogger()->warning("error while getting quota into", ['exception' => $e]);
355357
return [0, 0];
356358
}
357359
}

apps/files_trashbin/lib/Storage.php

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,43 +40,32 @@
4040
use OCP\Files\Storage\IStorage;
4141
use OCP\ILogger;
4242
use OCP\IUserManager;
43+
use Psr\Log\LoggerInterface;
4344

4445
class Storage extends Wrapper {
45-
/** @var IMountPoint */
46-
private $mountPoint;
47-
48-
/** @var IUserManager */
49-
private $userManager;
50-
51-
/** @var ILogger */
52-
private $logger;
53-
54-
/** @var IEventDispatcher */
55-
private $eventDispatcher;
56-
57-
/** @var IRootFolder */
58-
private $rootFolder;
59-
60-
/** @var ITrashManager */
61-
private $trashManager;
62-
63-
private $trashEnabled = true;
46+
private string $mountPoint;
47+
private IUserManager$userManager;
48+
private LoggerInterface $logger;
49+
private IEventDispatcher $eventDispatcher;
50+
private IRootFolder $rootFolder;
51+
private ITrashManager $trashManager;
52+
private bool $trashEnabled = true;
6453

6554
/**
6655
* Storage constructor.
6756
*
6857
* @param array $parameters
69-
* @param ITrashManager $trashManager
58+
* @param ITrashManager|null $trashManager
7059
* @param IUserManager|null $userManager
71-
* @param ILogger|null $logger
60+
* @param LoggerInterface|null $logger
7261
* @param IEventDispatcher|null $eventDispatcher
7362
* @param IRootFolder|null $rootFolder
7463
*/
7564
public function __construct(
7665
$parameters,
7766
ITrashManager $trashManager = null,
7867
IUserManager $userManager = null,
79-
ILogger $logger = null,
68+
LoggerInterface $logger = null,
8069
IEventDispatcher $eventDispatcher = null,
8170
IRootFolder $rootFolder = null
8271
) {
@@ -209,19 +198,27 @@ private function doDelete($path, $method) {
209198
}
210199

211200
/**
212-
* Setup the storate wrapper callback
201+
* Setup the storage wrapper callback
213202
*/
214203
public static function setupStorage() {
215-
\OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
216-
return new \OCA\Files_Trashbin\Storage(
217-
['storage' => $storage, 'mountPoint' => $mountPoint],
218-
\OC::$server->query(ITrashManager::class),
219-
\OC::$server->getUserManager(),
220-
\OC::$server->getLogger(),
221-
\OC::$server->get(IEventDispatcher::class),
222-
\OC::$server->getLazyRootFolder()
223-
);
224-
}, 1);
204+
$trashManager = \OC::$server->get(ITrashManager::class);
205+
$userManager = \OC::$server->get(IUserManager::class);
206+
$logger = \OC::$server->get(LoggerInterface::class);
207+
$eventDispatcher = \OC::$server->get(IEventDispatcher::class);
208+
$rootFolder = \OC::$server->get(IRootFolder::class);
209+
Filesystem::addStorageWrapper(
210+
'oc_trashbin',
211+
function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder) {
212+
return new Storage(
213+
['storage' => $storage, 'mountPoint' => $mountPoint],
214+
$trashManager,
215+
$userManager,
216+
$logger,
217+
$eventDispatcher,
218+
$rootFolder,
219+
);
220+
},
221+
1);
225222
}
226223

227224
public function getMountPoint() {

apps/files_trashbin/tests/StorageTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
use OCP\IUserManager;
5252
use OCP\Lock\ILockingProvider;
5353
use OCP\Share\IShare;
54+
use Psr\Log\LoggerInterface;
5455
use Test\Traits\MountProviderTrait;
5556

5657
class TemporaryNoCross extends Temporary {
@@ -606,7 +607,7 @@ public function testShouldMoveToTrash($mountPoint, $path, $userExists, $appDisab
606607
->disableOriginalConstructor()->getMock();
607608
$userManager->expects($this->any())
608609
->method('userExists')->willReturn($userExists);
609-
$logger = $this->getMockBuilder(ILogger::class)->getMock();
610+
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
610611
$eventDispatcher = $this->createMock(IEventDispatcher::class);
611612
$rootFolder = $this->createMock(IRootFolder::class);
612613
$userFolder = $this->createMock(Folder::class);

lib/private/Files/Cache/Wrapper/CacheWrapper.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
use OC\Files\Cache\QuerySearchHelper;
3434
use OCP\Files\Cache\ICache;
3535
use OCP\Files\Cache\ICacheEntry;
36+
use OCP\Files\IMimeTypeLoader;
3637
use OCP\Files\Search\ISearchOperator;
3738
use OCP\Files\Search\ISearchQuery;
39+
use OCP\IDBConnection;
3840

3941
class CacheWrapper extends Cache {
4042
/**
@@ -47,9 +49,15 @@ class CacheWrapper extends Cache {
4749
*/
4850
public function __construct($cache) {
4951
$this->cache = $cache;
50-
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
51-
$this->connection = \OC::$server->getDatabaseConnection();
52-
$this->querySearchHelper = \OC::$server->get(QuerySearchHelper::class);
52+
if ($cache instanceof Cache) {
53+
$this->mimetypeLoader = $cache->mimetypeLoader;
54+
$this->connection = $cache->connection;
55+
$this->querySearchHelper = $cache->querySearchHelper;
56+
} else {
57+
$this->mimetypeLoader = \OC::$server->get(IMimeTypeLoader::class);
58+
$this->connection = \OC::$server->get(IDBConnection::class);
59+
$this->querySearchHelper = \OC::$server->get(QuerySearchHelper::class);
60+
}
5361
}
5462

5563
protected function getCache() {

lib/private/Files/SetupManager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ private function setupBuiltinWrappers() {
164164
return $storage;
165165
});
166166

167-
Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) {
167+
$quotaIncludeExternal = $this->config->getSystemValue('quota_include_external_storage', false);
168+
Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) use ($quotaIncludeExternal) {
168169
// set up quota for home storages, even for other users
169170
// which can happen when using sharing
170171

@@ -176,7 +177,7 @@ private function setupBuiltinWrappers() {
176177
$user = $storage->getUser();
177178
return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) {
178179
return OC_Util::getUserQuota($user);
179-
}, 'root' => 'files']);
180+
}, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]);
180181
}
181182
}
182183

lib/private/Files/Storage/Wrapper/Quota.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Quota extends Wrapper {
4545
protected int|float|null $quota;
4646
protected string $sizeRoot;
4747
private SystemConfig $config;
48+
private bool $quotaIncludeExternalStorage;
4849

4950
/**
5051
* @param array $parameters
@@ -54,7 +55,7 @@ public function __construct($parameters) {
5455
$this->quota = $parameters['quota'] ?? null;
5556
$this->quotaCallback = $parameters['quotaCallback'] ?? null;
5657
$this->sizeRoot = $parameters['root'] ?? '';
57-
$this->config = \OC::$server->get(SystemConfig::class);
58+
$this->quotaIncludeExternalStorage = $parameters['include_external_storage'] ?? false;
5859
}
5960

6061
/**
@@ -82,7 +83,7 @@ private function hasQuota(): bool {
8283
* @return int|float
8384
*/
8485
protected function getSize($path, $storage = null) {
85-
if ($this->config->getValue('quota_include_external_storage', false)) {
86+
if ($this->quotaIncludeExternalStorage) {
8687
$rootInfo = Filesystem::getFileInfo('', 'ext');
8788
if ($rootInfo) {
8889
return $rootInfo->getSize(true);

lib/private/legacy/OC_Helper.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
*/
7070
class OC_Helper {
7171
private static $templateManager;
72+
private static ?ICacheFactory $cacheFactory = null;
73+
private static ?bool $quotaIncludeExternalStorage = null;
7274

7375
/**
7476
* Make a human file size
@@ -475,12 +477,15 @@ public static function findBinaryPath(string $program): ?string {
475477
* @throws \OCP\Files\NotFoundException
476478
*/
477479
public static function getStorageInfo($path, $rootInfo = null, $includeMountPoints = true, $useCache = true) {
478-
/** @var ICacheFactory $cacheFactory */
479-
$cacheFactory = \OC::$server->get(ICacheFactory::class);
480-
$memcache = $cacheFactory->createLocal('storage_info');
480+
if (!self::$cacheFactory) {
481+
self::$cacheFactory = \OC::$server->get(ICacheFactory::class);
482+
}
483+
$memcache = self::$cacheFactory->createLocal('storage_info');
481484

482485
// return storage info without adding mount points
483-
$includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false);
486+
if (self::$quotaIncludeExternalStorage === null) {
487+
self::$quotaIncludeExternalStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false);
488+
}
484489

485490
$view = Filesystem::getView();
486491
if (!$view) {
@@ -497,7 +502,7 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
497502
}
498503

499504
if (!$rootInfo) {
500-
$rootInfo = \OC\Files\Filesystem::getFileInfo($path, $includeExtStorage ? 'ext' : false);
505+
$rootInfo = \OC\Files\Filesystem::getFileInfo($path, self::$quotaIncludeExternalStorage ? 'ext' : false);
501506
}
502507
if (!$rootInfo instanceof \OCP\Files\FileInfo) {
503508
throw new \OCP\Files\NotFoundException('The root directory of the user\'s files is missing');
@@ -512,9 +517,9 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
512517
$storage = $mount->getStorage();
513518
$sourceStorage = $storage;
514519
if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
515-
$includeExtStorage = false;
520+
self::$quotaIncludeExternalStorage = false;
516521
}
517-
if ($includeExtStorage) {
522+
if (self::$quotaIncludeExternalStorage) {
518523
if ($storage->instanceOfStorage('\OC\Files\Storage\Home')
519524
|| $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')
520525
) {

tests/lib/Files/ViewTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use OCP\Files\GenericFileException;
2323
use OCP\Files\Mount\IMountManager;
2424
use OCP\Files\Storage\IStorage;
25+
use OCP\IDBConnection;
2526
use OCP\Lock\ILockingProvider;
2627
use OCP\Lock\LockedException;
2728
use OCP\Share\IShare;
@@ -1590,7 +1591,7 @@ private function createTestMovableMountPoints($mountPoints) {
15901591
->getMock();
15911592
$storage->method('getId')->willReturn('non-null-id');
15921593
$storage->method('getStorageCache')->willReturnCallback(function () use ($storage) {
1593-
return new \OC\Files\Cache\Storage($storage);
1594+
return new \OC\Files\Cache\Storage($storage, true, \OC::$server->get(IDBConnection::class));
15941595
});
15951596

15961597
$mounts[] = $this->getMockBuilder(TestMoveableMountPoint::class)

tests/lib/HelperStorageTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use OC\Files\Storage\Temporary;
1212
use OCP\Files\Mount\IMountManager;
13+
use OCP\IConfig;
1314
use Test\Traits\UserTrait;
1415

1516
/**
@@ -26,12 +27,14 @@ class HelperStorageTest extends \Test\TestCase {
2627
private $storageMock;
2728
/** @var \OC\Files\Storage\Storage */
2829
private $storage;
30+
private bool $savedQuotaIncludeExternalStorage;
2931

3032
protected function setUp(): void {
3133
parent::setUp();
3234

3335
$this->user = $this->getUniqueID('user_');
3436
$this->createUser($this->user, $this->user);
37+
$this->savedQuotaIncludeExternalStorage = $this->getIncludeExternalStorage();
3538

3639
\OC\Files\Filesystem::tearDown();
3740
\OC_User::setUserId($this->user);
@@ -45,6 +48,7 @@ protected function setUp(): void {
4548
}
4649

4750
protected function tearDown(): void {
51+
$this->setIncludeExternalStorage($this->savedQuotaIncludeExternalStorage);
4852
$this->user = null;
4953

5054
if ($this->storageMock) {
@@ -91,6 +95,19 @@ public function testGetStorageInfo() {
9195
$this->assertEquals(5, $storageInfo['used']);
9296
$this->assertEquals(17, $storageInfo['total']);
9397
}
98+
private function getIncludeExternalStorage(): bool {
99+
$class = new \ReflectionClass(\OC_Helper::class);
100+
$prop = $class->getProperty('quotaIncludeExternalStorage');
101+
$prop->setAccessible(true);
102+
return $prop->getValue(null) ?? false;
103+
}
104+
105+
private function setIncludeExternalStorage(bool $include) {
106+
$class = new \ReflectionClass(\OC_Helper::class);
107+
$prop = $class->getProperty('quotaIncludeExternalStorage');
108+
$prop->setAccessible(true);
109+
$prop->setValue(null, $include);
110+
}
94111

95112
/**
96113
* Test getting the storage info, ignoring extra mount points
@@ -104,8 +121,7 @@ public function testGetStorageInfoExcludingExtStorage() {
104121
$extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
105122
$extStorage->getScanner()->scan(''); // update root size
106123

107-
$config = \OC::$server->getConfig();
108-
$config->setSystemValue('quota_include_external_storage', false);
124+
$this->setIncludeExternalStorage(false);
109125

110126
\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');
111127

@@ -129,18 +145,16 @@ public function testGetStorageInfoIncludingExtStorage() {
129145

130146
\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');
131147

132-
$config = \OC::$server->getConfig();
133-
$oldConfig = $config->getSystemValue('quota_include_external_storage', false);
134-
$config->setSystemValue('quota_include_external_storage', 'true');
148+
$this->setIncludeExternalStorage(true);
135149

150+
$config = \OC::$server->get(IConfig::class);
136151
$config->setUserValue($this->user, 'files', 'quota', '25');
137152

138153
$storageInfo = \OC_Helper::getStorageInfo('');
139154
$this->assertEquals(3, $storageInfo['free']);
140155
$this->assertEquals(22, $storageInfo['used']);
141156
$this->assertEquals(25, $storageInfo['total']);
142157

143-
$config->setSystemValue('quota_include_external_storage', $oldConfig);
144158
$config->setUserValue($this->user, 'files', 'quota', 'default');
145159
}
146160

@@ -161,15 +175,12 @@ public function testGetStorageInfoIncludingExtStorageWithNoUserQuota() {
161175
\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');
162176

163177
$config = \OC::$server->getConfig();
164-
$oldConfig = $config->getSystemValue('quota_include_external_storage', false);
165-
$config->setSystemValue('quota_include_external_storage', 'true');
178+
$this->setIncludeExternalStorage(true);
166179

167180
$storageInfo = \OC_Helper::getStorageInfo('');
168181
$this->assertEquals(12, $storageInfo['free'], '12 bytes free in home storage');
169182
$this->assertEquals(22, $storageInfo['used'], '5 bytes of home storage and 17 bytes of the temporary storage are used');
170183
$this->assertEquals(34, $storageInfo['total'], '5 bytes used and 12 bytes free in home storage as well as 17 bytes used in temporary storage');
171-
172-
$config->setSystemValue('quota_include_external_storage', $oldConfig);
173184
}
174185

175186

0 commit comments

Comments
 (0)