Code

fda93f6fb0ccd1e8e7d71be3864d67cc090bbb7d
[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 "utils/error.h"
33 #include "utils/strbuf.h"
35 #include <errno.h>
37 /*
38  * private helper functions
39  */
41 typedef struct {
42         sdb_strbuf_t *buf;
43         sdb_store_matcher_t *filter;
45         size_t last_len;
46 } tojson_data_t;
48 static int
49 lookup_tojson(sdb_store_obj_t *obj, void *user_data)
50 {
51         tojson_data_t *data = user_data;
52         int status;
54         if (data->filter && (! sdb_store_matcher_matches(data->filter, obj, NULL)))
55                 return 0;
57         if (sdb_strbuf_len(data->buf) > data->last_len)
58                 sdb_strbuf_append(data->buf, ",");
59         data->last_len = sdb_strbuf_len(data->buf);
60         status = sdb_store_host_tojson(obj, data->buf,
61                         data->filter, /* flags = */ 0);
62         return status;
63 } /* lookup_tojson */
65 /*
66  * public API
67  */
69 int
70 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
71 {
72         if (! node)
73                 return -1;
75         switch (node->cmd) {
76                 case CONNECTION_FETCH:
77                         return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->name,
78                                         /* filter = */ NULL);
79                 case CONNECTION_LIST:
80                         return sdb_fe_exec_list(conn, /* filter = */ NULL);
81                 case CONNECTION_LOOKUP:
82                 {
83                         sdb_store_matcher_t *m = NULL, *filter = NULL;
84                         if (CONN_LOOKUP(node)->matcher)
85                                 m = CONN_LOOKUP(node)->matcher->matcher;
86                         if (CONN_LOOKUP(node)->filter)
87                                 filter = CONN_LOOKUP(node)->filter->matcher;
88                         return sdb_fe_exec_lookup(conn, m, filter);
89                 }
91                 default:
92                         sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
93                         return -1;
94         }
95         return -1;
96 } /* sdb_fe_exec */
98 int
99 sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name,
100                 sdb_store_matcher_t *filter)
102         sdb_strbuf_t *buf;
103         sdb_store_obj_t *host;
105         host = sdb_store_get_host(name);
106         if (! host) {
107                 sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': "
108                                 "not found", name);
110                 sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name);
111                 return -1;
112         }
114         buf = sdb_strbuf_create(1024);
115         if (! buf) {
116                 char errbuf[1024];
117                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
118                                 "buffer to handle FETCH command: %s",
119                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
121                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
122                 sdb_strbuf_destroy(buf);
123                 sdb_object_deref(SDB_OBJ(host));
124                 return -1;
125         }
127         if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) {
128                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
129                                 "host '%s' to JSON", name);
130                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
131                 sdb_strbuf_destroy(buf);
132                 sdb_object_deref(SDB_OBJ(host));
133                 return -1;
134         }
136         sdb_connection_send(conn, CONNECTION_OK,
137                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
138         sdb_strbuf_destroy(buf);
139         sdb_object_deref(SDB_OBJ(host));
140         return 0;
141 } /* sdb_fe_exec_fetch */
143 int
144 sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter)
146         sdb_strbuf_t *buf;
148         buf = sdb_strbuf_create(1024);
149         if (! buf) {
150                 char errbuf[1024];
151                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
152                                 "buffer to handle LIST command: %s",
153                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
155                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
156                 sdb_strbuf_destroy(buf);
157                 return -1;
158         }
160         if (sdb_store_tojson(buf, filter, /* flags = */ SDB_SKIP_ALL)) {
161                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
162                                 "store to JSON");
163                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
164                 sdb_strbuf_destroy(buf);
165                 return -1;
166         }
168         sdb_connection_send(conn, CONNECTION_OK,
169                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
170         sdb_strbuf_destroy(buf);
171         return 0;
172 } /* sdb_fe_exec_list */
174 int
175 sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
176                 sdb_store_matcher_t *filter)
178         tojson_data_t data = { NULL, filter, 0 };
180         data.buf = sdb_strbuf_create(1024);
181         if (! data.buf) {
182                 char errbuf[1024];
183                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
184                                 "buffer to handle LOOKUP command: %s",
185                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
187                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
188                 sdb_strbuf_destroy(data.buf);
189                 return -1;
190         }
192         sdb_strbuf_append(data.buf, "[");
194         /* Let the JSON serializer handle the filter instead of the scanner. Else,
195          * we'd have to filter twice -- once in the scanner and then again in the
196          * serializer. */
197         data.last_len = sdb_strbuf_len(data.buf);
198         if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, &data)) {
199                 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
200                 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
201                 sdb_strbuf_destroy(data.buf);
202                 return -1;
203         }
205         sdb_strbuf_append(data.buf, "]");
207         sdb_connection_send(conn, CONNECTION_OK,
208                         (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf));
209         sdb_strbuf_destroy(data.buf);
210         return 0;
211 } /* sdb_fe_exec_lookup */
213 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */