public function testFlushesWhenSizeMeetsThreshold()
 {
     $t = $this->getMock('Guzzle\\Batch\\BatchTransferInterface', array('transfer'));
     $d = $this->getMock('Guzzle\\Batch\\BatchDivisorInterface', array('createBatches'));
     $batch = new Batch($t, $d);
     $queue = $this->readAttribute($batch, 'queue');
     $d->expects($this->exactly(2))->method('createBatches')->will($this->returnCallback(function () use($queue) {
         $items = array();
         foreach ($queue as $item) {
             $items[] = $item;
         }
         return array($items);
     }));
     $t->expects($this->exactly(2))->method('transfer');
     $flush = new FlushingBatch($batch, 3);
     $this->assertEquals(3, $flush->getThreshold());
     $flush->setThreshold(2);
     $flush->add('foo')->add('baz')->add('bar')->add('bee')->add('boo');
     $this->assertEquals(1, count($flush));
 }
 /**
  * Flush creation and/or deletion batch
  * @return [type] [description]
  */
 public function flush()
 {
     if (!is_null($this->putBatch)) {
         $this->putBatch->flush();
         $this->putBatch = null;
     }
     if (!is_null($this->deleteBatch)) {
         $this->deleteBatch->flush();
         $this->deleteBatch = null;
     }
 }
示例#3
0
 /**
  * Clear the bucket
  *
  * @return int Returns the number of deleted keys
  * @throws ExceptionCollection
  */
 public function clear()
 {
     $that = $this;
     $batch = DeleteObjectsBatch::factory($this->client, $this->bucket, $this->mfa);
     $batch = new NotifyingBatch($batch, function ($items) use($that) {
         $that->dispatch(ClearBucket::AFTER_DELETE, array('keys' => $items));
     });
     $batch = new FlushingBatch(new ExceptionBufferingBatch($batch), 1000);
     // Let any listeners know that the bucket is about to be cleared
     $this->dispatch(self::BEFORE_CLEAR, array('iterator' => $this->getIterator(), 'batch' => $batch, 'mfa' => $this->mfa));
     $deleted = 0;
     foreach ($this->getIterator() as $object) {
         if (isset($object['VersionId'])) {
             $versionId = $object['VersionId'] == 'null' ? null : $object['VersionId'];
         } else {
             $versionId = null;
         }
         $batch->addKey($object['Key'], $versionId);
         $deleted++;
     }
     $batch->flush();
     // If any errors were encountered, then throw an ExceptionCollection
     if (count($batch->getExceptions())) {
         $e = new ExceptionCollection();
         foreach ($batch->getExceptions() as $exception) {
             $e->add($exception->getPrevious());
         }
         throw $e;
     }
     // Let any listeners know that the bucket was cleared
     $this->dispatch(self::AFTER_CLEAR, array('deleted' => $deleted));
     return $deleted;
 }