Skip to content

Commit 770c1b9

Browse files
authored
Remove extra structs for objects with properties (#1992)
* Replace internal structs with simple getters for properties * Introduce PHONGO_PROPERTY_GETTER macro to simplify property getters * Emit warning when truncating 64-bit server connection IDs
1 parent 1eb9430 commit 770c1b9

14 files changed

+353
-1404
lines changed

src/MongoDB/Monitoring/CommandFailedEvent.c

Lines changed: 52 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -32,254 +32,90 @@ zend_class_entry* phongo_commandfailedevent_ce;
3232

3333
PHONGO_DISABLED_CONSTRUCTOR(MongoDB_Driver_Monitoring_CommandFailedEvent)
3434

35-
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getCommandName)
35+
PHONGO_PROPERTY_GETTER(MongoDB_Driver_Monitoring_CommandFailedEvent, getCommandName, commandfailedevent, "commandName")
36+
PHONGO_PROPERTY_GETTER(MongoDB_Driver_Monitoring_CommandFailedEvent, getDatabaseName, commandfailedevent, "databaseName")
37+
PHONGO_PROPERTY_GETTER(MongoDB_Driver_Monitoring_CommandFailedEvent, getDurationMicros, commandfailedevent, "duration")
38+
PHONGO_PROPERTY_GETTER(MongoDB_Driver_Monitoring_CommandFailedEvent, getError, commandfailedevent, "error")
39+
PHONGO_PROPERTY_GETTER(MongoDB_Driver_Monitoring_CommandFailedEvent, getHost, commandfailedevent, "host")
40+
PHONGO_PROPERTY_GETTER(MongoDB_Driver_Monitoring_CommandFailedEvent, getOperationId, commandfailedevent, "operationId")
41+
PHONGO_PROPERTY_GETTER(MongoDB_Driver_Monitoring_CommandFailedEvent, getPort, commandfailedevent, "port")
42+
PHONGO_PROPERTY_GETTER(MongoDB_Driver_Monitoring_CommandFailedEvent, getReply, commandfailedevent, "reply")
43+
PHONGO_PROPERTY_GETTER(MongoDB_Driver_Monitoring_CommandFailedEvent, getRequestId, commandfailedevent, "requestId")
44+
PHONGO_PROPERTY_GETTER(MongoDB_Driver_Monitoring_CommandFailedEvent, getServiceId, commandfailedevent, "serviceId")
45+
PHONGO_PROPERTY_GETTER(MongoDB_Driver_Monitoring_CommandFailedEvent, getServerConnectionId, commandfailedevent, "serverConnectionId")
46+
47+
static void phongo_commandfailedevent_update_properties(zend_object* object, const mongoc_apm_command_failed_t* event)
3648
{
37-
PHONGO_INTERN_FROM_THIS(commandfailedevent);
38-
39-
PHONGO_PARSE_PARAMETERS_NONE();
40-
41-
RETVAL_STRING(intern->command_name);
42-
}
43-
44-
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getDatabaseName)
45-
{
46-
PHONGO_INTERN_FROM_THIS(commandfailedevent);
47-
48-
PHONGO_PARSE_PARAMETERS_NONE();
49-
50-
RETVAL_STRING(intern->database_name);
51-
}
52-
53-
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getDurationMicros)
54-
{
55-
PHONGO_INTERN_FROM_THIS(commandfailedevent);
56-
57-
PHONGO_PARSE_PARAMETERS_NONE();
58-
59-
RETURN_LONG(intern->duration_micros);
60-
}
61-
62-
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getError)
63-
{
64-
PHONGO_INTERN_FROM_THIS(commandfailedevent);
65-
66-
PHONGO_PARSE_PARAMETERS_NONE();
67-
68-
RETURN_ZVAL(&intern->z_error, 1, 0);
69-
}
70-
71-
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getHost)
72-
{
73-
PHONGO_INTERN_FROM_THIS(commandfailedevent);
74-
75-
PHONGO_PARSE_PARAMETERS_NONE();
76-
77-
RETVAL_STRING(intern->host.host);
78-
}
79-
80-
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getOperationId)
81-
{
82-
PHONGO_INTERN_FROM_THIS(commandfailedevent);
83-
84-
char operation_id[24];
85-
86-
PHONGO_PARSE_PARAMETERS_NONE();
87-
88-
snprintf(operation_id, sizeof(operation_id), "%" PRId64, intern->operation_id);
89-
RETVAL_STRING(operation_id);
90-
}
91-
92-
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getPort)
93-
{
94-
PHONGO_INTERN_FROM_THIS(commandfailedevent);
95-
96-
PHONGO_PARSE_PARAMETERS_NONE();
97-
98-
RETVAL_LONG(intern->host.port);
99-
}
100-
101-
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getReply)
102-
{
103-
PHONGO_INTERN_FROM_THIS(commandfailedevent);
104-
105-
phongo_bson_state state;
106-
107-
PHONGO_BSON_INIT_STATE(state);
108-
109-
PHONGO_PARSE_PARAMETERS_NONE();
110-
111-
if (!phongo_bson_to_zval_ex(intern->reply, &state)) {
112-
zval_ptr_dtor(&state.zchild);
113-
return;
114-
}
115-
116-
RETURN_ZVAL(&state.zchild, 0, 1);
117-
}
118-
119-
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getRequestId)
120-
{
121-
PHONGO_INTERN_FROM_THIS(commandfailedevent);
122-
123-
char request_id[24];
124-
125-
PHONGO_PARSE_PARAMETERS_NONE();
126-
127-
snprintf(request_id, sizeof(request_id), "%" PRId64, intern->request_id);
128-
RETVAL_STRING(request_id);
129-
}
130-
131-
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getServiceId)
132-
{
133-
PHONGO_INTERN_FROM_THIS(commandfailedevent);
49+
char operation_id[24], request_id[24];
50+
phongo_bson_state reply_state;
51+
int64_t server_connection_id = mongoc_apm_command_failed_get_server_connection_id_int64(event);
13452

135-
PHONGO_PARSE_PARAMETERS_NONE();
53+
PHONGO_BSON_INIT_STATE(reply_state);
13654

137-
if (!intern->has_service_id) {
138-
RETURN_NULL();
139-
}
55+
zend_update_property_string(phongo_commandfailedevent_ce, object, ZEND_STRL("host"), mongoc_apm_command_failed_get_host(event)->host);
56+
zend_update_property_long(phongo_commandfailedevent_ce, object, ZEND_STRL("port"), mongoc_apm_command_failed_get_host(event)->port);
57+
zend_update_property_string(phongo_commandfailedevent_ce, object, ZEND_STRL("commandName"), mongoc_apm_command_failed_get_command_name(event));
58+
zend_update_property_string(phongo_commandfailedevent_ce, object, ZEND_STRL("databaseName"), mongoc_apm_command_failed_get_database_name(event));
59+
zend_update_property_long(phongo_commandfailedevent_ce, object, ZEND_STRL("duration"), mongoc_apm_command_failed_get_duration(event));
14060

141-
phongo_objectid_new(return_value, &intern->service_id);
142-
}
61+
{
62+
bson_error_t tmp_error = { 0 };
63+
zval z_error;
14364

144-
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getServerConnectionId)
145-
{
146-
PHONGO_INTERN_FROM_THIS(commandfailedevent);
65+
mongoc_apm_command_failed_get_error(event, &tmp_error);
14766

148-
PHONGO_PARSE_PARAMETERS_NONE();
67+
object_init_ex(&z_error, phongo_exception_from_mongoc_domain(tmp_error.domain, tmp_error.code));
68+
zend_update_property_string(zend_ce_exception, Z_OBJ_P(&z_error), ZEND_STRL("message"), tmp_error.message);
69+
zend_update_property_long(zend_ce_exception, Z_OBJ_P(&z_error), ZEND_STRL("code"), tmp_error.code);
14970

150-
/* TODO: Use MONGOC_NO_SERVER_CONNECTION_ID once it is added to libmongoc's public API (CDRIVER-4176) */
151-
if (intern->server_connection_id == -1) {
152-
RETURN_NULL();
71+
zend_update_property(phongo_commandfailedevent_ce, object, ZEND_STRL("error"), &z_error);
72+
zval_ptr_dtor(&z_error);
15373
}
15474

155-
#if SIZEOF_ZEND_LONG == 4
156-
if (intern->server_connection_id > INT32_MAX || intern->server_connection_id < INT32_MIN) {
157-
zend_error(E_WARNING, "Truncating 64-bit value %" PRId64 " for serverConnectionId", intern->server_connection_id);
158-
}
159-
#endif
160-
161-
RETURN_LONG(intern->server_connection_id);
162-
}
163-
164-
static void phongo_commandfailedevent_update_properties(phongo_commandfailedevent_t* intern)
165-
{
166-
char operation_id[24], request_id[24];
167-
phongo_bson_state reply_state;
168-
169-
PHONGO_BSON_INIT_STATE(reply_state);
170-
171-
zend_update_property_string(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("host"), intern->host.host);
172-
zend_update_property_long(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("port"), intern->host.port);
173-
zend_update_property_string(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("commandName"), intern->command_name);
174-
zend_update_property_string(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("databaseName"), intern->database_name);
175-
zend_update_property_long(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("duration"), intern->duration_micros);
176-
zend_update_property(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("error"), &intern->z_error);
177-
178-
if (phongo_bson_to_zval_ex(intern->reply, &reply_state)) {
179-
zend_update_property(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("reply"), &reply_state.zchild);
75+
if (phongo_bson_to_zval_ex(mongoc_apm_command_failed_get_reply(event), &reply_state)) {
76+
zend_update_property(phongo_commandfailedevent_ce, object, ZEND_STRL("reply"), &reply_state.zchild);
18077
}
18178
zval_ptr_dtor(&reply_state.zchild);
18279

183-
snprintf(operation_id, sizeof(operation_id), "%" PRId64, intern->operation_id);
184-
zend_update_property_string(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("operationId"), operation_id);
80+
snprintf(operation_id, sizeof(operation_id), "%" PRId64, mongoc_apm_command_failed_get_operation_id(event));
81+
zend_update_property_string(phongo_commandfailedevent_ce, object, ZEND_STRL("operationId"), operation_id);
18582

186-
snprintf(request_id, sizeof(request_id), "%" PRId64, intern->request_id);
187-
zend_update_property_string(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("requestId"), request_id);
83+
snprintf(request_id, sizeof(request_id), "%" PRId64, mongoc_apm_command_failed_get_request_id(event));
84+
zend_update_property_string(phongo_commandfailedevent_ce, object, ZEND_STRL("requestId"), request_id);
18885

189-
if (intern->has_service_id) {
86+
if (mongoc_apm_command_failed_get_service_id(event)) {
19087
zval service_id;
19188

192-
if (phongo_objectid_new(&service_id, &intern->service_id)) {
193-
zend_update_property(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("serviceId"), &service_id);
89+
if (phongo_objectid_new(&service_id, mongoc_apm_command_failed_get_service_id(event))) {
90+
zend_update_property(phongo_commandfailedevent_ce, object, ZEND_STRL("serviceId"), &service_id);
19491
zval_ptr_dtor(&service_id);
19592
}
19693
} else {
197-
zend_update_property_null(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("serviceId"));
94+
zend_update_property_null(phongo_commandfailedevent_ce, object, ZEND_STRL("serviceId"));
19895
}
19996

20097
/* TODO: Use MONGOC_NO_SERVER_CONNECTION_ID once it is added to libmongoc's public API (CDRIVER-4176) */
201-
if (intern->server_connection_id == -1) {
202-
zend_update_property_null(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("serverConnectionId"));
98+
if (server_connection_id == -1) {
99+
zend_update_property_null(phongo_commandfailedevent_ce, object, ZEND_STRL("serverConnectionId"));
203100
} else {
204-
zend_update_property_long(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("serverConnectionId"), intern->server_connection_id);
205-
}
206-
}
207-
208-
/* MongoDB\Driver\Monitoring\CommandFailedEvent object handlers */
209-
static zend_object_handlers phongo_handler_commandfailedevent;
210-
211-
static void phongo_commandfailedevent_free_object(zend_object* object)
212-
{
213-
PHONGO_INTERN_FROM_Z_OBJ(commandfailedevent, object);
214-
215-
zend_object_std_dtor(&intern->std);
216-
217-
if (!Z_ISUNDEF(intern->z_error)) {
218-
zval_ptr_dtor(&intern->z_error);
219-
}
220-
221-
if (intern->reply) {
222-
bson_destroy(intern->reply);
223-
}
224-
225-
if (intern->command_name) {
226-
efree(intern->command_name);
227-
}
101+
#if SIZEOF_ZEND_LONG == 4
102+
if (server_connection_id > INT32_MAX || server_connection_id < INT32_MIN) {
103+
zend_error(E_WARNING, "Truncating 64-bit value %" PRId64 " for serverConnectionId", server_connection_id);
104+
}
105+
#endif
228106

229-
if (intern->database_name) {
230-
efree(intern->database_name);
107+
zend_update_property_long(phongo_commandfailedevent_ce, object, ZEND_STRL("serverConnectionId"), server_connection_id);
231108
}
232109
}
233110

234-
static zend_object* phongo_commandfailedevent_create_object(zend_class_entry* class_type)
235-
{
236-
PHONGO_INTERN_OBJECT_ALLOC(commandfailedevent, class_type);
237-
238-
intern->std.handlers = &phongo_handler_commandfailedevent;
239-
240-
return &intern->std;
241-
}
242-
243111
void phongo_commandfailedevent_init_ce(INIT_FUNC_ARGS)
244112
{
245-
phongo_commandfailedevent_ce = register_class_MongoDB_Driver_Monitoring_CommandFailedEvent();
246-
phongo_commandfailedevent_ce->create_object = phongo_commandfailedevent_create_object;
247-
248-
memcpy(&phongo_handler_commandfailedevent, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
249-
phongo_handler_commandfailedevent.free_obj = phongo_commandfailedevent_free_object;
250-
phongo_handler_commandfailedevent.offset = XtOffsetOf(phongo_commandfailedevent_t, std);
113+
phongo_commandfailedevent_ce = register_class_MongoDB_Driver_Monitoring_CommandFailedEvent();
251114
}
252115

253116
void phongo_commandfailedevent_init(zval* return_value, const mongoc_apm_command_failed_t* event)
254117
{
255-
PHONGO_INTERN_INIT_EX(commandfailedevent, return_value);
256-
257-
bson_error_t tmp_error = { 0 };
258-
259-
memcpy(&intern->host, mongoc_apm_command_failed_get_host(event), sizeof(mongoc_host_list_t));
260-
261-
intern->command_name = estrdup(mongoc_apm_command_failed_get_command_name(event));
262-
intern->database_name = estrdup(mongoc_apm_command_failed_get_database_name(event));
263-
intern->server_id = mongoc_apm_command_failed_get_server_id(event);
264-
intern->operation_id = mongoc_apm_command_failed_get_operation_id(event);
265-
intern->request_id = mongoc_apm_command_failed_get_request_id(event);
266-
intern->duration_micros = mongoc_apm_command_failed_get_duration(event);
267-
intern->reply = bson_copy(mongoc_apm_command_failed_get_reply(event));
268-
intern->server_connection_id = mongoc_apm_command_failed_get_server_connection_id_int64(event);
269-
intern->has_service_id = mongoc_apm_command_failed_get_service_id(event) != NULL;
270-
271-
if (intern->has_service_id) {
272-
bson_oid_copy(mongoc_apm_command_failed_get_service_id(event), &intern->service_id);
273-
}
274-
275-
/* We need to process and convert the error right here, otherwise
276-
* debug_info will turn into a recursive loop, and with the wrong trace
277-
* locations */
278-
mongoc_apm_command_failed_get_error(event, &tmp_error);
279-
280-
object_init_ex(&intern->z_error, phongo_exception_from_mongoc_domain(tmp_error.domain, tmp_error.code));
281-
zend_update_property_string(zend_ce_exception, Z_OBJ_P(&intern->z_error), ZEND_STRL("message"), tmp_error.message);
282-
zend_update_property_long(zend_ce_exception, Z_OBJ_P(&intern->z_error), ZEND_STRL("code"), tmp_error.code);
118+
PHONGO_OBJECT_INIT_EX(commandfailedevent, return_value);
283119

284-
phongo_commandfailedevent_update_properties(intern);
120+
phongo_commandfailedevent_update_properties(object, event);
285121
}

0 commit comments

Comments
 (0)