|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
if (class_exists('MongoWriteBatch', false)) { |
|
17
|
|
|
return; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
use Alcaeus\MongoDbAdapter\TypeConverter; |
|
21
|
|
|
use Alcaeus\MongoDbAdapter\Helper\WriteConcernConverter; |
|
22
|
|
|
use MongoDB\Driver\Exception\BulkWriteException; |
|
23
|
|
|
use MongoDB\Driver\WriteError; |
|
24
|
|
|
use MongoDB\Driver\WriteResult; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* MongoWriteBatch allows you to "batch up" multiple operations (of same type) |
|
28
|
|
|
* and shipping them all to MongoDB at the same time. This can be especially |
|
29
|
|
|
* useful when operating on many documents at the same time to reduce roundtrips. |
|
30
|
|
|
* |
|
31
|
|
|
* @see http://php.net/manual/en/class.mongowritebatch.php |
|
32
|
|
|
*/ |
|
33
|
|
|
class MongoWriteBatch |
|
34
|
|
|
{ |
|
35
|
|
|
use WriteConcernConverter; |
|
36
|
|
|
|
|
37
|
|
|
const COMMAND_INSERT = 1; |
|
38
|
|
|
const COMMAND_UPDATE = 2; |
|
39
|
|
|
const COMMAND_DELETE = 3; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var MongoCollection |
|
43
|
|
|
*/ |
|
44
|
|
|
private $collection; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var int |
|
48
|
|
|
*/ |
|
49
|
|
|
private $batchType; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
private $writeOptions; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
private $items = []; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Creates a new batch of write operations |
|
63
|
|
|
* |
|
64
|
|
|
* @see http://php.net/manual/en/mongowritebatch.construct.php |
|
65
|
|
|
* @param MongoCollection $collection |
|
66
|
|
|
* @param int $batchType |
|
67
|
|
|
* @param array $writeOptions |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function __construct(MongoCollection $collection, $batchType, $writeOptions) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->collection = $collection; |
|
72
|
|
|
$this->batchType = $batchType; |
|
73
|
|
|
$this->writeOptions = $writeOptions; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Adds a write operation to a batch |
|
78
|
|
|
* |
|
79
|
|
|
* @see http://php.net/manual/en/mongowritebatch.add.php |
|
80
|
|
|
* @param array|object $item |
|
81
|
|
|
* @return boolean |
|
82
|
|
|
*/ |
|
83
|
|
|
public function add($item) |
|
84
|
|
|
{ |
|
85
|
|
|
if (is_object($item)) { |
|
86
|
|
|
$item = (array) $item; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->validate($item); |
|
90
|
|
|
$this->addItem($item); |
|
91
|
|
|
|
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Executes a batch of write operations |
|
97
|
|
|
* |
|
98
|
|
|
* @see http://php.net/manual/en/mongowritebatch.execute.php |
|
99
|
|
|
* @param array $writeOptions |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
final public function execute(array $writeOptions = []) |
|
103
|
|
|
{ |
|
104
|
|
|
$writeOptions += $this->writeOptions; |
|
105
|
|
|
if (! count($this->items)) { |
|
106
|
|
|
return ['ok' => true]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (isset($writeOptions['j'])) { |
|
110
|
|
|
trigger_error('j parameter is not supported', E_USER_WARNING); |
|
111
|
|
|
} |
|
112
|
|
|
if (isset($writeOptions['fsync'])) { |
|
113
|
|
|
trigger_error('fsync parameter is not supported', E_USER_WARNING); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$options['writeConcern'] = $this->createWriteConcernFromArray($writeOptions); |
|
|
|
|
|
|
117
|
|
|
if (isset($writeOptions['ordered'])) { |
|
118
|
|
|
$options['ordered'] = $writeOptions['ordered']; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
try { |
|
122
|
|
|
$writeResult = $this->collection->getCollection()->bulkWrite($this->items, $options); |
|
123
|
|
|
$resultDocument = []; |
|
124
|
|
|
$ok = true; |
|
125
|
|
|
} catch (BulkWriteException $e) { |
|
126
|
|
|
$writeResult = $e->getWriteResult(); |
|
127
|
|
|
$resultDocument = ['writeErrors' => $this->convertWriteErrors($writeResult)]; |
|
128
|
|
|
$ok = false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->items = []; |
|
132
|
|
|
|
|
133
|
|
|
switch ($this->batchType) { |
|
134
|
|
|
case self::COMMAND_UPDATE: |
|
135
|
|
|
if ($options['writeConcern']->getW() === 0) { |
|
136
|
|
|
$resultDocument += [ |
|
137
|
|
|
'nMatched' => 0, |
|
138
|
|
|
'nModified' => 0, |
|
139
|
|
|
'nUpserted' => 0, |
|
140
|
|
|
'ok' => true, |
|
141
|
|
|
]; |
|
142
|
|
|
|
|
143
|
|
|
break; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$upsertedIds = []; |
|
147
|
|
|
foreach ($writeResult->getUpsertedIds() as $index => $id) { |
|
148
|
|
|
$upsertedIds[] = [ |
|
149
|
|
|
'index' => $index, |
|
150
|
|
|
'_id' => TypeConverter::toLegacy($id) |
|
151
|
|
|
]; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$resultDocument += [ |
|
155
|
|
|
'nMatched' => $writeResult->getMatchedCount(), |
|
156
|
|
|
'nModified' => $writeResult->getModifiedCount(), |
|
157
|
|
|
'nUpserted' => $writeResult->getUpsertedCount(), |
|
158
|
|
|
'ok' => true, |
|
159
|
|
|
]; |
|
160
|
|
|
|
|
161
|
|
|
if (count($upsertedIds)) { |
|
162
|
|
|
$resultDocument['upserted'] = $upsertedIds; |
|
163
|
|
|
} |
|
164
|
|
|
break; |
|
165
|
|
|
|
|
166
|
|
View Code Duplication |
case self::COMMAND_DELETE: |
|
|
|
|
|
|
167
|
|
|
if ($options['writeConcern']->getW() === 0) { |
|
168
|
|
|
$resultDocument += [ |
|
169
|
|
|
'nRemoved' => 0, |
|
170
|
|
|
'ok' => true, |
|
171
|
|
|
]; |
|
172
|
|
|
|
|
173
|
|
|
break; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$resultDocument += [ |
|
177
|
|
|
'nRemoved' => $writeResult->getDeletedCount(), |
|
178
|
|
|
'ok' => true, |
|
179
|
|
|
]; |
|
180
|
|
|
break; |
|
181
|
|
|
|
|
182
|
|
View Code Duplication |
case self::COMMAND_INSERT: |
|
|
|
|
|
|
183
|
|
|
if ($options['writeConcern']->getW() === 0) { |
|
184
|
|
|
$resultDocument += [ |
|
185
|
|
|
'nInserted' => 0, |
|
186
|
|
|
'ok' => true, |
|
187
|
|
|
]; |
|
188
|
|
|
|
|
189
|
|
|
break; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$resultDocument += [ |
|
193
|
|
|
'nInserted' => $writeResult->getInsertedCount(), |
|
194
|
|
|
'ok' => true, |
|
195
|
|
|
]; |
|
196
|
|
|
break; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
if (! $ok) { |
|
200
|
|
|
// Exception code is hardcoded to the value in ext-mongo, see |
|
201
|
|
|
// https://github.com/mongodb/mongo-php-driver-legacy/blob/ab4bc0d90e93b3f247f6bcb386d0abc8d2fa7d74/batch/write.c#L428 |
|
202
|
|
|
throw new \MongoWriteConcernException('Failed write', 911, null, $resultDocument); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $resultDocument; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
private function validate(array $item) |
|
209
|
|
|
{ |
|
210
|
|
|
switch ($this->batchType) { |
|
211
|
|
View Code Duplication |
case self::COMMAND_UPDATE: |
|
|
|
|
|
|
212
|
|
|
if (! isset($item['q'])) { |
|
213
|
|
|
throw new Exception("Expected \$item to contain 'q' key"); |
|
214
|
|
|
} |
|
215
|
|
|
if (! isset($item['u'])) { |
|
216
|
|
|
throw new Exception("Expected \$item to contain 'u' key"); |
|
217
|
|
|
} |
|
218
|
|
|
break; |
|
219
|
|
|
|
|
220
|
|
View Code Duplication |
case self::COMMAND_DELETE: |
|
|
|
|
|
|
221
|
|
|
if (! isset($item['q'])) { |
|
222
|
|
|
throw new Exception("Expected \$item to contain 'q' key"); |
|
223
|
|
|
} |
|
224
|
|
|
if (! isset($item['limit'])) { |
|
225
|
|
|
throw new Exception("Expected \$item to contain 'limit' key"); |
|
226
|
|
|
} |
|
227
|
|
|
break; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
private function addItem(array $item) |
|
232
|
|
|
{ |
|
233
|
|
|
switch ($this->batchType) { |
|
234
|
|
|
case self::COMMAND_UPDATE: |
|
235
|
|
|
$method = isset($item['multi']) ? 'updateMany' : 'updateOne'; |
|
236
|
|
|
|
|
237
|
|
|
$options = []; |
|
238
|
|
|
if (isset($item['upsert']) && $item['upsert']) { |
|
239
|
|
|
$options['upsert'] = true; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
$this->items[] = [$method => [TypeConverter::fromLegacy($item['q']), TypeConverter::fromLegacy($item['u']), $options]]; |
|
243
|
|
|
break; |
|
244
|
|
|
|
|
245
|
|
|
case self::COMMAND_INSERT: |
|
246
|
|
|
$this->items[] = ['insertOne' => [TypeConverter::fromLegacy($item)]]; |
|
247
|
|
|
break; |
|
248
|
|
|
|
|
249
|
|
|
case self::COMMAND_DELETE: |
|
250
|
|
|
$method = $item['limit'] === 0 ? 'deleteMany' : 'deleteOne'; |
|
251
|
|
|
|
|
252
|
|
|
$this->items[] = [$method => [TypeConverter::fromLegacy($item['q'])]]; |
|
253
|
|
|
break; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @param WriteResult $result |
|
259
|
|
|
* @return array |
|
260
|
|
|
*/ |
|
261
|
|
|
private function convertWriteErrors(WriteResult $result) |
|
262
|
|
|
{ |
|
263
|
|
|
$writeErrors = []; |
|
264
|
|
|
/** @var WriteError $writeError */ |
|
265
|
|
|
foreach ($result->getWriteErrors() as $writeError) { |
|
266
|
|
|
$writeErrors[] = [ |
|
267
|
|
|
'index' => $writeError->getIndex(), |
|
268
|
|
|
'code' => $writeError->getCode(), |
|
269
|
|
|
'errmsg' => $writeError->getMessage(), |
|
270
|
|
|
]; |
|
271
|
|
|
} |
|
272
|
|
|
return $writeErrors; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.