Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPC-1021: Remove support for ReadPreference integer modes #1666

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ UPGRADE FROM 1.x to 2.0
* The constants `MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW` and
`MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE_PREVIEW` have been
removed. Use the `ALGORITHM_RANGE` and `QUERY_TYPE_RANGE` instead.
* The `MongoDB\Driver\ReadPreference` class now requires a string value for its
constructor's `$mode` parameter. The integer constants for modes have been
removed along with the `getMode()` method. Use the string constants and
`getModeString()` instead.
* All tentative return types defined in interface and non-final classes are now
fixed and are required in implementing or extending classes.
* `MongoDB\Driver\CursorInterface` now extends `Iterator`, requiring
Expand Down
99 changes: 23 additions & 76 deletions src/MongoDB/ReadPreference.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ static bool php_phongo_readpreference_init_from_hash(php_phongo_readpreference_t
return false;
}

static const char* php_phongo_readpreference_get_mode_string(mongoc_read_mode_t mode)
static const char* php_phongo_readpreference_get_mode_string(const mongoc_read_prefs_t* read_prefs)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lets us eliminate some mongoc_read_prefs_get_mode() calls in parent contexts.

Additionally, I changed this function to return an empty string instead of throwing in the very unlikely event that the RP mode is invalid. _mongoc_read_mode_as_str() does the same, and libmongoc uses that for appending read preferences to command payloads. In our case, this is used for serialization and debugging, and the change allowed us to clean up the parent contexts.

{
switch (mode) {
switch (mongoc_read_prefs_get_mode(read_prefs)) {
case MONGOC_READ_PRIMARY:
return PHONGO_READ_PRIMARY;
case MONGOC_READ_PRIMARY_PREFERRED:
Expand All @@ -167,19 +167,15 @@ static const char* php_phongo_readpreference_get_mode_string(mongoc_read_mode_t
case MONGOC_READ_NEAREST:
return PHONGO_READ_NEAREST;
default:
/* Should never happen, but if it does: exception */
phongo_throw_exception(PHONGO_ERROR_LOGIC, "Mode '%d' should never have been passed to php_phongo_readpreference_get_mode_string, please file a bug report", mode);
break;
return "";
}

return NULL;
}

/* Constructs a new ReadPreference */
static PHP_METHOD(MongoDB_Driver_ReadPreference, __construct)
{
php_phongo_readpreference_t* intern;
zval* mode;
zend_string* mode;
zval* tagSets = NULL;
zval* options = NULL;

Expand All @@ -188,44 +184,24 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, __construct)
/* Separate the tagSets zval, since we may end up modifying it in
* php_phongo_read_preference_prep_tagsets() below. */
PHONGO_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_ZVAL(mode)
Z_PARAM_STR(mode)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_EX(tagSets, 1, 1)
Z_PARAM_ARRAY_OR_NULL(options)
PHONGO_PARSE_PARAMETERS_END();

if (Z_TYPE_P(mode) == IS_LONG) {
php_error_docref(NULL, E_DEPRECATED, "Passing an integer mode to \"MongoDB\\Driver\\ReadPreference::__construct\" is deprecated and will be removed in a future release.");

switch (Z_LVAL_P(mode)) {
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(Z_LVAL_P(mode));
break;
default:
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Invalid mode: %" PHONGO_LONG_FORMAT, Z_LVAL_P(mode));
return;
}
} else if (Z_TYPE_P(mode) == IS_STRING) {
if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_PRIMARY) == 0) {
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY);
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_PRIMARY_PREFERRED) == 0) {
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY_PREFERRED);
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_SECONDARY) == 0) {
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY);
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_SECONDARY_PREFERRED) == 0) {
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY_PREFERRED);
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_NEAREST) == 0) {
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_NEAREST);
} else {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Invalid mode: '%s'", Z_STRVAL_P(mode));
return;
}
if (zend_string_equals_literal_ci(mode, PHONGO_READ_PRIMARY)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was delighted to find this in zend_string.h.

intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY);
} else if (zend_string_equals_literal_ci(mode, PHONGO_READ_PRIMARY_PREFERRED)) {
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY_PREFERRED);
} else if (zend_string_equals_literal_ci(mode, PHONGO_READ_SECONDARY)) {
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY);
} else if (zend_string_equals_literal_ci(mode, PHONGO_READ_SECONDARY_PREFERRED)) {
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY_PREFERRED);
} else if (zend_string_equals_literal_ci(mode, PHONGO_READ_NEAREST)) {
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_NEAREST);
} else {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected mode to be integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(mode));
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Invalid mode: '%s'", ZSTR_VAL(mode));
return;
}

Expand Down Expand Up @@ -366,35 +342,16 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, getMaxStalenessSeconds)
RETURN_LONG(mongoc_read_prefs_get_max_staleness_seconds(intern->read_preference));
}

/* Returns the ReadPreference mode */
static PHP_METHOD(MongoDB_Driver_ReadPreference, getMode)
{
php_phongo_readpreference_t* intern;

intern = Z_READPREFERENCE_OBJ_P(getThis());

PHONGO_PARSE_PARAMETERS_NONE();

RETURN_LONG(mongoc_read_prefs_get_mode(intern->read_preference));
}

/* Returns the ReadPreference mode as string */
static PHP_METHOD(MongoDB_Driver_ReadPreference, getModeString)
{
php_phongo_readpreference_t* intern;
const char* mode_string;

intern = Z_READPREFERENCE_OBJ_P(getThis());

PHONGO_PARSE_PARAMETERS_NONE();

mode_string = php_phongo_readpreference_get_mode_string(mongoc_read_prefs_get_mode(intern->read_preference));
if (!mode_string) {
/* Exception already thrown */
return;
}

RETURN_STRING(mode_string);
RETURN_STRING(php_phongo_readpreference_get_mode_string(intern->read_preference));
}

/* Returns the ReadPreference tag sets */
Expand Down Expand Up @@ -429,10 +386,8 @@ static HashTable* php_phongo_readpreference_get_properties_hash(zend_object* obj
{
php_phongo_readpreference_t* intern;
HashTable* props;
const char* modeString = NULL;
const bson_t* tags;
const bson_t* hedge;
mongoc_read_mode_t mode;

intern = Z_OBJ_READPREFERENCE(object);

Expand All @@ -442,15 +397,13 @@ static HashTable* php_phongo_readpreference_get_properties_hash(zend_object* obj
return props;
}

tags = mongoc_read_prefs_get_tags(intern->read_preference);
mode = mongoc_read_prefs_get_mode(intern->read_preference);
modeString = php_phongo_readpreference_get_mode_string(mode);
hedge = mongoc_read_prefs_get_hedge(intern->read_preference);
tags = mongoc_read_prefs_get_tags(intern->read_preference);
hedge = mongoc_read_prefs_get_hedge(intern->read_preference);

if (modeString) {
{
zval z_mode;

ZVAL_STRING(&z_mode, modeString);
ZVAL_STRING(&z_mode, php_phongo_readpreference_get_mode_string(intern->read_preference));
zend_hash_str_update(props, "mode", sizeof("mode") - 1, &z_mode);
}

Expand Down Expand Up @@ -510,12 +463,10 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, serialize)
php_phongo_readpreference_t* intern;
zval retval;
php_serialize_data_t var_hash;
smart_str buf = { 0 };
const char* modeString = NULL;
smart_str buf = { 0 };
const bson_t* tags;
const bson_t* hedge;
int64_t maxStalenessSeconds;
mongoc_read_mode_t mode;

intern = Z_READPREFERENCE_OBJ_P(getThis());

Expand All @@ -526,16 +477,12 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, serialize)
}

tags = mongoc_read_prefs_get_tags(intern->read_preference);
mode = mongoc_read_prefs_get_mode(intern->read_preference);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this will conflict with #1663, depending on which gets merged first.

modeString = php_phongo_readpreference_get_mode_string(mode);
maxStalenessSeconds = mongoc_read_prefs_get_max_staleness_seconds(intern->read_preference);
hedge = mongoc_read_prefs_get_hedge(intern->read_preference);

array_init_size(&retval, 4);

if (modeString) {
ADD_ASSOC_STRING(&retval, "mode", modeString);
}
ADD_ASSOC_STRING(&retval, "mode", php_phongo_readpreference_get_mode_string(intern->read_preference));

if (!bson_empty0(tags)) {
php_phongo_bson_state state;
Expand Down
40 changes: 1 addition & 39 deletions src/MongoDB/ReadPreference.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,6 @@

final class ReadPreference implements \MongoDB\BSON\Serializable, \Serializable
{
/**
* @var int
* @cvalue MONGOC_READ_PRIMARY
* @deprecated
*/
public const RP_PRIMARY = UNKNOWN;

/**
* @var int
* @cvalue MONGOC_READ_PRIMARY_PREFERRED
* @deprecated
*/
public const RP_PRIMARY_PREFERRED = UNKNOWN;

/**
* @var int
* @cvalue MONGOC_READ_SECONDARY
* @deprecated
*/
public const RP_SECONDARY = UNKNOWN;

/**
* @var int
* @cvalue MONGOC_READ_SECONDARY_PREFERRED
* @deprecated
*/
public const RP_SECONDARY_PREFERRED = UNKNOWN;

/**
* @var int
* @cvalue MONGOC_READ_NEAREST
* @deprecated
*/
public const RP_NEAREST = UNKNOWN;

/**
* @var string
* @cvalue PHONGO_READ_PRIMARY
Expand Down Expand Up @@ -86,15 +51,12 @@ final class ReadPreference implements \MongoDB\BSON\Serializable, \Serializable
*/
public const SMALLEST_MAX_STALENESS_SECONDS = UNKNOWN;

final public function __construct(string|int $mode, ?array $tagSets = null, ?array $options = null) {}
final public function __construct(string $mode, ?array $tagSets = null, ?array $options = null) {}

final public function getHedge(): ?object {}

final public function getMaxStalenessSeconds(): int {}

/** @deprecated */
final public function getMode(): int {}

final public function getModeString(): string {}

final public function getTagSets(): array {}
Expand Down
38 changes: 2 additions & 36 deletions src/MongoDB/ReadPreference_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions tests/readPreference/readpreference-constants-002.phpt

This file was deleted.

Loading