Code

frontend, proto: Include the response data type in query replies.
[sysdb.git] / src / frontend / query.c
1 /*
2  * SysDB - src/frontend/query.c
3  * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include "sysdb.h"
30 #include "core/store.h"
31 #include "frontend/connection-private.h"
32 #include "frontend/parser.h"
33 #include "utils/error.h"
34 #include "utils/strbuf.h"
36 #include <errno.h>
37 #include <string.h>
39 /*
40  * private helper functions
41  */
43 typedef struct {
44         sdb_strbuf_t *buf;
45         sdb_store_matcher_t *filter;
47         size_t last_len;
48 } tojson_data_t;
50 static int
51 lookup_tojson(sdb_store_obj_t *obj, void *user_data)
52 {
53         tojson_data_t *data = user_data;
54         int status;
56         if (data->filter && (! sdb_store_matcher_matches(data->filter, obj, NULL)))
57                 return 0;
59         if (sdb_strbuf_len(data->buf) > data->last_len)
60                 sdb_strbuf_append(data->buf, ",");
61         data->last_len = sdb_strbuf_len(data->buf);
62         status = sdb_store_host_tojson(obj, data->buf,
63                         data->filter, /* flags = */ 0);
64         return status;
65 } /* lookup_tojson */
67 /*
68  * public API
69  */
71 int
72 sdb_fe_query(sdb_conn_t *conn)
73 {
74         sdb_llist_t *parsetree;
75         sdb_conn_node_t *node = NULL;
76         int status = 0;
78         if ((! conn) || (conn->cmd != CONNECTION_QUERY))
79                 return -1;
81         parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf),
82                         (int)conn->cmd_len);
83         if (! parsetree) {
84                 char query[conn->cmd_len + 1];
85                 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
86                 query[sizeof(query) - 1] = '\0';
87                 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s'",
88                                 query);
89                 return -1;
90         }
92         switch (sdb_llist_len(parsetree)) {
93                 case 0:
94                         /* skipping empty command */
95                         break;
96                 case 1:
97                         node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
98                         break;
100                 default:
101                         {
102                                 char query[conn->cmd_len + 1];
103                                 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
104                                 query[sizeof(query) - 1] = '\0';
105                                 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %zu command%s "
106                                                 "in multi-statement query '%s'",
107                                                 sdb_llist_len(parsetree) - 1,
108                                                 sdb_llist_len(parsetree) == 2 ? "" : "s",
109                                                 query);
110                                 node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
111                         }
112         }
114         if (node) {
115                 status = sdb_fe_exec(conn, node);
116                 sdb_object_deref(SDB_OBJ(node));
117         }
119         sdb_llist_destroy(parsetree);
120         return status;
121 } /* sdb_fe_query */
123 int
124 sdb_fe_fetch(sdb_conn_t *conn)
126         char hostname[conn->cmd_len + 1];
127         if ((! conn) || (conn->cmd != CONNECTION_FETCH))
128                 return -1;
129         strncpy(hostname, sdb_strbuf_string(conn->buf), conn->cmd_len);
130         hostname[sizeof(hostname) - 1] = '\0';
131         return sdb_fe_exec_fetch(conn, hostname, /* filter = */ NULL);
132 } /* sdb_fe_fetch */
134 int
135 sdb_fe_list(sdb_conn_t *conn)
137         if ((! conn) || (conn->cmd != CONNECTION_LIST))
138                 return -1;
139         return sdb_fe_exec_list(conn, /* filter = */ NULL);
140 } /* sdb_fe_list */
142 int
143 sdb_fe_lookup(sdb_conn_t *conn)
145         sdb_store_matcher_t *m;
146         int status;
148         if ((! conn) || (conn->cmd != CONNECTION_LOOKUP))
149                 return -1;
151         m = sdb_fe_parse_matcher(sdb_strbuf_string(conn->buf),
152                         (int)conn->cmd_len);
153         if (! m) {
154                 char expr[conn->cmd_len + 1];
155                 strncpy(expr, sdb_strbuf_string(conn->buf), conn->cmd_len);
156                 expr[sizeof(expr) - 1] = '\0';
157                 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse "
158                                 "lookup condition '%s'", expr);
159                 return -1;
160         }
162         status = sdb_fe_exec_lookup(conn, m, /* filter = */ NULL);
163         sdb_object_deref(SDB_OBJ(m));
164         return status;
165 } /* sdb_fe_lookup */
167 int
168 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
170         sdb_store_matcher_t *m = NULL, *filter = NULL;
172         if (! node)
173                 return -1;
175         switch (node->cmd) {
176                 case CONNECTION_FETCH:
177                         if (CONN_FETCH(node)->filter)
178                                 filter = CONN_FETCH(node)->filter->matcher;
179                         return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->name, filter);
180                 case CONNECTION_LIST:
181                         if (CONN_LIST(node)->filter)
182                                 filter = CONN_LIST(node)->filter->matcher;
183                         return sdb_fe_exec_list(conn, filter);
184                 case CONNECTION_LOOKUP:
185                         if (CONN_LOOKUP(node)->matcher)
186                                 m = CONN_LOOKUP(node)->matcher->matcher;
187                         if (CONN_LOOKUP(node)->filter)
188                                 filter = CONN_LOOKUP(node)->filter->matcher;
189                         return sdb_fe_exec_lookup(conn, m, filter);
190                 case CONNECTION_TIMESERIES:
191                         return sdb_fe_exec_timeseries(conn,
192                                         CONN_TS(node)->hostname, CONN_TS(node)->metric,
193                                         &CONN_TS(node)->opts);
195                 default:
196                         sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
197                         return -1;
198         }
199         return -1;
200 } /* sdb_fe_exec */
202 int
203 sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name,
204                 sdb_store_matcher_t *filter)
206         sdb_strbuf_t *buf;
207         sdb_store_obj_t *host;
208         uint32_t type = htonl(CONNECTION_FETCH);
210         host = sdb_store_get_host(name);
211         if (! host) {
212                 sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': "
213                                 "not found", name);
215                 sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name);
216                 return -1;
217         }
219         buf = sdb_strbuf_create(1024);
220         if (! buf) {
221                 char errbuf[1024];
222                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
223                                 "buffer to handle FETCH command: %s",
224                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
226                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
227                 sdb_strbuf_destroy(buf);
228                 sdb_object_deref(SDB_OBJ(host));
229                 return -1;
230         }
232         sdb_strbuf_memcpy(buf, &type, sizeof(uint32_t));
233         if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) {
234                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
235                                 "host '%s' to JSON", name);
236                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
237                 sdb_strbuf_destroy(buf);
238                 sdb_object_deref(SDB_OBJ(host));
239                 return -1;
240         }
242         sdb_connection_send(conn, CONNECTION_DATA,
243                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
244         sdb_strbuf_destroy(buf);
245         sdb_object_deref(SDB_OBJ(host));
246         return 0;
247 } /* sdb_fe_exec_fetch */
249 int
250 sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter)
252         sdb_strbuf_t *buf;
253         uint32_t type = htonl(CONNECTION_LIST);
255         buf = sdb_strbuf_create(1024);
256         if (! buf) {
257                 char errbuf[1024];
258                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
259                                 "buffer to handle LIST command: %s",
260                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
262                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
263                 sdb_strbuf_destroy(buf);
264                 return -1;
265         }
267         sdb_strbuf_memcpy(buf, &type, sizeof(uint32_t));
268         if (sdb_store_tojson(buf, filter, /* flags = */ SDB_SKIP_ALL)) {
269                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
270                                 "store to JSON");
271                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
272                 sdb_strbuf_destroy(buf);
273                 return -1;
274         }
276         sdb_connection_send(conn, CONNECTION_DATA,
277                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
278         sdb_strbuf_destroy(buf);
279         return 0;
280 } /* sdb_fe_exec_list */
282 int
283 sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
284                 sdb_store_matcher_t *filter)
286         tojson_data_t data = { NULL, filter, 0 };
287         uint32_t type = htonl(CONNECTION_LOOKUP);
289         data.buf = sdb_strbuf_create(1024);
290         if (! data.buf) {
291                 char errbuf[1024];
292                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
293                                 "buffer to handle LOOKUP command: %s",
294                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
296                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
297                 sdb_strbuf_destroy(data.buf);
298                 return -1;
299         }
301         sdb_strbuf_memcpy(data.buf, &type, sizeof(uint32_t));
302         sdb_strbuf_append(data.buf, "[");
304         /* Let the JSON serializer handle the filter instead of the scanner. Else,
305          * we'd have to filter twice -- once in the scanner and then again in the
306          * serializer. */
307         data.last_len = sdb_strbuf_len(data.buf);
308         if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, &data)) {
309                 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
310                 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
311                 sdb_strbuf_destroy(data.buf);
312                 return -1;
313         }
315         sdb_strbuf_append(data.buf, "]");
317         sdb_connection_send(conn, CONNECTION_DATA,
318                         (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf));
319         sdb_strbuf_destroy(data.buf);
320         return 0;
321 } /* sdb_fe_exec_lookup */
323 int
324 sdb_fe_exec_timeseries(sdb_conn_t *conn,
325                 const char *hostname, const char *metric,
326                 sdb_timeseries_opts_t *opts)
328         sdb_strbuf_t *buf;
329         uint32_t type = htonl(CONNECTION_TIMESERIES);
331         buf = sdb_strbuf_create(1024);
332         if (! buf) {
333                 char errbuf[1024];
334                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
335                                 "buffer to handle TIMESERIES command: %s",
336                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
338                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
339                 return -1;
340         }
342         sdb_strbuf_memcpy(buf, &type, sizeof(uint32_t));
343         if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) {
344                 sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series");
345                 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch time-series");
346                 sdb_strbuf_destroy(buf);
347                 return -1;
348         }
350         sdb_connection_send(conn, CONNECTION_DATA,
351                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
352         sdb_strbuf_destroy(buf);
353         return 0;
354 } /* sdb_fe_exec_timeseries */
356 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */