source: sandbox/expresso-solr/expressoMail1_2/inc/solrclient/library/Solarium/Plugin/ParallelExecution.php @ 7576

Revision 7576, 5.7 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
1<?php
2/**
3 * Copyright 2011 Bas de Nooijer. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 *    this listof conditions and the following disclaimer in the documentation
13 *    and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 *
27 * The views and conclusions contained in the software and documentation are
28 * those of the authors and should not be interpreted as representing official
29 * policies, either expressed or implied, of the copyright holder.
30 *
31 * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
32 * @license http://github.com/basdenooijer/solarium/raw/master/COPYING
33 * @link http://www.solarium-project.org/
34 *
35 * @package Solarium
36 */
37
38/**
39 * ParallelExecution plugin
40 *
41 * You can use this plugin to run multiple queries parallel. This functionality depends on the curl adapter so you
42 * do need to have curl available in your PHP environment.
43 *
44 * While query execution is parallel, the results only become available as soon as all requests have finished. So the
45 * time of the slowest query will be the effective execution time for all queries.
46 *
47 * @package Solarium
48 * @subpackage Plugin
49 */
50
51class Solarium_Plugin_ParallelExecution extends Solarium_Plugin_Abstract
52{
53
54    /**
55     * Default options
56     *
57     * @var array
58     */
59    protected $_options = array(
60        'curlmultiselecttimeout' => 0.1,
61    );
62
63    /**
64     * Queries (and optionally clients) to execute
65     *
66     * @var array
67     */
68    protected $_queries = array();
69
70    /**
71     * Add a query to execute
72     *
73     * @param string $key
74     * @param Solarium_Query $query
75     * @param null|Solarium_Client $client
76     * @return Solarium_Plugin_ParallelExecution
77     */
78    public function addQuery($key, $query, $client = null)
79    {
80        if($client == null) $client = $this->_client;
81
82        $this->_queries[$key] = array(
83            'query' => $query,
84            'client' => $client,
85        );
86
87        return $this;
88    }
89
90    /**
91     * Get queries (and coupled client instances)
92     *
93     * @return array
94     */
95    public function getQueries()
96    {
97        return $this->_queries;
98    }
99
100    /**
101     * Clear all queries
102     *
103     * @return self Provides fluent interface
104     */
105    public function clearQueries()
106    {
107        $this->_queries = array();
108        return $this;
109    }
110
111    // @codeCoverageIgnoreStart
112
113    /**
114     * Execute queries parallel
115     *
116     * Use an array of Solarium_Query objects as input. The keys of the array are important, as they are also used in
117     * the result array. You can mix all querytypes in the input array.
118     *
119     * @param array $queries (deprecated, use addQuery instead)
120     * @return array
121     */
122    public function execute($queries = null)
123    {
124        // this is for backwards compatibility
125        if (is_array($queries)) {
126            foreach ($queries as $key => $query) {
127                $this->addQuery($key, $query);
128            }
129        }
130
131        // create handles and add all handles to the multihandle
132        $multiHandle = curl_multi_init();
133        $handles = array();
134        foreach ($this->_queries as $key => $data) {
135            $request = $this->_client->createRequest($data['query']);
136            $adapter = $data['client']->setAdapter('Solarium_Client_Adapter_Curl')->getAdapter();
137            $handle = $adapter->createHandle($request);
138            curl_multi_add_handle($multiHandle, $handle);
139            $handles[$key] = $handle;
140        }
141
142        // executing multihandle (all requests)
143        $this->_client->triggerEvent('ParallelExecutionStart');
144
145        do {
146            $mrc = curl_multi_exec($multiHandle, $active);
147        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
148
149        $timeout = $this->getOption('curlmultiselecttimeout');
150        while ($active && $mrc == CURLM_OK) {
151            if (curl_multi_select($multiHandle, $timeout) != -1) {
152                do {
153                    $mrc = curl_multi_exec($multiHandle, $active);
154                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
155            }
156        }
157
158        $this->_client->triggerEvent('ParallelExecutionEnd');
159
160        // get the results
161        $results = array();
162        foreach ($handles as $key => $handle) {
163            try {
164                curl_multi_remove_handle($multiHandle, $handle);
165                $response = $adapter->getResponse($handle, curl_multi_getcontent($handle));
166                $results[$key] = $this->_client->createResult($this->_queries[$key]['query'], $response);
167            } catch(Solarium_Client_HttpException $e) {
168                $results[$key] = $e;
169            }
170        }
171
172        curl_multi_close($multiHandle);
173
174        return $results;
175    }
176
177    // @codeCoverageIgnoreEnd
178}
Note: See TracBrowser for help on using the repository browser.