1 | <?php |
||
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 | if (version_compare(\MongoClient::VERSION, '1.5', '<=')) { |
||
69 | $writeOptions['wtimeout'] = (int) $timeout; |
||
70 | } else { |
||
71 | $writeOptions['wTimeoutMS'] = (int) $timeout; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | if (true === $ordered) { |
||
76 | $writeOptions['ordered'] = true; |
||
77 | } |
||
78 | |||
79 | $className = $this->batchClass; |
||
80 | $this->batch = new $className( |
||
81 | $this->collection->getMongoCollection(), |
||
82 | $writeOptions |
||
83 | ); |
||
84 | |||
85 | $this->init(); |
||
86 | } |
||
87 | |||
88 | protected function init() {} |
||
89 | |||
90 | protected function add($data) |
||
96 | |||
97 | public function execute($writeConcern = null, $timeout = null, $ordered = null) |
||
117 | |||
118 | public function getResult() |
||
122 | |||
123 | public function count() |
||
127 | } |
||
128 |