lib: rename *_IS_TYPE() and *_HAS_ID() macros -> *_HAS_TYPE()
[babeltrace.git] / src / lib / graph / query-executor.c
CommitLineData
c7eee084 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
c7eee084 3 *
0235b0db 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
c7eee084
PP
5 */
6
350ad6c1 7#define BT_LOG_TAG "LIB/QUERY-EXECUTOR"
c2d9d9cf 8#include "lib/logging.h"
c7eee084 9
578e048b 10#include "common/assert.h"
f4e38e70 11#include "common/common.h"
d98421f2 12#include "lib/assert-cond.h"
3fadfbc0 13#include <babeltrace2/graph/query-executor.h>
3fadfbc0 14#include <babeltrace2/graph/component-class.h>
43c59509 15#include <babeltrace2/graph/query-executor.h>
3fadfbc0 16#include <babeltrace2/value.h>
578e048b
MJ
17#include "lib/object.h"
18#include "compat/compiler.h"
19
20#include "component-class.h"
21#include "query-executor.h"
9b4f9b42 22#include "interrupter.h"
d24d5663 23#include "lib/func-status.h"
c7eee084
PP
24
25static
26void bt_query_executor_destroy(struct bt_object *obj)
27{
28 struct bt_query_executor *query_exec =
29 container_of(obj, struct bt_query_executor, base);
30
d94d92ac 31 BT_LOGD("Destroying query executor: addr=%p", query_exec);
9b4f9b42
PP
32
33 if (query_exec->interrupters) {
34 BT_LOGD_STR("Putting interrupters.");
35 g_ptr_array_free(query_exec->interrupters, TRUE);
36 query_exec->interrupters = NULL;
37 }
38
3c729b9a
PP
39 BT_LOGD_STR("Putting component class.");
40 BT_OBJECT_PUT_REF_AND_RESET(query_exec->comp_cls);
41
42 if (query_exec->object) {
43 g_string_free(query_exec->object, TRUE);
44 query_exec->object = NULL;
45 }
9b4f9b42 46
3c729b9a
PP
47 BT_LOGD_STR("Putting parameters.");
48 BT_OBJECT_PUT_REF_AND_RESET(query_exec->params);
49 BT_OBJECT_PUT_REF_AND_RESET(query_exec->default_interrupter);
c7eee084
PP
50 g_free(query_exec);
51}
52
7c14d641 53struct bt_query_executor *bt_query_executor_create_with_method_data(
3c729b9a 54 const bt_component_class *comp_cls, const char *object,
7c14d641 55 const bt_value *params, void *method_data)
c7eee084
PP
56{
57 struct bt_query_executor *query_exec;
58
17f3083a 59 BT_ASSERT_PRE_NO_ERROR();
3c729b9a
PP
60 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
61 BT_ASSERT_PRE_NON_NULL(object, "Object");
62 BT_LIB_LOGD("Creating query executor: "
fb739d29 63 "%![comp-cls-]+C, object=\"%s\", %![params-]+v",
3c729b9a 64 comp_cls, object, params);
c7eee084
PP
65 query_exec = g_new0(struct bt_query_executor, 1);
66 if (!query_exec) {
870631a2
PP
67 BT_LIB_LOGE_APPEND_CAUSE(
68 "Failed to allocate one query executor.");
c7eee084
PP
69 goto end;
70 }
71
9b4f9b42 72 query_exec->interrupters = g_ptr_array_new_with_free_func(
6871026b 73 (GDestroyNotify) bt_object_put_ref_no_null_check);
9b4f9b42
PP
74 if (!query_exec->interrupters) {
75 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
76 BT_OBJECT_PUT_REF_AND_RESET(query_exec);
77 goto end;
78 }
79
80 query_exec->default_interrupter = bt_interrupter_create();
81 if (!query_exec->default_interrupter) {
82 BT_LIB_LOGE_APPEND_CAUSE(
83 "Failed to create one interrupter object.");
84 BT_OBJECT_PUT_REF_AND_RESET(query_exec);
85 goto end;
86 }
87
3c729b9a
PP
88 query_exec->object = g_string_new(object);
89 if (!query_exec->object) {
90 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
91 BT_OBJECT_PUT_REF_AND_RESET(query_exec);
92 goto end;
93 }
94
95 query_exec->comp_cls = comp_cls;
6871026b 96 bt_object_get_ref_no_null_check(query_exec->comp_cls);
3c729b9a
PP
97
98 if (!params) {
99 query_exec->params = bt_value_null;
100 } else {
101 query_exec->params = params;
102 }
103
6871026b 104 bt_object_get_ref_no_null_check(query_exec->params);
7c14d641 105 query_exec->method_data = method_data;
3c729b9a 106 query_exec->log_level = BT_LOGGING_LEVEL_NONE;
9b4f9b42
PP
107 bt_query_executor_add_interrupter(query_exec,
108 query_exec->default_interrupter);
3fea54f6
PP
109 bt_object_init_shared(&query_exec->base,
110 bt_query_executor_destroy);
3c729b9a 111 BT_LIB_LOGD("Created query executor: "
fb739d29 112 "addr=%p, %![comp-cls-]+C, object=\"%s\", %![params-]+v",
3c729b9a 113 query_exec, comp_cls, object, params);
c7eee084
PP
114
115end:
d94d92ac 116 return (void *) query_exec;
c7eee084
PP
117}
118
7c14d641
PP
119struct bt_query_executor *bt_query_executor_create(
120 const bt_component_class *comp_cls, const char *object,
121 const bt_value *params)
122{
17f3083a 123 BT_ASSERT_PRE_NO_ERROR();
7c14d641
PP
124 return bt_query_executor_create_with_method_data(comp_cls,
125 object, params, NULL);
126}
127
d24d5663 128enum bt_query_executor_query_status bt_query_executor_query(
0d72b8c3 129 struct bt_query_executor *query_exec,
05e21286 130 const struct bt_value **user_result)
c7eee084 131{
3c729b9a
PP
132 typedef enum bt_component_class_query_method_status (*method_t)(
133 void * /* self component class */,
134 void * /* private query executor */,
135 const char * /* object */,
136 const struct bt_value * /* parameters */,
7c14d641 137 void * /* method data */,
3c729b9a 138 const struct bt_value ** /* result */);
c7eee084 139
d24d5663
PP
140 enum bt_query_executor_query_status status;
141 enum bt_component_class_query_method_status query_status;
d94d92ac 142 method_t method = NULL;
c7eee084 143
17f3083a 144 BT_ASSERT_PRE_NO_ERROR();
d94d92ac 145 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
d94d92ac 146 BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)");
d94d92ac 147
647c6d45
PP
148 /*
149 * Initial check: is the query executor already interrupted? If
150 * so, return `BT_FUNC_STATUS_AGAIN`. Returning this status is
151 * harmless: it's not `BT_FUNC_STATUS_OK` (there's no result),
152 * and it's not `BT_FUNC_STATUS_ERROR` either (there's no
153 * legitimate error). Since any query operation could return
154 * `BT_FUNC_STATUS_AGAIN` when interrupted or instead of
155 * blocking, the caller is responsible for checking the
156 * interruption state of the query executor when getting this
157 * status.
158 */
159 if (bt_query_executor_is_interrupted(query_exec)) {
160 BT_LIB_LOGD("Query executor is interrupted: "
161 "not performing the query operation: "
162 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", "
163 "%![params-]+v, log-level=%s",
3c729b9a
PP
164 query_exec, query_exec->comp_cls,
165 query_exec->object->str, query_exec->params,
166 bt_common_logging_level_string(query_exec->log_level));
647c6d45
PP
167 status = BT_FUNC_STATUS_AGAIN;
168 goto end;
169 }
170
3c729b9a 171 switch (query_exec->comp_cls->type) {
d94d92ac
PP
172 case BT_COMPONENT_CLASS_TYPE_SOURCE:
173 {
3c729b9a
PP
174 struct bt_component_class_source *src_cc = (void *)
175 query_exec->comp_cls;
d94d92ac
PP
176
177 method = (method_t) src_cc->methods.query;
178 break;
c7eee084 179 }
d94d92ac
PP
180 case BT_COMPONENT_CLASS_TYPE_FILTER:
181 {
3c729b9a
PP
182 struct bt_component_class_filter *flt_cc = (void *)
183 query_exec->comp_cls;
c7eee084 184
d94d92ac
PP
185 method = (method_t) flt_cc->methods.query;
186 break;
c7eee084 187 }
d94d92ac
PP
188 case BT_COMPONENT_CLASS_TYPE_SINK:
189 {
3c729b9a
PP
190 struct bt_component_class_sink *sink_cc = (void *)
191 query_exec->comp_cls;
c7eee084 192
d94d92ac
PP
193 method = (method_t) sink_cc->methods.query;
194 break;
195 }
196 default:
498e7994 197 bt_common_abort();
c7eee084
PP
198 }
199
d94d92ac 200 if (!method) {
c7eee084 201 /* Not an error: nothing to query */
d94d92ac 202 BT_LIB_LOGD("Component class has no registered query method: "
3c729b9a 203 "%!+C", query_exec->comp_cls);
76b6c2f7 204 status = BT_FUNC_STATUS_UNKNOWN_OBJECT;
c7eee084
PP
205 goto end;
206 }
207
d94d92ac 208 BT_LIB_LOGD("Calling user's query method: "
f4e38e70
PP
209 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, "
210 "log-level=%s",
3c729b9a
PP
211 query_exec, query_exec->comp_cls, query_exec->object->str,
212 query_exec->params,
213 bt_common_logging_level_string(query_exec->log_level));
d94d92ac 214 *user_result = NULL;
3c729b9a
PP
215 query_status = method((void *) query_exec->comp_cls,
216 (void *) query_exec, query_exec->object->str,
7c14d641 217 query_exec->params, query_exec->method_data, user_result);
d94d92ac 218 BT_LIB_LOGD("User method returned: status=%s, %![res-]+v",
d24d5663
PP
219 bt_common_func_status_string(query_status), *user_result);
220 BT_ASSERT_POST(query_status != BT_FUNC_STATUS_OK || *user_result,
221 "User method returned `BT_FUNC_STATUS_OK` without a result.");
6ecdcca3 222 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(query_status);
d24d5663 223 status = (int) query_status;
c7eee084 224
a635e507
SM
225 if (status < 0) {
226 BT_LIB_LOGW_APPEND_CAUSE(
227 "Component class's \"query\" method failed: "
228 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", "
3c729b9a
PP
229 "%![params-]+v, log-level=%s", query_exec,
230 query_exec->comp_cls, query_exec->object->str,
231 query_exec->params,
232 bt_common_logging_level_string(query_exec->log_level));
a635e507
SM
233 goto end;
234 }
235
c7eee084 236end:
d24d5663 237 return status;
c7eee084
PP
238}
239
9b4f9b42
PP
240enum bt_query_executor_add_interrupter_status bt_query_executor_add_interrupter(
241 struct bt_query_executor *query_exec,
242 const struct bt_interrupter *intr)
c7eee084 243{
17f3083a 244 BT_ASSERT_PRE_NO_ERROR();
d94d92ac 245 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
9b4f9b42
PP
246 BT_ASSERT_PRE_NON_NULL(intr, "Interrupter");
247 g_ptr_array_add(query_exec->interrupters, (void *) intr);
6871026b 248 bt_object_get_ref_no_null_check(intr);
9b4f9b42
PP
249 BT_LIB_LOGD("Added interrupter to query executor: "
250 "query-exec-addr=%p, %![intr-]+z",
251 query_exec, intr);
d24d5663 252 return BT_FUNC_STATUS_OK;
c7eee084
PP
253}
254
9b4f9b42 255bt_bool bt_query_executor_is_interrupted(const struct bt_query_executor *query_exec)
c7eee084 256{
9b4f9b42
PP
257 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
258 return (bt_bool) bt_interrupter_array_any_is_set(
259 query_exec->interrupters);
260}
261
88d1a0b7
SM
262struct bt_interrupter *bt_query_executor_borrow_default_interrupter(
263 struct bt_query_executor *query_exec)
9b4f9b42
PP
264{
265 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
88d1a0b7 266 return query_exec->default_interrupter;
c7eee084 267}
c5b9b441 268
3c729b9a
PP
269enum bt_query_executor_set_logging_level_status
270bt_query_executor_set_logging_level(struct bt_query_executor *query_exec,
271 enum bt_logging_level log_level)
272{
273 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
274 query_exec->log_level = log_level;
275 return BT_FUNC_STATUS_OK;
276}
277
278enum bt_logging_level bt_query_executor_get_logging_level(
279 const struct bt_query_executor *query_exec)
280{
281 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
282 return query_exec->log_level;
283}
284
c5b9b441
PP
285void bt_query_executor_get_ref(const struct bt_query_executor *query_executor)
286{
287 bt_object_get_ref(query_executor);
288}
289
290void bt_query_executor_put_ref(const struct bt_query_executor *query_executor)
291{
292 bt_object_put_ref(query_executor);
293}
This page took 0.072243 seconds and 4 git commands to generate.