Skip to content

Commit 2369ad9

Browse files
authored
Merge pull request #55629 from nextcloud/carl/cleanup-propagator
refactor(IPropagator): Cleanup implementation
2 parents 492a8c3 + 475c847 commit 2369ad9

File tree

5 files changed

+54
-93
lines changed

5 files changed

+54
-93
lines changed
Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,19 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
68
* SPDX-License-Identifier: AGPL-3.0-only
79
*/
810
namespace OC\Files\Cache;
911

12+
use OCP\Files\Storage\IStorage;
1013
use OCP\IDBConnection;
1114

1215
class HomePropagator extends Propagator {
13-
private $ignoredBaseFolders;
14-
15-
/**
16-
* @param \OC\Files\Storage\Storage $storage
17-
*/
18-
public function __construct(\OC\Files\Storage\Storage $storage, IDBConnection $connection) {
19-
parent::__construct($storage, $connection);
20-
$this->ignoredBaseFolders = ['files_encryption'];
21-
}
22-
23-
24-
/**
25-
* @param string $internalPath
26-
* @param int $time
27-
* @param int $sizeDifference number of bytes the file has grown
28-
*/
29-
public function propagateChange($internalPath, $time, $sizeDifference = 0) {
30-
[$baseFolder] = explode('/', $internalPath, 2);
31-
if (in_array($baseFolder, $this->ignoredBaseFolders)) {
32-
return [];
33-
} else {
34-
parent::propagateChange($internalPath, $time, $sizeDifference);
35-
}
16+
public function __construct(IStorage $storage, IDBConnection $connection) {
17+
parent::__construct($storage, $connection, ignore: ['files_encryption']);
3618
}
3719
}

lib/private/Files/Cache/Propagator.php

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -12,60 +14,40 @@
1214
use OCP\DB\QueryBuilder\IQueryBuilder;
1315
use OCP\Files\Cache\IPropagator;
1416
use OCP\Files\Storage\IReliableEtagStorage;
17+
use OCP\Files\Storage\IStorage;
1518
use OCP\IDBConnection;
1619
use OCP\Server;
20+
use Override;
1721
use Psr\Clock\ClockInterface;
1822
use Psr\Log\LoggerInterface;
1923

20-
/**
21-
* Propagate etags and mtimes within the storage
22-
*/
2324
class Propagator implements IPropagator {
2425
public const MAX_RETRIES = 3;
25-
private $inBatch = false;
26-
27-
private $batch = [];
28-
29-
/**
30-
* @var \OC\Files\Storage\Storage
31-
*/
32-
protected $storage;
33-
34-
/**
35-
* @var IDBConnection
36-
*/
37-
private $connection;
38-
39-
/**
40-
* @var array
41-
*/
42-
private $ignore = [];
4326

27+
private bool $inBatch = false;
28+
private array $batch = [];
4429
private ClockInterface $clock;
4530

46-
public function __construct(\OC\Files\Storage\Storage $storage, IDBConnection $connection, array $ignore = []) {
47-
$this->storage = $storage;
48-
$this->connection = $connection;
49-
$this->ignore = $ignore;
31+
public function __construct(
32+
protected readonly IStorage $storage,
33+
private readonly IDBConnection $connection,
34+
private readonly array $ignore = [],
35+
) {
5036
$this->clock = Server::get(ClockInterface::class);
5137
}
5238

53-
/**
54-
* @param string $internalPath
55-
* @param int $time
56-
* @param int $sizeDifference number of bytes the file has grown
57-
*/
58-
public function propagateChange($internalPath, $time, $sizeDifference = 0) {
39+
#[Override]
40+
public function propagateChange(string $internalPath, int $time, int $sizeDifference = 0): void {
5941
// Do not propagate changes in ignored paths
6042
foreach ($this->ignore as $ignore) {
6143
if (str_starts_with($internalPath, $ignore)) {
6244
return;
6345
}
6446
}
6547

66-
$time = min((int)$time, $this->clock->now()->getTimestamp());
48+
$time = min($time, $this->clock->now()->getTimestamp());
6749

68-
$storageId = $this->storage->getStorageCache()->getNumericId();
50+
$storageId = $this->storage->getCache()->getNumericStorageId();
6951

7052
$parents = $this->getParents($internalPath);
7153

@@ -137,7 +119,10 @@ public function propagateChange($internalPath, $time, $sizeDifference = 0) {
137119
}
138120
}
139121

140-
protected function getParents($path) {
122+
/**
123+
* @return string[]
124+
*/
125+
protected function getParents(string $path): array {
141126
$parts = explode('/', $path);
142127
$parent = '';
143128
$parents = [];
@@ -148,19 +133,12 @@ protected function getParents($path) {
148133
return $parents;
149134
}
150135

151-
/**
152-
* Mark the beginning of a propagation batch
153-
*
154-
* Note that not all cache setups support propagation in which case this will be a noop
155-
*
156-
* Batching for cache setups that do support it has to be explicit since the cache state is not fully consistent
157-
* before the batch is committed.
158-
*/
159-
public function beginBatch() {
136+
#[Override]
137+
public function beginBatch(): void {
160138
$this->inBatch = true;
161139
}
162140

163-
private function addToBatch($internalPath, $time, $sizeDifference) {
141+
private function addToBatch(string $internalPath, int $time, int $sizeDifference): void {
164142
if (!isset($this->batch[$internalPath])) {
165143
$this->batch[$internalPath] = [
166144
'hash' => md5($internalPath),
@@ -175,10 +153,8 @@ private function addToBatch($internalPath, $time, $sizeDifference) {
175153
}
176154
}
177155

178-
/**
179-
* Commit the active propagation batch
180-
*/
181-
public function commitBatch() {
156+
#[Override]
157+
public function commitBatch(): void {
182158
if (!$this->inBatch) {
183159
throw new \BadMethodCallException('Not in batch');
184160
}
@@ -187,7 +163,7 @@ public function commitBatch() {
187163
$this->connection->beginTransaction();
188164

189165
$query = $this->connection->getQueryBuilder();
190-
$storageId = (int)$this->storage->getStorageCache()->getNumericId();
166+
$storageId = $this->storage->getCache()->getNumericStorageId();
191167

192168
$query->update('filecache')
193169
->set('mtime', $query->func()->greatest('mtime', $query->createParameter('time')))
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -8,21 +10,14 @@
810

911
use OC\Files\Cache\Propagator;
1012
use OC\Files\Storage\Wrapper\Jail;
13+
use Override;
1114

1215
class JailPropagator extends Propagator {
13-
/**
14-
* @var Jail
15-
*/
16-
protected $storage;
17-
18-
/**
19-
* @param string $internalPath
20-
* @param int $time
21-
* @param int $sizeDifference
22-
*/
23-
public function propagateChange($internalPath, $time, $sizeDifference = 0) {
24-
/** @var \OC\Files\Storage\Storage $storage */
25-
[$storage, $sourceInternalPath] = $this->storage->resolvePath($internalPath);
16+
#[Override]
17+
public function propagateChange(string $internalPath, int $time, int $sizeDifference = 0): void {
18+
/** @var Jail $jail */
19+
$jail = $this->storage;
20+
[$storage, $sourceInternalPath] = $jail->resolvePath($internalPath);
2621
$storage->getPropagator()->propagateChange($sourceInternalPath, $time, $sizeDifference);
2722
}
2823
}

lib/private/Files/Storage/Wrapper/Jail.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ public function changeLock(string $path, int $type, ILockingProvider $provider):
215215
}
216216

217217
/**
218-
* Resolve the path for the source of the share
218+
* Resolve the path for the source of the share.
219+
*
220+
* @return array{0: IStorage, 1: string}
219221
*/
220222
public function resolvePath(string $path): array {
221223
return [$this->getWrapperStorage(), $this->getUnjailedPath($path)];
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
68
* SPDX-License-Identifier: AGPL-3.0-only
79
*/
10+
811
namespace OCP\Files\Cache;
912

13+
use OCP\AppFramework\Attribute\Consumable;
14+
1015
/**
11-
* Propagate etags and mtimes within the storage
16+
* Propagate ETags and mtimes within the storage.
1217
*
1318
* @since 9.0.0
1419
*/
20+
#[Consumable(since: '9.0.0')]
1521
interface IPropagator {
1622
/**
17-
* Mark the beginning of a propagation batch
23+
* Mark the beginning of a propagation batch.
1824
*
1925
* Note that not all cache setups support propagation in which case this will be a noop
2026
*
@@ -23,20 +29,20 @@ interface IPropagator {
2329
*
2430
* @since 9.1.0
2531
*/
26-
public function beginBatch();
32+
public function beginBatch(): void;
2733

2834
/**
29-
* Commit the active propagation batch
35+
* Commit the active propagation batch.
3036
*
3137
* @since 9.1.0
3238
*/
33-
public function commitBatch();
39+
public function commitBatch(): void;
3440

3541
/**
3642
* @param string $internalPath
3743
* @param int $time
38-
* @param int $sizeDifference
44+
* @param int $sizeDifference The number of bytes the file has grown.
3945
* @since 9.0.0
4046
*/
41-
public function propagateChange($internalPath, $time, $sizeDifference = 0);
47+
public function propagateChange(string $internalPath, int $time, int $sizeDifference = 0): void;
4248
}

0 commit comments

Comments
 (0)