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