Skip to content

Commit 42ff60a

Browse files
authored
IBX-10186: Added limits to Repository Filtering count and subtree queries (#696)
For more details see https://ibexa.atlassian.net/browse/IBX-10186 and #696 Key changes: * Added limit parameter to count and subtree size methods in various services * [Tests] Updated tests coverage --------- Co-Authored-By: <ryanolee@users.noreply.github.com>
1 parent 6ccb8b0 commit 42ff60a

33 files changed

Lines changed: 336 additions & 69 deletions

File tree

phpstan-baseline.neon

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32133,7 +32133,7 @@ parameters:
3213332133
-
3213432134
message: '#^Cannot access property \$id on Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\Location\|null\.$#'
3213532135
identifier: property.nonObject
32136-
count: 11
32136+
count: 9
3213732137
path: tests/integration/Core/Repository/LocationServiceTest.php
3213832138

3213932139
-
@@ -32340,12 +32340,6 @@ parameters:
3234032340
count: 1
3234132341
path: tests/integration/Core/Repository/LocationServiceTest.php
3234232342

32343-
-
32344-
message: '#^Method Ibexa\\Tests\\Integration\\Core\\Repository\\LocationServiceTest\:\:testGetSubtreeSize\(\) should return Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\Location but returns Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\Location\|null\.$#'
32345-
identifier: return.type
32346-
count: 1
32347-
path: tests/integration/Core/Repository/LocationServiceTest.php
32348-
3234932343
-
3235032344
message: '#^Method Ibexa\\Tests\\Integration\\Core\\Repository\\LocationServiceTest\:\:testHideLocation\(\) has no return type specified\.$#'
3235132345
identifier: missingType.return
@@ -32598,12 +32592,6 @@ parameters:
3259832592
count: 1
3259932593
path: tests/integration/Core/Repository/LocationServiceTest.php
3260032594

32601-
-
32602-
message: '#^Parameter \#1 \$location of method Ibexa\\Contracts\\Core\\Repository\\LocationService\:\:getSubtreeSize\(\) expects Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\Location, Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\Location\|null given\.$#'
32603-
identifier: argument.type
32604-
count: 2
32605-
path: tests/integration/Core/Repository/LocationServiceTest.php
32606-
3260732595
-
3260832596
message: '#^Parameter \#1 \$location of method Ibexa\\Contracts\\Core\\Repository\\LocationService\:\:moveSubtree\(\) expects Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\Location, Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\Location\|null given\.$#'
3260932597
identifier: argument.type

src/contracts/Persistence/Content/Location/Handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function loadParentLocationsForDraftContent($contentId);
111111
*/
112112
public function copySubtree($sourceId, $destinationParentId);
113113

114-
public function getSubtreeSize(string $path): int;
114+
public function getSubtreeSize(string $path, ?int $limit = null): int;
115115

116116
/**
117117
* Moves location identified by $sourceId into new parent identified by $destinationParentId.

src/contracts/Persistence/Filter/Content/Handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ interface Handler
2222
*/
2323
public function find(Filter $filter): iterable;
2424

25-
public function count(Filter $filter): int;
25+
public function count(Filter $filter, ?int $limit = null): int;
2626
}

src/contracts/Persistence/Filter/Location/Handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ interface Handler
2222
*/
2323
public function find(Filter $filter): iterable;
2424

25-
public function count(Filter $filter): int;
25+
public function count(Filter $filter, ?int $limit = null): int;
2626
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Contracts\Core\Persistence\Filter\Query;
10+
11+
use Doctrine\DBAL\Query\QueryBuilder;
12+
13+
interface CountQueryBuilder
14+
{
15+
public function wrap(QueryBuilder $queryBuilder, string $countableField, ?int $limit = null): QueryBuilder;
16+
}

src/contracts/Repository/ContentService.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,10 @@ public function find(Filter $filter, ?array $languages = null): ContentList;
529529
* If skipped, by default, unless SiteAccessAware layer has been disabled, languages set
530530
* for a SiteAccess in a current context will be used.
531531
*
532+
* @param int|null $limit If set, the count will be limited to first $limit items found.
533+
* In some cases it can significantly speed up a count operation for more complex filters.
534+
*
532535
* @phpstan-return int<0, max>
533536
*/
534-
public function count(Filter $filter, ?array $languages = null): int;
537+
public function count(Filter $filter, ?array $languages = null, ?int $limit = null): int;
535538
}

