Skip to content

Commit f47bd87

Browse files
tanlisujmikola
authored andcommitted
PHPC-1891, PHPC-1892, PHPC-1893: Implement TopologyDescription, SDAMSubscriber, and TopologyChangedEvent (mongodb#1244)
1 parent 53ca560 commit f47bd87

19 files changed

+1010
-2
lines changed

config.m4

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ if test "$PHP_MONGODB" != "no"; then
148148
src/MongoDB/ServerApi.c \
149149
src/MongoDB/ServerDescription.c \
150150
src/MongoDB/Session.c \
151+
src/MongoDB/TopologyDescription.c \
151152
src/MongoDB/WriteConcern.c \
152153
src/MongoDB/WriteConcernError.c \
153154
src/MongoDB/WriteError.c \
@@ -171,7 +172,9 @@ if test "$PHP_MONGODB" != "no"; then
171172
src/MongoDB/Monitoring/CommandStartedEvent.c \
172173
src/MongoDB/Monitoring/CommandSubscriber.c \
173174
src/MongoDB/Monitoring/CommandSucceededEvent.c \
175+
src/MongoDB/Monitoring/SDAMSubscriber.c \
174176
src/MongoDB/Monitoring/Subscriber.c \
177+
src/MongoDB/Monitoring/TopologyChangedEvent.c \
175178
src/MongoDB/Monitoring/functions.c \
176179
"
177180

config.w32

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ if (PHP_MONGODB != "no") {
120120
EXTENSION("mongodb", "php_phongo.c phongo_compat.c", null, PHP_MONGODB_CFLAGS);
121121
MONGODB_ADD_SOURCES("/src", "bson.c bson-encode.c phongo_apm.c phongo_ini.c");
122122
MONGODB_ADD_SOURCES("/src/BSON", "Binary.c BinaryInterface.c DBPointer.c Decimal128.c Decimal128Interface.c Int64.c Javascript.c JavascriptInterface.c MaxKey.c MaxKeyInterface.c MinKey.c MinKeyInterface.c ObjectId.c ObjectIdInterface.c Persistable.c Regex.c RegexInterface.c Serializable.c Symbol.c Timestamp.c TimestampInterface.c Type.c Undefined.c Unserializable.c UTCDateTime.c UTCDateTimeInterface.c functions.c");
123-
MONGODB_ADD_SOURCES("/src/MongoDB", "BulkWrite.c ClientEncryption.c Command.c Cursor.c CursorId.c CursorInterface.c Manager.c Query.c ReadConcern.c ReadPreference.c Server.c ServerApi.c ServerDescription.c Session.c WriteConcern.c WriteConcernError.c WriteError.c WriteResult.c");
123+
MONGODB_ADD_SOURCES("/src/MongoDB", "BulkWrite.c ClientEncryption.c Command.c Cursor.c CursorId.c CursorInterface.c Manager.c Query.c ReadConcern.c ReadPreference.c Server.c ServerApi.c ServerDescription.c Session.c TopologyDescription.c WriteConcern.c WriteConcernError.c WriteError.c WriteResult.c");
124124
MONGODB_ADD_SOURCES("/src/MongoDB/Exception", "AuthenticationException.c BulkWriteException.c CommandException.c ConnectionException.c ConnectionTimeoutException.c EncryptionException.c Exception.c ExecutionTimeoutException.c InvalidArgumentException.c LogicException.c RuntimeException.c ServerException.c SSLConnectionException.c UnexpectedValueException.c WriteException.c");
125-
MONGODB_ADD_SOURCES("/src/MongoDB/Monitoring", "CommandFailedEvent.c CommandStartedEvent.c CommandSubscriber.c CommandSucceededEvent.c Subscriber.c functions.c");
125+
MONGODB_ADD_SOURCES("/src/MongoDB/Monitoring", "CommandFailedEvent.c CommandStartedEvent.c CommandSubscriber.c CommandSucceededEvent.c SDAMSubscriber.c Subscriber.c TopologyChangedEvent.c functions.c");
126126
MONGODB_ADD_SOURCES("/src/libmongoc/src/common", PHP_MONGODB_COMMON_SOURCES);
127127
MONGODB_ADD_SOURCES("/src/libmongoc/src/libbson/src/bson", PHP_MONGODB_BSON_SOURCES);
128128
MONGODB_ADD_SOURCES("/src/libmongoc/src/libbson/src/jsonsl", PHP_MONGODB_JSONSL_SOURCES);

php_phongo.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,17 @@ void phongo_objectid_init(zval* return_value, const bson_oid_t* oid) /* {{{ */
394394
}
395395
/* }}} */
396396

397+
void phongo_topologydescription_init(zval* return_value, mongoc_topology_description_t* topology_description) /* {{{ */
398+
{
399+
php_phongo_topologydescription_t* intern;
400+
401+
object_init_ex(return_value, php_phongo_topologydescription_ce);
402+
403+
intern = Z_TOPOLOGYDESCRIPTION_OBJ_P(return_value);
404+
intern->topology_description = topology_description;
405+
}
406+
/* }}} */
407+
397408
void phongo_readconcern_init(zval* return_value, const mongoc_read_concern_t* read_concern) /* {{{ */
398409
{
399410
php_phongo_readconcern_t* intern;
@@ -1147,6 +1158,35 @@ const mongoc_read_prefs_t* phongo_read_preference_from_zval(zval* zread_preferen
11471158
/* }}} */
11481159

11491160
/* {{{ phongo zval from mongoc types */
1161+
bool php_phongo_server_description_to_zval(zval* retval, mongoc_server_description_t* sd) /* {{{ */
1162+
{
1163+
mongoc_host_list_t* host = mongoc_server_description_host(sd);
1164+
const bson_t* hello_response = mongoc_server_description_hello_response(sd);
1165+
1166+
array_init(retval);
1167+
1168+
ADD_ASSOC_STRING(retval, "host", host->host);
1169+
ADD_ASSOC_LONG_EX(retval, "port", host->port);
1170+
ADD_ASSOC_LONG_EX(retval, "type", php_phongo_server_description_type(sd));
1171+
1172+
{
1173+
php_phongo_bson_state state;
1174+
1175+
PHONGO_BSON_INIT_DEBUG_STATE(state);
1176+
1177+
if (!php_phongo_bson_to_zval_ex(bson_get_data(hello_response), hello_response->len, &state)) {
1178+
zval_ptr_dtor(&state.zchild);
1179+
return false;
1180+
}
1181+
1182+
ADD_ASSOC_ZVAL_EX(retval, "hello_response", &state.zchild);
1183+
}
1184+
ADD_ASSOC_LONG_EX(retval, "last_update_time", (zend_long) mongoc_server_description_last_update_time(sd));
1185+
ADD_ASSOC_LONG_EX(retval, "round_trip_time", (zend_long) mongoc_server_description_round_trip_time(sd));
1186+
1187+
return true;
1188+
} /* }}} */
1189+
11501190
php_phongo_server_description_type_t php_phongo_server_description_type(mongoc_server_description_t* sd)
11511191
{
11521192
const char* name = mongoc_server_description_type(sd);
@@ -1244,6 +1284,41 @@ bool php_phongo_server_to_zval(zval* retval, mongoc_client_t* client, mongoc_ser
12441284
return true;
12451285
} /* }}} */
12461286

1287+
bool php_phongo_topology_description_to_zval(zval* retval, mongoc_topology_description_t* td) /* {{{ */
1288+
{
1289+
array_init(retval);
1290+
1291+
{
1292+
zval servers;
1293+
size_t i, n = 0;
1294+
mongoc_server_description_t** sds = mongoc_topology_description_get_servers(td, &n);
1295+
1296+
array_init_size(&servers, n);
1297+
1298+
for (i = 0; i < n; i++) {
1299+
zval obj;
1300+
1301+
if (!php_phongo_server_description_to_zval(&obj, sds[i])) {
1302+
/* Exception already thrown */
1303+
zval_ptr_dtor(&obj);
1304+
zval_ptr_dtor(&servers);
1305+
mongoc_server_descriptions_destroy_all(sds, n);
1306+
return false;
1307+
}
1308+
1309+
add_next_index_zval(&servers, &obj);
1310+
zval_ptr_dtor(&obj);
1311+
zval_ptr_dtor(&servers);
1312+
}
1313+
mongoc_server_descriptions_destroy_all(sds, n);
1314+
1315+
ADD_ASSOC_ZVAL_EX(retval, "servers", &servers);
1316+
}
1317+
ADD_ASSOC_STRING(retval, "type", mongoc_topology_description_type(td));
1318+
1319+
return true;
1320+
} /* }}} */
1321+
12471322
void php_phongo_read_concern_to_zval(zval* retval, const mongoc_read_concern_t* read_concern) /* {{{ */
12481323
{
12491324
const char* level = mongoc_read_concern_get_level(read_concern);
@@ -3612,6 +3687,7 @@ PHP_MINIT_FUNCTION(mongodb)
36123687
php_phongo_server_init_ce(INIT_FUNC_ARGS_PASSTHRU);
36133688
php_phongo_serverapi_init_ce(INIT_FUNC_ARGS_PASSTHRU);
36143689
php_phongo_serverdescription_init_ce(INIT_FUNC_ARGS_PASSTHRU);
3690+
php_phongo_topologydescription_init_ce(INIT_FUNC_ARGS_PASSTHRU);
36153691
php_phongo_session_init_ce(INIT_FUNC_ARGS_PASSTHRU);
36163692
php_phongo_writeconcern_init_ce(INIT_FUNC_ARGS_PASSTHRU);
36173693
php_phongo_writeconcernerror_init_ce(INIT_FUNC_ARGS_PASSTHRU);
@@ -3642,6 +3718,8 @@ PHP_MINIT_FUNCTION(mongodb)
36423718
php_phongo_commandfailedevent_init_ce(INIT_FUNC_ARGS_PASSTHRU);
36433719
php_phongo_commandstartedevent_init_ce(INIT_FUNC_ARGS_PASSTHRU);
36443720
php_phongo_commandsucceededevent_init_ce(INIT_FUNC_ARGS_PASSTHRU);
3721+
php_phongo_sdamsubscriber_init_ce(INIT_FUNC_ARGS_PASSTHRU);
3722+
php_phongo_topologychangedevent_init_ce(INIT_FUNC_ARGS_PASSTHRU);
36453723

36463724
REGISTER_STRING_CONSTANT("MONGODB_VERSION", (char*) PHP_MONGODB_VERSION, CONST_CS | CONST_PERSISTENT);
36473725
REGISTER_STRING_CONSTANT("MONGODB_STABILITY", (char*) PHP_MONGODB_STABILITY, CONST_CS | CONST_PERSISTENT);

php_phongo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ void phongo_server_init(zval* return_value, zval* manager, uint32_t server_id);
120120
void phongo_serverdescription_init(zval* return_value, mongoc_server_description_t* serverDescription);
121121
void phongo_session_init(zval* return_value, zval* manager, mongoc_client_session_t* client_session);
122122
void phongo_objectid_init(zval* return_value, const bson_oid_t* oid);
123+
void phongo_topologydescription_init(zval* return_value, mongoc_topology_description_t* topologyDescription);
123124
void phongo_readconcern_init(zval* return_value, const mongoc_read_concern_t* read_concern);
124125
void phongo_readpreference_init(zval* return_value, const mongoc_read_prefs_t* read_prefs);
125126
void phongo_writeconcern_init(zval* return_value, const mongoc_write_concern_t* write_concern);
@@ -146,6 +147,7 @@ bool php_phongo_read_preference_tags_are_valid(const bson_t* tags);
146147

147148
bool php_phongo_server_to_zval(zval* retval, mongoc_client_t* client, mongoc_server_description_t* sd);
148149
bool php_phongo_server_description_to_zval(zval* retval, mongoc_server_description_t* sd);
150+
bool php_phongo_topology_description_to_zval(zval* retval, mongoc_topology_description_t* td);
149151
void php_phongo_read_concern_to_zval(zval* retval, const mongoc_read_concern_t* read_concern);
150152
void php_phongo_write_concern_to_zval(zval* retval, const mongoc_write_concern_t* write_concern);
151153

php_phongo_classes.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ static inline php_phongo_serverdescription_t* php_serverdescription_fetch_object
6767
{
6868
return (php_phongo_serverdescription_t*) ((char*) obj - XtOffsetOf(php_phongo_serverdescription_t, std));
6969
}
70+
static inline php_phongo_topologydescription_t* php_topologydescription_fetch_object(zend_object* obj)
71+
{
72+
return (php_phongo_topologydescription_t*) ((char*) obj - XtOffsetOf(php_phongo_topologydescription_t, std));
73+
}
7074
static inline php_phongo_serverapi_t* php_serverapi_fetch_object(zend_object* obj)
7175
{
7276
return (php_phongo_serverapi_t*) ((char*) obj - XtOffsetOf(php_phongo_serverapi_t, std));
@@ -155,6 +159,10 @@ static inline php_phongo_commandsucceededevent_t* php_commandsucceededevent_fetc
155159
{
156160
return (php_phongo_commandsucceededevent_t*) ((char*) obj - XtOffsetOf(php_phongo_commandsucceededevent_t, std));
157161
}
162+
static inline php_phongo_topologychangedevent_t* php_topologychangedevent_fetch_object(zend_object* obj)
163+
{
164+
return (php_phongo_topologychangedevent_t*) ((char*) obj - XtOffsetOf(php_phongo_topologychangedevent_t, std));
165+
}
158166

159167
#define Z_CLIENTENCRYPTION_OBJ_P(zv) (php_clientencryption_fetch_object(Z_OBJ_P(zv)))
160168
#define Z_COMMAND_OBJ_P(zv) (php_command_fetch_object(Z_OBJ_P(zv)))
@@ -168,6 +176,7 @@ static inline php_phongo_commandsucceededevent_t* php_commandsucceededevent_fetc
168176
#define Z_SERVERAPI_OBJ_P(zv) (php_serverapi_fetch_object(Z_OBJ_P(zv)))
169177
#define Z_SERVERDESCRIPTION_OBJ_P(zv) (php_serverdescription_fetch_object(Z_OBJ_P(zv)))
170178
#define Z_SESSION_OBJ_P(zv) (php_session_fetch_object(Z_OBJ_P(zv)))
179+
#define Z_TOPOLOGYDESCRIPTION_OBJ_P(zv) (php_topologydescription_fetch_object(Z_OBJ_P(zv)))
171180
#define Z_BULKWRITE_OBJ_P(zv) (php_bulkwrite_fetch_object(Z_OBJ_P(zv)))
172181
#define Z_WRITECONCERN_OBJ_P(zv) (php_writeconcern_fetch_object(Z_OBJ_P(zv)))
173182
#define Z_WRITECONCERNERROR_OBJ_P(zv) (php_writeconcernerror_fetch_object(Z_OBJ_P(zv)))
@@ -189,6 +198,7 @@ static inline php_phongo_commandsucceededevent_t* php_commandsucceededevent_fetc
189198
#define Z_COMMANDFAILEDEVENT_OBJ_P(zv) (php_commandfailedevent_fetch_object(Z_OBJ_P(zv)))
190199
#define Z_COMMANDSTARTEDEVENT_OBJ_P(zv) (php_commandstartedevent_fetch_object(Z_OBJ_P(zv)))
191200
#define Z_COMMANDSUCCEEDEDEVENT_OBJ_P(zv) (php_commandsucceededevent_fetch_object(Z_OBJ_P(zv)))
201+
#define Z_TOPOLOGYCHANGEDEVENT_OBJ_P(zv) (php_topologychangedevent_fetch_object(Z_OBJ_P(zv)))
192202

193203
#define Z_OBJ_CLIENTENCRYPTION(zo) (php_clientencryption_fetch_object(zo))
194204
#define Z_OBJ_COMMAND(zo) (php_command_fetch_object(zo))
@@ -202,6 +212,7 @@ static inline php_phongo_commandsucceededevent_t* php_commandsucceededevent_fetc
202212
#define Z_OBJ_SERVERAPI(zo) (php_serverapi_fetch_object(zo))
203213
#define Z_OBJ_SERVERDESCRIPTION(zo) (php_serverdescription_fetch_object(zo))
204214
#define Z_OBJ_SESSION(zo) (php_session_fetch_object(zo))
215+
#define Z_OBJ_TOPOLOGYDESCRIPTION(zo) (php_topologydescription_fetch_object(zo))
205216
#define Z_OBJ_BULKWRITE(zo) (php_bulkwrite_fetch_object(zo))
206217
#define Z_OBJ_WRITECONCERN(zo) (php_writeconcern_fetch_object(zo))
207218
#define Z_OBJ_WRITECONCERNERROR(zo) (php_writeconcernerror_fetch_object(zo))
@@ -223,6 +234,7 @@ static inline php_phongo_commandsucceededevent_t* php_commandsucceededevent_fetc
223234
#define Z_OBJ_COMMANDFAILEDEVENT(zo) (php_commandfailedevent_fetch_object(zo))
224235
#define Z_OBJ_COMMANDSTARTEDEVENT(zo) (php_commandstartedevent_fetch_object(zo))
225236
#define Z_OBJ_COMMANDSUCCEEDEDEVENT(zo) (php_commandsucceededevent_fetch_object(zo))
237+
#define Z_OBJ_TOPOLOGYCHANGEDEVENT(zo) (php_topologychangedevent_fetch_object(zo))
226238

227239
extern zend_class_entry* php_phongo_clientencryption_ce;
228240
extern zend_class_entry* php_phongo_command_ce;
@@ -236,6 +248,7 @@ extern zend_class_entry* php_phongo_server_ce;
236248
extern zend_class_entry* php_phongo_serverapi_ce;
237249
extern zend_class_entry* php_phongo_serverdescription_ce;
238250
extern zend_class_entry* php_phongo_session_ce;
251+
extern zend_class_entry* php_phongo_topologydescription_ce;
239252
extern zend_class_entry* php_phongo_bulkwrite_ce;
240253
extern zend_class_entry* php_phongo_writeconcern_ce;
241254
extern zend_class_entry* php_phongo_writeconcernerror_ce;
@@ -292,7 +305,9 @@ extern zend_class_entry* php_phongo_commandfailedevent_ce;
292305
extern zend_class_entry* php_phongo_commandstartedevent_ce;
293306
extern zend_class_entry* php_phongo_commandsubscriber_ce;
294307
extern zend_class_entry* php_phongo_commandsucceededevent_ce;
308+
extern zend_class_entry* php_phongo_sdamsubscriber_ce;
295309
extern zend_class_entry* php_phongo_subscriber_ce;
310+
extern zend_class_entry* php_phongo_topologychangedevent_ce;
296311

297312
extern void php_phongo_binary_init_ce(INIT_FUNC_ARGS);
298313
extern void php_phongo_dbpointer_init_ce(INIT_FUNC_ARGS);
@@ -335,6 +350,7 @@ extern void php_phongo_server_init_ce(INIT_FUNC_ARGS);
335350
extern void php_phongo_serverapi_init_ce(INIT_FUNC_ARGS);
336351
extern void php_phongo_serverdescription_init_ce(INIT_FUNC_ARGS);
337352
extern void php_phongo_session_init_ce(INIT_FUNC_ARGS);
353+
extern void php_phongo_topologydescription_init_ce(INIT_FUNC_ARGS);
338354
extern void php_phongo_writeconcern_init_ce(INIT_FUNC_ARGS);
339355
extern void php_phongo_writeconcernerror_init_ce(INIT_FUNC_ARGS);
340356
extern void php_phongo_writeerror_init_ce(INIT_FUNC_ARGS);
@@ -362,7 +378,9 @@ extern void php_phongo_commandfailedevent_init_ce(INIT_FUNC_ARGS);
362378
extern void php_phongo_commandstartedevent_init_ce(INIT_FUNC_ARGS);
363379
extern void php_phongo_commandsubscriber_init_ce(INIT_FUNC_ARGS);
364380
extern void php_phongo_commandsucceededevent_init_ce(INIT_FUNC_ARGS);
381+
extern void php_phongo_sdamsubscriber_init_ce(INIT_FUNC_ARGS);
365382
extern void php_phongo_subscriber_init_ce(INIT_FUNC_ARGS);
383+
extern void php_phongo_topologychangedevent_init_ce(INIT_FUNC_ARGS);
366384

367385
/* Shared function entries for disabling constructors and unserialize() */
368386
PHP_FUNCTION(MongoDB_disabled___construct);

php_phongo_structs.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ typedef struct {
127127
zend_object std;
128128
} php_phongo_session_t;
129129

130+
typedef struct {
131+
mongoc_topology_description_t* topology_description;
132+
HashTable* properties;
133+
zend_object std;
134+
} php_phongo_topologydescription_t;
135+
130136
typedef struct {
131137
HashTable* properties;
132138
mongoc_write_concern_t* write_concern;
@@ -284,6 +290,13 @@ typedef struct {
284290
zend_object std;
285291
} php_phongo_commandsucceededevent_t;
286292

293+
typedef struct {
294+
bson_oid_t topology_id;
295+
mongoc_topology_description_t* new_topology_description;
296+
mongoc_topology_description_t* old_topology_description;
297+
zend_object std;
298+
} php_phongo_topologychangedevent_t;
299+
287300
#endif /* PHONGO_STRUCTS */
288301

289302
/*
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2021-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <php.h>
18+
#include <Zend/zend_interfaces.h>
19+
20+
#ifdef HAVE_CONFIG_H
21+
#include "config.h"
22+
#endif
23+
24+
#include "phongo_compat.h"
25+
#include "php_phongo.h"
26+
27+
zend_class_entry* php_phongo_sdamsubscriber_ce;
28+
29+
/* {{{ MongoDB\Driver\Monitoring\SDAMSubscriber function entries */
30+
ZEND_BEGIN_ARG_INFO_EX(ai_SDAMSubscriber_topologyChanged, 0, 0, 1)
31+
ZEND_ARG_OBJ_INFO(0, event, MongoDB\\Driver\\Monitoring\\TopologyChangedEvent, 0)
32+
ZEND_END_ARG_INFO()
33+
34+
static zend_function_entry php_phongo_sdamsubscriber_me[] = {
35+
/* clang-format off */
36+
ZEND_ABSTRACT_ME(SDAMSubscriber, topologyChanged, ai_SDAMSubscriber_topologyChanged)
37+
PHP_FE_END
38+
/* clang-format on */
39+
};
40+
/* }}} */
41+
42+
void php_phongo_sdamsubscriber_init_ce(INIT_FUNC_ARGS) /* {{{ */
43+
{
44+
zend_class_entry ce;
45+
(void) type;
46+
(void) module_number;
47+
48+
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver\\Monitoring", "SDAMSubscriber", php_phongo_sdamsubscriber_me);
49+
php_phongo_sdamsubscriber_ce = zend_register_internal_interface(&ce);
50+
zend_class_implements(php_phongo_sdamsubscriber_ce, 1, php_phongo_subscriber_ce);
51+
52+
return;
53+
} /* }}} */
54+
55+
/*
56+
* Local variables:
57+
* tab-width: 4
58+
* c-basic-offset: 4
59+
* End:
60+
* vim600: noet sw=4 ts=4 fdm=marker
61+
* vim<600: noet sw=4 ts=4
62+
*/

0 commit comments

Comments
 (0)