]> source.dussan.org Git - nextcloud-server.git/commitdiff
Cache: seperate handing of numeric storage id's to it's own class
authorRobin Appelman <icewind@owncloud.com>
Thu, 25 Apr 2013 22:00:18 +0000 (00:00 +0200)
committerRobin Appelman <icewind@owncloud.com>
Thu, 25 Apr 2013 22:00:18 +0000 (00:00 +0200)
lib/files/cache/cache.php
lib/files/cache/storage.php [new file with mode: 0644]
lib/files/storage/common.php
lib/files/storage/storage.php

index 71b70abe3fec5f586cf3f191e3fc8bc6b7d97c39..857fe980be640b74f446d29edc1b30f25164b766 100644 (file)
@@ -30,11 +30,9 @@ class Cache {
        private $storageId;
 
        /**
-        * numeric storage id
-        *
-        * @var int $numericId
+        * @var Storage $storageCache
         */
-       private $numericId;
+       private $storageCache;
 
        private $mimetypeIds = array();
        private $mimetypes = array();
@@ -52,19 +50,11 @@ class Cache {
                        $this->storageId = md5($this->storageId);
                }
 
-               $query = \OC_DB::prepare('SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?');
-               $result = $query->execute(array($this->storageId));
-               if ($row = $result->fetchRow()) {
-                       $this->numericId = $row['numeric_id'];
-               } else {
-                       $query = \OC_DB::prepare('INSERT INTO `*PREFIX*storages`(`id`) VALUES(?)');
-                       $query->execute(array($this->storageId));
-                       $this->numericId = \OC_DB::insertid('*PREFIX*storages');
-               }
+               $this->storageCache = new Storage($storage);
        }
 
        public function getNumericStorageId() {
-               return $this->numericId;
+               return $this->storageCache->getNumericId();
        }
 
        /**
@@ -111,7 +101,7 @@ class Cache {
        public function get($file) {
                if (is_string($file) or $file == '') {
                        $where = 'WHERE `storage` = ? AND `path_hash` = ?';
-                       $params = array($this->numericId, md5($file));
+                       $params = array($this->getNumericStorageId(), md5($file));
                } else { //file id
                        $where = 'WHERE `fileid` = ?';
                        $params = array($file);
@@ -198,14 +188,14 @@ class Cache {
 
                        list($queryParts, $params) = $this->buildParts($data);
                        $queryParts[] = '`storage`';
-                       $params[] = $this->numericId;
+                       $params[] = $this->getNumericStorageId();
                        $valuesPlaceholder = array_fill(0, count($queryParts), '?');
 
                        $query = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache`(' . implode(', ', $queryParts) . ')'
                                . ' VALUES(' . implode(', ', $valuesPlaceholder) . ')');
                        $result = $query->execute($params);
                        if (\OC_DB::isError($result)) {
-                               \OCP\Util::writeLog('cache', 'Insert to cache failed: '.$result, \OCP\Util::ERROR);
+                               \OCP\Util::writeLog('cache', 'Insert to cache failed: ' . $result, \OCP\Util::ERROR);
                        }
 
                        return (int)\OC_DB::insertid('*PREFIX*filecache');
@@ -264,7 +254,7 @@ class Cache {
                $pathHash = md5($file);
 
                $query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?');
-               $result = $query->execute(array($this->numericId, $pathHash));
+               $result = $query->execute(array($this->getNumericStorageId(), $pathHash));
 
                if ($row = $result->fetchRow()) {
                        return $row['fileid'];
@@ -353,7 +343,7 @@ class Cache {
         */
        public function clear() {
                $query = \OC_DB::prepare('DELETE FROM `*PREFIX*filecache` WHERE storage = ?');
-               $query->execute(array($this->numericId));
+               $query->execute(array($this->getNumericStorageId()));
 
                $query = \OC_DB::prepare('DELETE FROM `*PREFIX*storages` WHERE id = ?');
                $query->execute(array($this->storageId));
@@ -367,7 +357,7 @@ class Cache {
        public function getStatus($file) {
                $pathHash = md5($file);
                $query = \OC_DB::prepare('SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?');
-               $result = $query->execute(array($this->numericId, $pathHash));
+               $result = $query->execute(array($this->getNumericStorageId(), $pathHash));
                if ($row = $result->fetchRow()) {
                        if ((int)$row['size'] === -1) {
                                return self::SHALLOW;
@@ -394,7 +384,7 @@ class Cache {
                        SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`, `etag`
                        FROM `*PREFIX*filecache` WHERE `name` LIKE ? AND `storage` = ?'
                );
-               $result = $query->execute(array($pattern, $this->numericId));
+               $result = $query->execute(array($pattern, $this->getNumericStorageId()));
                $files = array();
                while ($row = $result->fetchRow()) {
                        $row['mimetype'] = $this->getMimetype($row['mimetype']);
@@ -421,7 +411,7 @@ class Cache {
                        FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'
                );
                $mimetype = $this->getMimetypeId($mimetype);
-               $result = $query->execute(array($mimetype, $this->numericId));
+               $result = $query->execute(array($mimetype, $this->getNumericStorageId()));
                $files = array();
                while ($row = $result->fetchRow()) {
                        $row['mimetype'] = $this->getMimetype($row['mimetype']);
@@ -459,7 +449,7 @@ class Cache {
                        return 0;
                }
                $query = \OC_DB::prepare('SELECT `size` FROM `*PREFIX*filecache` WHERE `parent` = ? AND `storage` = ?');
-               $result = $query->execute(array($id, $this->numericId));
+               $result = $query->execute(array($id, $this->getNumericStorageId()));
                $totalSize = 0;
                $hasChilds = 0;
                while ($row = $result->fetchRow()) {
@@ -486,7 +476,7 @@ class Cache {
         */
        public function getAll() {
                $query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?');
-               $result = $query->execute(array($this->numericId));
+               $result = $query->execute(array($this->getNumericStorageId()));
                $ids = array();
                while ($row = $result->fetchRow()) {
                        $ids[] = $row['fileid'];
@@ -506,7 +496,7 @@ class Cache {
        public function getIncomplete() {
                $query = \OC_DB::prepare('SELECT `path` FROM `*PREFIX*filecache`'
                        . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC LIMIT 1');
-               $result = $query->execute(array($this->numericId));
+               $result = $query->execute(array($this->getNumericStorageId()));
                if ($row = $result->fetchRow()) {
                        return $row['path'];
                } else {
@@ -517,6 +507,7 @@ class Cache {
        /**
         * get the storage id of the storage for a file and the internal path of the file
         *
+        * @param int $id
         * @return array, first element holding the storage id, second the path
         */
        static public function getById($id) {
@@ -529,10 +520,8 @@ class Cache {
                        return null;
                }
 
-               $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*storages` WHERE `numeric_id` = ?');
-               $result = $query->execute(array($numericId));
-               if ($row = $result->fetchRow()) {
-                       return array($row['id'], $path);
+               if ($id = Storage::getStorageId($numericId)) {
+                       return array($id, $path);
                } else {
                        return null;
                }
diff --git a/lib/files/cache/storage.php b/lib/files/cache/storage.php
new file mode 100644 (file)
index 0000000..72de376
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Files\Cache;
+
+/**
+ * Class Storage
+ *
+ * cache storage specific data
+ *
+ * @package OC\Files\Cache
+ */
+class Storage {
+       private $storageId;
+       private $numericId;
+
+       /**
+        * @param \OC\Files\Storage\Storage|string $storage
+        */
+       public function __construct($storage) {
+               if ($storage instanceof \OC\Files\Storage\Storage) {
+                       $this->storageId = $storage->getId();
+               } else {
+                       $this->storageId = $storage;
+               }
+               if (strlen($this->storageId) > 64) {
+                       $this->storageId = md5($this->storageId);
+               }
+
+               $query = \OC_DB::prepare('SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?');
+               $result = $query->execute(array($this->storageId));
+               if ($row = $result->fetchRow()) {
+                       $this->numericId = $row['numeric_id'];
+               } else {
+                       $query = \OC_DB::prepare('INSERT INTO `*PREFIX*storages`(`id`) VALUES(?)');
+                       $query->execute(array($this->storageId));
+                       $this->numericId = \OC_DB::insertid('*PREFIX*storages');
+               }
+       }
+
+       public function getNumericId() {
+               return $this->numericId;
+       }
+
+       public static function getStorageId($numericId) {
+               $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*storages` WHERE `numeric_id` = ?');
+               $result = $query->execute(array($numericId));
+               if ($row = $result->fetchRow()) {
+                       return $row['id'];
+               } else {
+                       return null;
+               }
+       }
+}
index 38fe5e546f6be008daa43ad64a6cfc870afb8aa7..e87fe3b5239bde3c75e3e9b5c79fe1ac3c67e2d4 100644 (file)
@@ -25,6 +25,7 @@ abstract class Common implements \OC\Files\Storage\Storage {
        private $scanner;
        private $permissioncache;
        private $watcher;
+       private $storageCache;
 
        public function __construct($parameters) {
        }
@@ -300,6 +301,13 @@ abstract class Common implements \OC\Files\Storage\Storage {
                return $this->watcher;
        }
 
+       public function getStorageCache(){
+               if (!isset($this->storageCache)) {
+                       $this->storageCache = new \OC\Files\Cache\Storage($this);
+               }
+               return $this->storageCache;
+       }
+
        /**
         * get the owner of a path
         *
@@ -361,7 +369,7 @@ abstract class Common implements \OC\Files\Storage\Storage {
         * get the free space in the storage
         *
         * @param $path
-        * return int
+        * @return int
         */
        public function free_space($path) {
                return \OC\Files\FREE_SPACE_UNKNOWN;
index 1da82da2163402c09c1cf3848ab07a0dcb2557d0..c96caebf4af6307d22fc3ebb375f46e0792ae770 100644 (file)
@@ -328,6 +328,11 @@ interface Storage {
         */
        public function getWatcher($path = '');
 
+       /**
+        * @return \OC\Files\Cache\Storage
+        */
+       public function getStorageCache();
+
        /**
         * get the ETag for a file or folder
         *