Skip to content

Commit 71831a1

Browse files
committed
Merge pull request #291
2 parents 888211c + ac19194 commit 71831a1

File tree

4 files changed

+193
-111
lines changed

4 files changed

+193
-111
lines changed

phongo_compat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
# define ZEND_HASH_APPLY_COUNT(ht) (ht)->u.v.nApplyCount
155155
# define PHONGO_RETVAL_STRINGL(s, slen) RETVAL_STRINGL(s, slen)
156156
# define PHONGO_RETURN_STRINGL(s, slen) RETURN_STRINGL(s, slen)
157+
# define PHONGO_RETVAL_STRING(s) RETVAL_STRING(s)
157158
# define PHONGO_RETURN_STRING(s) RETURN_STRING(s)
158159
#else
159160
# define phongo_char char
@@ -186,6 +187,7 @@
186187
# define ZEND_HASH_APPLY_COUNT(ht) (ht)->nApplyCount
187188
# define PHONGO_RETVAL_STRINGL(s, slen) RETVAL_STRINGL(s, slen, 1)
188189
# define PHONGO_RETURN_STRINGL(s, slen) RETURN_STRINGL(s, slen, 1)
190+
# define PHONGO_RETVAL_STRING(s) RETVAL_STRING(s, 1)
189191
# define PHONGO_RETURN_STRING(s) RETURN_STRING(s, 1)
190192
# define PHP_STREAM_CONTEXT(stream) ((php_stream_context*) (stream)->context)
191193
#endif

php_phongo.c

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ ZEND_DECLARE_MODULE_GLOBALS(mongodb)
8181
#endif
8282
#endif
8383

84+
php_phongo_server_description_type_map_t
85+
php_phongo_server_description_type_map[PHONGO_SERVER_DESCRIPTION_TYPES] = {
86+
{ PHONGO_SERVER_UNKNOWN, "Unknown" },
87+
{ PHONGO_SERVER_STANDALONE, "Standalone" },
88+
{ PHONGO_SERVER_MONGOS, "Mongos" },
89+
{ PHONGO_SERVER_POSSIBLE_PRIMARY, "PossiblePrimary" },
90+
{ PHONGO_SERVER_RS_PRIMARY, "RSPrimary" },
91+
{ PHONGO_SERVER_RS_SECONDARY, "RSSecondary" },
92+
{ PHONGO_SERVER_RS_ARBITER, "RSArbiter" },
93+
{ PHONGO_SERVER_RS_OTHER, "RSOther" },
94+
{ PHONGO_SERVER_RS_GHOST, "RSGhost" },
95+
};
96+
8497
/* {{{ phongo_std_object_handlers */
8598
zend_object_handlers phongo_std_object_handlers;
8699

@@ -1275,55 +1288,71 @@ void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS
12751288
bson_oid_to_string(oid, intern->oid);
12761289
} /* }}} */
12771290

1278-
void php_phongo_server_to_zval(zval *retval, const mongoc_server_description_t *sd) /* {{{ */
1291+
php_phongo_server_description_type_t php_phongo_server_description_type(mongoc_server_description_t *sd)
12791292
{
1280-
array_init(retval);
1281-
1282-
ADD_ASSOC_STRING(retval, "host", (char *)sd->host.host);
1283-
ADD_ASSOC_LONG_EX(retval, "port", sd->host.port);
1284-
ADD_ASSOC_LONG_EX(retval, "type", sd->type);
1285-
ADD_ASSOC_BOOL_EX(retval, "is_primary", sd->type == MONGOC_SERVER_RS_PRIMARY);
1286-
ADD_ASSOC_BOOL_EX(retval, "is_secondary", sd->type == MONGOC_SERVER_RS_SECONDARY);
1287-
ADD_ASSOC_BOOL_EX(retval, "is_arbiter", sd->type == MONGOC_SERVER_RS_ARBITER);
1288-
{
1289-
bson_iter_t iter;
1290-
zend_bool b = bson_iter_init_find_case(&iter, &sd->last_is_master, "hidden") && bson_iter_as_bool(&iter);
1293+
const char* name = mongoc_server_description_type(sd);
1294+
int i;
12911295

1292-
ADD_ASSOC_BOOL_EX(retval, "is_hidden", b);
1296+
for (i = 0; i < PHONGO_SERVER_DESCRIPTION_TYPES; i++) {
1297+
if (!strcmp(name, php_phongo_server_description_type_map[i].name)) {
1298+
return php_phongo_server_description_type_map[i].type;
1299+
}
12931300
}
1294-
{
1295-
bson_iter_t iter;
1296-
zend_bool b = bson_iter_init_find_case(&iter, &sd->last_is_master, "passive") && bson_iter_as_bool(&iter);
12971301

1298-
ADD_ASSOC_BOOL_EX(retval, "is_passive", b);
1299-
}
1300-
if (sd->tags.len) {
1302+
return PHONGO_SERVER_UNKNOWN;
1303+
}
1304+
1305+
void php_phongo_server_to_zval(zval *retval, mongoc_server_description_t *sd) /* {{{ */
1306+
{
1307+
mongoc_host_list_t *host = mongoc_server_description_host(sd);
1308+
const bson_t *is_master = mongoc_server_description_ismaster(sd);
1309+
bson_iter_t iter;
1310+
1311+
array_init(retval);
1312+
1313+
ADD_ASSOC_STRING(retval, "host", host->host);
1314+
ADD_ASSOC_LONG_EX(retval, "port", host->port);
1315+
ADD_ASSOC_LONG_EX(retval, "type", php_phongo_server_description_type(sd));
1316+
ADD_ASSOC_BOOL_EX(retval, "is_primary", !strcmp(mongoc_server_description_type(sd), php_phongo_server_description_type_map[PHONGO_SERVER_RS_PRIMARY].name));
1317+
ADD_ASSOC_BOOL_EX(retval, "is_secondary", !strcmp(mongoc_server_description_type(sd), php_phongo_server_description_type_map[PHONGO_SERVER_RS_SECONDARY].name));
1318+
ADD_ASSOC_BOOL_EX(retval, "is_arbiter", !strcmp(mongoc_server_description_type(sd), php_phongo_server_description_type_map[PHONGO_SERVER_RS_ARBITER].name));
1319+
ADD_ASSOC_BOOL_EX(retval, "is_hidden", bson_iter_init_find_case(&iter, is_master, "hidden") && bson_iter_as_bool(&iter));
1320+
ADD_ASSOC_BOOL_EX(retval, "is_passive", bson_iter_init_find_case(&iter, is_master, "passive") && bson_iter_as_bool(&iter));
1321+
1322+
if (bson_iter_init_find(&iter, is_master, "tags") && BSON_ITER_HOLDS_DOCUMENT(&iter)) {
1323+
const uint8_t *bytes;
1324+
uint32_t len;
13011325
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
1326+
13021327
/* Use native arrays for debugging output */
13031328
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
13041329
state.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
13051330

1306-
phongo_bson_to_zval_ex(bson_get_data(&sd->tags), sd->tags.len, &state);
1331+
bson_iter_document(&iter, &len, &bytes);
1332+
phongo_bson_to_zval_ex(bytes, len, &state);
1333+
13071334
#if PHP_VERSION_ID >= 70000
13081335
ADD_ASSOC_ZVAL_EX(retval, "tags", &state.zchild);
13091336
#else
13101337
ADD_ASSOC_ZVAL_EX(retval, "tags", state.zchild);
13111338
#endif
13121339
}
1340+
13131341
{
13141342
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
13151343
/* Use native arrays for debugging output */
13161344
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
13171345
state.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
13181346

1319-
phongo_bson_to_zval_ex(bson_get_data(&sd->last_is_master), sd->last_is_master.len, &state);
1347+
phongo_bson_to_zval_ex(bson_get_data(is_master), is_master->len, &state);
1348+
13201349
#if PHP_VERSION_ID >= 70000
13211350
ADD_ASSOC_ZVAL_EX(retval, "last_is_master", &state.zchild);
13221351
#else
13231352
ADD_ASSOC_ZVAL_EX(retval, "last_is_master", state.zchild);
13241353
#endif
13251354
}
1326-
ADD_ASSOC_LONG_EX(retval, "round_trip_time", sd->round_trip_time);
1355+
ADD_ASSOC_LONG_EX(retval, "round_trip_time", (phongo_long) mongoc_server_description_round_trip_time(sd));
13271356

13281357
} /* }}} */
13291358

php_phongo.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ ZEND_END_MODULE_GLOBALS(mongodb)
7070

7171
#include "php_phongo_classes.h"
7272

73+
/* This enum is necessary since mongoc_server_description_type_t is private and
74+
* we need to translate strings returned by mongoc_server_description_type() to
75+
* Server integer constants. */
76+
typedef enum {
77+
PHONGO_SERVER_UNKNOWN = 0,
78+
PHONGO_SERVER_STANDALONE = 1,
79+
PHONGO_SERVER_MONGOS = 2,
80+
PHONGO_SERVER_POSSIBLE_PRIMARY = 3,
81+
PHONGO_SERVER_RS_PRIMARY = 4,
82+
PHONGO_SERVER_RS_SECONDARY = 5,
83+
PHONGO_SERVER_RS_ARBITER = 6,
84+
PHONGO_SERVER_RS_OTHER = 7,
85+
PHONGO_SERVER_RS_GHOST = 8,
86+
PHONGO_SERVER_DESCRIPTION_TYPES = 9,
87+
} php_phongo_server_description_type_t;
88+
89+
typedef struct {
90+
php_phongo_server_description_type_t type;
91+
const char *name;
92+
} php_phongo_server_description_type_map_t;
93+
94+
extern php_phongo_server_description_type_map_t php_phongo_server_description_type_map[];
95+
7396
typedef enum {
7497
PHONGO_ERROR_INVALID_ARGUMENT = 1,
7598
PHONGO_ERROR_RUNTIME = 2,
@@ -130,7 +153,9 @@ const mongoc_read_prefs_t* phongo_read_preference_from_zval(zval *zread_prefe
130153
const mongoc_write_concern_t* phongo_write_concern_from_zval (zval *zwrite_concern TSRMLS_DC);
131154
const php_phongo_query_t* phongo_query_from_zval (zval *zquery TSRMLS_DC);
132155

133-
void php_phongo_server_to_zval(zval *retval, const mongoc_server_description_t *sd);
156+
php_phongo_server_description_type_t php_phongo_server_description_type(mongoc_server_description_t *sd);
157+
158+
void php_phongo_server_to_zval(zval *retval, mongoc_server_description_t *sd);
134159
void php_phongo_read_concern_to_zval(zval *retval, const mongoc_read_concern_t *read_concern);
135160
void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t *read_prefs);
136161
void php_phongo_write_concern_to_zval(zval *retval, const mongoc_write_concern_t *write_concern);

0 commit comments

Comments
 (0)