Commit 4509519b authored by Bas de Nooijer's avatar Bas de Nooijer

Parallelexecution plugin now uses endpoints.

Also updated unittest
parent d56401c3
...@@ -78,21 +78,31 @@ class ParallelExecution extends Plugin ...@@ -78,21 +78,31 @@ class ParallelExecution extends Plugin
*/ */
protected $queries = array(); protected $queries = array();
/**
* Set curl adapter (the only type that supports parallelexecution)
*/
protected function initPluginType()
{
$this->client->setAdapter('Solarium\Client\Adapter\Curl');
}
/** /**
* Add a query to execute * Add a query to execute
* *
* @param string $key * @param string $key
* @param Query $query * @param Query $query
* @param null|Client $client * @param null|string|Endpoint $endpoint
* @return self Provides fluent interface * @return self Provides fluent interface
*/ */
public function addQuery($key, $query, $client = null) public function addQuery($key, $query, $endpoint = null)
{ {
if($client == null) $client = $this->client; if (is_object($endpoint)) $endpoint = $endpoint->getKey();
if($endpoint == null) $endpoint = $this->client->getEndpoint()->getKey();
$this->queries[$key] = array( $this->queries[$key] = array(
'query' => $query, 'query' => $query,
'client' => $client, 'endpoint' => $endpoint,
); );
return $this; return $this;
...@@ -124,29 +134,18 @@ class ParallelExecution extends Plugin ...@@ -124,29 +134,18 @@ class ParallelExecution extends Plugin
/** /**
* Execute queries parallel * Execute queries parallel
* *
* Use an array of Solarium_Query objects as input. The keys of the array are important, as they are also used in
* the result array. You can mix all querytypes in the input array.
*
* @param array $queries (deprecated, use addQuery instead)
* @return array * @return array
*/ */
public function execute($queries = null) public function execute()
{ {
// this is for backwards compatibility
if (is_array($queries)) {
foreach ($queries as $key => $query) {
$this->addQuery($key, $query);
}
}
// create handles and add all handles to the multihandle // create handles and add all handles to the multihandle
$adapter = $this->client->getAdapter();
$multiHandle = curl_multi_init(); $multiHandle = curl_multi_init();
$handles = array(); $handles = array();
foreach ($this->queries as $key => $data) { foreach ($this->queries as $key => $data) {
$request = $this->client->createRequest($data['query']); $request = $this->client->createRequest($data['query']);
$adapter = $data['client']->setAdapter('Solarium\Client\Adapter\Curl')->getAdapter(); $endpoint = $this->client->getEndpoint($data['endpoint']);
$handle = $adapter->createHandle($request); $handle = $adapter->createHandle($request, $endpoint);
curl_multi_add_handle($multiHandle, $handle); curl_multi_add_handle($multiHandle, $handle);
$handles[$key] = $handle; $handles[$key] = $handle;
} }
...@@ -175,7 +174,7 @@ class ParallelExecution extends Plugin ...@@ -175,7 +174,7 @@ class ParallelExecution extends Plugin
try { try {
curl_multi_remove_handle($multiHandle, $handle); curl_multi_remove_handle($multiHandle, $handle);
$response = $adapter->getResponse($handle, curl_multi_getcontent($handle)); $response = $adapter->getResponse($handle, curl_multi_getcontent($handle));
$results[$key] = $this->client->createResult($queries[$key]['query'], $response); $results[$key] = $this->client->createResult($this->queries[$key]['query'], $response);
} catch(HttpException $e) { } catch(HttpException $e) {
$results[$key] = $e; $results[$key] = $e;
......
...@@ -48,26 +48,23 @@ class ParallelExecutionTest extends \PHPUnit_Framework_TestCase ...@@ -48,26 +48,23 @@ class ParallelExecutionTest extends \PHPUnit_Framework_TestCase
public function testAddAndGetQueries() public function testAddAndGetQueries()
{ {
$client1 = new Client(); $client = new Client();
$client2 = new Client(array( $client->clearEndpoints();
'adapter' => 'MyAdapter', $client->createEndpoint('local1');
'adapteroptions' => array( $endpoint2 = $client->createEndpoint('local2');
'host' => 'myhost',
) $this->plugin->initPlugin($client, array());
)
);
$this->plugin->initPlugin($client1, array());
$query1 = $client1->createSelect()->setQuery('test1'); $query1 = $client->createSelect()->setQuery('test1');
$query2 = $client1->createSelect()->setQuery('test2'); $query2 = $client->createSelect()->setQuery('test2');
$this->plugin->addQuery(1, $query1); $this->plugin->addQuery(1, $query1);
$this->plugin->addQuery(2, $query2, $client2); $this->plugin->addQuery(2, $query2, $endpoint2);
$this->assertEquals( $this->assertEquals(
array( array(
1 => array('query' => $query1, 'client' => $client1), 1 => array('query' => $query1, 'endpoint' => 'local1'),
2 => array('query' => $query2, 'client' => $client2), 2 => array('query' => $query2, 'endpoint' => 'local2'),
), ),
$this->plugin->getQueries() $this->plugin->getQueries()
); );
......
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