Skip to content

Commit f57787f

Browse files
committed
extend cache events
- adds cache remove event - expose storage id in event - emit events during cache move Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent fd76bf1 commit f57787f

File tree

7 files changed

+69
-5
lines changed

7 files changed

+69
-5
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
'OCP\\Files' => $baseDir . '/lib/public/Files.php',
220220
'OCP\\Files\\AlreadyExistsException' => $baseDir . '/lib/public/Files/AlreadyExistsException.php',
221221
'OCP\\Files\\Cache\\CacheInsertEvent' => $baseDir . '/lib/public/Files/Cache/CacheInsertEvent.php',
222+
'OCP\\Files\\Cache\\CacheRemoveEvent' => $baseDir . '/lib/public/Files/Cache/CacheRemoveEvent.php',
222223
'OCP\\Files\\Cache\\CacheUpdateEvent' => $baseDir . '/lib/public/Files/Cache/CacheUpdateEvent.php',
223224
'OCP\\Files\\Cache\\ICache' => $baseDir . '/lib/public/Files/Cache/ICache.php',
224225
'OCP\\Files\\Cache\\ICacheEntry' => $baseDir . '/lib/public/Files/Cache/ICacheEntry.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
248248
'OCP\\Files' => __DIR__ . '/../../..' . '/lib/public/Files.php',
249249
'OCP\\Files\\AlreadyExistsException' => __DIR__ . '/../../..' . '/lib/public/Files/AlreadyExistsException.php',
250250
'OCP\\Files\\Cache\\CacheInsertEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/CacheInsertEvent.php',
251+
'OCP\\Files\\Cache\\CacheRemoveEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/CacheRemoveEvent.php',
251252
'OCP\\Files\\Cache\\CacheUpdateEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/CacheUpdateEvent.php',
252253
'OCP\\Files\\Cache\\ICache' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICache.php',
253254
'OCP\\Files\\Cache\\ICacheEntry' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICacheEntry.php',

lib/private/Files/Cache/AbstractCacheEvent.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@ class AbstractCacheEvent extends Event implements ICacheEvent {
3636
protected $storage;
3737
protected $path;
3838
protected $fileId;
39+
protected $storageId;
3940

4041
/**
4142
* @param IStorage $storage
4243
* @param string $path
4344
* @param int $fileId
4445
* @since 16.0.0
4546
*/
46-
public function __construct(IStorage $storage, string $path, int $fileId) {
47+
public function __construct(IStorage $storage, string $path, int $fileId, int $storageId) {
4748
$this->storage = $storage;
4849
$this->path = $path;
4950
$this->fileId = $fileId;
51+
$this->storageId = $storageId;
5052
}
5153

5254
/**
@@ -80,4 +82,12 @@ public function setPath(string $path): void {
8082
public function getFileId(): int {
8183
return $this->fileId;
8284
}
85+
86+
/**
87+
* @return int
88+
* @since 21.0.0
89+
*/
90+
public function getStorageId(): int {
91+
return $this->storageId;
92+
}
8393
}

lib/private/Files/Cache/Cache.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
4343
use OCP\DB\QueryBuilder\IQueryBuilder;
4444
use OCP\Files\Cache\CacheInsertEvent;
45+
use OCP\Files\Cache\CacheRemoveEvent;
4546
use OCP\Files\Cache\CacheUpdateEvent;
4647
use OCP\Files\Cache\ICache;
4748
use OCP\Files\Cache\ICacheEntry;
@@ -284,7 +285,8 @@ public function insert($file, array $data) {
284285
$data['name'] = basename($file);
285286

286287
[$values, $extensionValues] = $this->normalizeData($data);
287-
$values['storage'] = $this->getNumericStorageId();
288+
$storageId = $this->getNumericStorageId();
289+
$values['storage'] = $storageId;
288290

289291
try {
290292
$builder = $this->connection->getQueryBuilder();
@@ -308,7 +310,7 @@ public function insert($file, array $data) {
308310
$query->execute();
309311
}
310312

311-
$this->eventDispatcher->dispatch(CacheInsertEvent::class, new CacheInsertEvent($this->storage, $file, $fileId));
313+
$this->eventDispatcher->dispatch(CacheInsertEvent::class, new CacheInsertEvent($this->storage, $file, $fileId, $storageId));
312314
return $fileId;
313315
}
314316
} catch (UniqueConstraintViolationException $e) {
@@ -399,7 +401,7 @@ public function update($id, array $data) {
399401
$path = $this->getPathById($id);
400402
// path can still be null if the file doesn't exist
401403
if ($path !== null) {
402-
$this->eventDispatcher->dispatch(CacheUpdateEvent::class, new CacheUpdateEvent($this->storage, $path, $id));
404+
$this->eventDispatcher->dispatch(CacheUpdateEvent::class, new CacheUpdateEvent($this->storage, $path, $id, $this->getNumericStorageId()));
403405
}
404406
}
405407

@@ -536,6 +538,8 @@ public function remove($file) {
536538
if ($entry->getMimeType() == FileInfo::MIMETYPE_FOLDER) {
537539
$this->removeChildren($entry);
538540
}
541+
542+
$this->eventDispatcher->dispatch(CacheRemoveEvent::class, new CacheRemoveEvent($this->storage, $entry->getPath(), $entry->getId(), $this->getNumericStorageId()));
539543
}
540544
}
541545

@@ -677,9 +681,17 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
677681
$query->execute();
678682

679683
$this->connection->commit();
684+
685+
if ($sourceCache->getNumericStorageId() !== $this->getNumericStorageId()) {
686+
$this->eventDispatcher->dispatch(CacheRemoveEvent::class, new CacheRemoveEvent($this->storage, $sourcePath, $sourceId, $sourceCache->getNumericStorageId()));
687+
$this->eventDispatcher->dispatch(CacheInsertEvent::class, new CacheInsertEvent($this->storage, $targetPath, $sourceId, $this->getNumericStorageId()));
688+
} else {
689+
$this->eventDispatcher->dispatch(CacheUpdateEvent::class, new CacheUpdateEvent($this->storage, $targetPath, $sourceId, $this->getNumericStorageId()));
690+
}
680691
} else {
681692
$this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
682693
}
694+
683695
}
684696

685697
/**

lib/private/Files/Cache/CacheEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getStorageId() {
6767

6868

6969
public function getPath() {
70-
return $this->data['path'];
70+
return (string)$this->data['path'];
7171
}
7272

7373

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCP\Files\Cache;
25+
26+
use OC\Files\Cache\AbstractCacheEvent;
27+
28+
/**
29+
* Event for when an existing entry in the cache gets removed
30+
*
31+
* @since 21.0.0
32+
*/
33+
class CacheRemoveEvent extends AbstractCacheEvent {
34+
}

lib/public/Files/Cache/ICacheEvent.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@ public function setPath(string $path): void;
5656
* @since 16.0.0
5757
*/
5858
public function getFileId(): int;
59+
60+
/**
61+
* @return int
62+
* @since 21.0.0
63+
*/
64+
public function getStorageId(): int;
5965
}

0 commit comments

Comments
 (0)