From d0bbbc695028507d6fd66069940d6874cbc406bb Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Mon, 25 Jun 2012 10:51:53 -0700 Subject: [PATCH] [Service] Dogfooding Guzzle\Common\Batch in ResourceIteratorApplyBatched --- .../Resource/ResourceIteratorApplyBatched.php | 79 ++++++++----------- 1 file changed, 32 insertions(+), 47 deletions(-) diff --git a/src/Guzzle/Service/Resource/ResourceIteratorApplyBatched.php b/src/Guzzle/Service/Resource/ResourceIteratorApplyBatched.php index 72cd7ef3..40701bf5 100644 --- a/src/Guzzle/Service/Resource/ResourceIteratorApplyBatched.php +++ b/src/Guzzle/Service/Resource/ResourceIteratorApplyBatched.php @@ -3,6 +3,9 @@ namespace Guzzle\Service\Resource; use Guzzle\Common\AbstractHasDispatcher; +use Guzzle\Common\Batch\BatchBuilder; +use Guzzle\Common\Batch\BatchSizeDivisor; +use Guzzle\Common\Batch\BatchClosureTransfer; /** * Apply a callback to the contents of a {@see ResourceIteratorInterface} @@ -38,7 +41,9 @@ class ResourceIteratorApplyBatched extends AbstractHasDispatcher // About to send a batch of requests to the callback 'iterator_batch.before_batch', // Finished sending a batch of requests to the callback - 'iterator_batch.after_batch' + 'iterator_batch.after_batch', + // Created the batch object + 'iterator_batch.created_batch' ); } @@ -58,34 +63,38 @@ class ResourceIteratorApplyBatched extends AbstractHasDispatcher /** * Apply the callback to the contents of the resource iterator * - * Calling this method dispatches four events: - * - before_apply: Before adding a resource to a batch. Context is the resource - * - after_apply: After adding the resource to a batch. Context is the resource - * - before_batch: Before a batch request is sent if one is sent. Context is an array of resources - * - after_batch: After a batch request is sent if one is sent. Context is an array of resources + * @param int $perBatch The number of records to group per batch transfer * - * @param int $perBatch The number of records to batch before executing the callback - * - * @return int Returns the number of resources iterated + * @return int Returns the number of iterated resources */ - public function apply($perBatch = 20) + public function apply($perBatch = 50) { - if ($this->iterated == 0) { - $batched = array(); - foreach ($this->iterator as $resource) { - $batched[] = $resource; - if (count($batched) >= $perBatch) { - $this->applyBatch($batched); - $batched = array(); - } - $this->iterated++; - } + $this->iterated = $this->batches = $batches = 0; + $that = $this; + $it = $this->iterator; + $callback = $this->callback; - if (count($batched)) { - $this->applyBatch($batched); - } + $batch = BatchBuilder::factory() + ->createBatchesWith(new BatchSizeDivisor($perBatch)) + ->transferWith(new BatchClosureTransfer(function (array $batch) use ($that, $callback, &$batches, $it) { + $batches++; + $that->dispatch('iterator_batch.before_batch', array('iterator' => $it, 'batch' => $batch)); + call_user_func_array($callback, array($it, $batch)); + $that->dispatch('iterator_batch.after_batch', array('iterator' => $it, 'batch' => $batch)); + })) + ->autoFlushAt($perBatch) + ->build(); + + $this->dispatch('iterator_batch.created_batch', array('batch' => $batch)); + + foreach ($this->iterator as $resource) { + $this->iterated++; + $batch->add($resource); } + $batch->flush(); + $this->batches = $batches; + return $this->iterated; } @@ -108,28 +117,4 @@ class ResourceIteratorApplyBatched extends AbstractHasDispatcher { return $this->iterated; } - - /** - * Apply the callback to a collection of resources - * - * @param array $batch - */ - private function applyBatch(array $batch) - { - $this->batches++; - - $this->dispatch('iterator_batch.before_batch', array( - 'iterator' => $this, - 'batch' => $batch - )); - - call_user_func_array($this->callback, array( - $this->iterator, $batch - )); - - $this->dispatch('iterator_batch.after_batch', array( - 'iterator' => $this, - 'batch' => $batch - )); - } }