| Total Complexity | 45 |
| Total Lines | 368 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like UserMountCache often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserMountCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class UserMountCache implements IUserMountCache { |
||
| 49 | /** |
||
| 50 | * @var IDBConnection |
||
| 51 | */ |
||
| 52 | private $connection; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var IUserManager |
||
| 56 | */ |
||
| 57 | private $userManager; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Cached mount info. |
||
| 61 | * Map of $userId to ICachedMountInfo. |
||
| 62 | * |
||
| 63 | * @var ICache |
||
| 64 | **/ |
||
| 65 | private $mountsForUsers; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var ILogger |
||
| 69 | */ |
||
| 70 | private $logger; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var ICache |
||
| 74 | */ |
||
| 75 | private $cacheInfoCache; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * UserMountCache constructor. |
||
| 79 | * |
||
| 80 | * @param IDBConnection $connection |
||
| 81 | * @param IUserManager $userManager |
||
| 82 | * @param ILogger $logger |
||
| 83 | */ |
||
| 84 | public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) { |
||
| 85 | $this->connection = $connection; |
||
| 86 | $this->userManager = $userManager; |
||
| 87 | $this->logger = $logger; |
||
| 88 | $this->cacheInfoCache = new CappedMemoryCache(); |
||
| 89 | $this->mountsForUsers = new CappedMemoryCache(); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function registerMounts(IUser $user, array $mounts) { |
||
| 93 | // filter out non-proper storages coming from unit tests |
||
| 94 | $mounts = array_filter($mounts, function (IMountPoint $mount) { |
||
| 95 | return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); |
||
| 96 | }); |
||
| 97 | /** @var ICachedMountInfo[] $newMounts */ |
||
| 98 | $newMounts = array_map(function (IMountPoint $mount) use ($user) { |
||
| 99 | // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) |
||
| 100 | if ($mount->getStorageRootId() === -1) { |
||
| 101 | return null; |
||
| 102 | } else { |
||
| 103 | return new LazyStorageMountInfo($user, $mount); |
||
| 104 | } |
||
| 105 | }, $mounts); |
||
| 106 | $newMounts = array_values(array_filter($newMounts)); |
||
| 107 | $newMountRootIds = array_map(function (ICachedMountInfo $mount) { |
||
| 108 | return $mount->getRootId(); |
||
| 109 | }, $newMounts); |
||
| 110 | $newMounts = array_combine($newMountRootIds, $newMounts); |
||
| 111 | |||
| 112 | $cachedMounts = $this->getMountsForUser($user); |
||
| 113 | $cachedMountRootIds = array_map(function (ICachedMountInfo $mount) { |
||
| 114 | return $mount->getRootId(); |
||
| 115 | }, $cachedMounts); |
||
| 116 | $cachedMounts = array_combine($cachedMountRootIds, $cachedMounts); |
||
| 117 | |||
| 118 | $addedMounts = []; |
||
| 119 | $removedMounts = []; |
||
| 120 | |||
| 121 | foreach ($newMounts as $rootId => $newMount) { |
||
| 122 | if (!isset($cachedMounts[$rootId])) { |
||
| 123 | $addedMounts[] = $newMount; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | foreach ($cachedMounts as $rootId => $cachedMount) { |
||
| 128 | if (!isset($newMounts[$rootId])) { |
||
| 129 | $removedMounts[] = $cachedMount; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts); |
||
| 134 | |||
| 135 | foreach ($addedMounts as $mount) { |
||
| 136 | $this->addToCache($mount); |
||
| 137 | $this->mountsForUsers[$user->getUID()][] = $mount; |
||
| 138 | } |
||
| 139 | foreach ($removedMounts as $mount) { |
||
| 140 | $this->removeFromCache($mount); |
||
| 141 | $index = array_search($mount, $this->mountsForUsers[$user->getUID()]); |
||
| 142 | unset($this->mountsForUsers[$user->getUID()][$index]); |
||
| 143 | } |
||
| 144 | foreach ($changedMounts as $mount) { |
||
| 145 | $this->updateCachedMount($mount); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param ICachedMountInfo[] $newMounts |
||
| 151 | * @param ICachedMountInfo[] $cachedMounts |
||
| 152 | * @return ICachedMountInfo[] |
||
| 153 | */ |
||
| 154 | private function findChangedMounts(array $newMounts, array $cachedMounts) { |
||
| 174 | } |
||
| 175 | |||
| 176 | private function addToCache(ICachedMountInfo $mount) { |
||
| 177 | if ($mount->getStorageId() !== -1) { |
||
| 178 | $this->connection->insertIfNotExist('*PREFIX*mounts', [ |
||
| 179 | 'storage_id' => $mount->getStorageId(), |
||
| 180 | 'root_id' => $mount->getRootId(), |
||
| 181 | 'user_id' => $mount->getUser()->getUID(), |
||
| 182 | 'mount_point' => $mount->getMountPoint(), |
||
| 183 | 'mount_id' => $mount->getMountId() |
||
| 184 | ], ['root_id', 'user_id']); |
||
| 185 | } else { |
||
| 186 | // in some cases this is legitimate, like orphaned shares |
||
| 187 | $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | private function updateCachedMount(ICachedMountInfo $mount) { |
||
| 192 | $builder = $this->connection->getQueryBuilder(); |
||
| 193 | |||
| 194 | $query = $builder->update('mounts') |
||
| 195 | ->set('storage_id', $builder->createNamedParameter($mount->getStorageId())) |
||
| 196 | ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint())) |
||
| 197 | ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT)) |
||
| 198 | ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
||
| 199 | ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
||
| 200 | |||
| 201 | $query->execute(); |
||
| 202 | } |
||
| 203 | |||
| 204 | private function removeFromCache(ICachedMountInfo $mount) { |
||
| 211 | } |
||
| 212 | |||
| 213 | private function dbRowToMountInfo(array $row) { |
||
| 214 | $user = $this->userManager->get($row['user_id']); |
||
| 215 | if (is_null($user)) { |
||
| 216 | return null; |
||
| 217 | } |
||
| 218 | $mount_id = $row['mount_id']; |
||
| 219 | if (!is_null($mount_id)) { |
||
| 220 | $mount_id = (int)$mount_id; |
||
| 221 | } |
||
| 222 | return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $mount_id, isset($row['path']) ? $row['path'] : ''); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param IUser $user |
||
| 227 | * @return ICachedMountInfo[] |
||
| 228 | */ |
||
| 229 | public function getMountsForUser(IUser $user) { |
||
| 230 | if (!isset($this->mountsForUsers[$user->getUID()])) { |
||
| 231 | $builder = $this->connection->getQueryBuilder(); |
||
| 232 | $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
||
| 233 | ->from('mounts', 'm') |
||
| 234 | ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
||
| 235 | ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); |
||
| 236 | |||
| 237 | $rows = $query->execute()->fetchAll(); |
||
| 238 | |||
| 239 | $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
||
| 240 | } |
||
| 241 | return $this->mountsForUsers[$user->getUID()]; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param int $numericStorageId |
||
| 246 | * @param string|null $user limit the results to a single user |
||
| 247 | * @return CachedMountInfo[] |
||
| 248 | */ |
||
| 249 | public function getMountsForStorageId($numericStorageId, $user = null) { |
||
| 250 | $builder = $this->connection->getQueryBuilder(); |
||
| 251 | $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
||
| 252 | ->from('mounts', 'm') |
||
| 253 | ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
||
| 254 | ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); |
||
| 255 | |||
| 256 | if ($user) { |
||
| 257 | $query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user))); |
||
| 258 | } |
||
| 259 | |||
| 260 | $rows = $query->execute()->fetchAll(); |
||
| 261 | |||
| 262 | return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param int $rootFileId |
||
| 267 | * @return CachedMountInfo[] |
||
| 268 | */ |
||
| 269 | public function getMountsForRootId($rootFileId) { |
||
| 270 | $builder = $this->connection->getQueryBuilder(); |
||
| 271 | $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
||
| 272 | ->from('mounts', 'm') |
||
| 273 | ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
||
| 274 | ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT))); |
||
| 275 | |||
| 276 | $rows = $query->execute()->fetchAll(); |
||
| 277 | |||
| 278 | return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param $fileId |
||
| 283 | * @return array |
||
| 284 | * @throws \OCP\Files\NotFoundException |
||
| 285 | */ |
||
| 286 | private function getCacheInfoFromFileId($fileId) { |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param int $fileId |
||
| 309 | * @param string|null $user optionally restrict the results to a single user |
||
| 310 | * @return ICachedMountFileInfo[] |
||
| 311 | * @since 9.0.0 |
||
| 312 | */ |
||
| 313 | public function getMountsForFileId($fileId, $user = null) { |
||
| 314 | try { |
||
| 315 | list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId); |
||
| 316 | } catch (NotFoundException $e) { |
||
| 317 | return []; |
||
| 318 | } |
||
| 319 | $mountsForStorage = $this->getMountsForStorageId($storageId, $user); |
||
| 320 | |||
| 321 | // filter mounts that are from the same storage but a different directory |
||
| 322 | $filteredMounts = array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { |
||
| 323 | if ($fileId === $mount->getRootId()) { |
||
| 324 | return true; |
||
| 325 | } |
||
| 326 | $internalMountPath = $mount->getRootInternalPath(); |
||
| 327 | |||
| 328 | return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; |
||
| 329 | }); |
||
| 330 | |||
| 331 | return array_map(function (ICachedMountInfo $mount) use ($internalPath) { |
||
| 332 | return new CachedMountFileInfo( |
||
| 333 | $mount->getUser(), |
||
| 334 | $mount->getStorageId(), |
||
| 335 | $mount->getRootId(), |
||
| 336 | $mount->getMountPoint(), |
||
| 337 | $mount->getMountId(), |
||
| 338 | $mount->getRootInternalPath(), |
||
| 339 | $internalPath |
||
| 340 | ); |
||
| 341 | }, $filteredMounts); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Remove all cached mounts for a user |
||
| 346 | * |
||
| 347 | * @param IUser $user |
||
| 348 | */ |
||
| 349 | public function removeUserMounts(IUser $user) { |
||
| 350 | $builder = $this->connection->getQueryBuilder(); |
||
| 351 | |||
| 352 | $query = $builder->delete('mounts') |
||
| 353 | ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID()))); |
||
| 354 | $query->execute(); |
||
| 355 | } |
||
| 356 | |||
| 357 | public function removeUserStorageMount($storageId, $userId) { |
||
| 364 | } |
||
| 365 | |||
| 366 | public function remoteStorageMounts($storageId) { |
||
| 367 | $builder = $this->connection->getQueryBuilder(); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param array $users |
||
| 376 | * @return array |
||
| 377 | * @suppress SqlInjectionChecker |
||
| 378 | */ |
||
| 379 | public function getUsedSpaceForUsers(array $users) { |
||
| 411 | } |
||
| 412 | |||
| 413 | public function clear(): void { |
||
| 418 |