Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query::chunked does not loop over the last chunk #196

Merged
merged 1 commit into from
Feb 3, 2022

Conversation

laurent-rizer
Copy link
Contributor

When using Query::chunked over a large number of emails the last chunk is not being populate and passed to the callback method.

The while ($this->limit * $this->page <= $available_messages_count); doesn't properly works

With $available_messages_count = 15 and $this->limit = 10;, at the end of first iteration, when testing while conditon: $this->page = 2 so $this->limit * $this->page = 20 and 20 > $available_messages_count and the loop stops.

Simplified original version

        $callback = function($messages, $page) {
            var_dump($messages);
        };

        $available_messages = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
        $available_messages_count = count($available_messages);
        $limit = 10;
        $page = 1;
        do {
            $messages = array_slice($available_messages, ($page - 1) * $limit, $limit, true);
            $callback($messages, $page);
            $page++;
        } while ($limit * $page <= $available_messages_count);
        
/*
Does only one loop over the first  messages and stops
array(10) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
  [6]=>
  int(7)
  [7]=>
  int(8)
  [8]=>
  int(9)
  [9]=>
  int(10)
}
*/

Proposed solution:

        $callback = function($messages, $page) {
            var_dump($messages);
        };

        $available_messages = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
        $available_messages_count = count($available_messages);
        $limit = 10;
        $page = 1;
        $handled_messages_count = 0;
        do {
            $messages = array_slice($available_messages, ($page - 1) * $limit, $limit, true);
            $handled_messages_count += count($messages); // Can be equal OR less than $limit (in the last iteration)
            $callback($messages, $page);
            $page++;
        } while ($handled_messages_count < $available_messages_count); // No or equal here cause it would endup in an infinit loop
        
/*
array(10) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
  [6]=>
  int(7)
  [7]=>
  int(8)
  [8]=>
  int(9)
  [9]=>
  int(10)
}
array(5) {
  [10]=>
  int(11)
  [11]=>
  int(12)
  [12]=>
  int(13)
  [13]=>
  int(14)
  [14]=>
  int(15)
}
*/

@Webklex
Copy link
Owner

Webklex commented Feb 3, 2022

Hi @laurent-rizer ,
many thanks for your pull request! I like your suggested solution and I'm happy to merge it :)

Best regards,

@Webklex Webklex merged commit 77c12b8 into Webklex:master Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants