Skip to content

Commit c70a2d8

Browse files
committed
Merged pull request #469
2 parents c729150 + 5c63b8d commit c70a2d8

19 files changed

+131
-106
lines changed

php_phongo.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -914,8 +914,8 @@ void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t
914914
#endif
915915
}
916916

917-
if (mongoc_read_prefs_get_max_staleness_ms(read_prefs) != 0) {
918-
ADD_ASSOC_LONG_EX(retval, "maxStalenessMS", mongoc_read_prefs_get_max_staleness_ms(read_prefs));
917+
if (mongoc_read_prefs_get_max_staleness_seconds(read_prefs) != MONGOC_NO_MAX_STALENESS) {
918+
ADD_ASSOC_LONG_EX(retval, "maxStalenessSeconds", mongoc_read_prefs_get_max_staleness_seconds(read_prefs));
919919
}
920920
} /* }}} */
921921

@@ -971,7 +971,7 @@ static mongoc_uri_t *php_phongo_make_uri(const char *uri_string, bson_t *options
971971
!strcasecmp(key, "slaveok") ||
972972
!strcasecmp(key, "w") ||
973973
!strcasecmp(key, "wtimeoutms") ||
974-
!strcasecmp(key, "maxstalenessms") ||
974+
!strcasecmp(key, "maxstalenessseconds") ||
975975
!strcasecmp(key, "appname")
976976
) {
977977
continue;
@@ -1060,7 +1060,7 @@ static bool php_phongo_apply_rp_options_to_uri(mongoc_uri_t *uri, bson_t *option
10601060
if (!bson_iter_init_find_case(&iter, options, "slaveok") &&
10611061
!bson_iter_init_find_case(&iter, options, "readpreference") &&
10621062
!bson_iter_init_find_case(&iter, options, "readpreferencetags") &&
1063-
!bson_iter_init_find_case(&iter, options, "maxstalenessms")
1063+
!bson_iter_init_find_case(&iter, options, "maxstalenessseconds")
10641064
) {
10651065
return true;
10661066
}
@@ -1124,29 +1124,32 @@ static bool php_phongo_apply_rp_options_to_uri(mongoc_uri_t *uri, bson_t *option
11241124
return false;
11251125
}
11261126

1127-
/* Handle maxStalenessMS, and make sure it is not combined with primary
1127+
/* Handle maxStalenessSeconds, and make sure it is not combined with primary
11281128
* readPreference */
1129-
if (bson_iter_init_find_case(&iter, options, "maxstalenessms") && BSON_ITER_HOLDS_INT32(&iter)) {
1130-
int32_t max_staleness_ms = bson_iter_int32(&iter);
1129+
if (bson_iter_init_find_case(&iter, options, "maxstalenessseconds") && BSON_ITER_HOLDS_INT32(&iter)) {
1130+
int32_t max_staleness_seconds = bson_iter_int32(&iter);
11311131

1132-
if (max_staleness_ms < 0) {
1133-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessMS to be >= 0, %" PRId32 " given", max_staleness_ms);
1134-
mongoc_read_prefs_destroy(new_rp);
1132+
if (max_staleness_seconds != MONGOC_NO_MAX_STALENESS) {
11351133

1136-
return false;
1137-
}
1134+
if (max_staleness_seconds < MONGOC_SMALLEST_MAX_STALENESS_SECONDS) {
1135+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessSeconds to be >= %d, %" PRId32 " given", MONGOC_SMALLEST_MAX_STALENESS_SECONDS, max_staleness_seconds);
1136+
mongoc_read_prefs_destroy(new_rp);
1137+
1138+
return false;
1139+
}
11381140

1139-
/* max_staleness_ms is fetched as an INT32, so there is no need to check
1140-
* if it exists INT32_MAX as we do in the ReadPreference constructor. */
1141+
/* max_staleness_seconds is fetched as an INT32, so there is no need to check
1142+
* if it exists INT32_MAX as we do in the ReadPreference constructor. */
11411143

1142-
if (mongoc_read_prefs_get_mode(new_rp) == MONGOC_READ_PRIMARY) {
1143-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Primary read preference mode conflicts with maxStalenessMS");
1144-
mongoc_read_prefs_destroy(new_rp);
1144+
if (mongoc_read_prefs_get_mode(new_rp) == MONGOC_READ_PRIMARY) {
1145+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Primary read preference mode conflicts with maxStalenessSeconds");
1146+
mongoc_read_prefs_destroy(new_rp);
11451147

1146-
return false;
1148+
return false;
1149+
}
11471150
}
11481151

1149-
mongoc_read_prefs_set_max_staleness_ms(new_rp, max_staleness_ms);
1152+
mongoc_read_prefs_set_max_staleness_seconds(new_rp, max_staleness_seconds);
11501153
}
11511154

11521155
/* This may be redundant in light of the last check (primary with tags), but

src/MongoDB/ReadPreference.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,25 @@ PHP_METHOD(ReadPreference, __construct)
105105
bson_destroy(tags);
106106
}
107107

108-
if (options && php_array_exists(options, "maxStalenessMS")) {
109-
phongo_long maxStalenessMS = php_array_fetchc_long(options, "maxStalenessMS");
110-
111-
if (maxStalenessMS < 0) {
112-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessMS to be >= 0, %" PHONGO_LONG_FORMAT " given", maxStalenessMS);
113-
return;
114-
}
115-
116-
if (maxStalenessMS > INT32_MAX) {
117-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessMS to be <= %" PRId32 ", %" PHONGO_LONG_FORMAT " given", INT32_MAX, maxStalenessMS);
118-
return;
119-
}
120-
121-
if (maxStalenessMS > 0 && mode == MONGOC_READ_PRIMARY) {
122-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "maxStalenessMS may not be used with primary mode");
123-
return;
108+
if (options && php_array_exists(options, "maxStalenessSeconds")) {
109+
phongo_long maxStalenessSeconds = php_array_fetchc_long(options, "maxStalenessSeconds");
110+
111+
if (maxStalenessSeconds != MONGOC_NO_MAX_STALENESS) {
112+
if (maxStalenessSeconds < MONGOC_SMALLEST_MAX_STALENESS_SECONDS) {
113+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessSeconds to be >= %d, %" PHONGO_LONG_FORMAT " given", MONGOC_SMALLEST_MAX_STALENESS_SECONDS, maxStalenessSeconds);
114+
return;
115+
}
116+
if (maxStalenessSeconds > INT32_MAX) {
117+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessSeconds to be <= %" PRId32 ", %" PHONGO_LONG_FORMAT " given", INT32_MAX, maxStalenessSeconds);
118+
return;
119+
}
120+
if (mode == MONGOC_READ_PRIMARY) {
121+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "maxStalenessSeconds may not be used with primary mode");
122+
return;
123+
}
124124
}
125125

126-
mongoc_read_prefs_set_max_staleness_ms(intern->read_preference, maxStalenessMS);
126+
mongoc_read_prefs_set_max_staleness_seconds(intern->read_preference, maxStalenessSeconds);
127127
}
128128

129129
if (!mongoc_read_prefs_is_valid(intern->read_preference)) {
@@ -133,9 +133,9 @@ PHP_METHOD(ReadPreference, __construct)
133133
}
134134
/* }}} */
135135

136-
/* {{{ proto integer ReadPreference::getMaxStalenessMS()
137-
Returns the ReadPreference maxStalenessMS value */
138-
PHP_METHOD(ReadPreference, getMaxStalenessMS)
136+
/* {{{ proto integer ReadPreference::getMaxStalenessSeconds()
137+
Returns the ReadPreference maxStalenessSeconds value */
138+
PHP_METHOD(ReadPreference, getMaxStalenessSeconds)
139139
{
140140
php_phongo_readpreference_t *intern;
141141
SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(return_value_used)
@@ -146,7 +146,7 @@ PHP_METHOD(ReadPreference, getMaxStalenessMS)
146146
return;
147147
}
148148

149-
RETURN_LONG(mongoc_read_prefs_get_max_staleness_ms(intern->read_preference));
149+
RETURN_LONG(mongoc_read_prefs_get_max_staleness_seconds(intern->read_preference));
150150
}
151151
/* }}} */
152152

@@ -232,7 +232,7 @@ ZEND_END_ARG_INFO()
232232

233233
static zend_function_entry php_phongo_readpreference_me[] = {
234234
PHP_ME(ReadPreference, __construct, ai_ReadPreference___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
235-
PHP_ME(ReadPreference, getMaxStalenessMS, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
235+
PHP_ME(ReadPreference, getMaxStalenessSeconds, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
236236
PHP_ME(ReadPreference, getMode, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
237237
PHP_ME(ReadPreference, getTagSets, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
238238
PHP_ME(ReadPreference, bsonSerialize, ai_ReadPreference_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
@@ -328,6 +328,8 @@ PHP_MINIT_FUNCTION(ReadPreference)
328328
zend_declare_class_constant_long(php_phongo_readpreference_ce, ZEND_STRL("RP_SECONDARY"), MONGOC_READ_SECONDARY TSRMLS_CC);
329329
zend_declare_class_constant_long(php_phongo_readpreference_ce, ZEND_STRL("RP_SECONDARY_PREFERRED"), MONGOC_READ_SECONDARY_PREFERRED TSRMLS_CC);
330330
zend_declare_class_constant_long(php_phongo_readpreference_ce, ZEND_STRL("RP_NEAREST"), MONGOC_READ_NEAREST TSRMLS_CC);
331+
zend_declare_class_constant_long(php_phongo_readpreference_ce, ZEND_STRL("NO_MAX_STALENESS"), MONGOC_NO_MAX_STALENESS TSRMLS_CC);
332+
zend_declare_class_constant_long(php_phongo_readpreference_ce, ZEND_STRL("SMALLEST_MAX_STALENESS_SECONDS"), MONGOC_SMALLEST_MAX_STALENESS_SECONDS TSRMLS_CC);
331333

332334
return SUCCESS;
333335
}

src/bson.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,10 +983,10 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
983983
if (instanceof_function(Z_OBJCE_P(object), php_phongo_javascript_ce TSRMLS_CC)) {
984984
if (php_phongo_javascript_has_scope(object TSRMLS_CC)) {
985985
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript with scope");
986-
bson_append_code(bson, key, key_len, php_phongo_javascript_get_code(object TSRMLS_CC));
986+
bson_append_code_with_scope(bson, key, key_len, php_phongo_javascript_get_code(object TSRMLS_CC), php_phongo_javascript_get_scope(object TSRMLS_CC));
987987
} else {
988988
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript without scope");
989-
bson_append_code_with_scope(bson, key, key_len, php_phongo_javascript_get_code(object TSRMLS_CC), php_phongo_javascript_get_scope(object TSRMLS_CC));
989+
bson_append_code(bson, key, key_len, php_phongo_javascript_get_code(object TSRMLS_CC));
990990
}
991991
return;
992992
}

src/libmongoc

Submodule libmongoc updated 187 files

tests/bson/bson-javascript-jsonserialize-003.phpt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ require_once __DIR__ . '/../utils/tools.php';
88
$doc = ['foo' => new MongoDB\BSON\Javascript('function foo(bar) { return bar; }')];
99
$json = json_encode($doc);
1010

11-
/* Note: libbson currently does properly handle Javascript types. Conversion of
12-
* BSON to JSON yields a single code string value instead of a document with
13-
* "$code" and "$scope" fields. Likewise, "$code" and "$scope" fields are not
14-
* parsed when converting JSON to BSON. See CDRIVER-1335 for more info. */
1511
echo toJSON(fromPHP($doc)), "\n";
1612
echo $json, "\n";
1713
var_dump(toPHP(fromJSON($json)));
@@ -20,7 +16,7 @@ var_dump(toPHP(fromJSON($json)));
2016
===DONE===
2117
<?php exit(0); ?>
2218
--EXPECTF--
23-
{ "foo" : "function foo(bar) { return bar; }" }
19+
{ "foo" : { "$code" : "function foo(bar) { return bar; }" } }
2420
{"foo":{"$code":"function foo(bar) { return bar; }"}}
2521
object(stdClass)#%d (%d) {
2622
["foo"]=>

tests/bson/bson-javascript-jsonserialize-004.phpt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ require_once __DIR__ . '/../utils/tools.php';
88
$doc = ['foo' => new MongoDB\BSON\Javascript('function foo(bar) { return bar; }', ['foo' => 42])];
99
$json = json_encode($doc);
1010

11-
/* Note: libbson currently does properly handle Javascript types. Conversion of
12-
* BSON to JSON yields a single code string value instead of a document with
13-
* "$code" and "$scope" fields. Likewise, "$code" and "$scope" fields are not
14-
* parsed when converting JSON to BSON. See CDRIVER-1335 for more info. */
1511
echo toJSON(fromPHP($doc)), "\n";
1612
echo $json, "\n";
1713
var_dump(toPHP(fromJSON($json)));
@@ -20,7 +16,7 @@ var_dump(toPHP(fromJSON($json)));
2016
===DONE===
2117
<?php exit(0); ?>
2218
--EXPECTF--
23-
{ "foo" : "function foo(bar) { return bar; }" }
19+
{ "foo" : { "$code" : "function foo(bar) { return bar; }", "$scope" : { "foo" : 42 } } }
2420
{"foo":{"$code":"function foo(bar) { return bar; }","$scope":{"foo":42}}}
2521
object(stdClass)#%d (%d) {
2622
["foo"]=>

tests/manager/manager-ctor-read_preference-001.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ MongoDB\Driver\Manager::__construct(): read preference options
66
$tests = [
77
['mongodb://127.0.0.1/?readPreference=primary', []],
88
['mongodb://127.0.0.1/?readPreference=secondary&readPreferenceTags=tag:one&readPreferenceTags=', []],
9-
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessMS=1000', []],
9+
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessSeconds=1000', []],
1010
[null, ['readPreference' => 'primary']],
1111
[null, ['readPreference' => 'secondary', 'readPreferenceTags' => [['tag' => 'one'], []]]],
12-
[null, ['readPreference' => 'secondary', 'maxStalenessMS' => 1000]],
12+
[null, ['readPreference' => 'secondary', 'maxStalenessSeconds' => 1000]],
1313
];
1414

1515
foreach ($tests as $test) {
@@ -45,7 +45,7 @@ object(MongoDB\Driver\ReadPreference)#%d (%d) {
4545
object(MongoDB\Driver\ReadPreference)#%d (%d) {
4646
["mode"]=>
4747
string(9) "secondary"
48-
["maxStalenessMS"]=>
48+
["maxStalenessSeconds"]=>
4949
int(1000)
5050
}
5151
object(MongoDB\Driver\ReadPreference)#%d (%d) {
@@ -70,7 +70,7 @@ object(MongoDB\Driver\ReadPreference)#%d (%d) {
7070
object(MongoDB\Driver\ReadPreference)#%d (%d) {
7171
["mode"]=>
7272
string(9) "secondary"
73-
["maxStalenessMS"]=>
73+
["maxStalenessSeconds"]=>
7474
int(1000)
7575
}
7676
===DONE===
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
--TEST--
2-
MongoDB\Driver\Manager::__construct(): read preference options (maxStalenessMS)
2+
MongoDB\Driver\Manager::__construct(): read preference options (maxStalenessSeconds)
33
--FILE--
44
<?php
55

66
$tests = [
7-
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessMS=1231', []],
8-
['mongodb://127.0.0.1/?readPreference=secondary', ['maxStalenessMS' => 1231]],
9-
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessMS=1000', ['maxStalenessMS' => 2000]],
10-
['mongodb://127.0.0.1/?readpreference=secondary&maxstalenessms=1231', []],
11-
['mongodb://127.0.0.1/?readpreference=secondary', ['maxstalenessms' => 1231]],
7+
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessSeconds=1231', []],
8+
['mongodb://127.0.0.1/?readPreference=secondary', ['maxStalenessSeconds' => 1231]],
9+
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessSeconds=1000', ['maxStalenessSeconds' => 2000]],
10+
['mongodb://127.0.0.1/?readpreference=secondary&maxstalenessseconds=1231', []],
11+
['mongodb://127.0.0.1/?readpreference=secondary', ['maxstalenessseconds' => 1231]],
1212
];
1313

1414
foreach ($tests as $test) {
@@ -24,31 +24,31 @@ foreach ($tests as $test) {
2424
object(MongoDB\Driver\ReadPreference)#%d (%d) {
2525
["mode"]=>
2626
string(9) "secondary"
27-
["maxStalenessMS"]=>
27+
["maxStalenessSeconds"]=>
2828
int(1231)
2929
}
3030
object(MongoDB\Driver\ReadPreference)#%d (%d) {
3131
["mode"]=>
3232
string(9) "secondary"
33-
["maxStalenessMS"]=>
33+
["maxStalenessSeconds"]=>
3434
int(1231)
3535
}
3636
object(MongoDB\Driver\ReadPreference)#%d (%d) {
3737
["mode"]=>
3838
string(9) "secondary"
39-
["maxStalenessMS"]=>
39+
["maxStalenessSeconds"]=>
4040
int(2000)
4141
}
4242
object(MongoDB\Driver\ReadPreference)#%d (%d) {
4343
["mode"]=>
4444
string(9) "secondary"
45-
["maxStalenessMS"]=>
45+
["maxStalenessSeconds"]=>
4646
int(1231)
4747
}
4848
object(MongoDB\Driver\ReadPreference)#%d (%d) {
4949
["mode"]=>
5050
string(9) "secondary"
51-
["maxStalenessMS"]=>
51+
["maxStalenessSeconds"]=>
5252
int(1231)
5353
}
5454
===DONE===

tests/manager/manager-ctor-read_preference-003.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ MongoDB\Driver\Manager::__construct(): read preference options of the wrong type
66
$tests = [
77
['mongodb://127.0.0.1/?readPreference=secondary', ['readPreference' => 1]],
88
['mongodb://127.0.0.1/?readPreference=secondary&readPreferenceTags=tag:one', ['readPreferenceTags' => 'invalid']],
9-
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessMS=1000', ['maxStalenessMS' => 'invalid']],
9+
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessSeconds=1000', ['maxStalenessSeconds' => 'invalid']],
1010
];
1111

1212
foreach ($tests as $test) {
@@ -38,7 +38,7 @@ object(MongoDB\Driver\ReadPreference)#%d (%d) {
3838
object(MongoDB\Driver\ReadPreference)#%d (%d) {
3939
["mode"]=>
4040
string(9) "secondary"
41-
["maxStalenessMS"]=>
41+
["maxStalenessSeconds"]=>
4242
int(1000)
4343
}
4444
===DONE===

0 commit comments

Comments
 (0)