multiple minor fies

This commit is contained in:
Robin Appelman 2015-12-02 13:36:33 +01:00
parent 222e719c87
commit 99415a9f7f
7 changed files with 37 additions and 22 deletions

View File

@ -93,11 +93,13 @@ class MountProviderCollection implements IMountProviderCollection, Emitter {
* @param IUser $user * @param IUser $user
* @param IMountPoint[] $mountPoints * @param IMountPoint[] $mountPoints
*/ */
public function cacheMounts(IUser $user, array $mountPoints) { public function registerMounts(IUser $user, array $mountPoints) {
$this->mountCache->registerMounts($user, $mountPoints); $this->mountCache->registerMounts($user, $mountPoints);
} }
/** /**
* Get the mount cache which can be used to search for mounts without setting up the filesystem
*
* @return IUserMountCache * @return IUserMountCache
*/ */
public function getMountCache() { public function getMountCache() {

View File

@ -31,6 +31,9 @@ use OCP\ILogger;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
/**
* Cache mounts points per user in the cache so we can easilly look them up
*/
class UserMountCache implements IUserMountCache { class UserMountCache implements IUserMountCache {
/** /**
* @var IDBConnection * @var IDBConnection
@ -64,15 +67,16 @@ class UserMountCache implements IUserMountCache {
} }
public function registerMounts(IUser $user, array $mounts) { public function registerMounts(IUser $user, array $mounts) {
// filter out non-proper storages coming from unit tests
$mounts = array_filter($mounts, function (IMountPoint $mount) { $mounts = array_filter($mounts, function (IMountPoint $mount) {
return $mount->getStorage()->getCache(); return $mount->getStorage()->getCache();
}); });
$mounts = array_values($mounts);
/** @var ICachedMountInfo[] $newMounts */ /** @var ICachedMountInfo[] $newMounts */
$newMounts = array_map(function (IMountPoint $mount) use ($user) { $newMounts = array_map(function (IMountPoint $mount) use ($user) {
$storage = $mount->getStorage(); $storage = $mount->getStorage();
$rootId = (int)$storage->getCache()->getId(''); $rootId = (int)$storage->getCache()->getId('');
$storageId = (int)$storage->getStorageCache()->getNumericId(); $storageId = (int)$storage->getStorageCache()->getNumericId();
// filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
if ($rootId === -1) { if ($rootId === -1) {
return null; return null;
} else { } else {
@ -80,9 +84,10 @@ class UserMountCache implements IUserMountCache {
} }
}, $mounts); }, $mounts);
$newMounts = array_values(array_filter($newMounts)); $newMounts = array_values(array_filter($newMounts));
$cachedMounts = $this->getMountsForUser($user);
$cachedMounts = $this->getMountsForUser($user);
$mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) { $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) {
// since we are only looking for mounts for a specific user comparing on root id is enough
return $mount1->getRootId() - $mount2->getRootId(); return $mount1->getRootId() - $mount2->getRootId();
}; };
@ -97,7 +102,8 @@ class UserMountCache implements IUserMountCache {
} }
foreach ($removedMounts as $mount) { foreach ($removedMounts as $mount) {
$this->removeFromCache($mount); $this->removeFromCache($mount);
$this->mountsForUsers[$user->getUID()] = []; $index = array_search($mount, $this->mountsForUsers[$user->getUID()]);
unset($this->mountsForUsers[$user->getUID()][$index]);
} }
} }
@ -121,6 +127,8 @@ class UserMountCache implements IUserMountCache {
$query->execute(); $query->execute();
} catch (UniqueConstraintViolationException $e) { } catch (UniqueConstraintViolationException $e) {
// seems to mainly happen in tests // seems to mainly happen in tests
// can also happen during concurrent access but we can safely ignore it
// since inserting the same data twice will still result in the correct data being inserted
$this->logger->error('Duplicate entry while inserting mount'); $this->logger->error('Duplicate entry while inserting mount');
$this->logger->logException($e); $this->logger->logException($e);
} }
@ -145,6 +153,7 @@ class UserMountCache implements IUserMountCache {
* @return ICachedMountInfo[] * @return ICachedMountInfo[]
*/ */
public function getMountsForUser(IUser $user) { public function getMountsForUser(IUser $user) {
if (!isset($this->mountsForUsers[$user->getUID()])) {
$builder = $this->connection->getQueryBuilder(); $builder = $this->connection->getQueryBuilder();
$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point') $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point')
->from('mounts') ->from('mounts')
@ -152,7 +161,9 @@ class UserMountCache implements IUserMountCache {
$rows = $query->execute()->fetchAll(); $rows = $query->execute()->fetchAll();
return array_map([$this, 'dbRowToMountInfo'], $rows); $this->mountsForUsers[$user->getUID()] = array_map([$this, 'dbRowToMountInfo'], $rows);
}
return $this->mountsForUsers[$user->getUID()];
} }
/** /**

View File

@ -428,7 +428,7 @@ class Filesystem {
$mounts = $mountConfigManager->getMountsForUser($userObject); $mounts = $mountConfigManager->getMountsForUser($userObject);
array_walk($mounts, array(self::$mounts, 'addMount')); array_walk($mounts, array(self::$mounts, 'addMount'));
$mounts[] = $mount; $mounts[] = $mount;
$mountConfigManager->cacheMounts($userObject, $mounts); $mountConfigManager->registerMounts($userObject, $mounts);
} }
self::listenForNewMountProviders($mountConfigManager, $userManager); self::listenForNewMountProviders($mountConfigManager, $userManager);

View File

@ -25,6 +25,8 @@ use OCP\Files\Node;
use OCP\IUser; use OCP\IUser;
/** /**
* Holds information about a mount for a user
*
* @since 9.0.0 * @since 9.0.0
*/ */
interface ICachedMountInfo { interface ICachedMountInfo {

View File

@ -46,4 +46,12 @@ interface IMountProviderCollection {
* @since 8.0.0 * @since 8.0.0
*/ */
public function registerProvider(IMountProvider $provider); public function registerProvider(IMountProvider $provider);
/**
* Get the mount cache which can be used to search for mounts without setting up the filesystem
*
* @return IUserMountCache
* @since 9.0.0
*/
public function getMountCache();
} }

View File

@ -25,6 +25,8 @@ use OCP\Files\Mount\IMountPoint;
use OCP\IUser; use OCP\IUser;
/** /**
* Cache mounts points per user in the cache so we can easily look them up
*
* @since 9.0.0 * @since 9.0.0
*/ */
interface IUserMountCache { interface IUserMountCache {

View File

@ -18,16 +18,6 @@ use OCP\IUserManager;
use Test\TestCase; use Test\TestCase;
use Test\Util\User\Dummy; use Test\Util\User\Dummy;
class NullLogger extends Log {
public function __construct($logger = null) {
//disable original constructor
}
public function log($level, $message, array $context = array()) {
//noop
}
}
/** /**
* @group DB * @group DB
*/ */
@ -54,7 +44,7 @@ class UserMountCache extends TestCase {
$userBackend->createUser('u1', ''); $userBackend->createUser('u1', '');
$userBackend->createUser('u2', ''); $userBackend->createUser('u2', '');
$this->userManager->registerBackend($userBackend); $this->userManager->registerBackend($userBackend);
$this->cache = new \OC\Files\Config\UserMountCache($this->connection, $this->userManager, new NullLogger()); $this->cache = new \OC\Files\Config\UserMountCache($this->connection, $this->userManager, $this->getMock('\OC\Log'));
} }
public function tearDown() { public function tearDown() {