Skip to content

Commit 865297f

Browse files
committed
Smiplify cursor/results to prep for PHPC-116
1 parent 30558f2 commit 865297f

File tree

3 files changed

+57
-69
lines changed

3 files changed

+57
-69
lines changed

php_phongo.c

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,10 +1057,7 @@ void php_phongo_cursor_new_from_result(zval *object, php_phongo_result_t *result
10571057
object_init_ex(object, php_phongo_cursor_ce);
10581058

10591059
intern = (php_phongo_cursor_t *)zend_object_store_get_object(object TSRMLS_CC);
1060-
intern->cursor = result->cursor;
1061-
intern->is_command_cursor = result->is_command_cursor;
1062-
intern->firstBatch = result->firstBatch;
1063-
intern->hint = result->hint;
1060+
intern->result = result;
10641061
} /* }}} */
10651062

10661063
void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS_DC) /* {{{ */
@@ -1169,48 +1166,58 @@ void php_phongo_result_free(php_phongo_result_t *result)
11691166
/* {{{ Iterator */
11701167
typedef struct {
11711168
zend_object_iterator iterator;
1172-
mongoc_cursor_t *cursor;
1173-
bson_t *firstBatch;
11741169
bson_iter_t first_batch_iter;
1175-
int hint;
1176-
zend_bool is_command_cursor;
1177-
php_phongo_bson_state visitor_data;
11781170
} phongo_cursor_it;
11791171

1180-
static void phongo_cursor_it_invalidate_current(zend_object_iterator *iter TSRMLS_DC)
1172+
static void phongo_cursor_it_invalidate_current(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
11811173
{
1182-
phongo_cursor_it *cursor_it = (phongo_cursor_it *)iter;
1174+
php_phongo_result_t *result = NULL;
11831175

1184-
if (cursor_it->visitor_data.zchild) {
1185-
zval_ptr_dtor(&cursor_it->visitor_data.zchild);
1186-
cursor_it->visitor_data.zchild = NULL;
1176+
result = ((phongo_cursor_it *)iter)->iterator.data;
1177+
if (result->visitor_data.zchild) {
1178+
zval_ptr_dtor(&result->visitor_data.zchild);
1179+
result->visitor_data.zchild = NULL;
11871180
}
1188-
}
1181+
} /* }}} */
1182+
11891183
static void phongo_cursor_it_dtor(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
11901184
{
11911185
phongo_cursor_it *cursor_it = (phongo_cursor_it *)iter;
11921186

11931187
iter->funcs->invalidate_current(iter TSRMLS_CC);
11941188

1195-
zval_ptr_dtor((zval**)&cursor_it->iterator.data);
11961189
efree(cursor_it);
11971190
} /* }}} */
11981191

11991192
static int phongo_cursor_it_valid(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
12001193
{
1201-
return ((phongo_cursor_it *)iter)->visitor_data.zchild ? SUCCESS : FAILURE;
1194+
php_phongo_result_t *result = NULL;
1195+
1196+
result = ((phongo_cursor_it *)iter)->iterator.data;
1197+
1198+
if (result->visitor_data.zchild) {
1199+
return SUCCESS;
1200+
}
1201+
1202+
return FAILURE;
12021203
} /* }}} */
12031204

12041205
static void phongo_cursor_it_get_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC) /* {{{ */
12051206
{
1206-
*data = &((phongo_cursor_it *)iter)->visitor_data.zchild;
1207+
php_phongo_result_t *result = NULL;
1208+
1209+
result = ((phongo_cursor_it *)iter)->iterator.data;
1210+
1211+
*data = &result->visitor_data.zchild;
12071212
} /* }}} */
12081213

12091214
static void phongo_cursor_it_move_forward(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
12101215
{
1216+
php_phongo_result_t *result = NULL;
12111217
phongo_cursor_it *cursor_it = (phongo_cursor_it *)iter;
12121218
const bson_t *doc;
12131219

1220+
result = ((phongo_cursor_it *)iter)->iterator.data;
12141221
iter->funcs->invalidate_current(iter TSRMLS_CC);
12151222

12161223
iter->index++;
@@ -1221,14 +1228,14 @@ static void phongo_cursor_it_move_forward(zend_object_iterator *iter TSRMLS_DC)
12211228

12221229
bson_iter_document(&cursor_it->first_batch_iter, &data_len, &data);
12231230

1224-
MAKE_STD_ZVAL(cursor_it->visitor_data.zchild);
1225-
bson_to_zval(data, data_len, &cursor_it->visitor_data);
1231+
MAKE_STD_ZVAL(result->visitor_data.zchild);
1232+
bson_to_zval(data, data_len, &result->visitor_data);
12261233
return;
12271234
}
12281235
}
1229-
if (mongoc_cursor_next(cursor_it->cursor, &doc)) {
1230-
MAKE_STD_ZVAL(cursor_it->visitor_data.zchild);
1231-
bson_to_zval(bson_get_data(doc), doc->len, &cursor_it->visitor_data);
1236+
if (mongoc_cursor_next(result->cursor, &doc)) {
1237+
MAKE_STD_ZVAL(result->visitor_data.zchild);
1238+
bson_to_zval(bson_get_data(doc), doc->len, &result->visitor_data);
12321239
} else {
12331240
iter->funcs->invalidate_current(iter TSRMLS_CC);
12341241
}
@@ -1237,14 +1244,17 @@ static void phongo_cursor_it_move_forward(zend_object_iterator *iter TSRMLS_DC)
12371244

12381245
static void phongo_cursor_it_rewind(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
12391246
{
1240-
phongo_cursor_it *cursor_it = (phongo_cursor_it *)iter;
1247+
php_phongo_result_t *result = NULL;
1248+
phongo_cursor_it *cursor_it = (phongo_cursor_it *)iter;
1249+
1250+
result = ((phongo_cursor_it *)iter)->iterator.data;
12411251

12421252
iter->funcs->invalidate_current(iter TSRMLS_CC);
12431253

12441254
/* firstBatch is empty when the query simply didn't return any results */
1245-
if (cursor_it->firstBatch) {
1246-
if (cursor_it->is_command_cursor) {
1247-
if (!bson_iter_init(&cursor_it->first_batch_iter, cursor_it->firstBatch)) {
1255+
if (result->firstBatch) {
1256+
if (result->is_command_cursor) {
1257+
if (!bson_iter_init(&cursor_it->first_batch_iter, result->firstBatch)) {
12481258
return;
12491259
}
12501260
if (bson_iter_next (&cursor_it->first_batch_iter)) {
@@ -1253,13 +1263,13 @@ static void phongo_cursor_it_rewind(zend_object_iterator *iter TSRMLS_DC) /* {{{
12531263
uint32_t data_len = 0;
12541264

12551265
bson_iter_document(&cursor_it->first_batch_iter, &data_len, &data);
1256-
MAKE_STD_ZVAL(cursor_it->visitor_data.zchild);
1257-
bson_to_zval(data, data_len, &cursor_it->visitor_data);
1266+
MAKE_STD_ZVAL(result->visitor_data.zchild);
1267+
bson_to_zval(data, data_len, &result->visitor_data);
12581268
}
12591269
}
12601270
} else {
1261-
MAKE_STD_ZVAL(cursor_it->visitor_data.zchild);
1262-
bson_to_zval(bson_get_data(cursor_it->firstBatch), cursor_it->firstBatch->len, &cursor_it->visitor_data);
1271+
MAKE_STD_ZVAL(result->visitor_data.zchild);
1272+
bson_to_zval(bson_get_data(result->firstBatch), result->firstBatch->len, &result->visitor_data);
12631273
}
12641274
}
12651275
} /* }}} */
@@ -1296,30 +1306,25 @@ zend_object_iterator *phongo_cursor_get_iterator(zend_class_entry *ce, zval *obj
12961306

12971307
cursor_it = ecalloc(1, sizeof(phongo_cursor_it));
12981308

1299-
Z_ADDREF_P(object);
1300-
cursor_it->iterator.data = (void*)object;
1309+
cursor_it->iterator.data = intern->result;
13011310
cursor_it->iterator.funcs = &phongo_cursor_it_funcs;
1302-
cursor_it->cursor = intern->cursor;
1303-
cursor_it->is_command_cursor = intern->is_command_cursor;
1304-
cursor_it->firstBatch = intern->firstBatch;
1305-
cursor_it->hint = intern->hint;
13061311

13071312
return (zend_object_iterator*)cursor_it;
13081313
} /* }}} */
13091314
zend_object_iterator *phongo_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC) /* {{{ */
13101315
{
1311-
php_phongo_result_t *intern = (php_phongo_result_t *)zend_object_store_get_object(object TSRMLS_CC);
1316+
php_phongo_result_t *result = (php_phongo_result_t *)zend_object_store_get_object(object TSRMLS_CC);
13121317

13131318
if (by_ref) {
13141319
zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
13151320
}
13161321

13171322
/* If we have a custom iterator */
1318-
if (intern->ce_get_iterator != NULL) {
1323+
if (result->ce_get_iterator != NULL) {
13191324
zend_user_iterator *iterator = NULL;
13201325

13211326
zval_ptr_dtor(&object);
1322-
object_init_ex(object, intern->ce_get_iterator);
1327+
object_init_ex(object, result->ce_get_iterator);
13231328

13241329
iterator = emalloc(sizeof(zend_user_iterator));
13251330

@@ -1334,18 +1339,12 @@ zend_object_iterator *phongo_result_get_iterator(zend_class_entry *ce, zval *obj
13341339

13351340
cursor_it = ecalloc(1, sizeof(phongo_cursor_it));
13361341

1337-
if (cursor_it->visitor_data.zchild) {
1338-
zval_ptr_dtor(&cursor_it->visitor_data.zchild);
1339-
cursor_it->visitor_data.zchild = NULL;
1342+
if (result->visitor_data.zchild) {
1343+
zval_ptr_dtor(&result->visitor_data.zchild);
1344+
result->visitor_data.zchild = NULL;
13401345
}
1341-
Z_ADDREF_P(object);
1342-
cursor_it->iterator.data = (void*)object;
1346+
cursor_it->iterator.data = (void*)result;
13431347
cursor_it->iterator.funcs = &phongo_cursor_it_funcs;
1344-
cursor_it->cursor = intern->cursor;
1345-
cursor_it->firstBatch = intern->firstBatch;
1346-
cursor_it->hint = intern->hint;
1347-
cursor_it->is_command_cursor = intern->is_command_cursor;
1348-
cursor_it->visitor_data = intern->visitor_data;
13491348

13501349
return (zend_object_iterator*)cursor_it;
13511350
}

php_phongo_classes.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ typedef struct {
4747

4848
typedef struct {
4949
zend_object std;
50-
zend_class_entry *ce_get_iterator;
5150
mongoc_cursor_t *cursor;
5251
bson_t *firstBatch;
5352
int hint;
5453
zend_bool is_command_cursor;
54+
zend_class_entry *ce_get_iterator;
5555
php_phongo_bson_state visitor_data;
5656
} php_phongo_result_t;
5757

@@ -61,10 +61,7 @@ typedef struct {
6161

6262
typedef struct {
6363
zend_object std;
64-
mongoc_cursor_t *cursor;
65-
bson_t *firstBatch;
66-
int hint;
67-
zend_bool is_command_cursor;
64+
php_phongo_result_t *result;
6865
} php_phongo_cursor_t;
6966

7067
typedef struct {

src/MongoDB/Cursor.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ PHP_METHOD(Cursor, getId)
8484
}
8585
zend_restore_error_handling(&error_handling TSRMLS_CC);
8686

87-
php_phongo_cursor_id_new_from_id(return_value, mongoc_cursor_get_id(intern->cursor) TSRMLS_CC);
87+
php_phongo_cursor_id_new_from_id(return_value, mongoc_cursor_get_id(intern->result->cursor) TSRMLS_CC);
8888
}
8989
/* }}} */
9090
/* {{{ proto MongoDB\Driver\Server Cursor::getServer()
@@ -123,7 +123,7 @@ PHP_METHOD(Cursor, isDead)
123123
}
124124
zend_restore_error_handling(&error_handling TSRMLS_CC);
125125

126-
RETURN_BOOL(!mongoc_cursor_is_alive(intern->cursor));
126+
RETURN_BOOL(!mongoc_cursor_is_alive(intern->result->cursor));
127127
}
128128
/* }}} */
129129
/* {{{ proto void Cursor::kill()
@@ -144,8 +144,8 @@ PHP_METHOD(Cursor, kill)
144144
}
145145
zend_restore_error_handling(&error_handling TSRMLS_CC);
146146

147-
hint = mongoc_cursor_get_hint(intern->cursor);
148-
mongoc_client_kill_cursor(intern->cursor->client, mongoc_cursor_get_id(intern->cursor));
147+
hint = mongoc_cursor_get_hint(intern->result->cursor);
148+
mongoc_client_kill_cursor(intern->result->cursor->client, mongoc_cursor_get_id(intern->result->cursor));
149149
}
150150
/* }}} */
151151
/* {{{ proto boolean Cursor::setBatchSize(integer $batchSize)
@@ -166,7 +166,7 @@ PHP_METHOD(Cursor, setBatchSize)
166166
}
167167
zend_restore_error_handling(&error_handling TSRMLS_CC);
168168

169-
mongoc_cursor_set_batch_size(intern->cursor, batchSize);
169+
mongoc_cursor_set_batch_size(intern->result->cursor, batchSize);
170170
}
171171
/* }}} */
172172
/* {{{ proto boolean Cursor::getBatchSize()
@@ -186,7 +186,7 @@ PHP_METHOD(Cursor, getBatchSize)
186186
}
187187
zend_restore_error_handling(&error_handling TSRMLS_CC);
188188

189-
RETURN_LONG(mongoc_cursor_get_batch_size(intern->cursor));
189+
RETURN_LONG(mongoc_cursor_get_batch_size(intern->result->cursor));
190190
}
191191
/* }}} */
192192
/* {{{ proto mixed Cursor::current()
@@ -366,14 +366,6 @@ static void php_phongo_cursor_free_object(void *object TSRMLS_DC) /* {{{ */
366366

367367
zend_object_std_dtor(&intern->std TSRMLS_CC);
368368

369-
if (intern->firstBatch) {
370-
/* FIXME: ? */
371-
//bson_clear(&intern->firstBatch);
372-
}
373-
if (intern->cursor) {
374-
//mongoc_cursor_destroy(intern->cursor);
375-
}
376-
377369
efree(intern);
378370
} /* }}} */
379371

0 commit comments

Comments
 (0)