Code

frontend: Added 'FILTER' support to the 'LOOKUP' command.
[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                 {
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_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_fetch(sdb_conn_t *conn, const char *name, sdb_store_matcher_t *filter)
101         sdb_strbuf_t *buf;
102         sdb_store_obj_t *host;
104         host = sdb_store_get_host(name);
105         if (! host) {
106                 sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': "
107                                 "not found", name);
109                 sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name);
110                 return -1;
111         }
113         buf = sdb_strbuf_create(1024);
114         if (! buf) {
115                 char errbuf[1024];
116                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
117                                 "buffer to handle FETCH command: %s",
118                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
120                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
121                 sdb_strbuf_destroy(buf);
122                 sdb_object_deref(SDB_OBJ(host));
123                 return -1;
124         }
126         if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) {
127                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
128                                 "host '%s' to JSON", name);
129                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
130                 sdb_strbuf_destroy(buf);
131                 sdb_object_deref(SDB_OBJ(host));
132                 return -1;
133         }
135         sdb_connection_send(conn, CONNECTION_OK,
136                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
137         sdb_strbuf_destroy(buf);
138         sdb_object_deref(SDB_OBJ(host));
139         return 0;
140 } /* sdb_fe_fetch */
142 int
143 sdb_fe_list(sdb_conn_t *conn, sdb_store_matcher_t *filter)
145         sdb_strbuf_t *buf;
147         buf = sdb_strbuf_create(1024);
148         if (! buf) {
149                 char errbuf[1024];
150                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
151                                 "buffer to handle LIST command: %s",
152                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
154                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
155                 sdb_strbuf_destroy(buf);
156                 return -1;
157         }
159         if (sdb_store_tojson(buf, filter, /* flags = */ SDB_SKIP_ALL)) {
160                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
161                                 "store to JSON");
162                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
163                 sdb_strbuf_destroy(buf);
164                 return -1;
165         }
167         sdb_connection_send(conn, CONNECTION_OK,
168                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
169         sdb_strbuf_destroy(buf);
170         return 0;
171 } /* sdb_fe_list */
173 int
174 sdb_fe_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m,
175                 sdb_store_matcher_t *filter)
177         tojson_data_t data = { NULL, filter, 0 };
179         data.buf = sdb_strbuf_create(1024);
180         if (! data.buf) {
181                 char errbuf[1024];
182                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
183                                 "buffer to handle LOOKUP command: %s",
184                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
186                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
187                 sdb_strbuf_destroy(data.buf);
188                 return -1;
189         }
191         sdb_strbuf_append(data.buf, "[");
193         /* Let the JSON serializer handle the filter instead of the scanner. Else,
194          * we'd have to filter twice -- once in the scanner and then again in the
195          * serializer. */
196         data.last_len = sdb_strbuf_len(data.buf);
197         if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, &data)) {
198                 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
199                 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
200                 sdb_strbuf_destroy(data.buf);
201                 return -1;
202         }
204         sdb_strbuf_append(data.buf, "]");
206         sdb_connection_send(conn, CONNECTION_OK,
207                         (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf));
208         sdb_strbuf_destroy(data.buf);
209         return 0;
210 } /* sdb_fe_lookup */
212 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */