59c44f7b89be4920e2fc6231652a1decaeb4ed53
[babeltrace.git] / src / lib / graph / query-executor.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_TAG "LIB/QUERY-EXECUTOR"
24 #include "lib/logging.h"
25
26 #include "common/assert.h"
27 #include "common/common.h"
28 #include "lib/assert-pre.h"
29 #include "lib/assert-post.h"
30 #include <babeltrace2/graph/query-executor-const.h>
31 #include <babeltrace2/graph/query-executor.h>
32 #include <babeltrace2/graph/component-class.h>
33 #include <babeltrace2/value.h>
34 #include <babeltrace2/value-const.h>
35 #include "lib/object.h"
36 #include "compat/compiler.h"
37
38 #include "component-class.h"
39 #include "query-executor.h"
40
41 static
42 void bt_query_executor_destroy(struct bt_object *obj)
43 {
44 struct bt_query_executor *query_exec =
45 container_of(obj, struct bt_query_executor, base);
46
47 BT_LOGD("Destroying query executor: addr=%p", query_exec);
48 g_free(query_exec);
49 }
50
51 struct bt_query_executor *bt_query_executor_create(void)
52 {
53 struct bt_query_executor *query_exec;
54
55 BT_LOGD_STR("Creating query executor.");
56 query_exec = g_new0(struct bt_query_executor, 1);
57 if (!query_exec) {
58 BT_LOGE_STR("Failed to allocate one query executor.");
59 goto end;
60 }
61
62 bt_object_init_shared(&query_exec->base,
63 bt_query_executor_destroy);
64 BT_LOGD("Created query executor: addr=%p", query_exec);
65
66 end:
67 return (void *) query_exec;
68 }
69
70 enum bt_query_executor_status bt_query_executor_query(
71 struct bt_query_executor *query_exec,
72 const struct bt_component_class *comp_cls,
73 const char *object, const struct bt_value *params,
74 bt_logging_level log_level,
75 const struct bt_value **user_result)
76 {
77 typedef enum bt_query_status (*method_t)(void *, const void *,
78 const void *, const void *, bt_logging_level, const void *);
79
80 enum bt_query_status status;
81 enum bt_query_executor_status exec_status;
82 method_t method = NULL;
83
84 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
85 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
86 BT_ASSERT_PRE_NON_NULL(object, "Object");
87 BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)");
88 BT_ASSERT_PRE(!query_exec->canceled, "Query executor is canceled.");
89
90 if (!params) {
91 params = bt_value_null;
92 }
93
94 switch (comp_cls->type) {
95 case BT_COMPONENT_CLASS_TYPE_SOURCE:
96 {
97 struct bt_component_class_source *src_cc = (void *) comp_cls;
98
99 method = (method_t) src_cc->methods.query;
100 break;
101 }
102 case BT_COMPONENT_CLASS_TYPE_FILTER:
103 {
104 struct bt_component_class_filter *flt_cc = (void *) comp_cls;
105
106 method = (method_t) flt_cc->methods.query;
107 break;
108 }
109 case BT_COMPONENT_CLASS_TYPE_SINK:
110 {
111 struct bt_component_class_sink *sink_cc = (void *) comp_cls;
112
113 method = (method_t) sink_cc->methods.query;
114 break;
115 }
116 default:
117 abort();
118 }
119
120 if (!method) {
121 /* Not an error: nothing to query */
122 BT_LIB_LOGD("Component class has no registered query method: "
123 "%!+C", comp_cls);
124 exec_status = BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED;
125 goto end;
126 }
127
128 BT_LIB_LOGD("Calling user's query method: "
129 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, "
130 "log-level=%s",
131 query_exec, comp_cls, object, params,
132 bt_common_logging_level_string(log_level));
133 *user_result = NULL;
134 status = method((void *) comp_cls, query_exec, object, params,
135 log_level, user_result);
136 BT_LIB_LOGD("User method returned: status=%s, %![res-]+v",
137 bt_query_status_string(status), *user_result);
138 BT_ASSERT_POST(status != BT_QUERY_STATUS_OK || *user_result,
139 "User method returned `BT_QUERY_STATUS_OK` without a result.");
140 exec_status = (int) status;
141 if (query_exec->canceled) {
142 BT_OBJECT_PUT_REF_AND_RESET(*user_result);
143 exec_status = BT_QUERY_EXECUTOR_STATUS_CANCELED;
144 goto end;
145 }
146
147 end:
148 return exec_status;
149 }
150
151 enum bt_query_executor_status bt_query_executor_cancel(
152 struct bt_query_executor *query_exec)
153 {
154 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
155 query_exec->canceled = BT_TRUE;
156 BT_LOGI("Canceled query executor: addr=%p", query_exec);
157 return BT_QUERY_EXECUTOR_STATUS_OK;
158 }
159
160 bt_bool bt_query_executor_is_canceled(const struct bt_query_executor *query_exec)
161 {
162 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
163 return query_exec->canceled;
164 }
165
166 void bt_query_executor_get_ref(const struct bt_query_executor *query_executor)
167 {
168 bt_object_get_ref(query_executor);
169 }
170
171 void bt_query_executor_put_ref(const struct bt_query_executor *query_executor)
172 {
173 bt_object_put_ref(query_executor);
174 }
This page took 0.032467 seconds and 3 git commands to generate.