Skip to content

Commit 8e3bd3d

Browse files
committed
PHPC-116: Cursor methods should call their respective iterator function handlers
1 parent 865297f commit 8e3bd3d

File tree

4 files changed

+222
-11
lines changed

4 files changed

+222
-11
lines changed

php_phongo.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,11 +1164,6 @@ void php_phongo_result_free(php_phongo_result_t *result)
11641164
}
11651165

11661166
/* {{{ Iterator */
1167-
typedef struct {
1168-
zend_object_iterator iterator;
1169-
bson_iter_t first_batch_iter;
1170-
} phongo_cursor_it;
1171-
11721167
static void phongo_cursor_it_invalidate_current(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
11731168
{
11741169
php_phongo_result_t *result = NULL;
@@ -1182,11 +1177,9 @@ static void phongo_cursor_it_invalidate_current(zend_object_iterator *iter TSRML
11821177

11831178
static void phongo_cursor_it_dtor(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
11841179
{
1185-
phongo_cursor_it *cursor_it = (phongo_cursor_it *)iter;
1186-
11871180
iter->funcs->invalidate_current(iter TSRMLS_CC);
11881181

1189-
efree(cursor_it);
1182+
efree(iter);
11901183
} /* }}} */
11911184

11921185
static int phongo_cursor_it_valid(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
@@ -1202,6 +1195,11 @@ static int phongo_cursor_it_valid(zend_object_iterator *iter TSRMLS_DC) /* {{{ *
12021195
return FAILURE;
12031196
} /* }}} */
12041197

1198+
static void phongo_cursor_it_get_current_key(zend_object_iterator *iter, zval *key TSRMLS_DC) /* {{{ */
1199+
{
1200+
ZVAL_LONG(key, ((phongo_cursor_it *)iter)->current);
1201+
} /* }}} */
1202+
12051203
static void phongo_cursor_it_get_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC) /* {{{ */
12061204
{
12071205
php_phongo_result_t *result = NULL;
@@ -1213,14 +1211,14 @@ static void phongo_cursor_it_get_current_data(zend_object_iterator *iter, zval *
12131211

12141212
static void phongo_cursor_it_move_forward(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
12151213
{
1216-
php_phongo_result_t *result = NULL;
1214+
php_phongo_result_t *result = NULL;
12171215
phongo_cursor_it *cursor_it = (phongo_cursor_it *)iter;
12181216
const bson_t *doc;
12191217

12201218
result = ((phongo_cursor_it *)iter)->iterator.data;
12211219
iter->funcs->invalidate_current(iter TSRMLS_CC);
12221220

1223-
iter->index++;
1221+
((phongo_cursor_it *)iter)->current++;
12241222
if (bson_iter_next(&cursor_it->first_batch_iter)) {
12251223
if (BSON_ITER_HOLDS_DOCUMENT (&cursor_it->first_batch_iter)) {
12261224
const uint8_t *data = NULL;
@@ -1250,6 +1248,7 @@ static void phongo_cursor_it_rewind(zend_object_iterator *iter TSRMLS_DC) /* {{{
12501248
result = ((phongo_cursor_it *)iter)->iterator.data;
12511249

12521250
iter->funcs->invalidate_current(iter TSRMLS_CC);
1251+
((phongo_cursor_it *)iter)->current = 0;
12531252

12541253
/* firstBatch is empty when the query simply didn't return any results */
12551254
if (result->firstBatch) {
@@ -1279,7 +1278,7 @@ zend_object_iterator_funcs phongo_cursor_it_funcs = {
12791278
phongo_cursor_it_dtor,
12801279
phongo_cursor_it_valid,
12811280
phongo_cursor_it_get_current_data,
1282-
NULL, /* void (*get_current_key)(zend_object_iterator *iter, zval *key TSRMLS_DC); */
1281+
phongo_cursor_it_get_current_key,
12831282
phongo_cursor_it_move_forward,
12841283
phongo_cursor_it_rewind,
12851284
phongo_cursor_it_invalidate_current

php_phongo_classes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@ typedef struct {
5959
php_phongo_result_t result;
6060
} php_phongo_commandresult_t;
6161

62+
typedef struct {
63+
zend_object_iterator iterator;
64+
bson_iter_t first_batch_iter;
65+
long current;
66+
} phongo_cursor_it;
67+
6268
typedef struct {
6369
zend_object std;
6470
php_phongo_result_t *result;
71+
phongo_cursor_it *it;
6572
} php_phongo_cursor_t;
6673

6774
typedef struct {

src/MongoDB/Cursor.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,14 @@ PHP_METHOD(Cursor, getBatchSize)
189189
RETURN_LONG(mongoc_cursor_get_batch_size(intern->result->cursor));
190190
}
191191
/* }}} */
192+
192193
/* {{{ proto mixed Cursor::current()
193194
Returns the current element */
194195
PHP_METHOD(Cursor, current)
195196
{
196197
php_phongo_cursor_t *intern;
197198
zend_error_handling error_handling;
199+
zval **data = NULL;
198200

199201

200202
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
@@ -206,6 +208,17 @@ PHP_METHOD(Cursor, current)
206208
}
207209
zend_restore_error_handling(&error_handling TSRMLS_CC);
208210

211+
if (!intern->it) {
212+
zend_class_entry *ce;
213+
214+
ce = Z_OBJCE_P(getThis());
215+
intern->it = (phongo_cursor_it *)ce->get_iterator(ce, getThis(), 0 TSRMLS_CC);
216+
}
217+
218+
intern->it->iterator.funcs->get_current_data(&intern->it->iterator, &data TSRMLS_CC);
219+
if (data && *data) {
220+
RETURN_ZVAL(*data, 1, 0);
221+
}
209222
}
210223
/* }}} */
211224
/* {{{ proto mixed Cursor::key()
@@ -225,6 +238,18 @@ PHP_METHOD(Cursor, key)
225238
}
226239
zend_restore_error_handling(&error_handling TSRMLS_CC);
227240

241+
if (!intern->it) {
242+
zend_class_entry *ce;
243+
244+
ce = Z_OBJCE_P(getThis());
245+
intern->it = (phongo_cursor_it *)ce->get_iterator(ce, getThis(), 0 TSRMLS_CC);
246+
}
247+
248+
if (intern->it->iterator.funcs->get_current_key) {
249+
intern->it->iterator.funcs->get_current_key(&intern->it->iterator, return_value TSRMLS_CC);
250+
} else {
251+
RETURN_LONG(intern->it->current);
252+
}
228253
}
229254
/* }}} */
230255
/* {{{ proto void Cursor::next()
@@ -244,6 +269,14 @@ PHP_METHOD(Cursor, next)
244269
}
245270
zend_restore_error_handling(&error_handling TSRMLS_CC);
246271

272+
if (!intern->it) {
273+
zend_class_entry *ce;
274+
275+
ce = Z_OBJCE_P(getThis());
276+
intern->it = (phongo_cursor_it *)ce->get_iterator(ce, getThis(), 0 TSRMLS_CC);
277+
}
278+
279+
intern->it->iterator.funcs->move_forward(&intern->it->iterator TSRMLS_CC);
247280
}
248281
/* }}} */
249282
/* {{{ proto void Cursor::rewind()
@@ -263,6 +296,14 @@ PHP_METHOD(Cursor, rewind)
263296
}
264297
zend_restore_error_handling(&error_handling TSRMLS_CC);
265298

299+
if (!intern->it) {
300+
zend_class_entry *ce;
301+
302+
ce = Z_OBJCE_P(getThis());
303+
intern->it = (phongo_cursor_it *)ce->get_iterator(ce, getThis(), 0 TSRMLS_CC);
304+
}
305+
306+
intern->it->iterator.funcs->rewind(&intern->it->iterator TSRMLS_CC);
266307
}
267308
/* }}} */
268309
/* {{{ proto boolean Cursor::valid()
@@ -282,6 +323,14 @@ PHP_METHOD(Cursor, valid)
282323
}
283324
zend_restore_error_handling(&error_handling TSRMLS_CC);
284325

326+
if (!intern->it) {
327+
zend_class_entry *ce;
328+
329+
ce = Z_OBJCE_P(getThis());
330+
intern->it = (phongo_cursor_it *)ce->get_iterator(ce, getThis(), 0 TSRMLS_CC);
331+
}
332+
333+
RETURN_BOOL(intern->it->iterator.funcs->valid(&intern->it->iterator TSRMLS_CC) == SUCCESS);
285334
}
286335
/* }}} */
287336

@@ -364,6 +413,10 @@ static void php_phongo_cursor_free_object(void *object TSRMLS_DC) /* {{{ */
364413
{
365414
php_phongo_cursor_t *intern = (php_phongo_cursor_t*)object;
366415

416+
if (intern->it) {
417+
intern->it->iterator.funcs->dtor(&intern->it->iterator TSRMLS_CC);
418+
intern->it = NULL;
419+
}
367420
zend_object_std_dtor(&intern->std TSRMLS_CC);
368421

369422
efree(intern);

tests/functional/cursor-002.phpt

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
--TEST--
2+
Sorting single field, ascending, using the Cursor Iterator
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc" ?>
5+
<?php require "tests/utils/fixtures-users.inc" ?>
6+
--FILE--
7+
<?php
8+
require_once "tests/utils/basic.inc";
9+
10+
$manager = new MongoDB\Driver\Manager(MONGODB_URI);
11+
12+
$query = new MongoDB\Driver\Query(array(), array(
13+
'projection' => array('_id' => 0, 'username' => 1),
14+
'sort' => array('username' => 1),
15+
));
16+
17+
$qr = $manager->executeQuery(NS, $query);
18+
$cursor = $qr->getIterator();
19+
20+
var_dump(
21+
$cursor->getBatchSize(),
22+
$cursor->setBatchSize(15),
23+
$cursor->getBatchSize(),
24+
$cursor->isDead()
25+
);
26+
27+
28+
29+
for($j=0, $cursor->rewind(); $cursor->valid(); $cursor->next(), $j++) {
30+
$document = $cursor->current();
31+
echo $document['username'] . "\n";
32+
}
33+
34+
var_dump(
35+
$cursor->getBatchSize(),
36+
$cursor->setBatchSize(15),
37+
$cursor->getBatchSize(),
38+
$cursor->isDead()
39+
);
40+
?>
41+
===DONE===
42+
<?php exit(0); ?>
43+
--EXPECT--
44+
int(0)
45+
NULL
46+
int(15)
47+
bool(true)
48+
abernathy.audrey
49+
alda.murray
50+
andreanne.steuber
51+
armstrong.gordon
52+
aschuppe
53+
ashton.o'kon
54+
balistreri.donald
55+
bartell.susie
56+
beahan.oleta
57+
bergnaum.roberto
58+
camilla20
59+
cartwright.garland
60+
chance.conroy
61+
crona.jaclyn
62+
cronin.clint
63+
dereck.ward
64+
dhudson
65+
doyle.nelle
66+
dwolf
67+
ebert.cordie
68+
ebony59
69+
edibbert
70+
eldora.steuber
71+
eliseo49
72+
ernser.addison
73+
ervin.carter
74+
farrell.asha
75+
ford85
76+
fosinski
77+
german.leffler
78+
gladyce88
79+
haley.krystel
80+
hartmann.dedrick
81+
helene.o'connell
82+
isac13
83+
jailyn62
84+
jamaal.cassin
85+
janelle93
86+
javier.volkman
87+
javier13
88+
jazmyne63
89+
jessika.schmeler
90+
jessy16
91+
jfeest
92+
jschinner
93+
justina63
94+
kattie12
95+
keebler.rupert
96+
kelvin.jakubowski
97+
khills
98+
koch.sophia
99+
lmckenzie
100+
lonnie.little
101+
lonnie10
102+
lubowitz.colleen
103+
lucio20
104+
mante.ashlee
105+
marietta.swift
106+
marlene95
107+
mraz.marcelina
108+
myra43
109+
nayeli.vandervort
110+
ngoldner
111+
npurdy
112+
o'conner.arthur
113+
odell96
114+
ohowe
115+
orn.katelyn
116+
pacocha.quentin
117+
qkunze
118+
qschiller
119+
rau.brent
120+
rodger.raynor
121+
sawayn.catharine
122+
shyann28
123+
spencer.darrel
124+
ssteuber
125+
swyman
126+
tabitha.mohr
127+
talon74
128+
thalia22
129+
von.britney
130+
vrolfson
131+
vwaters
132+
walker.alec
133+
walter.lester
134+
west.jude
135+
wisozk.cortez
136+
witting.chris
137+
wschaefer
138+
wschimmel
139+
wwilkinson
140+
xgibson
141+
yasmin55
142+
yhudson
143+
yost.ari
144+
yost.magali
145+
ypredovic
146+
ywyman
147+
zstanton
148+
int(15)
149+
NULL
150+
int(15)
151+
bool(true)
152+
===DONE===

0 commit comments

Comments
 (0)