perf: Don't fetch all the trash items when deleting a single item

Signed-off-by: Carl Schwan <carlschwan@kde.org>
pull/61906/head
Carl Schwan 1 month ago committed by Andy Scherzinger
parent 7d77d0d5f7
commit 3d63b0fffa
  1. 24
      apps/files_trashbin/lib/Helper.php
  2. 24
      apps/files_trashbin/lib/Sabre/TrashRoot.php
  3. 9
      apps/files_trashbin/lib/Trash/ITrashManager.php
  4. 8
      apps/files_trashbin/lib/Trash/LegacyTrashBackend.php
  5. 20
      apps/files_trashbin/lib/Trash/TrashManager.php

@ -53,6 +53,30 @@ class Helper {
return $result;
}
public static function getTrashFile(string $dir, string $user, string $name): ?FileInfo {
$timestamp = null;
$view = new View('/' . $user . '/files_trashbin/files');
if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) {
throw new \Exception('Directory does not exists');
}
$mount = $view->getMount($dir);
$storage = $mount->getStorage();
$absoluteDir = $view->getAbsolutePath($dir);
$internalPath = $mount->getInternalPath($absoluteDir);
$extraData = Trashbin::getExtraData($user);
$entry = $storage->getCache()->get($mount->getInternalPath($view->getAbsolutePath($dir . '/' . $name)));
if ($entry === false) {
return null;
}
$timestamp = null;
return self::buildFileInfo($entry, $dir, $timestamp, $extraData, $storage, $absoluteDir, $internalPath, $mount);
}
private static function buildFileInfo(ICacheEntry $entry, string $dir, ?string &$timestamp, array $extraData, $storage, string $absoluteDir, string $internalPath, IMountPoint $mount): FileInfo {
$entryName = $entry->getName();
$name = $entryName;

@ -63,37 +63,31 @@ class TrashRoot implements ICollection {
public function getChildren(): array {
$entries = $this->trashManager->listTrashRoot($this->user);
$children = array_map(function (ITrashItem $entry) {
return array_map(function (ITrashItem $entry): TrashFile|TrashFolder {
if ($entry->getType() === FileInfo::TYPE_FOLDER) {
return new TrashFolder($this->trashManager, $entry);
}
return new TrashFile($this->trashManager, $entry);
}, $entries);
return $children;
}
#[\Override]
public function getChild($name): ITrash {
$entries = $this->getChildren();
$entry = $this->trashManager->getTrashRootItem($this->user, $name);
foreach ($entries as $entry) {
if ($entry->getName() === $name) {
return $entry;
}
if ($entry === null) {
throw new NotFound();
}
throw new NotFound();
if ($entry->getType() === FileInfo::TYPE_FOLDER) {
return new TrashFolder($this->trashManager, $entry);
}
return new TrashFile($this->trashManager, $entry);
}
#[\Override]
public function childExists($name): bool {
try {
$this->getChild($name);
return true;
} catch (NotFound $e) {
return false;
}
return $this->trashManager->getTrashRootItem($this->user, $name) !== null;
}
#[\Override]

@ -31,6 +31,15 @@ interface ITrashManager extends ITrashBackend {
#[\Override]
public function listTrashRoot(IUser $user): array;
/**
* Get a specific item in the root of the trashbin
*
* @param IUser $user
* @return ?ITrashItem
* @since 35.0.0
*/
public function getTrashRootItem(IUser $user, string $name): ?ITrashItem;
/**
* Temporally prevent files from being moved to the trash
*

@ -65,6 +65,14 @@ class LegacyTrashBackend implements ITrashBackend {
return $this->mapTrashItems($entries, $user);
}
public function getTrashRootItem(IUser $user, string $name): ?ITrashItem {
$entry = Helper::getTrashFile('/', $user->getUID(), $name);
if ($entry === null) {
return null;
}
return $this->mapTrashItems([$entry], $user)[0];
}
#[\Override]
public function listTrashFolder(ITrashItem $folder): array {
$user = $folder->getUser();

@ -39,6 +39,26 @@ class TrashManager implements ITrashManager {
return $items;
}
#[\Override]
public function getTrashRootItem(IUser $user, string $name): ?ITrashItem {
foreach ($this->getBackends() as $backend) {
if (method_exists($backend, 'getTrashRootItem')) {
$item = $backend->getTrashRootItem($user, $name);
if ($item !== null) {
return $item;
}
} else {
$items = $backend->listTrashRoot($user);
foreach ($items as $item) {
if ($item->getName() === $name) {
return $item;
}
}
}
}
return null;
}
private function getBackendForItem(ITrashItem $item) {
return $item->getTrashBackend();
}

Loading…
Cancel
Save