From 748bcd407b4079adeceff3455749ad1e4d316c9d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 22 Jan 2019 17:08:32 +0100 Subject: [PATCH 1/2] add event for inserting cache entries this provides a reliable way for apps to listen to new files without the need to of cache wrappers to hook into inserts themselves (something which isn't 100% reliable) Signed-off-by: Robin Appelman --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Files/Cache/Cache.php | 24 ++++--- lib/private/Files/Cache/Storage.php | 4 +- lib/public/Files/Cache/CacheInsertEvent.php | 72 +++++++++++++++++++++ 5 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 lib/public/Files/Cache/CacheInsertEvent.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 5997825289..a96a851a5e 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -172,6 +172,7 @@ return array( 'OCP\\Federation\\ICloudIdManager' => $baseDir . '/lib/public/Federation/ICloudIdManager.php', 'OCP\\Files' => $baseDir . '/lib/public/Files.php', 'OCP\\Files\\AlreadyExistsException' => $baseDir . '/lib/public/Files/AlreadyExistsException.php', + 'OCP\\Files\\Cache\\CacheInsertEvent' => $baseDir . '/lib/public/Files/Cache/CacheInsertEvent.php', 'OCP\\Files\\Cache\\ICache' => $baseDir . '/lib/public/Files/Cache/ICache.php', 'OCP\\Files\\Cache\\ICacheEntry' => $baseDir . '/lib/public/Files/Cache/ICacheEntry.php', 'OCP\\Files\\Cache\\IPropagator' => $baseDir . '/lib/public/Files/Cache/IPropagator.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index ff8bea3bd9..21717fef65 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -202,6 +202,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Federation\\ICloudIdManager' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudIdManager.php', 'OCP\\Files' => __DIR__ . '/../../..' . '/lib/public/Files.php', 'OCP\\Files\\AlreadyExistsException' => __DIR__ . '/../../..' . '/lib/public/Files/AlreadyExistsException.php', + 'OCP\\Files\\Cache\\CacheInsertEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/CacheInsertEvent.php', 'OCP\\Files\\Cache\\ICache' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICache.php', 'OCP\\Files\\Cache\\ICacheEntry' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICacheEntry.php', 'OCP\\Files\\Cache\\IPropagator' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/IPropagator.php', diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index 43e48e5165..17e870bcbb 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -40,10 +40,12 @@ namespace OC\Files\Cache; use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use OCP\DB\QueryBuilder\IQueryBuilder; use Doctrine\DBAL\Driver\Statement; +use OCP\Files\Cache\CacheInsertEvent; use OCP\Files\Cache\ICache; use OCP\Files\Cache\ICacheEntry; use \OCP\Files\IMimeTypeLoader; use OCP\Files\Search\ISearchQuery; +use OCP\Files\Storage\IStorage; use OCP\IDBConnection; /** @@ -71,6 +73,8 @@ class Cache implements ICache { */ protected $storageId; + private $storage; + /** * @var Storage $storageCache */ @@ -84,18 +88,17 @@ class Cache implements ICache { */ protected $connection; + protected $eventDispatcher; + /** @var QuerySearchHelper */ protected $querySearchHelper; /** - * @param \OC\Files\Storage\Storage|string $storage + * @param IStorage $storage */ - public function __construct($storage) { - if ($storage instanceof \OC\Files\Storage\Storage) { - $this->storageId = $storage->getId(); - } else { - $this->storageId = $storage; - } + public function __construct(IStorage $storage) { + $this->storageId = $storage->getId(); + $this->storage = $storage; if (strlen($this->storageId) > 64) { $this->storageId = md5($this->storageId); } @@ -103,6 +106,7 @@ class Cache implements ICache { $this->storageCache = new Storage($storage); $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); $this->connection = \OC::$server->getDatabaseConnection(); + $this->eventDispatcher = \OC::$server->getEventDispatcher(); $this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader); } @@ -283,9 +287,11 @@ class Cache implements ICache { } if ($builder->execute()) { - return (int)$this->connection->lastInsertId('*PREFIX*filecache'); + $fileId = (int)$this->connection->lastInsertId('*PREFIX*filecache'); + $this->eventDispatcher->dispatch(CacheInsertEvent::class, new CacheInsertEvent($this->storage, $file, $fileId)); + return $fileId; } - } catch(UniqueConstraintViolationException $e) { + } catch (UniqueConstraintViolationException $e) { // entry exists already } diff --git a/lib/private/Files/Cache/Storage.php b/lib/private/Files/Cache/Storage.php index 794c4872c9..5b37c1f43f 100644 --- a/lib/private/Files/Cache/Storage.php +++ b/lib/private/Files/Cache/Storage.php @@ -28,6 +28,8 @@ namespace OC\Files\Cache; +use OCP\Files\Storage\IStorage; + /** * Handle the mapping between the string and numeric storage ids * @@ -61,7 +63,7 @@ class Storage { * @throws \RuntimeException */ public function __construct($storage, $isAvailable = true) { - if ($storage instanceof \OC\Files\Storage\Storage) { + if ($storage instanceof IStorage) { $this->storageId = $storage->getId(); } else { $this->storageId = $storage; diff --git a/lib/public/Files/Cache/CacheInsertEvent.php b/lib/public/Files/Cache/CacheInsertEvent.php new file mode 100644 index 0000000000..071f039d45 --- /dev/null +++ b/lib/public/Files/Cache/CacheInsertEvent.php @@ -0,0 +1,72 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see . + * + */ + +namespace OCP\Files\Cache; + +use OCP\Files\Storage\IStorage; +use Symfony\Component\EventDispatcher\Event; + +/** + * @since 16.0.0 + */ +class CacheInsertEvent extends Event { + private $storage; + private $path; + private $fileId; + + /** + * CacheInsertEvent constructor. + * + * @param IStorage $storage + * @param string $path + * @param int $fileId + * @since 16.0.0 + */ + public function __construct(IStorage $storage, string $path, int $fileId) { + $this->storage = $storage; + $this->path = $path; + $this->fileId = $fileId; + } + + /** + * @return IStorage + * @since 16.0.0 + */ + public function getStorage(): IStorage { + return $this->storage; + } + + /** + * @return string + * @since 16.0.0 + */ + public function getPath(): string { + return $this->path; + } + + /** + * @return int + * @since 16.0.0 + */ + public function getFileId(): int { + return $this->fileId; + } +} From 0ccd970f9d7c154dc4c2aedcbedd6658ca69a1c3 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 22 Jan 2019 17:28:57 +0100 Subject: [PATCH 2/2] adjust tests Signed-off-by: Robin Appelman --- tests/lib/Files/Node/FolderTest.php | 30 +++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/tests/lib/Files/Node/FolderTest.php b/tests/lib/Files/Node/FolderTest.php index 2c87b645dd..e1ee96c37e 100644 --- a/tests/lib/Files/Node/FolderTest.php +++ b/tests/lib/Files/Node/FolderTest.php @@ -290,7 +290,8 @@ class FolderTest extends NodeTest { ->method('getUser') ->will($this->returnValue($this->user)); $storage = $this->createMock(Storage::class); - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock(); + $storage->method('getId')->willReturn(''); + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $storage->expects($this->once()) ->method('getCache') @@ -340,8 +341,10 @@ class FolderTest extends NodeTest { $root->expects($this->any()) ->method('getUser') ->will($this->returnValue($this->user)); + /** @var \PHPUnit_Framework_MockObject_MockObject|Storage $storage */ $storage = $this->createMock(Storage::class); - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock(); + $storage->method('getId')->willReturn(''); + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $mount = $this->createMock(IMountPoint::class); $mount->expects($this->once()) @@ -391,7 +394,8 @@ class FolderTest extends NodeTest { ->method('getUser') ->will($this->returnValue($this->user)); $storage = $this->createMock(Storage::class); - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock(); + $storage->method('getId')->willReturn(''); + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $mount = $this->createMock(IMountPoint::class); $mount->expects($this->once()) @@ -441,7 +445,8 @@ class FolderTest extends NodeTest { ->method('getUser') ->will($this->returnValue($this->user)); $storage = $this->createMock(Storage::class); - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock(); + $storage->method('getId')->willReturn(''); + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $mount = $this->createMock(IMountPoint::class); $mount->expects($this->once()) @@ -491,8 +496,9 @@ class FolderTest extends NodeTest { ->method('getUser') ->will($this->returnValue($this->user)); $storage = $this->createMock(Storage::class); - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock(); - $subCache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock(); + $storage->method('getId')->willReturn(''); + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); + $subCache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $subStorage = $this->createMock(Storage::class); $subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock(); @@ -572,7 +578,8 @@ class FolderTest extends NodeTest { ->getMock(); $storage = $this->createMock(\OC\Files\Storage\Storage::class); $mount = new MountPoint($storage, '/bar'); - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock(); + $storage->method('getId')->willReturn(''); + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null); @@ -625,7 +632,8 @@ class FolderTest extends NodeTest { ->getMock(); $storage = $this->createMock(\OC\Files\Storage\Storage::class); $mount = new MountPoint($storage, '/bar'); - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock(); + $storage->method('getId')->willReturn(''); + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $fileInfo = new CacheEntry(['path' => '', 'mimetype' => 'text/plain'], null); @@ -673,7 +681,8 @@ class FolderTest extends NodeTest { ->getMock(); $storage = $this->createMock(\OC\Files\Storage\Storage::class); $mount = new MountPoint($storage, '/bar'); - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock(); + $storage->method('getId')->willReturn(''); + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $fileInfo = new CacheEntry(['path' => 'foobar', 'mimetype' => 'text/plain'], null); @@ -726,7 +735,8 @@ class FolderTest extends NodeTest { $storage = $this->createMock(\OC\Files\Storage\Storage::class); $mount1 = new MountPoint($storage, '/bar'); $mount2 = new MountPoint($storage, '/bar/foo/asd'); - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock(); + $storage->method('getId')->willReturn(''); + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);