From 65750cb2448af6d543132819ea7f7ee9720b825c Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Fri, 4 Oct 2013 16:17:19 +0300 Subject: Load all mimetypes in one go --- lib/private/files/cache/cache.php | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index e69733727af..852036929dd 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -64,30 +64,33 @@ class Cache { * @return int */ public function getMimetypeId($mime) { + if (empty($this->mimetypeIds)) { + $this->loadMimetypes(); + } + if (!isset($this->mimetypeIds[$mime])) { - $result = \OC_DB::executeAudited('SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = ?', array($mime)); - if ($row = $result->fetchRow()) { - $this->mimetypeIds[$mime] = $row['id']; - } else { - $result = \OC_DB::executeAudited('INSERT INTO `*PREFIX*mimetypes`(`mimetype`) VALUES(?)', array($mime)); - $this->mimetypeIds[$mime] = \OC_DB::insertid('*PREFIX*mimetypes'); - } + $result = \OC_DB::executeAudited('INSERT INTO `*PREFIX*mimetypes`(`mimetype`) VALUES(?)', array($mime)); + $this->mimetypeIds[$mime] = \OC_DB::insertid('*PREFIX*mimetypes'); $this->mimetypes[$this->mimetypeIds[$mime]] = $mime; - } + } + return $this->mimetypeIds[$mime]; } public function getMimetype($id) { - if (!isset($this->mimetypes[$id])) { - $sql = 'SELECT `mimetype` FROM `*PREFIX*mimetypes` WHERE `id` = ?'; - $result = \OC_DB::executeAudited($sql, array($id)); - if ($row = $result->fetchRow()) { - $this->mimetypes[$id] = $row['mimetype']; - } else { - return null; - } + if (empty($this->mimetypes)) { + $this->loadMimetypes(); } - return $this->mimetypes[$id]; + + return isset($this->mimetypes[$id]) ? $this->mimetypes[$id] : null; + } + + protected function loadMimetypes(){ + $result = \OC_DB::executeAudited('SELECT `id`, `mimetype` FROM `*PREFIX*mimetypes`', array()); + while ($result && $row = $result->fetchRow()) { + $this->mimetypeIds[$row['mimetype']] = $row['id']; + $this->mimetypes[$row['id']] = $row['mimetype']; + } } /** -- cgit v1.2.3 From ef65037211f9111bb5c77fe657e50dc948db4f75 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Fri, 4 Oct 2013 18:09:42 +0300 Subject: Make mimetypes static. Jenkis will be happy --- lib/private/files/cache/cache.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index 852036929dd..82f5f504cba 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -34,8 +34,8 @@ class Cache { */ private $storageCache; - private $mimetypeIds = array(); - private $mimetypes = array(); + private static $mimetypeIds = array(); + private static $mimetypes = array(); /** * @param \OC\Files\Storage\Storage|string $storage @@ -64,32 +64,32 @@ class Cache { * @return int */ public function getMimetypeId($mime) { - if (empty($this->mimetypeIds)) { + if (empty(self::$mimetypeIds)) { $this->loadMimetypes(); } - if (!isset($this->mimetypeIds[$mime])) { + if (!isset(self::$mimetypeIds[$mime])) { $result = \OC_DB::executeAudited('INSERT INTO `*PREFIX*mimetypes`(`mimetype`) VALUES(?)', array($mime)); - $this->mimetypeIds[$mime] = \OC_DB::insertid('*PREFIX*mimetypes'); - $this->mimetypes[$this->mimetypeIds[$mime]] = $mime; + self::$mimetypeIds[$mime] = \OC_DB::insertid('*PREFIX*mimetypes'); + self::$mimetypes[self::$mimetypeIds[$mime]] = $mime; } - return $this->mimetypeIds[$mime]; + return self::$mimetypeIds[$mime]; } public function getMimetype($id) { - if (empty($this->mimetypes)) { + if (empty(self::$mimetypes)) { $this->loadMimetypes(); } - return isset($this->mimetypes[$id]) ? $this->mimetypes[$id] : null; + return isset(self::$mimetypes[$id]) ? self::$mimetypes[$id] : null; } protected function loadMimetypes(){ $result = \OC_DB::executeAudited('SELECT `id`, `mimetype` FROM `*PREFIX*mimetypes`', array()); while ($result && $row = $result->fetchRow()) { - $this->mimetypeIds[$row['mimetype']] = $row['id']; - $this->mimetypes[$row['id']] = $row['mimetype']; + self::$mimetypeIds[$row['mimetype']] = $row['id']; + self::$mimetypes[$row['id']] = $row['mimetype']; } } -- cgit v1.2.3 From 8da1aac1d0065e2da6841bf98b8bb60fcefa14c8 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Fri, 4 Oct 2013 23:24:38 +0300 Subject: Check result only once --- lib/private/files/cache/cache.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index 82f5f504cba..364a50d377c 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -87,9 +87,11 @@ class Cache { protected function loadMimetypes(){ $result = \OC_DB::executeAudited('SELECT `id`, `mimetype` FROM `*PREFIX*mimetypes`', array()); - while ($result && $row = $result->fetchRow()) { - self::$mimetypeIds[$row['mimetype']] = $row['id']; - self::$mimetypes[$row['id']] = $row['mimetype']; + if ($result) { + while ($row = $result->fetchRow()) { + self::$mimetypeIds[$row['mimetype']] = $row['id']; + self::$mimetypes[$row['id']] = $row['mimetype']; + } } } -- cgit v1.2.3