From 091ea974eb483abddedb3aa87475c751766e6fa8 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 23 Feb 2019 07:29:07 +0100 Subject: [PATCH] Simplified implementation iterating unlocated visits --- .../Adapter/PaginableQueryAdapter.php | 34 ---------- .../src/Paginator/ImplicitLoopPaginator.php | 38 ----------- .../Paginator/ImplicitLoopPaginatorTest.php | 65 ------------------- .../Core/src/Repository/VisitRepository.php | 23 ++++--- 4 files changed, 13 insertions(+), 147 deletions(-) delete mode 100644 module/Common/src/Paginator/Adapter/PaginableQueryAdapter.php delete mode 100644 module/Common/src/Paginator/ImplicitLoopPaginator.php delete mode 100644 module/Common/test/Paginator/ImplicitLoopPaginatorTest.php diff --git a/module/Common/src/Paginator/Adapter/PaginableQueryAdapter.php b/module/Common/src/Paginator/Adapter/PaginableQueryAdapter.php deleted file mode 100644 index d6de1ada..00000000 --- a/module/Common/src/Paginator/Adapter/PaginableQueryAdapter.php +++ /dev/null @@ -1,34 +0,0 @@ -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; - } -} diff --git a/module/Common/src/Paginator/ImplicitLoopPaginator.php b/module/Common/src/Paginator/ImplicitLoopPaginator.php deleted file mode 100644 index 9019f248..00000000 --- a/module/Common/src/Paginator/ImplicitLoopPaginator.php +++ /dev/null @@ -1,38 +0,0 @@ -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); - } -} diff --git a/module/Common/test/Paginator/ImplicitLoopPaginatorTest.php b/module/Common/test/Paginator/ImplicitLoopPaginatorTest.php deleted file mode 100644 index 0e494afb..00000000 --- a/module/Common/test/Paginator/ImplicitLoopPaginatorTest.php +++ /dev/null @@ -1,65 +0,0 @@ -paginator = new Paginator(new ArrayAdapter(range(1, self::TOTAL_ITEMS))); - } - - /** - * @test - * @dataProvider provideItemsPerPage - */ - public function allElementsAreIteratedRegardlessThePageSize(int $itemsPerPage): void - { - $this->paginator->setItemCountPerPage($itemsPerPage); - $implicitLoopPaginator = new ImplicitLoopPaginator($this->paginator); - - $iteratedItems = 0; - foreach ($implicitLoopPaginator as $item) { - $iteratedItems++; - } - - $this->assertEquals(self::TOTAL_ITEMS, $iteratedItems); - } - - public function provideItemsPerPage(): iterable - { - return map(range(1, 20), function (int $i) { - return [$i]; - }); - } - - /** @test */ - public function valuesWrappedInPaginatorAreProperlyParsed(): void - { - $valueParser = function (int $item) { - return $item * 3; - }; - $this->paginator->setItemCountPerPage(5); - $implicitLoopPaginator = new ImplicitLoopPaginator($this->paginator, $valueParser); - - $items = []; - foreach ($implicitLoopPaginator as $item) { - $items[] = $item; - } - - $this->assertEquals([3, 6, 9, 12, 15, 18, 21, 24, 27, 30], $items); - } -} diff --git a/module/Core/src/Repository/VisitRepository.php b/module/Core/src/Repository/VisitRepository.php index dfa5498f..9c65877c 100644 --- a/module/Core/src/Repository/VisitRepository.php +++ b/module/Core/src/Repository/VisitRepository.php @@ -5,12 +5,8 @@ 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 { @@ -19,18 +15,25 @@ class VisitRepository extends EntityRepository implements VisitRepositoryInterfa */ public function findUnlocatedVisits(int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable { - $count = $this->count(['visitLocation' => null]); $dql = <<getEntityManager()->createQuery($dql); + $remainingVisitsToProcess = $this->count(['visitLocation' => null]); + $offset = 0; - $paginator = new Paginator(new PaginableQueryAdapter($query, $count)); - $paginator->setItemCountPerPage($blockSize); + while ($remainingVisitsToProcess > 0) { + $iterator = $query->setMaxResults($blockSize) + ->setFirstResult($offset) + ->iterate(); - return new ImplicitLoopPaginator($paginator, function (array $value) { - return array_shift($value); - }); + foreach ($iterator as $key => [$value]) { + yield $key => $value; + } + + $remainingVisitsToProcess -= $blockSize; + $offset += $blockSize; + } } /**