Browse Source

Add SimplePaginator which skips count query.

This is useful for paginating repositories with very large number
of records where count queries can be quite expensive.
ADmad 6 years ago
parent
commit
409389f710

+ 16 - 13
src/Datasource/Paginator.php

@@ -211,7 +211,7 @@ class Paginator implements PaginatorInterface
      *
      * @param \Cake\Datasource\QueryInterface $query Query instance.
      * @param array $data Pagination data.
-     * @return int
+     * @return int|null
      */
     protected function getCount(QueryInterface $query, array $data)
     {
@@ -252,11 +252,9 @@ class Paginator implements PaginatorInterface
     {
         $defaults = $data['defaults'];
         $count = $data['count'];
-        $page = $data['options']['page'];
+        $numResults = $data['numResults'];
+        $requestedPage = $page = $data['options']['page'];
         $limit = $data['options']['limit'];
-        $pageCount = max((int)ceil($count / $limit), 1);
-        $requestedPage = $page;
-        $page = min($page, $pageCount);
 
         $order = (array)$data['options']['order'];
         $sortDefault = $directionDefault = false;
@@ -265,26 +263,31 @@ class Paginator implements PaginatorInterface
             $directionDefault = current($defaults['order']);
         }
 
-        $start = 0;
-        if ($count >= 1) {
-            $start = (($page - 1) * $limit) + 1;
+        $pageCount = 0;
+        if ($count !== null) {
+            $pageCount = max((int)ceil($count / $limit), 1);
+            $page = min($page, $pageCount);
+        } elseif ($numResults === 0 && $requestedPage > 1) {
+            $page = 1;
         }
-        $end = $start + $limit - 1;
-        if ($count < $end) {
-            $end = $count;
+
+        $start = $end = 0;
+        if ($numResults > 0) {
+            $start = (($page - 1) * $limit) + 1;
+            $end = $start + $numResults - 1;
         }
 
         $paging = [
             'finder' => $data['finder'],
             'requestedPage' => $requestedPage,
             'page' => $page,
-            'current' => $data['numResults'],
+            'current' => $numResults,
             'count' => $count,
             'perPage' => $limit,
             'start' => $start,
             'end' => $end,
             'prevPage' => $page > 1,
-            'nextPage' => $count > ($page * $limit),
+            'nextPage' => $count === null ? true : ($count > ($page * $limit)),
             'pageCount' => $pageCount,
             'sort' => $data['options']['sort'],
             'direction' => isset($data['options']['sort']) ? current($order) : null,

+ 35 - 0
src/Datasource/SimplePaginator.php

@@ -0,0 +1,35 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.9.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Datasource;
+
+use Cake\Datasource\Exception\PageOutOfBoundsException;
+
+/**
+ * Simplified paginator which avoids query to get total count of records.
+ */
+class SimplePaginator extends Paginator
+{
+    /**
+     * Simple pagination does not perform any count query, so this method returns `null`.
+     *
+     * @param \Cake\Datasource\QueryInterface $query Query instance.
+     * @param array $data Pagination data.
+     * @return int|null
+     */
+    protected function getCount(QueryInterface $query, array $data)
+    {
+        return null;
+    }
+}

+ 7 - 12
tests/TestCase/Datasource/PaginatorTest.php

@@ -171,25 +171,23 @@ class PaginatorTest extends TestCase
     {
         $settings = [
             'PaginatorPosts' => [
-                'finder' => 'popular',
+                'finder' => 'published',
                 'fields' => ['id', 'title'],
                 'maxLimit' => 10,
             ]
         ];
 
-        $table = $this->_getMockPosts(['findPopular']);
-        $query = $this->_getMockFindQuery();
-
-        $table->expects($this->any())
-            ->method('findPopular')
-            ->will($this->returnValue($query));
+        $this->loadFixtures('Posts');
+        $table = $this->getTableLocator()->get('PaginatorPosts');
+        $table->updateAll(['published' => 'N'], ['id' => 2]);
 
         $this->Paginator->paginate($table, [], $settings);
         $pagingParams = $this->Paginator->getPagingParams();
-        $this->assertSame('popular', $pagingParams['PaginatorPosts']['finder']);
+        $this->assertSame('published', $pagingParams['PaginatorPosts']['finder']);
 
         $this->assertSame(1, $pagingParams['PaginatorPosts']['start']);
         $this->assertSame(2, $pagingParams['PaginatorPosts']['end']);
+        $this->assertFalse($pagingParams['PaginatorPosts']['nextPage']);
     }
 
     /**
@@ -1494,6 +1492,7 @@ class PaginatorTest extends TestCase
         $results = $this->getMockBuilder('Cake\ORM\ResultSet')
             ->disableOriginalConstructor()
             ->getMock();
+
         $query->expects($this->any())
             ->method('count')
             ->will($this->returnValue(2));
@@ -1502,10 +1501,6 @@ class PaginatorTest extends TestCase
             ->method('all')
             ->will($this->returnValue($results));
 
-        $query->expects($this->any())
-            ->method('count')
-            ->will($this->returnValue(2));
-
         if ($table) {
             $query->repository($table);
         }

+ 160 - 0
tests/TestCase/Datasource/SimplePaginatorTest.php

@@ -0,0 +1,160 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.5.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\Datasource;
+
+use Cake\Core\Configure;
+use Cake\Datasource\SimplePaginator;
+use Cake\ORM\Entity;
+
+class SimplePaginatorTest extends PaginatorTest
+{
+    public function setUp()
+    {
+        parent::setUp();
+
+        Configure::write('App.namespace', 'TestApp');
+
+        $this->Paginator = new SimplePaginator();
+
+        $this->Post = $this->getMockRepository();
+    }
+
+    /**
+     * test paginate() and custom find, to make sure the correct count is returned.
+     *
+     * @return void
+     */
+    public function testPaginateCustomFind()
+    {
+        $this->loadFixtures('Posts');
+        $titleExtractor = function ($result) {
+            $ids = [];
+            foreach ($result as $record) {
+                $ids[] = $record->title;
+            }
+
+            return $ids;
+        };
+
+        $table = $this->getTableLocator()->get('PaginatorPosts');
+        $data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N'];
+        $result = $table->save(new Entity($data));
+        $this->assertNotEmpty($result);
+
+        $result = $this->Paginator->paginate($table);
+        $this->assertCount(4, $result, '4 rows should come back');
+        $this->assertEquals(['First Post', 'Second Post', 'Third Post', 'Fourth Post'], $titleExtractor($result));
+
+        $pagingParams = $this->Paginator->getPagingParams();
+        $this->assertEquals(4, $pagingParams['PaginatorPosts']['current']);
+        $this->assertNull($pagingParams['PaginatorPosts']['count']);
+
+        $settings = ['finder' => 'published'];
+        $result = $this->Paginator->paginate($table, [], $settings);
+        $this->assertCount(3, $result, '3 rows should come back');
+        $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result));
+
+        $pagingParams = $this->Paginator->getPagingParams();
+        $this->assertEquals(3, $pagingParams['PaginatorPosts']['current']);
+        $this->assertNull($pagingParams['PaginatorPosts']['count']);
+
+        $settings = ['finder' => 'published', 'limit' => 2, 'page' => 2];
+        $result = $this->Paginator->paginate($table, [], $settings);
+        $this->assertCount(1, $result, '1 rows should come back');
+        $this->assertEquals(['Third Post'], $titleExtractor($result));
+
+        $pagingParams = $this->Paginator->getPagingParams();
+        $this->assertEquals(1, $pagingParams['PaginatorPosts']['current']);
+        $this->assertNull($pagingParams['PaginatorPosts']['count']);
+        $this->assertSame(0, $pagingParams['PaginatorPosts']['pageCount']);
+
+        $settings = ['finder' => 'published', 'limit' => 2];
+        $result = $this->Paginator->paginate($table, [], $settings);
+        $this->assertCount(2, $result, '2 rows should come back');
+        $this->assertEquals(['First Post', 'Second Post'], $titleExtractor($result));
+
+        $pagingParams = $this->Paginator->getPagingParams();
+        $this->assertEquals(2, $pagingParams['PaginatorPosts']['current']);
+        $this->assertNull($pagingParams['PaginatorPosts']['count']);
+        $this->assertEquals(0, $pagingParams['PaginatorPosts']['pageCount']);
+        $this->assertTrue($pagingParams['PaginatorPosts']['nextPage']);
+        $this->assertFalse($pagingParams['PaginatorPosts']['prevPage']);
+        $this->assertEquals(2, $pagingParams['PaginatorPosts']['perPage']);
+        $this->assertNull($pagingParams['PaginatorPosts']['limit']);
+    }
+
+    /**
+     * test paginate() and custom find with fields array, to make sure the correct count is returned.
+     *
+     * @return void
+     */
+    public function testPaginateCustomFindFieldsArray()
+    {
+        $this->loadFixtures('Posts');
+        $table = $this->getTableLocator()->get('PaginatorPosts');
+        $data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'];
+        $table->save(new Entity($data));
+
+        $settings = [
+            'finder' => 'list',
+            'conditions' => ['PaginatorPosts.published' => 'Y'],
+            'limit' => 2
+        ];
+        $results = $this->Paginator->paginate($table, [], $settings);
+
+        $result = $results->toArray();
+        $expected = [
+            1 => 'First Post',
+            2 => 'Second Post',
+        ];
+        $this->assertEquals($expected, $result);
+
+        $result = $this->Paginator->getPagingParams()['PaginatorPosts'];
+        $this->assertEquals(2, $result['current']);
+        $this->assertNull($result['count']);
+        $this->assertEquals(0, $result['pageCount']);
+        $this->assertTrue($result['nextPage']);
+        $this->assertFalse($result['prevPage']);
+    }
+
+    /**
+     * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
+     *
+     * @return void
+     */
+    public function testPaginateCustomFinder()
+    {
+        $settings = [
+            'PaginatorPosts' => [
+                'finder' => 'published',
+                'fields' => ['id', 'title'],
+                'maxLimit' => 10,
+            ]
+        ];
+
+        $this->loadFixtures('Posts');
+        $table = $this->getTableLocator()->get('PaginatorPosts');
+        $table->updateAll(['published' => 'N'], ['id' => 2]);
+
+        $this->Paginator->paginate($table, [], $settings);
+        $pagingParams = $this->Paginator->getPagingParams();
+        $this->assertSame('published', $pagingParams['PaginatorPosts']['finder']);
+
+        $this->assertSame(1, $pagingParams['PaginatorPosts']['start']);
+        $this->assertSame(2, $pagingParams['PaginatorPosts']['end']);
+        // nextPage will be always true for SimplePaginator
+        $this->assertTrue($pagingParams['PaginatorPosts']['nextPage']);
+    }
+}