2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "QUERY-EXECUTOR"
24 #include <babeltrace/lib-logging-internal.h>
26 #include <babeltrace/graph/query-executor.h>
27 #include <babeltrace/graph/query-executor-internal.h>
28 #include <babeltrace/graph/component-class.h>
29 #include <babeltrace/graph/component-class-internal.h>
30 #include <babeltrace/values.h>
31 #include <babeltrace/object-internal.h>
32 #include <babeltrace/compiler-internal.h>
35 void bt_query_executor_destroy(struct bt_object
*obj
)
37 struct bt_query_executor
*query_exec
=
38 container_of(obj
, struct bt_query_executor
, base
);
40 BT_LOGD("Destroying port: addr=%p", query_exec
);
44 struct bt_query_executor
*bt_query_executor_create(void)
46 struct bt_query_executor
*query_exec
;
48 BT_LOGD_STR("Creating query executor.");
49 query_exec
= g_new0(struct bt_query_executor
, 1);
51 BT_LOGE_STR("Failed to allocate one query executor.");
55 bt_object_init(query_exec
, bt_query_executor_destroy
);
56 BT_LOGD("Created query executor: addr=%p", query_exec
);
62 enum bt_query_status
bt_query_executor_query(
63 struct bt_query_executor
*query_exec
,
64 struct bt_component_class
*component_class
,
65 const char *object
, struct bt_value
*params
,
66 struct bt_value
**user_result
)
68 struct bt_component_class_query_method_return ret
= {
70 .status
= BT_QUERY_STATUS_OK
,
74 BT_LOGW_STR("Invalid parameter: query executor is NULL.");
75 ret
.status
= BT_QUERY_STATUS_INVALID
;
79 if (query_exec
->canceled
) {
80 BT_LOGW_STR("Invalid parameter: query executor is canceled.");
81 ret
.status
= BT_QUERY_STATUS_EXECUTOR_CANCELED
;
85 if (!component_class
) {
86 BT_LOGW_STR("Invalid parameter: component class is NULL.");
87 ret
.status
= BT_QUERY_STATUS_INVALID
;
92 BT_LOGW_STR("Invalid parameter: object string is NULL.");
93 ret
.status
= BT_QUERY_STATUS_INVALID
;
98 params
= bt_value_null
;
101 if (!component_class
->methods
.query
) {
102 /* Not an error: nothing to query */
103 BT_LOGD("Component class has no registered query method: "
104 "addr=%p, name=\"%s\", type=%s",
106 bt_component_class_get_name(component_class
),
107 bt_component_class_type_string(component_class
->type
));
108 ret
.status
= BT_QUERY_STATUS_ERROR
;
112 BT_LOGD("Calling user's query method: "
113 "query-exec-addr=%p, comp-class-addr=%p, "
114 "comp-class-name=\"%s\", comp-class-type=%s, "
115 "object=\"%s\", params-addr=%p",
116 query_exec
, component_class
,
117 bt_component_class_get_name(component_class
),
118 bt_component_class_type_string(component_class
->type
),
120 ret
= component_class
->methods
.query(component_class
, query_exec
,
122 BT_LOGD("User method returned: status=%s, result-addr=%p",
123 bt_query_status_string(ret
.status
), ret
.result
);
124 if (query_exec
->canceled
) {
126 ret
.status
= BT_QUERY_STATUS_EXECUTOR_CANCELED
;
129 if (ret
.status
== BT_QUERY_STATUS_EXECUTOR_CANCELED
) {
131 * The user cannot decide that the executor is
132 * canceled if it's not.
135 ret
.status
= BT_QUERY_STATUS_ERROR
;
140 switch (ret
.status
) {
141 case BT_QUERY_STATUS_INVALID
:
143 * This is reserved for invalid parameters passed to
147 ret
.status
= BT_QUERY_STATUS_ERROR
;
149 case BT_QUERY_STATUS_OK
:
151 ret
.result
= bt_value_null
;
156 BT_LOGW("User method did not return BT_QUERY_STATUS_OK, but result is not NULL: "
157 "status=%s, result-addr=%p",
158 bt_query_status_string(ret
.status
), ret
.result
);
165 *user_result
= ret
.result
;
173 enum bt_query_status
bt_query_executor_cancel(
174 struct bt_query_executor
*query_exec
)
176 enum bt_query_status ret
= BT_QUERY_STATUS_OK
;
179 BT_LOGW_STR("Invalid parameter: query executor is NULL.");
180 ret
= BT_QUERY_STATUS_INVALID
;
184 query_exec
->canceled
= BT_TRUE
;
185 BT_LOGV("Canceled query executor: addr=%p", query_exec
);
191 bt_bool
bt_query_executor_is_canceled(struct bt_query_executor
*query_exec
)
193 bt_bool canceled
= BT_FALSE
;
196 BT_LOGW_STR("Invalid parameter: query executor is NULL.");
200 canceled
= query_exec
->canceled
;