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 | * Buffered add plugin |
---|
40 | * |
---|
41 | * If you need to add (or update) a big number of documents to Solr it's much more efficient to do so in batches. |
---|
42 | * This plugin makes this as easy as possible. |
---|
43 | * |
---|
44 | * @package Solarium |
---|
45 | * @subpackage Plugin |
---|
46 | */ |
---|
47 | class Solarium_Plugin_BufferedAdd extends Solarium_Plugin_Abstract |
---|
48 | { |
---|
49 | |
---|
50 | /** |
---|
51 | * Default options |
---|
52 | * |
---|
53 | * @var array |
---|
54 | */ |
---|
55 | protected $_options = array( |
---|
56 | 'buffersize' => 100, |
---|
57 | ); |
---|
58 | |
---|
59 | /** |
---|
60 | * Update query instance |
---|
61 | * |
---|
62 | * @var Solarium_Query_Update |
---|
63 | */ |
---|
64 | protected $_updateQuery; |
---|
65 | |
---|
66 | /** |
---|
67 | * Buffered documents |
---|
68 | * |
---|
69 | * @var array |
---|
70 | */ |
---|
71 | protected $_buffer = array(); |
---|
72 | |
---|
73 | /** |
---|
74 | * Plugin init function |
---|
75 | * |
---|
76 | * This is an extension point for plugin implementations. |
---|
77 | * Will be called as soon as $this->_client and options have been set. |
---|
78 | * |
---|
79 | * @return void |
---|
80 | */ |
---|
81 | protected function _initPlugin() |
---|
82 | { |
---|
83 | $this->_updateQuery = $this->_client->createUpdate(); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Set buffer size option |
---|
88 | * |
---|
89 | * @param int $size |
---|
90 | * @return Solarium_Configurable |
---|
91 | */ |
---|
92 | public function setBufferSize($size) |
---|
93 | { |
---|
94 | return $this->_setOption('buffersize', $size); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Get buffer size option value |
---|
99 | * |
---|
100 | * @return int |
---|
101 | */ |
---|
102 | public function getBufferSize() |
---|
103 | { |
---|
104 | return $this->getOption('buffersize'); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Create a document object instance and add it to the buffer |
---|
109 | * |
---|
110 | * @param array $fields |
---|
111 | * @param array $boosts |
---|
112 | * @return self Provides fluent interface |
---|
113 | */ |
---|
114 | public function createDocument($fields, $boosts = array()) |
---|
115 | { |
---|
116 | $doc = $this->_updateQuery->createDocument($fields, $boosts); |
---|
117 | $this->addDocument($doc); |
---|
118 | |
---|
119 | return $this; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Add a document |
---|
124 | * |
---|
125 | * @param Solarium_Document_ReadOnly $document |
---|
126 | * @return self Provides fluent interface |
---|
127 | */ |
---|
128 | public function addDocument($document) |
---|
129 | { |
---|
130 | $this->_buffer[] = $document; |
---|
131 | if (count($this->_buffer) == $this->_options['buffersize']) { |
---|
132 | $this->flush(); |
---|
133 | } |
---|
134 | |
---|
135 | return $this; |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Add multiple documents |
---|
140 | * |
---|
141 | * @param array |
---|
142 | * @return self Provides fluent interface |
---|
143 | */ |
---|
144 | public function addDocuments($documents) |
---|
145 | { |
---|
146 | foreach ($documents as $document) { |
---|
147 | $this->addDocument($document); |
---|
148 | } |
---|
149 | |
---|
150 | return $this; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Get all documents currently in the buffer |
---|
155 | * |
---|
156 | * Any previously flushed documents will not be included! |
---|
157 | * |
---|
158 | * @return array |
---|
159 | */ |
---|
160 | public function getDocuments() |
---|
161 | { |
---|
162 | return $this->_buffer; |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * Clear any buffered documents |
---|
167 | * |
---|
168 | * @return self Provides fluent interface |
---|
169 | */ |
---|
170 | public function clear() |
---|
171 | { |
---|
172 | $this->_updateQuery = $this->_client->createUpdate(); |
---|
173 | $this->_buffer = array(); |
---|
174 | return $this; |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Flush any buffered documents to Solr |
---|
179 | * |
---|
180 | * @param boolean $overwrite |
---|
181 | * @param int $commitWithin |
---|
182 | * @return boolean|Solarium_Result_Update |
---|
183 | */ |
---|
184 | public function flush($overwrite = null, $commitWithin = null) |
---|
185 | { |
---|
186 | if (count($this->_buffer) == 0) { |
---|
187 | // nothing to do |
---|
188 | return false; |
---|
189 | } |
---|
190 | |
---|
191 | $this->_client->triggerEvent('BufferedAddFlushStart', array($this->_buffer)); |
---|
192 | |
---|
193 | $this->_updateQuery->addDocuments($this->_buffer, $overwrite, $commitWithin); |
---|
194 | $result = $this->_client->update($this->_updateQuery); |
---|
195 | $this->clear(); |
---|
196 | |
---|
197 | $this->_client->triggerEvent('BufferedAddFlushEnd', array($result)); |
---|
198 | |
---|
199 | return $result; |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * Commit changes |
---|
204 | * |
---|
205 | * Any remaining documents in the buffer will also be flushed |
---|
206 | * |
---|
207 | * @param boolean $overwrite |
---|
208 | * @param boolean $waitFlush |
---|
209 | * @param boolean $waitSearcher |
---|
210 | * @param boolean $expungeDeletes |
---|
211 | * @return Solarium_Result_Update |
---|
212 | */ |
---|
213 | public function commit($overwrite = null, $waitFlush = null, $waitSearcher = null, $expungeDeletes = null) |
---|
214 | { |
---|
215 | $this->_client->triggerEvent('BufferedAddCommitStart', array($this->_buffer)); |
---|
216 | |
---|
217 | $this->_updateQuery->addDocuments($this->_buffer, $overwrite); |
---|
218 | $this->_updateQuery->addCommit($waitFlush, $waitSearcher, $expungeDeletes); |
---|
219 | $result = $this->_client->update($this->_updateQuery); |
---|
220 | $this->clear(); |
---|
221 | |
---|
222 | $this->_client->triggerEvent('BufferedAddCommitEnd', array($result)); |
---|
223 | |
---|
224 | return $result; |
---|
225 | } |
---|
226 | |
---|
227 | } |
---|