diff options
author | Morris Jobke <hey@morrisjobke.de> | 2016-09-08 21:09:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-08 21:09:14 +0200 |
commit | 5ac26d12f0a771bcaf8a44acdb469b68daebc446 (patch) | |
tree | 0393b11ecdfa483f287f6137a119bd67c32e6783 /lib | |
parent | e18fecae472ef9a9da0be724849285891d855b67 (diff) | |
parent | e8e950a4d2972a4950e00a67cc8c1f0447701986 (diff) | |
download | nextcloud-server-5ac26d12f0a771bcaf8a44acdb469b68daebc446.tar.gz nextcloud-server-5ac26d12f0a771bcaf8a44acdb469b68daebc446.zip |
Merge pull request #1247 from nextcloud/storage-id-global-cache
more efficient querying of numeric storage ids
Diffstat (limited to 'lib')
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | lib/private/Files/Cache/Storage.php | 18 | ||||
-rw-r--r-- | lib/private/Files/Cache/StorageGlobal.php | 78 | ||||
-rw-r--r-- | lib/private/Repair/RepairLegacyStorages.php | 2 |
5 files changed, 96 insertions, 4 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 097995ab554..94170933e5e 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -466,6 +466,7 @@ return array( 'OC\\Files\\Cache\\Propagator' => $baseDir . '/lib/private/Files/Cache/Propagator.php', 'OC\\Files\\Cache\\Scanner' => $baseDir . '/lib/private/Files/Cache/Scanner.php', 'OC\\Files\\Cache\\Storage' => $baseDir . '/lib/private/Files/Cache/Storage.php', + 'OC\\Files\\Cache\\StorageGlobal' => $baseDir . '/lib/private/Files/Cache/StorageGlobal.php', 'OC\\Files\\Cache\\Updater' => $baseDir . '/lib/private/Files/Cache/Updater.php', 'OC\\Files\\Cache\\Watcher' => $baseDir . '/lib/private/Files/Cache/Watcher.php', 'OC\\Files\\Cache\\Wrapper\\CacheJail' => $baseDir . '/lib/private/Files/Cache/Wrapper/CacheJail.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 9cd402d2cb8..c7e833c02fd 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -496,6 +496,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Files\\Cache\\Propagator' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Propagator.php', 'OC\\Files\\Cache\\Scanner' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Scanner.php', 'OC\\Files\\Cache\\Storage' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Storage.php', + 'OC\\Files\\Cache\\StorageGlobal' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/StorageGlobal.php', 'OC\\Files\\Cache\\Updater' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Updater.php', 'OC\\Files\\Cache\\Watcher' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Watcher.php', 'OC\\Files\\Cache\\Wrapper\\CacheJail' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Wrapper/CacheJail.php', diff --git a/lib/private/Files/Cache/Storage.php b/lib/private/Files/Cache/Storage.php index 8a076084ac5..b2bb8fd0078 100644 --- a/lib/private/Files/Cache/Storage.php +++ b/lib/private/Files/Cache/Storage.php @@ -40,10 +40,22 @@ namespace OC\Files\Cache; * @package OC\Files\Cache */ class Storage { + /** @var StorageGlobal|null */ + private static $globalCache = null; private $storageId; private $numericId; /** + * @return StorageGlobal + */ + public static function getGlobalCache() { + if (is_null(self::$globalCache)) { + self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection()); + } + return self::$globalCache; + } + + /** * @param \OC\Files\Storage\Storage|string $storage * @param bool $isAvailable * @throws \RuntimeException @@ -75,12 +87,10 @@ class Storage { /** * @param string $storageId - * @return array|null + * @return array */ public static function getStorageById($storageId) { - $sql = 'SELECT * FROM `*PREFIX*storages` WHERE `id` = ?'; - $result = \OC_DB::executeAudited($sql, array($storageId)); - return $result->fetchRow(); + return self::getGlobalCache()->getStorageInfo($storageId); } /** diff --git a/lib/private/Files/Cache/StorageGlobal.php b/lib/private/Files/Cache/StorageGlobal.php new file mode 100644 index 00000000000..791835baccc --- /dev/null +++ b/lib/private/Files/Cache/StorageGlobal.php @@ -0,0 +1,78 @@ +<?php +/** + * @copyright Robin Appelman <robin@icewind.nl> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Files\Cache; + +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\Files\Mount\IMountPoint; +use OCP\IDBConnection; + +/** + * Handle the mapping between the string and numeric storage ids + * + * Each storage has 2 different ids + * a string id which is generated by the storage backend and reflects the configuration of the storage (e.g. 'smb://user@host/share') + * and a numeric storage id which is referenced in the file cache + * + * A mapping between the two storage ids is stored in the database and accessible trough this class + * + * @package OC\Files\Cache + */ +class StorageGlobal { + /** @var IDBConnection */ + private $connection; + + /** @var array[] */ + private $cache = []; + + public function __construct(IDBConnection $connection) { + $this->connection = $connection; + } + + /** + * @param string[] $storageIds + */ + public function loadForStorageIds(array $storageIds) { + $builder = $this->connection->getQueryBuilder(); + $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked']) + ->from('storages') + ->where($builder->expr()->in('id', $builder->createNamedParameter(array_values($storageIds), IQueryBuilder::PARAM_STR_ARRAY))); + + $result = $query->execute(); + while ($row = $result->fetch()) { + $this->cache[$row['id']] = $row; + } + } + + /** + * @param string $storageId + * @return array|null + */ + public function getStorageInfo($storageId) { + if (!isset($this->cache[$storageId])) { + $this->loadForStorageIds([$storageId]); + } + return isset($this->cache[$storageId]) ? $this->cache[$storageId] : null; + } + + public function clearCache() { + $this->cache = []; + } +} diff --git a/lib/private/Repair/RepairLegacyStorages.php b/lib/private/Repair/RepairLegacyStorages.php index 8bb4eb5017f..228bdb67fe8 100644 --- a/lib/private/Repair/RepairLegacyStorages.php +++ b/lib/private/Repair/RepairLegacyStorages.php @@ -245,6 +245,8 @@ class RepairLegacyStorages implements IRepairStep{ $this->connection->commit(); + Storage::getGlobalCache()->clearCache(); + if ($hasWarnings) { $out->warning('Some legacy storages could not be repaired. Please manually fix them then re-run ./occ maintenance:repair'); } else { |