Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/Folder/FolderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,20 @@ public function getAllFoldersWithSize(int $offset = 0, ?int $limit = null, strin
$query->setFirstResult($offset);
$query->setMaxResults($limit);
if ($orderBy === 'groups') {
$query
->leftJoin('f', 'group_folders_groups', 'g', $query->expr()->eq('f.folder_id', 'g.folder_id'))
->groupBy('f.folder_id')
->orderBy($query->func()->count('g.applicable_id'), $order);
// Order by the number of groups/circles applicable to each folder.
//
// We deliberately avoid a "JOIN group_folders_groups + GROUP BY f.folder_id"
// here. Grouping forces the database to materialize an internal temporary
// table, and because the selected `options` column is a LONGTEXT that
// temporary table cannot use the in-memory MEMORY engine, so MariaDB/MySQL
// always spills it to an on-disk (Aria) temp table regardless of
// `tmp_table_size`. A correlated sub-query yields the same ordering without
// grouping the wide result set, so no temporary table is created.
$countSubQuery = $this->connection->getQueryBuilder();
$countSubQuery->select($countSubQuery->func()->count('g.applicable_id'))
->from('group_folders_groups', 'g')
->where($countSubQuery->expr()->eq('g.folder_id', 'f.folder_id'));
$query->orderBy($query->createFunction('(' . $countSubQuery->getSQL() . ')'), $order);
} else {
$query->orderBy($orderBy, $order);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Folder/FolderManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\GroupFolders\Folder\FolderDefinition;
use OCA\GroupFolders\Folder\FolderDefinitionWithPermissions;
use OCA\GroupFolders\Folder\FolderManager;
use OCA\GroupFolders\Folder\FolderWithMappingsAndCache;
use OCA\GroupFolders\Mount\FolderStorageManager;
use OCA\GroupFolders\ResponseDefinitions;
use OCP\Constants;
Expand Down Expand Up @@ -517,4 +518,37 @@ public function testQuotaDefaultValue(): void {
}
$this->assertEquals(1024 ** 4, $folder->quota);
}

public function testGetAllFoldersWithSizeOrderedByGroups(): void {
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);

// Create folders with a different number of applicable groups each.
$oneGroup = $this->manager->createFolder('one-group');
$threeGroups = $this->manager->createFolder('three-groups');
$twoGroups = $this->manager->createFolder('two-groups');

$this->manager->addApplicableGroup($oneGroup, 'g1');

$this->manager->addApplicableGroup($twoGroups, 'g1');
$this->manager->addApplicableGroup($twoGroups, 'g2');

$this->manager->addApplicableGroup($threeGroups, 'g1');
$this->manager->addApplicableGroup($threeGroups, 'g2');
$this->manager->addApplicableGroup($threeGroups, 'g3');

$ascending = array_map(
fn (FolderWithMappingsAndCache $folder): string => $folder->mountPoint,
array_values($this->manager->getAllFoldersWithSize(0, null, 'groups', 'ASC')),
);
$this->assertEquals(['one-group', 'two-groups', 'three-groups'], $ascending);

$descending = array_map(
fn (FolderWithMappingsAndCache $folder): string => $folder->mountPoint,
array_values($this->manager->getAllFoldersWithSize(0, null, 'groups', 'DESC')),
);
$this->assertEquals(['three-groups', 'two-groups', 'one-group'], $descending);
}
}
Loading