mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-28 00:38:46 +03:00
Updated VisitRepository::findUnlocatedVisits methods so that it paginates the amount of elements loaded in memory
This commit is contained in:
parent
08bd4f131c
commit
292937b962
7 changed files with 115 additions and 11 deletions
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Common\Paginator\Adapter;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Zend\Paginator\Adapter\AdapterInterface;
|
||||
|
||||
class PaginableQueryAdapter implements AdapterInterface
|
||||
{
|
||||
/** @var Query */
|
||||
private $query;
|
||||
/** @var int */
|
||||
private $totalItems;
|
||||
|
||||
public function __construct(Query $query, int $totalItems)
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->totalItems = $totalItems;
|
||||
}
|
||||
|
||||
public function getItems($offset, $itemCountPerPage): iterable
|
||||
{
|
||||
return $this->query
|
||||
->setMaxResults($itemCountPerPage)
|
||||
->setFirstResult($offset)
|
||||
->iterate();
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return $this->totalItems;
|
||||
}
|
||||
}
|
38
module/Common/src/Paginator/ImplicitLoopPaginator.php
Normal file
38
module/Common/src/Paginator/ImplicitLoopPaginator.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Common\Paginator;
|
||||
|
||||
use IteratorAggregate;
|
||||
use Zend\Paginator\Paginator;
|
||||
|
||||
class ImplicitLoopPaginator implements IteratorAggregate
|
||||
{
|
||||
/** @var Paginator */
|
||||
private $paginator;
|
||||
/** @var callable */
|
||||
private $valueParser;
|
||||
|
||||
public function __construct(Paginator $paginator, callable $valueParser = null)
|
||||
{
|
||||
$this->paginator = $paginator;
|
||||
$this->valueParser = $valueParser ?? function ($value) {
|
||||
return $value;
|
||||
};
|
||||
}
|
||||
|
||||
public function getIterator(): iterable
|
||||
{
|
||||
$totalPages = $this->paginator->count();
|
||||
$processedPages = 0;
|
||||
|
||||
do {
|
||||
$processedPages++;
|
||||
$this->paginator->setCurrentPageNumber($processedPages);
|
||||
|
||||
foreach ($this->paginator as $key => $value) {
|
||||
yield $key => ($this->valueParser)($value);
|
||||
}
|
||||
} while ($processedPages < $totalPages);
|
||||
}
|
||||
}
|
|
@ -5,17 +5,32 @@ namespace Shlinkio\Shlink\Core\Repository;
|
|||
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableQueryAdapter;
|
||||
use Shlinkio\Shlink\Common\Paginator\ImplicitLoopPaginator;
|
||||
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||
use Zend\Paginator\Paginator;
|
||||
use function array_shift;
|
||||
|
||||
class VisitRepository extends EntityRepository implements VisitRepositoryInterface
|
||||
{
|
||||
public function findUnlocatedVisits(): iterable
|
||||
/**
|
||||
* @return iterable|Visit[]
|
||||
*/
|
||||
public function findUnlocatedVisits(int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable
|
||||
{
|
||||
$dql = 'SELECT v FROM Shlinkio\Shlink\Core\Entity\Visit AS v WHERE v.visitLocation IS NULL';
|
||||
$count = $this->count(['visitLocation' => null]);
|
||||
$dql = <<<DQL
|
||||
SELECT v FROM Shlinkio\Shlink\Core\Entity\Visit AS v WHERE v.visitLocation IS NULL
|
||||
DQL;
|
||||
$query = $this->getEntityManager()->createQuery($dql);
|
||||
|
||||
return $query->iterate();
|
||||
$paginator = new Paginator(new PaginableQueryAdapter($query, $count));
|
||||
$paginator->setItemCountPerPage($blockSize);
|
||||
|
||||
return new ImplicitLoopPaginator($paginator, function (array $value) {
|
||||
return array_shift($value);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,7 +9,12 @@ use Shlinkio\Shlink\Core\Entity\Visit;
|
|||
|
||||
interface VisitRepositoryInterface extends ObjectRepository
|
||||
{
|
||||
public function findUnlocatedVisits(): iterable;
|
||||
public const DEFAULT_BLOCK_SIZE = 10000;
|
||||
|
||||
/**
|
||||
* @return iterable|Visit[]
|
||||
*/
|
||||
public function findUnlocatedVisits(int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable;
|
||||
|
||||
/**
|
||||
* @return Visit[]
|
||||
|
|
|
@ -26,7 +26,7 @@ class VisitService implements VisitServiceInterface
|
|||
$repo = $this->em->getRepository(Visit::class);
|
||||
$results = $repo->findUnlocatedVisits();
|
||||
|
||||
foreach ($results as [$visit]) {
|
||||
foreach ($results as $visit) {
|
||||
try {
|
||||
/** @var Location $location */
|
||||
$location = $geolocateVisit($visit);
|
||||
|
|
|
@ -12,6 +12,8 @@ use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
|||
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Repository\VisitRepository;
|
||||
use ShlinkioTest\Shlink\Common\DbTest\DatabaseTestCase;
|
||||
use function Functional\map;
|
||||
use function range;
|
||||
use function sprintf;
|
||||
|
||||
class VisitRepositoryTest extends DatabaseTestCase
|
||||
|
@ -30,8 +32,11 @@ class VisitRepositoryTest extends DatabaseTestCase
|
|||
$this->repo = $this->getEntityManager()->getRepository(Visit::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function findUnlocatedVisitsReturnsProperVisits(): void
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideBlockSize
|
||||
*/
|
||||
public function findUnlocatedVisitsReturnsProperVisits(int $blockSize): void
|
||||
{
|
||||
$shortUrl = new ShortUrl('');
|
||||
$this->getEntityManager()->persist($shortUrl);
|
||||
|
@ -50,7 +55,7 @@ class VisitRepositoryTest extends DatabaseTestCase
|
|||
$this->getEntityManager()->flush();
|
||||
|
||||
$resultsCount = 0;
|
||||
$results = $this->repo->findUnlocatedVisits();
|
||||
$results = $this->repo->findUnlocatedVisits($blockSize);
|
||||
foreach ($results as $value) {
|
||||
$resultsCount++;
|
||||
}
|
||||
|
@ -58,6 +63,13 @@ class VisitRepositoryTest extends DatabaseTestCase
|
|||
$this->assertEquals(3, $resultsCount);
|
||||
}
|
||||
|
||||
public function provideBlockSize(): iterable
|
||||
{
|
||||
return map(range(1, 5), function (int $value) {
|
||||
return [$value];
|
||||
});
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function findVisitsByShortCodeReturnsProperData(): void
|
||||
{
|
||||
|
|
|
@ -36,8 +36,8 @@ class VisitServiceTest extends TestCase
|
|||
public function locateVisitsIteratesAndLocatesUnlocatedVisits(): void
|
||||
{
|
||||
$unlocatedVisits = [
|
||||
[new Visit(new ShortUrl('foo'), Visitor::emptyInstance())],
|
||||
[new Visit(new ShortUrl('bar'), Visitor::emptyInstance())],
|
||||
new Visit(new ShortUrl('foo'), Visitor::emptyInstance()),
|
||||
new Visit(new ShortUrl('bar'), Visitor::emptyInstance()),
|
||||
];
|
||||
|
||||
$repo = $this->prophesize(VisitRepository::class);
|
||||
|
@ -71,7 +71,7 @@ class VisitServiceTest extends TestCase
|
|||
public function visitsWhichCannotBeLocatedAreIgnored()
|
||||
{
|
||||
$unlocatedVisits = [
|
||||
[new Visit(new ShortUrl('foo'), Visitor::emptyInstance())],
|
||||
new Visit(new ShortUrl('foo'), Visitor::emptyInstance()),
|
||||
];
|
||||
|
||||
$repo = $this->prophesize(VisitRepository::class);
|
||||
|
|
Loading…
Reference in a new issue