Skip to content

Commit fb448fc

Browse files
committed
Reuse do_select_server, and set serverId instead of ReadPreference
1 parent a939e39 commit fb448fc

File tree

1 file changed

+50
-40
lines changed

1 file changed

+50
-40
lines changed

php_phongo.c

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,46 @@ static int process_read_concern(zval *option, bson_t *mongoc_opts TSRMLS_DC)
507507
return true;
508508
}
509509

510-
static int process_read_preference(zval *option, zval **zreadPreference TSRMLS_DC)
510+
static int phongo_do_select_server(mongoc_client_t *client, bson_t *opts, zval *zreadPreference, int server_id TSRMLS_DC)
511+
{
512+
bson_error_t error;
513+
uint32_t selected_server_id;
514+
515+
if (server_id > 0) {
516+
bson_append_int32(opts, "serverId", -1, server_id);
517+
selected_server_id = server_id;
518+
} else {
519+
mongoc_server_description_t *selected_server = NULL;
520+
521+
selected_server = mongoc_client_select_server(client, false, (zreadPreference ? phongo_read_preference_from_zval(zreadPreference TSRMLS_CC) : mongoc_client_get_read_prefs(client)), &error);
522+
if (selected_server) {
523+
selected_server_id = mongoc_server_description_id(selected_server);
524+
bson_append_int32(opts, "serverId", -1, selected_server_id);
525+
mongoc_server_description_destroy(selected_server);
526+
} else {
527+
/* Check for connection related exceptions */
528+
if (!EG(exception)) {
529+
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
530+
}
531+
532+
return false;
533+
}
534+
}
535+
536+
return selected_server_id;
537+
}
538+
539+
static int process_read_preference(zval *option, bson_t *mongoc_opts, zval **zreadPreference, mongoc_client_t *client, int server_id TSRMLS_DC)
511540
{
512541
if (Z_TYPE_P(option) == IS_OBJECT && instanceof_function(Z_OBJCE_P(option), php_phongo_readpreference_ce TSRMLS_CC)) {
542+
int selected_server_id;
543+
513544
*zreadPreference = option;
545+
546+
selected_server_id = phongo_do_select_server(client, mongoc_opts, *zreadPreference, server_id TSRMLS_CC);
547+
if (!selected_server_id) {
548+
return false;
549+
}
514550
} else {
515551
phongo_throw_exception(
516552
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
@@ -522,10 +558,12 @@ static int process_read_preference(zval *option, zval **zreadPreference TSRMLS_D
522558
return true;
523559
}
524560

525-
static int process_write_concern(zval *option, zval **zwriteConcern TSRMLS_DC)
561+
static int process_write_concern(zval *option, bson_t *command_opts, zval **zwriteConcern TSRMLS_DC)
526562
{
527563
if (Z_TYPE_P(option) == IS_OBJECT && instanceof_function(Z_OBJCE_P(option), php_phongo_writeconcern_ce TSRMLS_CC)) {
528564
*zwriteConcern = option;
565+
566+
error: add write concern to command_opts too
529567
} else {
530568
phongo_throw_exception(
531569
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
@@ -537,7 +575,7 @@ static int process_write_concern(zval *option, zval **zwriteConcern TSRMLS_DC)
537575
return true;
538576
}
539577

540-
static int phongo_execute_parse_options(zval *driver_options, int flags, bson_t *mongoc_opts, zval **zreadPreference, zval **zwriteConcern TSRMLS_DC)
578+
static int phongo_execute_parse_options(mongoc_client_t* client, int server_id, zval *driver_options, int flags, bson_t *mongoc_opts, zval **zreadPreference, zval **zwriteConcern TSRMLS_DC)
541579
{
542580
if (driver_options && Z_TYPE_P(driver_options) == IS_ARRAY) {
543581
HashTable *ht_data = HASH_OF(driver_options);
@@ -561,11 +599,11 @@ static int phongo_execute_parse_options(zval *driver_options, int flags, bson_t
561599
return false;
562600
}
563601
} else if ((!strcasecmp(ZSTR_VAL(string_key), "readPreference")) && (flags & PHONGO_READPREFERENCE_ALLOWED)) {
564-
if (!process_read_preference(driver_option, zreadPreference)) {
602+
if (!process_read_preference(driver_option, mongoc_opts, zreadPreference, client, server_id)) {
565603
return false;
566604
}
567605
} else if ((!strcasecmp(ZSTR_VAL(string_key), "writeConcern")) && (flags & PHONGO_WRITECONCERN_ALLOWED)) {
568-
if (!process_write_concern(driver_option, zwriteConcern)) {
606+
if (!process_write_concern(driver_option, mongoc_opts, zwriteConcern)) {
569607
return false;
570608
}
571609
} else {
@@ -638,7 +676,7 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, php_ph
638676
/* FIXME: Legacy way of specifying the writeConcern option into this function */
639677
if (options && Z_TYPE_P(options) == IS_OBJECT && instanceof_function(Z_OBJCE_P(options), php_phongo_writeconcern_ce TSRMLS_CC)) {
640678
zwriteConcern = options;
641-
} else if (!phongo_execute_parse_options(options, PHONGO_WRITECONCERN_ALLOWED, NULL, NULL, &zwriteConcern TSRMLS_CC)) {
679+
} else if (!phongo_execute_parse_options(client, server_id, options, PHONGO_WRITECONCERN_ALLOWED, NULL, NULL, &zwriteConcern TSRMLS_CC)) {
642680
return false;
643681
}
644682

@@ -745,7 +783,7 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
745783
/* FIXME: Legacy way of specifying the readPreference option into this function */
746784
if (options && Z_TYPE_P(options) == IS_OBJECT && instanceof_function(Z_OBJCE_P(options), php_phongo_readpreference_ce TSRMLS_CC)) {
747785
zreadPreference = options;
748-
} else if (!phongo_execute_parse_options(options, PHONGO_READPREFERENCE_ALLOWED, NULL, &zreadPreference, NULL TSRMLS_CC)) {
786+
} else if (!phongo_execute_parse_options(client, server_id, options, PHONGO_READPREFERENCE_ALLOWED, NULL, &zreadPreference, NULL TSRMLS_CC)) {
749787
return false;
750788
}
751789

@@ -788,35 +826,6 @@ static bson_t *create_wrapped_command_envelope(const char *db, bson_t *reply)
788826
return tmp;
789827
}
790828

791-
static int phongo_do_select_server(mongoc_client_t *client, bson_t *opts, zval *zreadPreference, int server_id TSRMLS_DC)
792-
{
793-
bson_error_t error;
794-
uint32_t selected_server_id;
795-
796-
if (server_id > 0) {
797-
bson_append_int32(opts, "serverId", -1, server_id);
798-
selected_server_id = server_id;
799-
} else {
800-
mongoc_server_description_t *selected_server = NULL;
801-
802-
selected_server = mongoc_client_select_server(client, false, (zreadPreference ? phongo_read_preference_from_zval(zreadPreference TSRMLS_CC) : mongoc_client_get_read_prefs(client)), &error);
803-
if (selected_server) {
804-
selected_server_id = mongoc_server_description_id(selected_server);
805-
bson_append_int32(opts, "serverId", -1, selected_server_id);
806-
mongoc_server_description_destroy(selected_server);
807-
} else {
808-
/* Check for connection related exceptions */
809-
if (!EG(exception)) {
810-
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
811-
}
812-
813-
return false;
814-
}
815-
}
816-
817-
return selected_server_id;
818-
}
819-
820829
int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t type, const char *db, zval *zcommand, zval *options, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
821830
{
822831
const php_phongo_command_t *command;
@@ -836,7 +845,7 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
836845
/* FIXME: Legacy way of specifying the readPreference option into this function */
837846
if (options && Z_TYPE_P(options) == IS_OBJECT && instanceof_function(Z_OBJCE_P(options), php_phongo_readpreference_ce TSRMLS_CC)) {
838847
zreadPreference = options;
839-
} else if (!phongo_execute_parse_options(options, PHONGO_READPREFERENCE_ALLOWED, opts, &zreadPreference, NULL TSRMLS_CC)) {
848+
} else if (!phongo_execute_parse_options(client, server_id, options, PHONGO_READPREFERENCE_ALLOWED, opts, &zreadPreference, NULL TSRMLS_CC)) {
840849
return false;
841850
}
842851

@@ -860,11 +869,12 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
860869
result = mongoc_client_write_command_with_opts(client, db, command->bson, opts, &reply, &error);
861870
break;
862871
case PHONGO_COMMAND_READ_WRITE:
863-
result = mongoc_client_read_write_command_with_opts(client, db, command->bson, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC), opts, &reply, &error);
872+
/* We can pass NULL as readPreference, as this argument was added historically, but has no function */
873+
result = mongoc_client_read_write_command_with_opts(client, db, command->bson, NULL, opts, &reply, &error);
864874
break;
865875
default:
866-
/* Should never happen, but if it does: abort */
867-
assert(false);
876+
/* Should never happen, but if it does: exception */
877+
phongo_throw_exception(PHONGO_ERROR_LOGIC TSRMLS_CC, "Type '%d' should never have been passed to phongo_execute_command, please file a bug report", type);
868878
}
869879
if (!result) {
870880
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);

0 commit comments

Comments
 (0)