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_shared(&query_exec
->base
,
56 bt_query_executor_destroy
);
57 BT_LOGD("Created query executor: addr=%p", query_exec
);
63 enum bt_query_status
bt_query_executor_query(
64 struct bt_query_executor
*query_exec
,
65 struct bt_component_class
*component_class
,
66 const char *object
, struct bt_value
*params
,
67 struct bt_value
**user_result
)
69 struct bt_component_class_query_method_return ret
= {
71 .status
= BT_QUERY_STATUS_OK
,
75 BT_LOGW_STR("Invalid parameter: query executor is NULL.");
76 ret
.status
= BT_QUERY_STATUS_INVALID
;
80 if (query_exec
->canceled
) {
81 BT_LOGW_STR("Invalid parameter: query executor is canceled.");
82 ret
.status
= BT_QUERY_STATUS_EXECUTOR_CANCELED
;
86 if (!component_class
) {
87 BT_LOGW_STR("Invalid parameter: component class is NULL.");
88 ret
.status
= BT_QUERY_STATUS_INVALID
;
93 BT_LOGW_STR("Invalid parameter: object string is NULL.");
94 ret
.status
= BT_QUERY_STATUS_INVALID
;
99 params
= bt_value_null
;
102 if (!component_class
->methods
.query
) {
103 /* Not an error: nothing to query */
104 BT_LOGD("Component class has no registered query method: "
105 "addr=%p, name=\"%s\", type=%s",
107 bt_component_class_get_name(component_class
),
108 bt_component_class_type_string(component_class
->type
));
109 ret
.status
= BT_QUERY_STATUS_ERROR
;
113 BT_LOGD("Calling user's query method: "
114 "query-exec-addr=%p, comp-class-addr=%p, "
115 "comp-class-name=\"%s\", comp-class-type=%s, "
116 "object=\"%s\", params-addr=%p",
117 query_exec
, component_class
,
118 bt_component_class_get_name(component_class
),
119 bt_component_class_type_string(component_class
->type
),
121 ret
= component_class
->methods
.query(component_class
, query_exec
,
123 BT_LOGD("User method returned: status=%s, result-addr=%p",
124 bt_query_status_string(ret
.status
), ret
.result
);
125 if (query_exec
->canceled
) {
126 BT_OBJECT_PUT_REF_AND_RESET(ret
.result
);
127 ret
.status
= BT_QUERY_STATUS_EXECUTOR_CANCELED
;
130 if (ret
.status
== BT_QUERY_STATUS_EXECUTOR_CANCELED
) {
132 * The user cannot decide that the executor is
133 * canceled if it's not.
135 BT_OBJECT_PUT_REF_AND_RESET(ret
.result
);
136 ret
.status
= BT_QUERY_STATUS_ERROR
;
141 switch (ret
.status
) {
142 case BT_QUERY_STATUS_INVALID
:
144 * This is reserved for invalid parameters passed to
147 BT_OBJECT_PUT_REF_AND_RESET(ret
.result
);
148 ret
.status
= BT_QUERY_STATUS_ERROR
;
150 case BT_QUERY_STATUS_OK
:
152 ret
.result
= bt_value_null
;
157 BT_LOGW("User method did not return BT_QUERY_STATUS_OK, but result is not NULL: "
158 "status=%s, result-addr=%p",
159 bt_query_status_string(ret
.status
), ret
.result
);
160 BT_OBJECT_PUT_REF_AND_RESET(ret
.result
);
166 *user_result
= ret
.result
;
170 bt_object_put_ref(ret
.result
);
174 enum bt_query_status
bt_query_executor_cancel(
175 struct bt_query_executor
*query_exec
)
177 enum bt_query_status ret
= BT_QUERY_STATUS_OK
;
180 BT_LOGW_STR("Invalid parameter: query executor is NULL.");
181 ret
= BT_QUERY_STATUS_INVALID
;
185 query_exec
->canceled
= BT_TRUE
;
186 BT_LOGV("Canceled query executor: addr=%p", query_exec
);
192 bt_bool
bt_query_executor_is_canceled(struct bt_query_executor
*query_exec
)
194 bt_bool canceled
= BT_FALSE
;
197 BT_LOGW_STR("Invalid parameter: query executor is NULL.");
201 canceled
= query_exec
->canceled
;