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"); } }
bool _mongoc_read_prefs_validate (const mongoc_read_prefs_t *read_prefs, bson_error_t *error) { if (read_prefs && !mongoc_read_prefs_is_valid (read_prefs)) { bson_set_error (error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_COMMAND_INVALID_ARG, "Invalid mongoc_read_prefs_t"); return false; } return true; }
mongoc_uri_t * mongoc_uri_new (const char *uri_string) { mongoc_uri_t *uri; #ifdef MONGOC_EXPERIMENTAL_FEATURES int32_t max_staleness_ms; #endif uri = (mongoc_uri_t *)bson_malloc0(sizeof *uri); bson_init(&uri->options); bson_init(&uri->credentials); /* Initialize read_prefs since tag parsing may add to it */ uri->read_prefs = mongoc_read_prefs_new(MONGOC_READ_PRIMARY); /* Initialize empty read_concern */ uri->read_concern = mongoc_read_concern_new (); if (!uri_string) { uri_string = "mongodb://127.0.0.1/"; } if (!mongoc_uri_parse(uri, uri_string)) { mongoc_uri_destroy(uri); return NULL; } uri->str = bson_strdup(uri_string); _mongoc_uri_assign_read_prefs_mode(uri); #ifdef MONGOC_EXPERIMENTAL_FEATURES max_staleness_ms = mongoc_uri_get_option_as_int32 (uri, "maxstalenessms", 0); mongoc_read_prefs_set_max_staleness_ms (uri->read_prefs, max_staleness_ms); #endif if (!mongoc_read_prefs_is_valid(uri->read_prefs)) { mongoc_uri_destroy(uri); return NULL; } _mongoc_uri_build_write_concern (uri); if (!mongoc_write_concern_is_valid (uri->write_concern)) { mongoc_uri_destroy(uri); return NULL; } return uri; }
void HHVM_METHOD(MongoDBDriverReadPreference, _setMaxStalenessMS, int maxStalenessMS) { MongoDBDriverReadPreferenceData* data = Native::data<MongoDBDriverReadPreferenceData>(this_); /* Validate that maxStalenessMS is not used with PRIMARY readPreference */ if (mongoc_read_prefs_get_mode(data->m_read_preference) == MONGOC_READ_PRIMARY) { throw MongoDriver::Utils::throwInvalidArgumentException("maxStalenessMS may not be used with primary mode"); } mongoc_read_prefs_set_max_staleness_ms(data->m_read_preference, maxStalenessMS); 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_uri_t * mongoc_uri_new (const char *uri_string) { mongoc_uri_t *uri; uri = (mongoc_uri_t *)bson_malloc0(sizeof *uri); bson_init(&uri->options); bson_init(&uri->credentials); /* Initialize read_prefs since tag parsing may add to it */ uri->read_prefs = mongoc_read_prefs_new(MONGOC_READ_PRIMARY); /* Initialize empty read_concern */ uri->read_concern = mongoc_read_concern_new (); if (!uri_string) { uri_string = "mongodb://127.0.0.1/"; } if (!mongoc_uri_parse(uri, uri_string)) { mongoc_uri_destroy(uri); return NULL; } uri->str = bson_strdup(uri_string); _mongoc_uri_assign_read_prefs_mode(uri); if (!mongoc_read_prefs_is_valid(uri->read_prefs)) { mongoc_uri_destroy(uri); return NULL; } _mongoc_uri_build_write_concern (uri); if (!mongoc_write_concern_is_valid (uri->write_concern)) { mongoc_uri_destroy(uri); return NULL; } return uri; }
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; }