Skip to content

Commit f6f6d74

Browse files
committed
optimize UserMountCache::registerStorage
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent a08281c commit f6f6d74

File tree

4 files changed

+60
-52
lines changed

4 files changed

+60
-52
lines changed

lib/private/Files/Config/CachedMountInfo.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class CachedMountInfo implements ICachedMountInfo {
3535
protected ?int $mountId;
3636
protected string $rootInternalPath;
3737
protected string $mountProvider;
38+
protected string $key;
3839

3940
/**
4041
* CachedMountInfo constructor.
@@ -65,6 +66,7 @@ public function __construct(
6566
throw new \Exception("Mount provider $mountProvider name exceeds the limit of 128 characters");
6667
}
6768
$this->mountProvider = $mountProvider;
69+
$this->key = $rootId . '::' . $mountPoint;
6870
}
6971

7072
/**
@@ -132,4 +134,8 @@ public function getRootInternalPath(): string {
132134
public function getMountProvider(): string {
133135
return $this->mountProvider;
134136
}
137+
138+
public function getKey(): string {
139+
return $this->key;
140+
}
135141
}

lib/private/Files/Config/LazyStorageMountInfo.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct(IUser $user, IMountPoint $mount) {
3939
$this->rootId = 0;
4040
$this->storageId = 0;
4141
$this->mountPoint = '';
42+
$this->key = '';
4243
}
4344

4445
/**
@@ -87,4 +88,11 @@ public function getRootInternalPath(): string {
8788
public function getMountProvider(): string {
8889
return $this->mount->getMountProvider();
8990
}
91+
92+
public function getKey(): string {
93+
if (!$this->key) {
94+
$this->key = $this->getRootId() . '::' . $this->getMountPoint();
95+
}
96+
return $this->key;
97+
}
9098
}

lib/private/Files/Config/UserMountCache.php

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -78,41 +78,27 @@ public function __construct(
7878

7979
public function registerMounts(IUser $user, array $mounts, array $mountProviderClasses = null) {
8080
$this->eventLogger->start('fs:setup:user:register', 'Registering mounts for user');
81-
// filter out non-proper storages coming from unit tests
82-
$mounts = array_filter($mounts, function (IMountPoint $mount) {
83-
return $mount instanceof SharedMount || ($mount->getStorage() && $mount->getStorage()->getCache());
84-
});
85-
/** @var ICachedMountInfo[] $newMounts */
86-
$newMounts = array_map(function (IMountPoint $mount) use ($user) {
81+
/** @var array<string, ICachedMountInfo> $newMounts */
82+
$newMounts = [];
83+
foreach ($mounts as $mount) {
8784
// filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
88-
if ($mount->getStorageRootId() === -1) {
89-
return null;
90-
} else {
91-
return new LazyStorageMountInfo($user, $mount);
85+
if ($mount->getStorageRootId() !== -1) {
86+
$mountInfo = new LazyStorageMountInfo($user, $mount);
87+
$newMounts[$mountInfo->getKey()] = $mountInfo;
9288
}
93-
}, $mounts);
94-
$newMounts = array_values(array_filter($newMounts));
95-
$newMountKeys = array_map(function (ICachedMountInfo $mount) {
96-
return $mount->getRootId() . '::' . $mount->getMountPoint();
97-
}, $newMounts);
98-
$newMounts = array_combine($newMountKeys, $newMounts);
89+
}
9990

10091
$cachedMounts = $this->getMountsForUser($user);
10192
if (is_array($mountProviderClasses)) {
10293
$cachedMounts = array_filter($cachedMounts, function (ICachedMountInfo $mountInfo) use ($mountProviderClasses, $newMounts) {
10394
// for existing mounts that didn't have a mount provider set
10495
// we still want the ones that map to new mounts
105-
$mountKey = $mountInfo->getRootId() . '::' . $mountInfo->getMountPoint();
106-
if ($mountInfo->getMountProvider() === '' && isset($newMounts[$mountKey])) {
96+
if ($mountInfo->getMountProvider() === '' && isset($newMounts[$mountInfo->getKey()])) {
10797
return true;
10898
}
10999
return in_array($mountInfo->getMountProvider(), $mountProviderClasses);
110100
});
111101
}
112-
$cachedRootKeys = array_map(function (ICachedMountInfo $mount) {
113-
return $mount->getRootId() . '::' . $mount->getMountPoint();
114-
}, $cachedMounts);
115-
$cachedMounts = array_combine($cachedRootKeys, $cachedMounts);
116102

117103
$addedMounts = [];
118104
$removedMounts = [];
@@ -131,46 +117,42 @@ public function registerMounts(IUser $user, array $mounts, array $mountProviderC
131117

132118
$changedMounts = $this->findChangedMounts($newMounts, $cachedMounts);
133119

134-
$this->connection->beginTransaction();
135-
try {
136-
foreach ($addedMounts as $mount) {
137-
$this->addToCache($mount);
138-
/** @psalm-suppress InvalidArgument */
139-
$this->mountsForUsers[$user->getUID()][] = $mount;
140-
}
141-
foreach ($removedMounts as $mount) {
142-
$this->removeFromCache($mount);
143-
$index = array_search($mount, $this->mountsForUsers[$user->getUID()]);
144-
unset($this->mountsForUsers[$user->getUID()][$index]);
145-
}
146-
foreach ($changedMounts as $mount) {
147-
$this->updateCachedMount($mount);
120+
if ($addedMounts || $removedMounts || $changedMounts) {
121+
$this->connection->beginTransaction();
122+
try {
123+
foreach ($addedMounts as $mount) {
124+
$this->addToCache($mount);
125+
/** @psalm-suppress InvalidArgument */
126+
$this->mountsForUsers[$user->getUID()][] = $mount;
127+
}
128+
foreach ($removedMounts as $mount) {
129+
$this->removeFromCache($mount);
130+
$index = array_search($mount, $this->mountsForUsers[$user->getUID()]);
131+
unset($this->mountsForUsers[$user->getUID()][$index]);
132+
}
133+
foreach ($changedMounts as $mount) {
134+
$this->updateCachedMount($mount);
135+
}
136+
$this->connection->commit();
137+
} catch (\Throwable $e) {
138+
$this->connection->rollBack();
139+
throw $e;
148140
}
149-
$this->connection->commit();
150-
} catch (\Throwable $e) {
151-
$this->connection->rollBack();
152-
throw $e;
153141
}
154142
$this->eventLogger->end('fs:setup:user:register');
155143
}
156144

157145
/**
158-
* @param ICachedMountInfo[] $newMounts
159-
* @param ICachedMountInfo[] $cachedMounts
146+
* @param array<string, ICachedMountInfo> $newMounts
147+
* @param array<string, ICachedMountInfo> $cachedMounts
160148
* @return ICachedMountInfo[]
161149
*/
162150
private function findChangedMounts(array $newMounts, array $cachedMounts) {
163-
$new = [];
164-
foreach ($newMounts as $mount) {
165-
$new[$mount->getRootId() . '::' . $mount->getMountPoint()] = $mount;
166-
}
167151
$changed = [];
168-
foreach ($cachedMounts as $cachedMount) {
169-
$key = $cachedMount->getRootId() . '::' . $cachedMount->getMountPoint();
170-
if (isset($new[$key])) {
171-
$newMount = $new[$key];
152+
foreach ($cachedMounts as $key => $cachedMount) {
153+
if (isset($newMounts[$key])) {
154+
$newMount = $newMounts[$key];
172155
if (
173-
$newMount->getMountPoint() !== $cachedMount->getMountPoint() ||
174156
$newMount->getStorageId() !== $cachedMount->getStorageId() ||
175157
$newMount->getMountId() !== $cachedMount->getMountId() ||
176158
$newMount->getMountProvider() !== $cachedMount->getMountProvider()
@@ -258,7 +240,12 @@ public function getMountsForUser(IUser $user) {
258240
$rows = $result->fetchAll();
259241
$result->closeCursor();
260242

261-
$this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
243+
$this->mountsForUsers[$user->getUID()] = [];
244+
/** @var array<string, ICachedMountInfo> $mounts */
245+
foreach ($rows as $row) {
246+
$mount = $this->dbRowToMountInfo($row);
247+
$this->mountsForUsers[$user->getUID()][$mount->getKey()] = $mount;
248+
}
262249
}
263250
return $this->mountsForUsers[$user->getUID()];
264251
}

lib/public/Files/Config/ICachedMountInfo.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,11 @@ public function getRootInternalPath(): string;
8484
* @since 24.0.0
8585
*/
8686
public function getMountProvider(): string;
87+
88+
/**
89+
* Get a key that uniquely identifies the mount
90+
*
91+
* @return string
92+
*/
93+
public function getKey(): string;
8794
}

0 commit comments

Comments
 (0)