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
*/
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
*
* @param string $key
* @param Query $query
* @param null|Client $client
* @param null|string|Endpoint $endpoint
* @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(
'query' => $query,
'client' => $client,
'endpoint' => $endpoint,
);
return $this;
......@@ -124,29 +134,18 @@ class ParallelExecution extends Plugin
/**
* 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
*/
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
$adapter = $this->client->getAdapter();
$multiHandle = curl_multi_init();
$handles = array();
foreach ($this->queries as $key => $data) {
$request = $this->client->createRequest($data['query']);
$adapter = $data['client']->setAdapter('Solarium\Client\Adapter\Curl')->getAdapter();
$handle = $adapter->createHandle($request);
$endpoint = $this->client->getEndpoint($data['endpoint']);
$handle = $adapter->createHandle($request, $endpoint);
curl_multi_add_handle($multiHandle, $handle);
$handles[$key] = $handle;
}
......@@ -175,7 +174,7 @@ class ParallelExecution extends Plugin
try {
curl_multi_remove_handle($multiHandle, $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) {
$results[$key] = $e;
......
......@@ -48,26 +48,23 @@ class ParallelExecutionTest extends \PHPUnit_Framework_TestCase
public function testAddAndGetQueries()
{
$client1 = new Client();
$client2 = new Client(array(
'adapter' => 'MyAdapter',
'adapteroptions' => array(
'host' => 'myhost',
)
)
);
$this->plugin->initPlugin($client1, array());
$client = new Client();
$client->clearEndpoints();
$client->createEndpoint('local1');
$endpoint2 = $client->createEndpoint('local2');
$this->plugin->initPlugin($client, array());
$query1 = $client1->createSelect()->setQuery('test1');
$query2 = $client1->createSelect()->setQuery('test2');
$query1 = $client->createSelect()->setQuery('test1');
$query2 = $client->createSelect()->setQuery('test2');
$this->plugin->addQuery(1, $query1);
$this->plugin->addQuery(2, $query2, $client2);
$this->plugin->addQuery(2, $query2, $endpoint2);
$this->assertEquals(
array(
1 => array('query' => $query1, 'client' => $client1),
2 => array('query' => $query2, 'client' => $client2),
1 => array('query' => $query1, 'endpoint' => 'local1'),
2 => array('query' => $query2, 'endpoint' => 'local2'),
),
$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