Skip to content

Commit 4d95c62

Browse files
committed
perf(MountProvider): optimize and improve conflict handling
Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
1 parent 25818e9 commit 4d95c62

1 file changed

Lines changed: 42 additions & 21 deletions

File tree

lib/Mount/MountProvider.php

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,43 +52,64 @@ public function __construct(
5252
public function getMountsForUser(IUser $user, IStorageFactory $loader): array {
5353
$folders = $this->folderManager->getFoldersForUser($user);
5454

55-
$mountPoints = array_map(fn (FolderDefinitionWithPermissions $folder): string => 'files/' . $folder->mountPoint, $folders);
56-
$conflicts = $this->findConflictsForUser($user, $mountPoints);
55+
// Single pass to build both mountPoints and ACL data
56+
$mountPoints = [];
57+
$rootFileIds = [];
58+
59+
foreach ($folders as $folder) {
60+
$mountPoints[] = 'files/' . $folder->mountPoint;
61+
if ($folder->acl) {
62+
$rootFileIds[] = $folder->rootId;
63+
}
64+
}
5765

58-
/** @var array<FolderDefinitionWithPermissions> $foldersWithAcl */
59-
$foldersWithAcl = array_filter($folders, fn (FolderDefinitionWithPermissions $folder): bool => $folder->acl);
60-
$rootFileIds = array_map(fn (FolderDefinitionWithPermissions $folder): int => $folder->rootId, $foldersWithAcl);
66+
$conflicts = array_flip($this->findConflictsForUser($user, $mountPoints));
6167
$aclManager = $this->aclManagerFactory->getACLManager($user);
62-
$rootRules = $aclManager->getRulesByFileIds($rootFileIds);
68+
$rootRules = $rootFileIds
69+
? $aclManager->getRulesByFileIds($rootFileIds)
70+
: [];
71+
72+
$userStorage = null;
73+
$mounts = [];
6374

64-
return array_map(function (FolderDefinitionWithPermissions $folder) use ($user, $loader, $conflicts, $aclManager, $rootRules): IMountPoint {
65-
// check for existing files in the user home and rename them if needed
75+
foreach ($folders as $folder) {
6676
$originalFolderName = $folder->mountPoint;
67-
if (in_array($originalFolderName, $conflicts)) {
68-
/** @var IStorage $userStorage */
69-
$userStorage = $this->mountProviderCollection->getHomeMountForUser($user)->getStorage();
70-
$userCache = $userStorage->getCache();
71-
$i = 1;
72-
$folderName = $folder->mountPoint . ' (' . $i++ . ')';
7377

74-
while ($userCache->inCache("files/$folderName")) {
78+
if (isset($conflicts[$originalFolderName])) {
79+
if ($userStorage === null) {
80+
/** @var IStorage $userStorage */
81+
$userStorage = $this->mountProviderCollection->getHomeMountForUser($user)->getStorage();
82+
}
83+
84+
$i = 1;
85+
$folderName = $originalFolderName . ' (' . $i++ . ')';
86+
$cache = $userStorage->getCache();
87+
while ($cache->inCache('files/' . $folderName)) {
7588
$folderName = $originalFolderName . ' (' . $i++ . ')';
7689
}
7790

78-
$userStorage->rename("files/$originalFolderName", "files/$folderName");
79-
$userCache->move("files/$originalFolderName", "files/$folderName");
80-
$userStorage->getPropagator()->propagateChange("files/$folderName", time());
91+
$userStorage->rename(
92+
'files/' . $originalFolderName,
93+
'files/' . $folderName
94+
);
95+
$cache->move(
96+
'files/' . $originalFolderName,
97+
'files/' . $folderName
98+
);
99+
$userStorage->getPropagator()->propagateChange('files/' . $folderName, time());
81100
}
82101

83-
return $this->getMount(
102+
$mounts[] = $this->getMount(
84103
$folder,
85-
'/' . $user->getUID() . '/files/' . $folder->mountPoint,
104+
'/' . $user->getUID() . '/files/' . $originalFolderName,
86105
$loader,
87106
$user,
88107
$aclManager,
89108
$rootRules[$folder->storageId] ?? [],
90109
);
91-
}, $folders);
110+
}
111+
112+
return $mounts;
92113
}
93114

94115
private function getCurrentUID(): ?string {

0 commit comments

Comments
 (0)