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