summaryrefslogtreecommitdiffstats
path: root/lib/private/files
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2015-11-26 17:47:53 +0100
committerRobin Appelman <icewind@owncloud.com>2016-01-20 16:30:22 +0100
commitcf6ee1c866ccca617cb2cae98966f04612084e7f (patch)
tree221d3acea8c7c39035cbfa3133dda6f74f4b3184 /lib/private/files
parent647d8ea5decad8140f9c280ae90039aa71c4eb93 (diff)
downloadnextcloud-server-cf6ee1c866ccca617cb2cae98966f04612084e7f.tar.gz
nextcloud-server-cf6ee1c866ccca617cb2cae98966f04612084e7f.zip
cache mountpoints in the db
Diffstat (limited to 'lib/private/files')
-rw-r--r--lib/private/files/config/cachedmountinfo.php107
-rw-r--r--lib/private/files/config/mountprovidercollection.php28
-rw-r--r--lib/private/files/config/usermountcache.php167
-rw-r--r--lib/private/files/filesystem.php7
4 files changed, 307 insertions, 2 deletions
diff --git a/lib/private/files/config/cachedmountinfo.php b/lib/private/files/config/cachedmountinfo.php
new file mode 100644
index 00000000000..dba07715edc
--- /dev/null
+++ b/lib/private/files/config/cachedmountinfo.php
@@ -0,0 +1,107 @@
+<?php
+/**
+ * @author Robin Appelman <icewind@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @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\Config;
+
+use OC\Files\Filesystem;
+use OCP\Files\Config\ICachedMountInfo;
+use OCP\Files\Node;
+use OCP\IUser;
+
+class CachedMountInfo implements ICachedMountInfo {
+ /**
+ * @var IUser
+ */
+ private $user;
+
+ /**
+ * @var int
+ */
+ private $storageId;
+
+ /**
+ * @var int
+ */
+ private $rootId;
+
+ /**
+ * @var string
+ */
+ private $mountPoint;
+
+ /**
+ * CachedMountInfo constructor.
+ *
+ * @param IUser $user
+ * @param int $storageId
+ * @param int $rootId
+ * @param string $mountPoint
+ */
+ public function __construct(IUser $user, $storageId, $rootId, $mountPoint) {
+ $this->user = $user;
+ $this->storageId = $storageId;
+ $this->rootId = $rootId;
+ $this->mountPoint = $mountPoint;
+ }
+
+ /**
+ * @return IUser
+ */
+ public function getUser() {
+ return $this->user;
+ }
+
+ /**
+ * @return int the numeric storage id of the mount
+ */
+ public function getStorageId() {
+ return $this->storageId;
+ }
+
+ /**
+ * @return int the fileid of the root of the mount
+ */
+ public function getRootId() {
+ return $this->rootId;
+ }
+
+ /**
+ * @return Node the root node of the mount
+ */
+ public function getMountPointNode() {
+ // TODO injection etc
+ Filesystem::initMountPoints($this->user->getUID());
+ $userNode = \OC::$server->getUserFolder($this->user->getUID());
+ $nodes = $userNode->getById($this->rootId);
+ if (count($nodes) > 0) {
+ return $nodes[0];
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * @return string the mount point of the mount for the user
+ */
+ public function getMountPoint() {
+ return $this->mountPoint;
+ }
+}
diff --git a/lib/private/files/config/mountprovidercollection.php b/lib/private/files/config/mountprovidercollection.php
index eb61ec3f5d5..acb106f926a 100644
--- a/lib/private/files/config/mountprovidercollection.php
+++ b/lib/private/files/config/mountprovidercollection.php
@@ -26,6 +26,8 @@ use OC\Hooks\Emitter;
use OC\Hooks\EmitterTrait;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\Config\IMountProvider;
+use OCP\Files\Config\IUserMountCache;
+use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage\IStorageFactory;
use OCP\IUser;
@@ -43,10 +45,17 @@ class MountProviderCollection implements IMountProviderCollection, Emitter {
private $loader;
/**
+ * @var \OCP\Files\Config\IUserMountCache
+ */
+ private $mountCache;
+
+ /**
* @param \OCP\Files\Storage\IStorageFactory $loader
+ * @param IUserMountCache $mountCache
*/
- public function __construct(IStorageFactory $loader) {
+ public function __construct(IStorageFactory $loader, IUserMountCache $mountCache) {
$this->loader = $loader;
+ $this->mountCache = $mountCache;
}
/**
@@ -77,4 +86,21 @@ class MountProviderCollection implements IMountProviderCollection, Emitter {
$this->providers[] = $provider;
$this->emit('\OC\Files\Config', 'registerMountProvider', [$provider]);
}
+
+ /**
+ * Cache mounts for user
+ *
+ * @param IUser $user
+ * @param IMountPoint[] $mountPoints
+ */
+ public function cacheMounts(IUser $user, array $mountPoints) {
+ $this->mountCache->registerMounts($user, $mountPoints);
+ }
+
+ /**
+ * @return IUserMountCache
+ */
+ public function getMountCache() {
+ return $this->mountCache;
+ }
}
diff --git a/lib/private/files/config/usermountcache.php b/lib/private/files/config/usermountcache.php
new file mode 100644
index 00000000000..b191926a5a4
--- /dev/null
+++ b/lib/private/files/config/usermountcache.php
@@ -0,0 +1,167 @@
+<?php
+/**
+ * @author Robin Appelman <icewind@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @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\Config;
+
+use OCP\Files\Config\ICachedMountInfo;
+use OCP\Files\Config\IUserMountCache;
+use OCP\Files\Mount\IMountPoint;
+use OCP\ICache;
+use OCP\IDBConnection;
+use OCP\IUser;
+use OCP\IUserManager;
+
+class UserMountCache implements IUserMountCache {
+ /**
+ * @var IDBConnection
+ */
+ private $connection;
+
+ /**
+ * @var IUserManager
+ */
+ private $userManager;
+
+ /** @var ICachedMountInfo[][] [$userId => [$cachedMountInfo, ....], ...] */
+ private $mountsForUsers = [];
+
+ /**
+ * UserMountCache constructor.
+ *
+ * @param IDBConnection $connection
+ * @param IUserManager $userManager
+ */
+ public function __construct(IDBConnection $connection, IUserManager $userManager) {
+ $this->connection = $connection;
+ $this->userManager = $userManager;
+ }
+
+ public function registerMounts(IUser $user, array $mounts) {
+ $mounts = array_values($mounts);
+ /** @var ICachedMountInfo[] $newMounts */
+ $newMounts = array_map(function (IMountPoint $mount) use ($user) {
+ $storage = $mount->getStorage();
+ $rootId = (int)$storage->getCache()->getId('');
+ $storageId = (int)$storage->getStorageCache()->getNumericId();
+ return new CachedMountInfo($user, $storageId, $rootId, $mount->getMountPoint());
+ }, $mounts);
+ $cachedMounts = $this->getMountsForUser($user);
+
+ $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) {
+ return $mount1->getRootId() - $mount2->getRootId();
+ };
+
+ /** @var ICachedMountInfo[] $addedMounts */
+ $addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff);
+ /** @var ICachedMountInfo[] $removedMounts */
+ $removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff);
+
+ foreach ($addedMounts as $mount) {
+ $this->addToCache($mount);
+ $this->mountsForUsers[$user->getUID()][] = $mount;
+ }
+ foreach ($removedMounts as $mount) {
+ $this->removeFromCache($mount);
+ $this->mountsForUsers[$user->getUID()] = [];
+ }
+ }
+
+ private function addToCache(ICachedMountInfo $mount) {
+ $builder = $this->connection->getQueryBuilder();
+
+ $query = $builder->insert('mounts')
+ ->values([
+ 'storage_id' => ':storage',
+ 'root_id' => ':root',
+ 'user_id' => ':user',
+ 'mount_point' => ':mount'
+ ]);
+ $query->setParameters([
+ ':storage' => $mount->getStorageId(),
+ ':root' => $mount->getRootId(),
+ ':user' => $mount->getUser()->getUID(),
+ ':mount' => $mount->getMountPoint()
+ ]);
+ $query->execute();
+ }
+
+ private function removeFromCache(ICachedMountInfo $mount) {
+ $builder = $this->connection->getQueryBuilder();
+
+ $query = $builder->delete('mounts')
+ ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
+ ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), \PDO::PARAM_INT)));
+ $query->execute();
+ }
+
+ private function dbRowToMountInfo(array $row) {
+ $user = $this->userManager->get($row['user_id']);
+ return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point']);
+ }
+
+ /**
+ * @param IUser $user
+ * @return ICachedMountInfo[]
+ */
+ public function getMountsForUser(IUser $user) {
+ if (!isset($this->mountsForUsers[$user->getUID()])) {
+ $builder = $this->connection->getQueryBuilder();
+ $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point')
+ ->from('mounts')
+ ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID())));
+
+ $rows = $query->execute()->fetchAll();
+
+ $this->mountsForUsers[$user->getUID()] = array_map([$this, 'dbRowToMountInfo'], $rows);
+ }
+ return $this->mountsForUsers[$user->getUID()];
+ }
+
+ /**
+ * @param int $numericStorageId
+ * @return CachedMountInfo[]
+ */
+ public function getMountsForStorageId($numericStorageId) {
+ $builder = $this->connection->getQueryBuilder();
+ $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point')
+ ->from('mounts')
+ ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, \PDO::PARAM_INT)));
+
+ $rows = $query->execute()->fetchAll();
+
+ return array_map([$this, 'dbRowToMountInfo'], $rows);
+ }
+
+ /**
+ * @param int $rootFileId
+ * @return CachedMountInfo[]
+ */
+ public function getMountsForRootId($rootFileId) {
+ $builder = $this->connection->getQueryBuilder();
+ $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point')
+ ->from('mounts')
+ ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, \PDO::PARAM_INT)));
+
+ $rows = $query->execute()->fetchAll();
+
+ return array_map([$this, 'dbRowToMountInfo'], $rows);
+ }
+}
diff --git a/lib/private/files/filesystem.php b/lib/private/files/filesystem.php
index ffe3a594ba8..ac39e385316 100644
--- a/lib/private/files/filesystem.php
+++ b/lib/private/files/filesystem.php
@@ -59,8 +59,10 @@
namespace OC\Files;
use OC\Files\Config\MountProviderCollection;
+use OC\Files\Mount\MountPoint;
use OC\Files\Storage\StorageFactory;
use OCP\Files\Config\IMountProvider;
+use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\IUserManager;
@@ -412,7 +414,8 @@ class Filesystem {
$homeStorage['arguments']['legacy'] = true;
}
- self::mount($homeStorage['class'], $homeStorage['arguments'], $user);
+ $mount = new MountPoint($homeStorage['class'], '/' . $user, $homeStorage['arguments'], self::getLoader());
+ self::$mounts->addMount($mount);
$home = \OC\Files\Filesystem::getStorage($user);
@@ -424,6 +427,8 @@ class Filesystem {
if ($userObject) {
$mounts = $mountConfigManager->getMountsForUser($userObject);
array_walk($mounts, array(self::$mounts, 'addMount'));
+ $mounts[] = $mount;
+ $mountConfigManager->cacheMounts($userObject, $mounts);
}
self::listenForNewMountProviders($mountConfigManager, $userManager);