Updated VisitRepository::findUnlocatedVisits methods so that it paginates the amount of elements loaded in memory

This commit is contained in:
Alejandro Celaya 2019-02-22 19:31:03 +01:00
parent 08bd4f131c
commit 292937b962
7 changed files with 115 additions and 11 deletions

View file

@ -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;
}
}

View 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);
}
}

View file

@ -5,17 +5,32 @@ namespace Shlinkio\Shlink\Core\Repository;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder; 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\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\Visit;
use Zend\Paginator\Paginator;
use function array_shift;
class VisitRepository extends EntityRepository implements VisitRepositoryInterface 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); $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);
});
} }
/** /**

View file

@ -9,7 +9,12 @@ use Shlinkio\Shlink\Core\Entity\Visit;
interface VisitRepositoryInterface extends ObjectRepository 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[] * @return Visit[]

View file

@ -26,7 +26,7 @@ class VisitService implements VisitServiceInterface
$repo = $this->em->getRepository(Visit::class); $repo = $this->em->getRepository(Visit::class);
$results = $repo->findUnlocatedVisits(); $results = $repo->findUnlocatedVisits();
foreach ($results as [$visit]) { foreach ($results as $visit) {
try { try {
/** @var Location $location */ /** @var Location $location */
$location = $geolocateVisit($visit); $location = $geolocateVisit($visit);

View file

@ -12,6 +12,8 @@ use Shlinkio\Shlink\Core\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Repository\VisitRepository; use Shlinkio\Shlink\Core\Repository\VisitRepository;
use ShlinkioTest\Shlink\Common\DbTest\DatabaseTestCase; use ShlinkioTest\Shlink\Common\DbTest\DatabaseTestCase;
use function Functional\map;
use function range;
use function sprintf; use function sprintf;
class VisitRepositoryTest extends DatabaseTestCase class VisitRepositoryTest extends DatabaseTestCase
@ -30,8 +32,11 @@ class VisitRepositoryTest extends DatabaseTestCase
$this->repo = $this->getEntityManager()->getRepository(Visit::class); $this->repo = $this->getEntityManager()->getRepository(Visit::class);
} }
/** @test */ /**
public function findUnlocatedVisitsReturnsProperVisits(): void * @test
* @dataProvider provideBlockSize
*/
public function findUnlocatedVisitsReturnsProperVisits(int $blockSize): void
{ {
$shortUrl = new ShortUrl(''); $shortUrl = new ShortUrl('');
$this->getEntityManager()->persist($shortUrl); $this->getEntityManager()->persist($shortUrl);
@ -50,7 +55,7 @@ class VisitRepositoryTest extends DatabaseTestCase
$this->getEntityManager()->flush(); $this->getEntityManager()->flush();
$resultsCount = 0; $resultsCount = 0;
$results = $this->repo->findUnlocatedVisits(); $results = $this->repo->findUnlocatedVisits($blockSize);
foreach ($results as $value) { foreach ($results as $value) {
$resultsCount++; $resultsCount++;
} }
@ -58,6 +63,13 @@ class VisitRepositoryTest extends DatabaseTestCase
$this->assertEquals(3, $resultsCount); $this->assertEquals(3, $resultsCount);
} }
public function provideBlockSize(): iterable
{
return map(range(1, 5), function (int $value) {
return [$value];
});
}
/** @test */ /** @test */
public function findVisitsByShortCodeReturnsProperData(): void public function findVisitsByShortCodeReturnsProperData(): void
{ {

View file

@ -36,8 +36,8 @@ class VisitServiceTest extends TestCase
public function locateVisitsIteratesAndLocatesUnlocatedVisits(): void public function locateVisitsIteratesAndLocatesUnlocatedVisits(): void
{ {
$unlocatedVisits = [ $unlocatedVisits = [
[new Visit(new ShortUrl('foo'), Visitor::emptyInstance())], new Visit(new ShortUrl('foo'), Visitor::emptyInstance()),
[new Visit(new ShortUrl('bar'), Visitor::emptyInstance())], new Visit(new ShortUrl('bar'), Visitor::emptyInstance()),
]; ];
$repo = $this->prophesize(VisitRepository::class); $repo = $this->prophesize(VisitRepository::class);
@ -71,7 +71,7 @@ class VisitServiceTest extends TestCase
public function visitsWhichCannotBeLocatedAreIgnored() public function visitsWhichCannotBeLocatedAreIgnored()
{ {
$unlocatedVisits = [ $unlocatedVisits = [
[new Visit(new ShortUrl('foo'), Visitor::emptyInstance())], new Visit(new ShortUrl('foo'), Visitor::emptyInstance()),
]; ];
$repo = $this->prophesize(VisitRepository::class); $repo = $this->prophesize(VisitRepository::class);