Code

frontend, sysdb: Correctly handle empty queries.
[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; send back an empty reply */
95                         sdb_connection_send(conn, CONNECTION_DATA, 0, NULL);
96                         break;
97                 case 1:
98                         node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
99                         break;
101                 default:
102                         {
103                                 char query[conn->cmd_len + 1];
104                                 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
105                                 query[sizeof(query) - 1] = '\0';
106                                 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %zu command%s "
107                                                 "in multi-statement query '%s'",
108                                                 sdb_llist_len(parsetree) - 1,
109                                                 sdb_llist_len(parsetree) == 2 ? "" : "s",
110                                                 query);
111                                 node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
112                         }
113         }
115         if (node) {
116                 status = sdb_fe_exec(conn, node);
117                 sdb_object_deref(SDB_OBJ(node));
118         }
120         sdb_llist_destroy(parsetree);
121         return status;
122 } /* sdb_fe_query */
124 int
125 sdb_fe_fetch(sdb_conn_t *conn)
127         char hostname[conn->cmd_len + 1];
128         if ((! conn) || (conn->cmd != CONNECTION_FETCH))
129                 return -1;
130         strncpy(hostname, sdb_strbuf_string(conn->buf), conn->cmd_len);
131         hostname[sizeof(hostname) - 1] = '\0';
132         return sdb_fe_exec_fetch(conn, hostname, /* filter = */ NULL);
133 } /* sdb_fe_fetch */
135 int
136 sdb_fe_list(sdb_conn_t *conn)
138         if ((! conn) || (conn->cmd != CONNECTION_LIST))
139                 return -1;
140         return sdb_fe_exec_list(conn, /* filter = */ NULL);
141 } /* sdb_fe_list */
143 int
144 sdb_fe_lookup(sdb_conn_t *conn)
146         sdb_store_matcher_t *m;
147         int status;
149         if ((! conn) || (conn->cmd != CONNECTION_LOOKUP))
150                 return -1;
152         m = sdb_fe_parse_matcher(sdb_strbuf_string(conn->buf),
153                         (int)conn->cmd_len);
154         if (! m) {
155                 char expr[conn->cmd_len + 1];
156                 strncpy(expr, sdb_strbuf_string(conn->buf), conn->cmd_len);
157                 expr[sizeof(expr) - 1] = '\0';
158                 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse "
159                                 "lookup condition '%s'", expr);
160                 return -1;
161         }
163         status = sdb_fe_exec_lookup(conn, m, /* filter = */ NULL);
164         sdb_object_deref(SDB_OBJ(m));
165         return status;
166 } /* sdb_fe_lookup */
168 int
169 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
171         sdb_store_matcher_t *m = NULL, *filter = NULL;
173         if (! node)
174                 return -1;
176         switch (node->cmd) {
177                 case CONNECTION_FETCH:
178                         if (CONN_FETCH(node)->filter)
179                                 filter = CONN_FETCH(node)->filter->matcher;
180                         return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->name, filter);
181                 case CONNECTION_LIST:
182                         if (CONN_LIST(node)->filter)
183                                 filter = CONN_LIST(node)->filter->matcher;
184                         return sdb_fe_exec_list(conn, filter);
185                 case CONNECTION_LOOKUP:
186                         if (CONN_LOOKUP(node)->matcher)
187                                 m = CONN_LOOKUP(node)->matcher->matcher;
188                         if (CONN_LOOKUP(node)->filter)
189                                 filter = CONN_LOOKUP(node)->filter->matcher;
190                         return sdb_fe_exec_lookup(conn, m, filter);
191                 case CONNECTION_TIMESERIES:
192                         return sdb_fe_exec_timeseries(conn,
193                                         CONN_TS(node)->hostname, CONN_TS(node)->metric,
194                                         &CONN_TS(node)->opts);
196                 default:
197                         sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
198                         return -1;
199         }
200         return -1;
201 } /* sdb_fe_exec */
203 int
204 sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name,
205                 sdb_store_matcher_t *filter)
207         sdb_strbuf_t *buf;
208         sdb_store_obj_t *host;
209         uint32_t type = htonl(CONNECTION_FETCH);
211         host = sdb_store_get_host(name);
212         if (! host) {
213                 sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': "
214                                 "not found", name);
216                 sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name);
217                 return -1;
218         }
220         buf = sdb_strbuf_create(1024);
221         if (! buf) {
222                 char errbuf[1024];
223                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
224                                 "buffer to handle FETCH command: %s",
225                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
227                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
228                 sdb_strbuf_destroy(buf);
229                 sdb_object_deref(SDB_OBJ(host));
230                 return -1;
231         }
233         sdb_strbuf_memcpy(buf, &type, sizeof(uint32_t));
234         if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) {
235                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
236                                 "host '%s' to JSON", name);
237                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
238                 sdb_strbuf_destroy(buf);
239                 sdb_object_deref(SDB_OBJ(host));
240                 return -1;
241         }
243         sdb_connection_send(conn, CONNECTION_DATA,
244                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
245         sdb_strbuf_destroy(buf);
246         sdb_object_deref(SDB_OBJ(host));
247         return 0;
248 } /* sdb_fe_exec_fetch */
250 int
251 sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter)
253         sdb_strbuf_t *buf;
254         uint32_t type = htonl(CONNECTION_LIST);
256         buf = sdb_strbuf_create(1024);
257         if (! buf) {
258                 char errbuf[1024];
259                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
260                                 "buffer to handle LIST command: %s",
261                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
263                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
264                 sdb_strbuf_destroy(buf);
265                 return -1;
266         }
268         sdb_strbuf_memcpy(buf, &type, sizeof(uint32_t));
269         if (sdb_store_tojson(buf, filter, /* flags = */ SDB_SKIP_ALL)) {
270                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
271                                 "store to JSON");
272                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
273                 sdb_strbuf_destroy(buf);
274                 return -1;
275         }
277         sdb_connection_send(conn, CONNECTION_DATA,
278                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
279         sdb_strbuf_destroy(buf);
280         return 0;
281 } /* sdb_fe_exec_list */
283 int
284 sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
285                 sdb_store_matcher_t *filter)
287         tojson_data_t data = { NULL, filter, 0 };
288         uint32_t type = htonl(CONNECTION_LOOKUP);
290         data.buf = sdb_strbuf_create(1024);
291         if (! data.buf) {
292                 char errbuf[1024];
293                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
294                                 "buffer to handle LOOKUP command: %s",
295                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
297                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
298                 sdb_strbuf_destroy(data.buf);
299                 return -1;
300         }
302         sdb_strbuf_memcpy(data.buf, &type, sizeof(uint32_t));
303         sdb_strbuf_append(data.buf, "[");
305         /* Let the JSON serializer handle the filter instead of the scanner. Else,
306          * we'd have to filter twice -- once in the scanner and then again in the
307          * serializer. */
308         data.last_len = sdb_strbuf_len(data.buf);
309         if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, &data)) {
310                 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
311                 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
312                 sdb_strbuf_destroy(data.buf);
313                 return -1;
314         }
316         sdb_strbuf_append(data.buf, "]");
318         sdb_connection_send(conn, CONNECTION_DATA,
319                         (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf));
320         sdb_strbuf_destroy(data.buf);
321         return 0;
322 } /* sdb_fe_exec_lookup */
324 int
325 sdb_fe_exec_timeseries(sdb_conn_t *conn,
326                 const char *hostname, const char *metric,
327                 sdb_timeseries_opts_t *opts)
329         sdb_strbuf_t *buf;
330         uint32_t type = htonl(CONNECTION_TIMESERIES);
332         buf = sdb_strbuf_create(1024);
333         if (! buf) {
334                 char errbuf[1024];
335                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
336                                 "buffer to handle TIMESERIES command: %s",
337                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
339                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
340                 return -1;
341         }
343         sdb_strbuf_memcpy(buf, &type, sizeof(uint32_t));
344         if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) {
345                 sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series");
346                 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch time-series");
347                 sdb_strbuf_destroy(buf);
348                 return -1;
349         }
351         sdb_connection_send(conn, CONNECTION_DATA,
352                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
353         sdb_strbuf_destroy(buf);
354         return 0;
355 } /* sdb_fe_exec_timeseries */
357 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */