Skip to content

Commit 78839bd

Browse files
committed
PHPC-688: Change Cursor debug handler to use libmongoc public API
This significantly alters the structure of the Cursor's debug output, since we not longer have access to private fields within mongoc_cursor_t.
1 parent 184b79b commit 78839bd

14 files changed

+595
-681
lines changed

php_phongo.c

Lines changed: 69 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ PHONGO_API zend_object_handlers *phongo_get_std_object_handlers(void)
103103
}
104104
/* }}} */
105105

106+
/* Forward declarations */
107+
static bool phongo_split_namespace(const char *namespace, char **dbname, char **cname);
108+
106109
/* {{{ Error reporting and logging */
107110
zend_class_entry* phongo_exception_from_phongo_domain(php_phongo_error_domain_t domain)
108111
{
@@ -235,7 +238,7 @@ static void php_phongo_log(mongoc_log_level_t log_level, const char *log_domain,
235238
/* }}} */
236239

237240
/* {{{ Init objects */
238-
static void phongo_cursor_init(zval *return_value, mongoc_cursor_t *cursor, mongoc_client_t *client TSRMLS_DC) /* {{{ */
241+
static void phongo_cursor_init(zval *return_value, mongoc_cursor_t *cursor, mongoc_client_t *client, zval *readPreference TSRMLS_DC) /* {{{ */
239242
{
240243
php_phongo_cursor_t *intern;
241244

@@ -245,6 +248,50 @@ static void phongo_cursor_init(zval *return_value, mongoc_cursor_t *cursor, mong
245248
intern->cursor = cursor;
246249
intern->server_id = mongoc_cursor_get_hint(cursor);
247250
intern->client = client;
251+
252+
if (readPreference) {
253+
#if PHP_VERSION_ID >= 70000
254+
ZVAL_ZVAL(&intern->read_preference, readPreference, 1, 0);
255+
#else
256+
Z_ADDREF_P(readPreference);
257+
intern->read_preference = readPreference;
258+
#endif
259+
}
260+
} /* }}} */
261+
262+
static void phongo_cursor_init_for_command(zval *return_value, mongoc_cursor_t *cursor, mongoc_client_t *client, const char *db, zval *command, zval *readPreference TSRMLS_DC) /* {{{ */
263+
{
264+
php_phongo_cursor_t *intern;
265+
266+
phongo_cursor_init(return_value, cursor, client, readPreference TSRMLS_CC);
267+
intern = Z_CURSOR_OBJ_P(return_value);
268+
269+
intern->database = estrdup(db);
270+
271+
#if PHP_VERSION_ID >= 70000
272+
ZVAL_ZVAL(&intern->command, command, 1, 0);
273+
#else
274+
Z_ADDREF_P(command);
275+
intern->command = command;
276+
#endif
277+
} /* }}} */
278+
279+
static void phongo_cursor_init_for_query(zval *return_value, mongoc_cursor_t *cursor, mongoc_client_t *client, const char *namespace, zval *query, zval *readPreference TSRMLS_DC) /* {{{ */
280+
{
281+
php_phongo_cursor_t *intern;
282+
283+
phongo_cursor_init(return_value, cursor, client, readPreference TSRMLS_CC);
284+
intern = Z_CURSOR_OBJ_P(return_value);
285+
286+
/* namespace has already been validated by phongo_execute_query() */
287+
phongo_split_namespace(namespace, &intern->database, &intern->collection);
288+
289+
#if PHP_VERSION_ID >= 70000
290+
ZVAL_ZVAL(&intern->query, query, 1, 0);
291+
#else
292+
Z_ADDREF_P(query);
293+
intern->query = query;
294+
#endif
248295
} /* }}} */
249296

250297
void phongo_server_init(zval *return_value, mongoc_client_t *client, int server_id TSRMLS_DC) /* {{{ */
@@ -495,7 +542,7 @@ php_phongo_writeresult_t *phongo_writeresult_init(zval *return_value, bson_t *re
495542

496543
/* {{{ CRUD */
497544
/* Splits a namespace name into the database and collection names, allocated with estrdup. */
498-
bool phongo_split_namespace(const char *namespace, char **dbname, char **cname) /* {{{ */
545+
static bool phongo_split_namespace(const char *namespace, char **dbname, char **cname) /* {{{ */
499546
{
500547
char *dot = strchr(namespace, '.');
501548

@@ -616,8 +663,9 @@ static bool phongo_advance_cursor_and_check_for_error(mongoc_cursor_t *cursor TS
616663
return true;
617664
}
618665

619-
int phongo_execute_query(mongoc_client_t *client, const char *namespace, const php_phongo_query_t *query, const mongoc_read_prefs_t *read_preference, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
666+
int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *zquery, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
620667
{
668+
const php_phongo_query_t *query;
621669
mongoc_cursor_t *cursor;
622670
char *dbname;
623671
char *collname;
@@ -631,11 +679,13 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, const p
631679
efree(dbname);
632680
efree(collname);
633681

682+
query = Z_QUERY_OBJ_P(zquery);
683+
634684
if (query->read_concern) {
635685
mongoc_collection_set_read_concern(collection, query->read_concern);
636686
}
637687

638-
cursor = mongoc_collection_find(collection, query->flags, query->skip, query->limit, query->batch_size, query->query, query->selector, read_preference);
688+
cursor = mongoc_collection_find(collection, query->flags, query->skip, query->limit, query->batch_size, query->query, query->selector, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC));
639689
mongoc_collection_destroy(collection);
640690

641691
/* mongoc issues a warning we need to catch somehow */
@@ -658,17 +708,19 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, const p
658708
return true;
659709
}
660710

661-
phongo_cursor_init(return_value, cursor, client TSRMLS_CC);
711+
phongo_cursor_init_for_query(return_value, cursor, client, namespace, zquery, zreadPreference TSRMLS_CC);
662712
return true;
663713
} /* }}} */
664714

665-
int phongo_execute_command(mongoc_client_t *client, const char *db, const bson_t *command, const mongoc_read_prefs_t *read_preference, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
715+
int phongo_execute_command(mongoc_client_t *client, const char *db, zval *zcommand, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
666716
{
717+
const php_phongo_command_t *command;
667718
mongoc_cursor_t *cursor;
668719
bson_iter_t iter;
669720

721+
command = Z_COMMAND_OBJ_P(zcommand);
670722

671-
cursor = mongoc_client_command(client, db, MONGOC_QUERY_NONE, 0, 1, 0, command, NULL, read_preference);
723+
cursor = mongoc_client_command(client, db, MONGOC_QUERY_NONE, 0, 1, 0, command->bson, NULL, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC));
672724

673725
if (server_id > 0 && !mongoc_cursor_set_hint(cursor, server_id)) {
674726
phongo_throw_exception(PHONGO_ERROR_MONGOC_FAILED TSRMLS_CC, "%s", "Could not set cursor server_id");
@@ -697,11 +749,11 @@ int phongo_execute_command(mongoc_client_t *client, const char *db, const bson_t
697749
return false;
698750
}
699751

700-
phongo_cursor_init(return_value, cmd_cursor, client TSRMLS_CC);
752+
phongo_cursor_init_for_command(return_value, cmd_cursor, client, db, zcommand, zreadPreference TSRMLS_CC);
701753
return true;
702754
}
703755

704-
phongo_cursor_init(return_value, cursor, client TSRMLS_CC);
756+
phongo_cursor_init_for_command(return_value, cursor, client, db, zcommand, zreadPreference TSRMLS_CC);
705757
return true;
706758
} /* }}} */
707759

@@ -1269,13 +1321,6 @@ const mongoc_read_prefs_t* phongo_read_preference_from_zval(zval *zread_preferen
12691321

12701322
return NULL;
12711323
} /* }}} */
1272-
1273-
const php_phongo_query_t* phongo_query_from_zval(zval *zquery TSRMLS_DC) /* {{{ */
1274-
{
1275-
php_phongo_query_t *intern = Z_QUERY_OBJ_P(zquery);
1276-
1277-
return intern;
1278-
} /* }}} */
12791324
/* }}} */
12801325

12811326
/* {{{ phongo zval from mongoc types */
@@ -1431,94 +1476,6 @@ void php_phongo_write_concern_to_zval(zval *retval, const mongoc_write_concern_t
14311476
ADD_ASSOC_NULL_EX(retval, "journal");
14321477
}
14331478
} /* }}} */
1434-
1435-
void php_phongo_cursor_to_zval(zval *retval, const mongoc_cursor_t *cursor) /* {{{ */
1436-
{
1437-
1438-
array_init_size(retval, 19);
1439-
1440-
ADD_ASSOC_LONG_EX(retval, "stamp", cursor->stamp);
1441-
1442-
#define _ADD_BOOL(z, field) ADD_ASSOC_BOOL_EX(z, #field, cursor->field)
1443-
_ADD_BOOL(retval, is_command);
1444-
_ADD_BOOL(retval, sent);
1445-
_ADD_BOOL(retval, done);
1446-
_ADD_BOOL(retval, end_of_event);
1447-
_ADD_BOOL(retval, in_exhaust);
1448-
_ADD_BOOL(retval, has_fields);
1449-
#undef _ADD_BOOL
1450-
1451-
/* Avoid using PHONGO_TYPEMAP_NATIVE_ARRAY for decoding query, selector,
1452-
* and current documents so that users can differentiate BSON arrays
1453-
* and documents. */
1454-
{
1455-
#if PHP_VERSION_ID >= 70000
1456-
zval zv;
1457-
#else
1458-
zval *zv;
1459-
#endif
1460-
1461-
phongo_bson_to_zval(bson_get_data(&cursor->query), cursor->query.len, &zv);
1462-
#if PHP_VERSION_ID >= 70000
1463-
ADD_ASSOC_ZVAL_EX(retval, "query", &zv);
1464-
#else
1465-
ADD_ASSOC_ZVAL_EX(retval, "query", zv);
1466-
#endif
1467-
}
1468-
{
1469-
#if PHP_VERSION_ID >= 70000
1470-
zval zv;
1471-
#else
1472-
zval *zv;
1473-
#endif
1474-
1475-
phongo_bson_to_zval(bson_get_data(&cursor->fields), cursor->fields.len, &zv);
1476-
#if PHP_VERSION_ID >= 70000
1477-
ADD_ASSOC_ZVAL_EX(retval, "fields", &zv);
1478-
#else
1479-
ADD_ASSOC_ZVAL_EX(retval, "fields", zv);
1480-
#endif
1481-
}
1482-
{
1483-
#if PHP_VERSION_ID >= 70000
1484-
zval read_preference;
1485-
1486-
php_phongo_read_preference_to_zval(&read_preference, cursor->read_prefs);
1487-
ADD_ASSOC_ZVAL_EX(retval, "read_preference", &read_preference);
1488-
#else
1489-
zval *read_preference = NULL;
1490-
MAKE_STD_ZVAL(read_preference);
1491-
php_phongo_read_preference_to_zval(read_preference, cursor->read_prefs);
1492-
ADD_ASSOC_ZVAL_EX(retval, "read_preference", read_preference);
1493-
#endif
1494-
1495-
}
1496-
1497-
#define _ADD_INT(z, field) ADD_ASSOC_LONG_EX(z, #field, cursor->field)
1498-
_ADD_INT(retval, flags);
1499-
_ADD_INT(retval, skip);
1500-
_ADD_INT(retval, limit);
1501-
_ADD_INT(retval, count);
1502-
_ADD_INT(retval, batch_size);
1503-
#undef _ADD_INT
1504-
1505-
ADD_ASSOC_STRING(retval, "ns", (char *)cursor->ns);
1506-
if (cursor->current) {
1507-
#if PHP_VERSION_ID >= 70000
1508-
zval zv;
1509-
#else
1510-
zval *zv;
1511-
#endif
1512-
1513-
phongo_bson_to_zval(bson_get_data(cursor->current), cursor->current->len, &zv);
1514-
#if PHP_VERSION_ID >= 70000
1515-
ADD_ASSOC_ZVAL_EX(retval, "current_doc", &zv);
1516-
#else
1517-
ADD_ASSOC_ZVAL_EX(retval, "current_doc", zv);
1518-
#endif
1519-
}
1520-
1521-
} /* }}} */
15221479
/* }}} */
15231480

15241481

@@ -2158,13 +2115,17 @@ static int php_phongo_cursor_iterator_valid(zend_object_iterator *iter TSRMLS_DC
21582115
#if PHP_VERSION_ID < 50500
21592116
static int php_phongo_cursor_iterator_get_current_key(zend_object_iterator *iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC) /* {{{ */
21602117
{
2161-
*int_key = (ulong) ((php_phongo_cursor_iterator *)iter)->current;
2118+
php_phongo_cursor_t *cursor = ((php_phongo_cursor_iterator *)iter)->cursor;
2119+
2120+
*int_key = (ulong) cursor->current;
21622121
return HASH_KEY_IS_LONG;
21632122
} /* }}} */
21642123
#else
21652124
static void php_phongo_cursor_iterator_get_current_key(zend_object_iterator *iter, zval *key TSRMLS_DC) /* {{{ */
21662125
{
2167-
ZVAL_LONG(key, ((php_phongo_cursor_iterator *)iter)->current);
2126+
php_phongo_cursor_t *cursor = ((php_phongo_cursor_iterator *)iter)->cursor;
2127+
2128+
ZVAL_LONG(key, cursor->current);
21682129
} /* }}} */
21692130
#endif
21702131

@@ -2191,7 +2152,7 @@ static void php_phongo_cursor_iterator_move_forward(zend_object_iterator *iter T
21912152
const bson_t *doc;
21922153

21932154
php_phongo_cursor_free_current(cursor);
2194-
cursor_it->current++;
2155+
cursor->current++;
21952156

21962157
if (mongoc_cursor_next(cursor->cursor, &doc)) {
21972158
phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &cursor->visitor_data);
@@ -2212,7 +2173,7 @@ static void php_phongo_cursor_iterator_rewind(zend_object_iterator *iter TSRMLS_
22122173
php_phongo_cursor_t *cursor = cursor_it->cursor;
22132174
const bson_t *doc;
22142175

2215-
if (cursor_it->current > 0) {
2176+
if (cursor->current > 0) {
22162177
phongo_throw_exception(PHONGO_ERROR_LOGIC TSRMLS_CC, "Cursors cannot rewind after starting iteration");
22172178
return;
22182179
}

php_phongo.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,13 @@ void phongo_writeconcern_init (zval *return_value, const
144144
bool phongo_query_init (php_phongo_query_t *query, bson_t *filter, bson_t *options TSRMLS_DC);
145145
mongoc_bulk_operation_t* phongo_bulkwrite_init (zend_bool ordered);
146146
bool phongo_execute_write (mongoc_client_t *client, const char *namespace, php_phongo_bulkwrite_t *bulk_write, const mongoc_write_concern_t *write_concern, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
147-
int phongo_execute_command (mongoc_client_t *client, const char *db, const bson_t *command, const mongoc_read_prefs_t *read_preference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
148-
int phongo_execute_query (mongoc_client_t *client, const char *namespace, const php_phongo_query_t *query, const mongoc_read_prefs_t *read_preference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
147+
int phongo_execute_command (mongoc_client_t *client, const char *db, zval *zcommand, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
148+
int phongo_execute_query (mongoc_client_t *client, const char *namespace, zval *zquery, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
149149

150150
mongoc_stream_t* phongo_stream_initiator (const mongoc_uri_t *uri, const mongoc_host_list_t *host, void *user_data, bson_error_t *error);
151151
const mongoc_read_concern_t* phongo_read_concern_from_zval (zval *zread_concern TSRMLS_DC);
152152
const mongoc_read_prefs_t* phongo_read_preference_from_zval(zval *zread_preference TSRMLS_DC);
153153
const mongoc_write_concern_t* phongo_write_concern_from_zval (zval *zwrite_concern TSRMLS_DC);
154-
const php_phongo_query_t* phongo_query_from_zval (zval *zquery TSRMLS_DC);
155154

156155
php_phongo_server_description_type_t php_phongo_server_description_type(mongoc_server_description_t *sd);
157156

php_phongo_classes.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@
130130
typedef struct {
131131
zend_object_iterator intern;
132132
php_phongo_cursor_t *cursor;
133-
long current;
134133
} php_phongo_cursor_iterator;
135134

136135

php_phongo_structs-5.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ typedef struct {
3838
} php_phongo_command_t;
3939

4040
typedef struct {
41-
zend_object std;
42-
mongoc_cursor_t *cursor;
43-
mongoc_client_t *client;
44-
int server_id;
45-
php_phongo_bson_state visitor_data;
46-
int got_iterator;
41+
zend_object std;
42+
mongoc_cursor_t *cursor;
43+
mongoc_client_t *client;
44+
int server_id;
45+
php_phongo_bson_state visitor_data;
46+
int got_iterator;
47+
long current;
48+
char *database;
49+
char *collection;
50+
zval *query;
51+
zval *command;
52+
zval *read_preference;
4753
} php_phongo_cursor_t;
4854

4955
typedef struct {

php_phongo_structs-7.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ typedef struct {
3838
} php_phongo_command_t;
3939

4040
typedef struct {
41-
mongoc_cursor_t *cursor;
42-
mongoc_client_t *client;
43-
int server_id;
44-
php_phongo_bson_state visitor_data;
45-
int got_iterator;
46-
zend_object std;
41+
mongoc_cursor_t *cursor;
42+
mongoc_client_t *client;
43+
int server_id;
44+
php_phongo_bson_state visitor_data;
45+
int got_iterator;
46+
long current;
47+
char *database;
48+
char *collection;
49+
zval query;
50+
zval command;
51+
zval read_preference;
52+
zend_object std;
4753
} php_phongo_cursor_t;
4854

4955
typedef struct {

0 commit comments

Comments
 (0)