src/contracts/Repository/Decorator/ContentServiceDecorator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ public function find(Filter $filter, ?array $languages = null): ContentList
284284
return $this->innerService->find($filter, $languages);
285285
}
286286

287-
public function count(Filter $filter, ?array $languages = null): int
287+
public function count(Filter $filter, ?array $languages = null, ?int $limit = null): int
288288
{
289-
return $this->innerService->count($filter, $languages);
289+
return $this->innerService->count($filter, $languages, $limit);
290290
}
291291
}

src/contracts/Repository/Decorator/LocationServiceDecorator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ public function loadParentLocationsForDraftContent(
8181
return $this->innerService->loadParentLocationsForDraftContent($versionInfo, $prioritizedLanguages);
8282
}
8383

84-
public function getLocationChildCount(Location $location): int
84+
public function getLocationChildCount(Location $location, ?int $limit = null): int
8585
{
86-
return $this->innerService->getLocationChildCount($location);
86+
return $this->innerService->getLocationChildCount($location, $limit);
8787
}
8888

89-
public function getSubtreeSize(Location $location): int
89+
public function getSubtreeSize(Location $location, ?int $limit = null): int
9090
{
91-
return $this->innerService->getSubtreeSize($location);
91+
return $this->innerService->getSubtreeSize($location, $limit);
9292
}
9393

9494
public function createLocation(
@@ -159,8 +159,8 @@ public function find(Filter $filter, ?array $languages = null): LocationList
159159
return $this->innerService->find($filter, $languages);
160160
}
161161

162-
public function count(Filter $filter, ?array $languages = null): int
162+
public function count(Filter $filter, ?array $languages = null, ?int $limit = null): int
163163
{
164-
return $this->innerService->count($filter, $languages);
164+
return $this->innerService->count($filter, $languages, $limit);
165165
}
166166
}

src/contracts/Repository/LocationService.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,20 @@ public function loadParentLocationsForDraftContent(VersionInfo $versionInfo, ?ar
123123
* Returns the number of children which are readable by the current user of a location object.
124124
*
125125
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Location $location
126+
* @param int|null $limit If set, the count will be limited to first $limit items found.
126127
*
127128
* @return int
128129
*/
129-
public function getLocationChildCount(Location $location): int;
130+
public function getLocationChildCount(Location $location, ?int $limit = null): int;
130131

131132
/**
132133
* Return the subtree size of a given location.
133134
*
134135
* Warning! This method is not permission aware by design.
136+
*
137+
* @param int|null $limit
135138
*/
136-
public function getSubtreeSize(Location $location): int;
139+
public function getSubtreeSize(Location $location, ?int $limit = null): int;
137140

138141
/**
139142
* Creates the new $location in the content repository for the given content.
@@ -280,6 +283,8 @@ public function find(Filter $filter, ?array $languages = null): LocationList;
280283
* @param array<int, string>|null $languages a list of language codes to be added as additional constraints.
281284
* If skipped, by default, unless SiteAccessAware layer has been disabled, languages set
282285
* for a SiteAccess in a current context will be used.
286+
* @param int|null $limit If set, the count will be limited to first $limit items found.
287+
* In some cases it can significantly speed up a count operation for more complex filters.
283288
*/
284-
public function count(Filter $filter, ?array $languages = null): int;
289+
public function count(Filter $filter, ?array $languages = null, ?int $limit = null): int;
285290
}

src/lib/Persistence/Cache/LocationHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,14 @@ public function copySubtree($sourceId, $destinationParentId, $newOwnerId = null)
257257
return $this->persistenceHandler->locationHandler()->copySubtree($sourceId, $destinationParentId, $newOwnerId);
258258
}
259259

260-
public function getSubtreeSize(string $path): int
260+
public function getSubtreeSize(string $path, ?int $limit = null): int
261261
{
262262
$this->logger->logCall(__METHOD__, [
263263
'path' => $path,
264+
'limit' => $limit,
264265
]);
265266

266-
return $this->persistenceHandler->locationHandler()->getSubtreeSize($path);
267+
return $this->persistenceHandler->locationHandler()->getSubtreeSize($path, $limit);
267268
}
268269

269270
/**

0 commit comments

Comments
 (0)