1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHPMongo package. |
5
|
|
|
* |
6
|
|
|
* (c) Dmytro Sokil <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sokil\Mongo; |
13
|
|
|
|
14
|
|
|
abstract class BatchOperation implements \Countable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Batch operation class name. Must be override in child classes |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $batchClass; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Collection |
24
|
|
|
*/ |
25
|
|
|
protected $collection; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Batch operation instance |
29
|
|
|
* @var \MongoWriteBatch |
30
|
|
|
*/ |
31
|
|
|
private $batch; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Amount of operations in batch operation |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $counter = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Result of executed batch operation |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $result; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Collection $collection |
47
|
|
|
* @param int|string $writeConcern Write concern. Default is 1 (Acknowledged). |
48
|
|
|
* More info at http://php.net/manual/ru/mongo.writeconcerns.php |
49
|
|
|
* @param int $timeout Timeout for write concern. Default is 10000 milliseconds |
50
|
|
|
* @param bool $ordered Set to true if MongoDB must apply this batch in order (sequentially, |
51
|
|
|
* one item at a time) or can rearrange it. Defaults to TRUE |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
Collection $collection, |
55
|
|
|
$writeConcern = null, |
56
|
|
|
$timeout = null, |
57
|
|
|
$ordered = null |
58
|
|
|
) { |
59
|
|
|
$this->collection = $collection; |
60
|
|
|
|
61
|
|
|
$writeOptions = array(); |
62
|
|
|
|
63
|
|
|
if (null !== $writeConcern) { |
64
|
|
|
$writeOptions['w'] = $writeConcern; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (null !== $timeout && is_numeric($timeout)) { |
68
|
|
|
$writeOptions['wtimeout'] = (int) $timeout; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (true === $ordered) { |
72
|
|
|
$writeOptions['ordered'] = true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$className = $this->batchClass; |
76
|
|
|
$this->batch = new $className( |
77
|
|
|
$this->collection->getMongoCollection(), |
78
|
|
|
$writeOptions |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$this->init(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function init() {} |
85
|
|
|
|
86
|
|
|
protected function add($data) |
87
|
|
|
{ |
88
|
|
|
$this->batch->add($data); |
89
|
|
|
$this->counter++; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function execute($writeConcern = null, $timeout = null, $ordered = null) |
94
|
|
|
{ |
95
|
|
|
$writeOptions = array(); |
96
|
|
|
|
97
|
|
|
if (null !== $writeConcern) { |
98
|
|
|
$writeOptions['w'] = $writeConcern; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($timeout && is_numeric($timeout)) { |
102
|
|
|
$writeOptions['wtimeout'] = (int) $timeout; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (true === $ordered) { |
106
|
|
|
$writeOptions['ordered'] = true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->result = $this->batch->execute($writeOptions); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getResult() |
115
|
|
|
{ |
116
|
|
|
return $this->result; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function count() |
120
|
|
|
{ |
121
|
|
|
return $this->counter; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|