Commit be7b0bcc authored by Bram Gerritsen's avatar Bram Gerritsen

Implemented an event which is fired on each document added to the BufferAdd...

Implemented an event which is fired on each document added to the BufferAdd plugin so you can provide some more status feedback while importing.
parent 10c455fe
......@@ -47,6 +47,7 @@ use Solarium\Plugin\BufferedAdd\Event\PreFlush as PreFlushEvent;
use Solarium\Plugin\BufferedAdd\Event\PostFlush as PostFlushEvent;
use Solarium\Plugin\BufferedAdd\Event\PreCommit as PreCommitEvent;
use Solarium\Plugin\BufferedAdd\Event\PostCommit as PostCommitEvent;
use Solarium\Plugin\BufferedAdd\Event\AddDocument as AddDocumentEvent;
/**
* Buffered add plugin
......@@ -138,6 +139,10 @@ class BufferedAdd extends Plugin
public function addDocument($document)
{
$this->buffer[] = $document;
$event = new AddDocumentEvent($document);
$this->client->getEventDispatcher()->dispatch(Events::ADD_DOCUMENT, $event);
if (count($this->buffer) == $this->options['buffersize']) {
$this->flush();
}
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*/
/**
* @namespace
*/
namespace Solarium\Plugin\BufferedAdd\Event;
use Symfony\Component\EventDispatcher\Event;
use Solarium\QueryType\Update\Result;
use Solarium\QueryType\Select\Result\DocumentInterface;
/**
* AddDocument event, see Events for details
*/
class AddDocument extends Event
{
/**
* @var DocumentInterface
*/
protected $document;
/**
* Event constructor
*
* @param DocumentInterface $document
*/
public function __construct($document)
{
$this->document = $document;
}
/**
* Get the result for this event
*
* @return DocumentInterface
*/
public function getDocument()
{
return $this->document;
}
}
......@@ -79,4 +79,13 @@ class Events
* @var string
*/
const POST_COMMIT = 'solarium.bufferedAdd.postCommit';
/**
* This event is called when a new document is added
*
* The event listener receives the Document
*
* @var string
*/
const ADD_DOCUMENT = 'solarium.bufferedAdd.addDocument';
}
......@@ -30,9 +30,11 @@
*/
namespace Solarium\Tests\Plugin\BufferedAdd;
use Solarium\QueryType\Update\Query\Document;
use Solarium\QueryType\Update\Query\Document\Document;
use Solarium\Plugin\BufferedAdd\Event\AddDocument;
use Solarium\Plugin\BufferedAdd\BufferedAdd;
use Solarium\Core\Client\Client;
use Solarium\Plugin\BufferedAdd\Event\Events;
class BufferedAddTest extends \PHPUnit_Framework_TestCase
{
......@@ -93,9 +95,12 @@ class BufferedAddTest extends \PHPUnit_Framework_TestCase
public function testAddDocumentAutoFlush()
{
$observer = $this->getMock('Solarium\Plugin\BufferedAdd\BufferedAdd', array('flush'));
$observer->expects($this->once())->method('flush');
$observer->setBufferSize(1);
$mockUpdate = $this->getMock('Solarium\QueryType\Update\Query\Query', array('addDocuments'));
$mockUpdate->expects($this->exactly(2))->method('addDocuments');
$mockClient = $this->getMock('Solarium\Core\Client\Client', array('createUpdate', 'update', 'triggerEvent'));
$mockClient->expects($this->exactly(3))->method('createUpdate')->will($this->returnValue($mockUpdate));
$mockClient->expects($this->exactly(2))->method('update')->will($this->returnValue('dummyResult'));
$doc1 = new Document();
$doc1->id = '123';
......@@ -107,7 +112,10 @@ class BufferedAddTest extends \PHPUnit_Framework_TestCase
$docs = array($doc1, $doc2);
$observer->addDocuments($docs);
$plugin = new BufferedAdd();
$plugin->initPlugin($mockClient, array());
$plugin->setBufferSize(1);
$plugin->addDocuments($docs);
}
public function testClear()
......@@ -166,4 +174,25 @@ class BufferedAddTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('dummyResult', $plugin->commit(true, false, true, false));
}
public function testAddDocumentEventIsTriggered()
{
$data = array('id' => '123', 'name' => 'test');
$doc = new Document($data);
$expectedEvent = new AddDocument($doc);
$mockEventDispatcher = $this->getMock('Solarium\QueryType\Update\Query\Query', array('dispatch'));
$mockEventDispatcher
->expects($this->once())
->method('dispatch')
->with($this->equalTo(Events::ADD_DOCUMENT), $this->equalTo($expectedEvent));
$mockClient = $this->getMock('Solarium\Core\Client\Client', array('getEventDispatcher'));
$mockClient->expects($this->once())->method('getEventDispatcher')->will($this->returnValue($mockEventDispatcher));
$plugin = new BufferedAdd();
$plugin->initPlugin($mockClient, array());
$plugin->addDocument($doc);
}
}
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*/
namespace Solarium\Tests\Plugin\BufferedAdd\Event;
use Solarium\Plugin\BufferedAdd\Event\AddDocument;
use Solarium\QueryType\Update\Query\Document\Document;
class AddDocumentTest extends \PHPUnit_Framework_TestCase
{
public function testConstructorAndGetters()
{
$document = new Document();
$event = new AddDocument($document);
$this->assertEquals($document, $event->getDocument());
return $event;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment