1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Maslosoft\Mangan\Bridge; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* MongoCursor |
13
|
|
|
* |
14
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
15
|
|
|
*/ |
16
|
|
|
class MongoCursor |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
const DEFAULT_BATCH_SIZE = 100; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var integer |
23
|
|
|
*/ |
24
|
|
|
public static $timeout = 30000; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var MongoClient |
28
|
|
|
*/ |
29
|
|
|
private $client; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Full collection name |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $fcn; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array[] |
39
|
|
|
*/ |
40
|
|
|
private $documents = []; |
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
private $currKey = -1; |
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var null|int |
49
|
|
|
*/ |
50
|
|
|
private $cursorId = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
private $fetching = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
private $end = false; |
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
private $hasMore = false; |
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
private $query = []; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var int |
74
|
|
|
*/ |
75
|
|
|
private $queryLimit = 0; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var int |
79
|
|
|
*/ |
80
|
|
|
private $querySkip = 0; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var int |
84
|
|
|
*/ |
85
|
|
|
private $queryTimeout = null; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var int |
89
|
|
|
*/ |
90
|
|
|
private $batchSize = self::DEFAULT_BATCH_SIZE; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var int |
94
|
|
|
*/ |
95
|
|
|
private $flags = 0; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var array |
99
|
|
|
*/ |
100
|
|
|
private $readPreference; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Create a new cursor |
104
|
|
|
* |
105
|
|
|
* @param MongoClient $client - Database connection. |
106
|
|
|
* @param string $ns - Full name of database and collection. |
107
|
|
|
* @param array $query - Database query. |
108
|
|
|
* @param array $fields - Fields to return. |
109
|
|
|
*/ |
110
|
|
|
public function __construct(MongoClient $client, $ns, array $query = [], array $fields = []) |
111
|
|
|
{ |
112
|
|
|
$this->client = $client; |
113
|
|
|
$this->readPreference = $client->getReadPreference(); |
114
|
|
|
$this->fcn = $ns; |
115
|
|
|
$this->fields = $fields; |
|
|
|
|
116
|
|
|
$this->query['$query'] = $query; |
117
|
|
|
$this->queryTimeout = self::$timeout; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Sorts the results by given fields |
122
|
|
|
* |
123
|
|
|
* @param array $fields - An array of fields by which to sort. Each |
124
|
|
|
* element in the array has as key the field name, and as value either |
125
|
|
|
* 1 for ascending sort, or -1 for descending sort. |
126
|
|
|
* |
127
|
|
|
* @return MongoCursor - Returns the same cursor that this method was |
128
|
|
|
* called on. |
129
|
|
|
*/ |
130
|
|
|
public function sort(array $fields) |
131
|
|
|
{ |
132
|
|
|
$this->query['$orderby'] = $fields; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Sets the fields for a query |
139
|
|
|
* |
140
|
|
|
* @param array $fields - Fields to return (or not return). |
141
|
|
|
* |
142
|
|
|
* @return MongoCursor - Returns this cursor. |
143
|
|
|
*/ |
144
|
|
|
public function fields(array $fields) |
145
|
|
|
{ |
146
|
|
|
$this->fields = $fields; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Limits the number of results returned |
153
|
|
|
* |
154
|
|
|
* @param int $num - The number of results to return. |
155
|
|
|
* |
156
|
|
|
* @return MongoCursor - Returns this cursor. |
157
|
|
|
*/ |
158
|
|
|
public function limit($num) |
159
|
|
|
{ |
160
|
|
|
$this->queryLimit = $num; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Skips a number of results |
167
|
|
|
* |
168
|
|
|
* @param int $num - The number of results to skip. |
169
|
|
|
* |
170
|
|
|
* @return MongoCursor - Returns this cursor. |
171
|
|
|
*/ |
172
|
|
|
public function skip($num) |
173
|
|
|
{ |
174
|
|
|
$this->querySkip = $num; |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Gets the query, fields, limit, and skip for this cursor |
181
|
|
|
* |
182
|
|
|
* @return array - Returns the namespace, limit, skip, query, and |
183
|
|
|
* fields for this cursor. |
184
|
|
|
*/ |
185
|
|
|
public function info() |
186
|
|
|
{ |
187
|
|
|
$info = [ |
188
|
|
|
'ns' => $this->fcn, |
189
|
|
|
'limit' => $this->queryLimit, |
190
|
|
|
'batchSize' => $this->batchSize, |
191
|
|
|
'skip' => $this->querySkip, |
192
|
|
|
'flags' => $this->flags | Mongofill\Protocol::QF_SLAVE_OK, |
193
|
|
|
'query' => $this->query['$query'], |
194
|
|
|
'fields' => $this->fields, |
195
|
|
|
'started_iterating' => $this->fetching, |
196
|
|
|
'id' => $this->cursorId, |
197
|
|
|
'server' => $this->client->_getReadProtocol($this->readPreference)->getServerHash(), |
|
|
|
|
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
//TODO: missing opReplay information |
201
|
|
|
return $info; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Counts the number of results for this query |
206
|
|
|
* |
207
|
|
|
* @param bool $foundOnly - |
208
|
|
|
* |
209
|
|
|
* @return int - The number of documents returned by this cursor's |
210
|
|
|
* query. |
211
|
|
|
*/ |
212
|
|
|
public function count($foundOnly = false) |
213
|
|
|
{ |
214
|
|
|
$this->doQuery(); |
215
|
|
|
|
216
|
|
|
if ($foundOnly) |
217
|
|
|
{ |
218
|
|
|
return $this->countLocalData(); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $this->countQuerying(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
private function countQuerying() |
225
|
|
|
{ |
226
|
|
|
$ns = explode('.', $this->fcn, 2); |
227
|
|
|
|
228
|
|
|
$query = [ |
229
|
|
|
'count' => $ns[1], |
230
|
|
|
'query' => $this->query['$query'] |
231
|
|
|
]; |
232
|
|
|
|
233
|
|
|
$response = $this->client->_getReadProtocol($this->readPreference)->opQuery( |
|
|
|
|
234
|
|
|
$ns[0] . '.$cmd', $query, 0, -1, 0, $this->queryTimeout |
235
|
|
|
); |
236
|
|
|
|
237
|
|
|
return (int) $response['result'][0]['n']; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Execute the query. |
242
|
|
|
* |
243
|
|
|
* @return void - NULL. |
244
|
|
|
*/ |
245
|
|
|
protected function doQuery() |
246
|
|
|
{ |
247
|
|
|
if (!$this->fetching) |
248
|
|
|
{ |
249
|
|
|
$this->fetchDocuments(); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
private function fetchDocuments() |
254
|
|
|
{ |
255
|
|
|
$this->fetching = true; |
256
|
|
|
$response = $this->client->_getReadProtocol($this->readPreference)->opQuery( |
|
|
|
|
257
|
|
|
$this->fcn, $this->getQuery(), $this->querySkip, $this->calculateRequestLimit(), $this->flags | Mongofill\Protocol::QF_SLAVE_OK, $this->queryTimeout, $this->fields |
|
|
|
|
258
|
|
|
); |
259
|
|
|
|
260
|
|
|
$this->cursorId = $response['cursorId']; |
261
|
|
|
$this->setDocuments($response); |
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
private function getQuery() |
265
|
|
|
{ |
266
|
|
|
if (isset($this->query['$query']) && count($this->query) == 1) |
267
|
|
|
{ |
268
|
|
|
return $this->query['$query']; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $this->query; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.