Code

d83ffd82ca76ab34bd03d0a18359baeaab194c1c
[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_fetch(conn, CONN_FETCH(node)->name,
78                                         /* filter = */ NULL);
79                 case CONNECTION_LIST:
80                         return sdb_fe_list(conn, /* filter = */ NULL);
81                 case CONNECTION_LOOKUP:
82                         return sdb_fe_lookup(conn, CONN_LOOKUP(node)->matcher->matcher,
83                                         /* filter = */ NULL);
85                 default:
86                         sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
87                         return -1;
88         }
89         return -1;
90 } /* sdb_fe_exec */
92 int
93 sdb_fe_fetch(sdb_conn_t *conn, const char *name, sdb_store_matcher_t *filter)
94 {
95         sdb_strbuf_t *buf;
96         sdb_store_obj_t *host;
98         host = sdb_store_get_host(name);
99         if (! host) {
100                 sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': "
101                                 "not found", name);
103                 sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name);
104                 return -1;
105         }
107         buf = sdb_strbuf_create(1024);
108         if (! buf) {
109                 char errbuf[1024];
110                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
111                                 "buffer to handle FETCH command: %s",
112                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
114                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
115                 sdb_strbuf_destroy(buf);
116                 sdb_object_deref(SDB_OBJ(host));
117                 return -1;
118         }
120         if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) {
121                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
122                                 "host '%s' to JSON", name);
123                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
124                 sdb_strbuf_destroy(buf);
125                 sdb_object_deref(SDB_OBJ(host));
126                 return -1;
127         }
129         sdb_connection_send(conn, CONNECTION_OK,
130                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
131         sdb_strbuf_destroy(buf);
132         sdb_object_deref(SDB_OBJ(host));
133         return 0;
134 } /* sdb_fe_fetch */
136 int
137 sdb_fe_list(sdb_conn_t *conn, sdb_store_matcher_t *filter)
139         sdb_strbuf_t *buf;
141         buf = sdb_strbuf_create(1024);
142         if (! buf) {
143                 char errbuf[1024];
144                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
145                                 "buffer to handle LIST command: %s",
146                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
148                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
149                 sdb_strbuf_destroy(buf);
150                 return -1;
151         }
153         if (sdb_store_tojson(buf, filter, /* flags = */ SDB_SKIP_ALL)) {
154                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
155                                 "store to JSON");
156                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
157                 sdb_strbuf_destroy(buf);
158                 return -1;
159         }
161         sdb_connection_send(conn, CONNECTION_OK,
162                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
163         sdb_strbuf_destroy(buf);
164         return 0;
165 } /* sdb_fe_list */
167 int
168 sdb_fe_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
169                 sdb_store_matcher_t *filter)
171         tojson_data_t data = { NULL, filter, 0 };
173         data.buf = sdb_strbuf_create(1024);
174         if (! data.buf) {
175                 char errbuf[1024];
176                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
177                                 "buffer to handle LOOKUP command: %s",
178                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
180                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
181                 sdb_strbuf_destroy(data.buf);
182                 return -1;
183         }
185         sdb_strbuf_append(data.buf, "[");
187         /* Let the JSON serializer handle the filter instead of the scanner. Else,
188          * we'd have to filter twice -- once in the scanner and then again in the
189          * serializer. */
190         data.last_len = sdb_strbuf_len(data.buf);
191         if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, &data)) {
192                 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
193                 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
194                 sdb_strbuf_destroy(data.buf);
195                 return -1;
196         }
198         sdb_strbuf_append(data.buf, "]");
200         sdb_connection_send(conn, CONNECTION_OK,
201                         (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf));
202         sdb_strbuf_destroy(data.buf);
203         return 0;
204 } /* sdb_fe_lookup */
206 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */