void HHVM_METHOD(MongoDBDriverReadPreference, _setReadPreferenceTags, const Array &tagSets) { MongoDBDriverReadPreferenceData* data = Native::data<MongoDBDriverReadPreferenceData>(this_); bson_t *bson; /* Check validity */ if (!hippo_mongo_driver_readpreference_are_valid(tagSets)) { throw MongoDriver::Utils::throwInvalidArgumentException("tagSets must be an array of zero or more documents"); } /* Validate that readPreferenceTags are not used with PRIMARY readPreference */ if (mongoc_read_prefs_get_mode(data->m_read_preference) == MONGOC_READ_PRIMARY) { throw MongoDriver::Utils::throwInvalidArgumentException("tagSets may not be used with primary mode"); } /* Convert argument */ VariantToBsonConverter converter(tagSets, HIPPO_BSON_NO_FLAGS); bson = bson_new(); converter.convert(bson); /* Set and check errors */ mongoc_read_prefs_set_tags(data->m_read_preference, bson); bson_destroy(bson); if (!mongoc_read_prefs_is_valid(data->m_read_preference)) { /* Throw exception */ throw MongoDriver::Utils::throwInvalidArgumentException("Read preference is not valid"); } }
/* {{{ proto MongoDB\Driver\ReadPreference ReadPreference::__construct(integer $readPreference[, array $tagSets = array()]) Constructs a new ReadPreference */ PHP_METHOD(ReadPreference, __construct) { php_phongo_readpreference_t *intern; zend_error_handling error_handling; long readPreference; zval *tagSets = NULL; (void)return_value_ptr; (void)return_value; (void)return_value_used; zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC); intern = (php_phongo_readpreference_t *)zend_object_store_get_object(getThis() TSRMLS_CC); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|a!", &readPreference, &tagSets) == FAILURE) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } zend_restore_error_handling(&error_handling TSRMLS_CC); switch(readPreference) { case MONGOC_READ_PRIMARY: case MONGOC_READ_SECONDARY: case MONGOC_READ_PRIMARY_PREFERRED: case MONGOC_READ_SECONDARY_PREFERRED: case MONGOC_READ_NEAREST: intern->read_preference = mongoc_read_prefs_new(readPreference); if (tagSets) { bson_t *tags = bson_new(); zval_to_bson(tagSets, PHONGO_BSON_NONE, (bson_t *)tags, NULL TSRMLS_CC); mongoc_read_prefs_set_tags(intern->read_preference, tags); bson_destroy(tags); if (!mongoc_read_prefs_is_valid(intern->read_preference)) { phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s", "Invalid tagSet"); return; } } break; default: phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s", "Invalid ReadPreference"); return; } }
mongoc_client_t * mongoc_client_new (const char *uri_string) { const mongoc_write_concern_t *write_concern; mongoc_client_t *client; const bson_t *read_prefs_tags; mongoc_uri_t *uri; const bson_t *options; bson_iter_t iter; bool has_ssl = false; bool slave_okay = false; if (!uri_string) { uri_string = "mongodb://127.0.0.1/"; } if (!(uri = mongoc_uri_new(uri_string))) { return NULL; } options = mongoc_uri_get_options (uri); if (bson_iter_init_find (&iter, options, "ssl") && BSON_ITER_HOLDS_BOOL (&iter) && bson_iter_bool (&iter)) { has_ssl = true; } if (bson_iter_init_find_case (&iter, options, "slaveok") && BSON_ITER_HOLDS_BOOL (&iter) && bson_iter_bool (&iter)) { slave_okay = true; } client = bson_malloc0(sizeof *client); client->uri = uri; client->request_id = rand (); client->initiator = mongoc_client_default_stream_initiator; client->initiator_data = client; write_concern = mongoc_uri_get_write_concern (uri); client->write_concern = mongoc_write_concern_copy (write_concern); if (slave_okay) { client->read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY_PREFERRED); } else { client->read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY); } read_prefs_tags = mongoc_uri_get_read_prefs (client->uri); if (!bson_empty (read_prefs_tags)) { mongoc_read_prefs_set_tags (client->read_prefs, read_prefs_tags); } _mongoc_cluster_init (&client->cluster, client->uri, client); #ifdef MONGOC_ENABLE_SSL if (has_ssl) { mongoc_client_set_ssl_opts (client, mongoc_ssl_opt_get_default ()); } #endif mongoc_counter_clients_active_inc (); return client; }
static bool hippo_mongo_driver_manager_apply_rp(mongoc_uri_t *uri, const Array options) { mongoc_read_prefs_t *new_rp; const mongoc_read_prefs_t *old_rp; const char *rp_str = NULL; bson_t *b_tags; if (!(old_rp = mongoc_uri_get_read_prefs_t(uri))) { throw MongoDriver::Utils::throwRunTimeException("mongoc_uri_t does not have a read preference"); return false; } if (options.size() == 0) { return true; } if ( !options.exists(s_MongoDBDriverManager_slaveok) && !options.exists(s_MongoDBDriverManager_readpreference) && !options.exists(s_MongoDBDriverManager_readpreferencetags) && !options.exists(s_MongoDBDriverManager_readPreference) && !options.exists(s_MongoDBDriverManager_readPreferenceTags) ) { return true; } new_rp = mongoc_read_prefs_copy(old_rp); if (options.exists(s_MongoDBDriverManager_slaveok) && options[s_MongoDBDriverManager_slaveok].isBoolean()) { mongoc_read_prefs_set_mode(new_rp, MONGOC_READ_SECONDARY_PREFERRED); } if (options.exists(s_MongoDBDriverManager_readpreference) && options[s_MongoDBDriverManager_readpreference].isString()) { rp_str = options[s_MongoDBDriverManager_readpreference].toString().c_str(); } if (options.exists(s_MongoDBDriverManager_readPreference) && options[s_MongoDBDriverManager_readPreference].isString()) { rp_str = options[s_MongoDBDriverManager_readPreference].toString().c_str(); } if (rp_str) { if (0 == strcasecmp("primary", rp_str)) { mongoc_read_prefs_set_mode(new_rp, MONGOC_READ_PRIMARY); } else if (0 == strcasecmp("primarypreferred", rp_str)) { mongoc_read_prefs_set_mode(new_rp, MONGOC_READ_PRIMARY_PREFERRED); } else if (0 == strcasecmp("secondary", rp_str)) { mongoc_read_prefs_set_mode(new_rp, MONGOC_READ_SECONDARY); } else if (0 == strcasecmp("secondarypreferred", rp_str)) { mongoc_read_prefs_set_mode(new_rp, MONGOC_READ_SECONDARY_PREFERRED); } else if (0 == strcasecmp("nearest", rp_str)) { mongoc_read_prefs_set_mode(new_rp, MONGOC_READ_NEAREST); } else { throw MongoDriver::Utils::throwInvalidArgumentException("Unsupported readPreference value: '" + Variant(rp_str).toString() + "'"); mongoc_read_prefs_destroy(new_rp); return false; } } if (options.exists(s_MongoDBDriverManager_readpreferencetags) && options[s_MongoDBDriverManager_readpreferencetags].isArray()) { VariantToBsonConverter converter(options[s_MongoDBDriverManager_readpreferencetags].toArray(), HIPPO_BSON_NO_FLAGS); b_tags = bson_new(); converter.convert(b_tags); mongoc_read_prefs_set_tags(new_rp, b_tags); } else if (options.exists(s_MongoDBDriverManager_readPreferenceTags) && options[s_MongoDBDriverManager_readPreferenceTags].isArray()) { VariantToBsonConverter converter(options[s_MongoDBDriverManager_readPreferenceTags].toArray(), HIPPO_BSON_NO_FLAGS); b_tags = bson_new(); converter.convert(b_tags); mongoc_read_prefs_set_tags(new_rp, b_tags); } if ( mongoc_read_prefs_get_mode(new_rp) == MONGOC_READ_PRIMARY && !bson_empty(mongoc_read_prefs_get_tags(new_rp)) ) { throw MongoDriver::Utils::throwInvalidArgumentException("Primary read preference mode conflicts with tags"); mongoc_read_prefs_destroy(new_rp); return false; } /* This may be redundant in light of the last check (primary with tags), * but we'll check anyway in case additional validation is implemented. */ if (!mongoc_read_prefs_is_valid(new_rp)) { throw MongoDriver::Utils::throwInvalidArgumentException("Read preference is not valid"); mongoc_read_prefs_destroy(new_rp); return false; } mongoc_uri_set_read_prefs_t(uri, new_rp); mongoc_read_prefs_destroy(new_rp); return true; }