From e5272e331f6579d21aa8b99850e1cac989b5c858 Mon Sep 17 00:00:00 2001 From: Ari Selseng Date: Fri, 1 Mar 2019 23:52:58 +0100 Subject: [PATCH] Avoid calculating folder size for parent that also needs proper scan. Signed-off-by: Ari Selseng --- lib/private/Files/Cache/Cache.php | 30 +++++++++++++++++-- lib/private/Files/Cache/Scanner.php | 2 +- .../Files/Cache/Wrapper/CacheWrapper.php | 4 +-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index 7b42cc2aa5704..fd26b94997993 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -3,6 +3,7 @@ * @copyright Copyright (c) 2016, ownCloud, Inc. * * @author Andreas Fischer + * @author Ari Selseng * @author Artem Kochnev * @author Björn Schießle * @author Florin Peter @@ -774,15 +775,38 @@ public function searchByTag($tag, $userId) { * @param string|boolean $path * @param array $data (optional) meta data of the folder */ - public function correctFolderSize($path, $data = null) { + public function correctFolderSize($path, $data = null, $isBackgroundScan = false) { $this->calculateFolderSize($path, $data); if ($path !== '') { $parent = dirname($path); if ($parent === '.' or $parent === '/') { $parent = ''; } - $this->correctFolderSize($parent); + if ($isBackgroundScan) { + $parent_data = $this->get($parent); + if ($parent_data['size'] !== -1 && $this->getIncompleteChildrenCount($parent_data['fileid']) === 0) { + $this->correctFolderSize($parent, $parent_data, $isBackgroundScan); + } + } else { + $this->correctFolderSize($parent); + } + } + } + + /** + * get the incomplete count that shares parent $folder + * + * @param int $fileId the file id of the folder + * @return int + */ + public function getIncompleteChildrenCount($fileId) { + if ($fileId > -1) { + $sql = 'SELECT count(*) + FROM `*PREFIX*filecache` WHERE `parent` = ? AND size = -1'; + $result = $this->connection->executeQuery($sql, [$fileId]); + return (int)$result->fetchColumn(); } + return -1; } /** @@ -919,4 +943,4 @@ public function normalize($path) { return trim(\OC_Util::normalizeUnicode($path), '/'); } -} +} \ No newline at end of file diff --git a/lib/private/Files/Cache/Scanner.php b/lib/private/Files/Cache/Scanner.php index ca9a0b794f99f..e684b8531035e 100644 --- a/lib/private/Files/Cache/Scanner.php +++ b/lib/private/Files/Cache/Scanner.php @@ -532,7 +532,7 @@ private function runBackgroundScanJob(callable $callback, $path) { $callback(); \OC_Hook::emit('Scanner', 'correctFolderSize', array('path' => $path)); if ($this->cacheActive && $this->cache instanceof Cache) { - $this->cache->correctFolderSize($path); + $this->cache->correctFolderSize($path, null, true); } } catch (\OCP\Files\StorageInvalidException $e) { // skip unavailable storages diff --git a/lib/private/Files/Cache/Wrapper/CacheWrapper.php b/lib/private/Files/Cache/Wrapper/CacheWrapper.php index da0a1b54e7f74..223e678f323f8 100644 --- a/lib/private/Files/Cache/Wrapper/CacheWrapper.php +++ b/lib/private/Files/Cache/Wrapper/CacheWrapper.php @@ -258,9 +258,9 @@ public function searchByTag($tag, $userId) { * @param string|boolean $path * @param array $data (optional) meta data of the folder */ - public function correctFolderSize($path, $data = null) { + public function correctFolderSize($path, $data = null, $isBackgroundScan = false) { if ($this->getCache() instanceof Cache) { - $this->getCache()->correctFolderSize($path, $data); + $this->getCache()->correctFolderSize($path, $data, $isBackgroundScan); } }