From da386aad5950235d6d1089f18b5ef4260057d78e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 28 Feb 2014 14:23:07 +0100 Subject: [PATCH 1/3] Allow re-using already known fileinfo when calculating folder sizes --- apps/files_sharing/lib/cache.php | 3 ++- lib/private/files/cache/cache.php | 12 ++++++++---- lib/private/files/cache/homecache.php | 13 ++++++++++--- lib/private/files/cache/scanner.php | 8 +++++--- lib/private/files/cache/updater.php | 4 ++-- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index 4b0da0b002d..579780dcd2b 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -354,9 +354,10 @@ class Shared_Cache extends Cache { * get the size of a folder and set it in the cache * * @param string $path + * @param array $entry (optional) meta data of the folder * @return int */ - public function calculateFolderSize($path) { + public function calculateFolderSize($path, $entry = null) { if ($cache = $this->getSourceCache($path)) { return $cache->calculateFolderSize($this->files[$path]); } diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index 9b18257088c..d886fd0fe76 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -498,9 +498,10 @@ class Cache { * update the folder size and the size of all parent folders * * @param string|boolean $path + * @param array $data (optional) meta data of the folder */ - public function correctFolderSize($path) { - $this->calculateFolderSize($path); + public function correctFolderSize($path, $data = null) { + $this->calculateFolderSize($path, $data); if ($path !== '') { $parent = dirname($path); if ($parent === '.' or $parent === '/') { @@ -514,11 +515,14 @@ class Cache { * get the size of a folder and set it in the cache * * @param string $path + * @param array $entry (optional) meta data of the folder * @return int */ - public function calculateFolderSize($path) { + public function calculateFolderSize($path, $entry = null) { $totalSize = 0; - $entry = $this->get($path); + if (is_null($entry)) { + $entry = $this->get($path); + } if ($entry && $entry['mimetype'] === 'httpd/unix-directory') { $id = $entry['fileid']; $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2, ' . diff --git a/lib/private/files/cache/homecache.php b/lib/private/files/cache/homecache.php index a7c310a3782..fad39a7fdad 100644 --- a/lib/private/files/cache/homecache.php +++ b/lib/private/files/cache/homecache.php @@ -13,15 +13,21 @@ class HomeCache extends Cache { * get the size of a folder and set it in the cache * * @param string $path + * @param array $entry (optional) meta data of the folder * @return int */ - public function calculateFolderSize($path) { + public function calculateFolderSize($path, $entry = null) { if ($path !== '/' and $path !== '' and $path !== 'files') { - return parent::calculateFolderSize($path); + return parent::calculateFolderSize($path, $entry); + } elseif ($path === '' or $path === '/') { + // since the size of / isn't used (the size of /files is used instead) there is no use in calculating it + return 0; } $totalSize = 0; - $entry = $this->get($path); + if (is_null($entry)) { + $entry = $this->get($path); + } if ($entry && $entry['mimetype'] === 'httpd/unix-directory') { $id = $entry['fileid']; $sql = 'SELECT SUM(`size`) FROM `*PREFIX*filecache` ' . @@ -40,6 +46,7 @@ class HomeCache extends Cache { /** * @param string $path + * @return array */ public function get($path) { $data = parent::get($path); diff --git a/lib/private/files/cache/scanner.php b/lib/private/files/cache/scanner.php index 92a4c01841b..ee6a828f7cc 100644 --- a/lib/private/files/cache/scanner.php +++ b/lib/private/files/cache/scanner.php @@ -173,14 +173,16 @@ class Scanner extends BasicEmitter { * @param string $path * @param bool $recursive * @param int $reuse - * @return int the size of the scanned folder or -1 if the size is unknown at this stage + * @return array with the meta data of the scanned file or folder */ public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1) { if ($reuse === -1) { $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : 0; } - $this->scanFile($path, $reuse); - return $this->scanChildren($path, $recursive, $reuse); + $data = $this->scanFile($path, $reuse); + $size = $this->scanChildren($path, $recursive, $reuse); + $data['size'] = $size; + return $data; } /** diff --git a/lib/private/files/cache/updater.php b/lib/private/files/cache/updater.php index 7a45b9e9e96..666d5dd7fe5 100644 --- a/lib/private/files/cache/updater.php +++ b/lib/private/files/cache/updater.php @@ -38,8 +38,8 @@ class Updater { if ($storage) { $cache = $storage->getCache($internalPath); $scanner = $storage->getScanner($internalPath); - $scanner->scan($internalPath, Scanner::SCAN_SHALLOW); - $cache->correctFolderSize($internalPath); + $data = $scanner->scan($internalPath, Scanner::SCAN_SHALLOW); + $cache->correctFolderSize($internalPath, $data); self::correctFolder($path, $storage->filemtime($internalPath)); self::correctParentStorageMtime($storage, $internalPath); } From bb8a7a2230ce1b13ab97e1558a56f54009064066 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 3 Mar 2014 16:48:06 +0100 Subject: [PATCH 2/3] Remove assert since the root size of the home storage is no longer calculated --- tests/lib/files/cache/homecache.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/lib/files/cache/homecache.php b/tests/lib/files/cache/homecache.php index dbcf6e9caa0..80dc54c9d19 100644 --- a/tests/lib/files/cache/homecache.php +++ b/tests/lib/files/cache/homecache.php @@ -90,7 +90,6 @@ class HomeCache extends \PHPUnit_Framework_TestCase { // check that files and root size ignored the unknown sizes $this->assertEquals(1000, $this->cache->calculateFolderSize('files')); - $this->assertEquals(1000, $this->cache->calculateFolderSize('')); // clean up $this->cache->remove(''); From 06c6163265bf10e7aa84c2621d58323b3ad94963 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 3 Mar 2014 16:48:28 +0100 Subject: [PATCH 3/3] Check if fields we need are actually set to prevent errors --- lib/private/files/cache/cache.php | 4 ++-- lib/private/files/cache/scanner.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index d886fd0fe76..abc11e76470 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -520,7 +520,7 @@ class Cache { */ public function calculateFolderSize($path, $entry = null) { $totalSize = 0; - if (is_null($entry)) { + if (is_null($entry) or !isset($entry['fileid'])) { $entry = $this->get($path); } if ($entry && $entry['mimetype'] === 'httpd/unix-directory') { @@ -544,7 +544,7 @@ class Cache { if ($entry['size'] !== $totalSize) { $update['size'] = $totalSize; } - if ($entry['unencrypted_size'] !== $unencryptedSum) { + if (!isset($entry['unencrypted_size']) or $entry['unencrypted_size'] !== $unencryptedSum) { $update['unencrypted_size'] = $unencryptedSum; } if (count($update) > 0) { diff --git a/lib/private/files/cache/scanner.php b/lib/private/files/cache/scanner.php index ee6a828f7cc..79159724d16 100644 --- a/lib/private/files/cache/scanner.php +++ b/lib/private/files/cache/scanner.php @@ -155,7 +155,7 @@ class Scanner extends BasicEmitter { } } if (!empty($newData)) { - $this->cache->put($file, $newData); + $data['fileid'] = $this->cache->put($file, $newData); $this->emit('\OC\Files\Cache\Scanner', 'postScanFile', array($file, $this->storageId)); \OC_Hook::emit('\OC\Files\Cache\Scanner', 'post_scan_file', array('path' => $file, 'storage' => $this->storageId)); }