Databases per-service.
[pazpar2-moved-to-github.git] / src / database.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
26 #include <assert.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #include "pazpar2.h"
31 #include "host.h"
32 #include "settings.h"
33 #include "http.h"
34 #include "zeerex.h"
35 #include "database.h"
36
37 #include <sys/types.h>
38 #if HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41 #if HAVE_NETDB_H
42 #include <netdb.h>
43 #endif
44 #if HAVE_NETINET_IN_H
45 #include <netinet/in.h>
46 #endif
47
48 static struct host *hosts = 0;  // The hosts we know about 
49
50 static xmlDoc *get_explain_xml(const char *id)
51 {
52     struct stat st;
53     char *dir;
54     char path[256];
55     char ide[256];
56     if (!config || !config->targetprofiles)
57     {
58         yaz_log(YLOG_WARN, "Config must be loaded and specify targetprofiles");
59         return 0;
60     }
61     if (config->targetprofiles->type != Targetprofiles_local)
62     {
63         yaz_log(YLOG_FATAL, "Only supports local type");
64         return 0;
65     }
66     dir = config->targetprofiles->src;
67     urlencode(id, ide);
68     sprintf(path, "%s/%s", dir, ide);
69     if (!stat(path, &st))
70         return xmlParseFile(path);
71     else
72         return 0;
73 }
74
75 // Create a new host structure for hostport
76 static struct host *create_host(const char *hostport)
77 {
78     struct host *host;
79
80     host = xmalloc(sizeof(struct host));
81     host->hostport = xstrdup(hostport);
82     host->connections = 0;
83     host->ipport = 0;
84
85     if (host_getaddrinfo(host))
86     {
87         xfree(host->hostport);
88         xfree(host);
89         return 0;
90     }
91     host->next = hosts;
92     hosts = host;
93     return host;
94 }
95
96 static struct host *find_host(const char *hostport)
97 {
98     struct host *p;
99     for (p = hosts; p; p = p->next)
100         if (!strcmp(p->hostport, hostport))
101             return p;
102     return create_host(hostport);
103 }
104
105 static struct database *load_database(const char *id,
106     struct conf_service *service)
107 {
108     xmlDoc *doc = 0;
109     struct zr_explain *explain = 0;
110     struct database *db;
111     struct host *host;
112     char hostport[256];
113     char *dbname;
114     struct setting *idset;
115
116     yaz_log(YLOG_LOG, "New database: %s", id);
117
118     if (config && config->targetprofiles 
119         && (doc = get_explain_xml(id)))
120     {
121         explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
122         if (!explain)
123             return 0;
124     }
125
126     if (strlen(id) > 255)
127         return 0;
128     strcpy(hostport, id);
129     if ((dbname = strchr(hostport, '/')))
130         *(dbname++) = '\0';
131     else
132         dbname = "";
133     if (!(host = find_host(hostport)))
134         return 0;
135     db = nmem_malloc(service->nmem, sizeof(*db));
136     memset(db, 0, sizeof(*db));
137     db->host = host;
138     db->url = nmem_strdup(service->nmem, id);
139     db->databases = xmalloc(2 * sizeof(char *));
140     db->databases[0] = nmem_strdup(service->nmem, dbname);
141     db->databases[1] = 0;
142     db->errors = 0;
143     db->explain = explain;
144
145     db->settings = 0;
146
147     db->settings = nmem_malloc(service->nmem, sizeof(struct settings*) * 
148                                settings_num(service));
149     memset(db->settings, 0, sizeof(struct settings*) * settings_num(service));
150     idset = nmem_malloc(service->nmem, sizeof(*idset));
151     idset->precedence = 0;
152     idset->name = "pz:id";
153     idset->target = idset->value = db->url;
154     idset->next = 0;
155     db->settings[PZ_ID] = idset;
156
157     db->next = service->databases;
158     service->databases = db;
159
160     return db;
161 }
162
163 // Return a database structure by ID. Load and add to list if necessary
164 // new==1 just means we know it's not in the list
165 struct database *find_database(const char *id, int new,
166                                struct conf_service *service)
167 {
168     struct database *p;
169     if (!new)
170     {
171         for (p = service->databases; p; p = p->next)
172             if (!strcmp(p->url, id))
173                 return p;
174     }
175     return load_database(id, service);
176 }
177
178 // This whole session_grep database thing should be moved elsewhere
179
180 int match_zurl(const char *zurl, const char *pattern)
181 {
182     int len;
183
184     if (!strcmp(pattern, "*"))
185         return 1;
186     else if (!strncmp(pattern, "*/", 2))   // host wildcard.. what the heck is that for?
187     {
188         char *db = strchr(zurl, '/');
189         if (!db)
190             return 0;
191         if (!strcmp(pattern + 2, db))
192             return 1;
193         else
194             return 0;
195     }
196     else if (*(pattern + (len = strlen(pattern) - 1)) == '*')  // db wildcard
197     {
198         if (!strncmp(pattern, zurl, len))
199             return 1;
200         else
201             return 2;
202     }
203     else if (!strcmp(pattern, zurl))
204         return 1;
205     else
206         return 0;
207 }
208
209 // This will be generalized at some point
210 static int match_criterion(struct setting **settings,
211                            struct conf_service *service, 
212                            struct database_criterion *c)
213 {
214     int offset = settings_offset(service, c->name);
215     struct database_criterion_value *v;
216
217     if (offset < 0)
218     {
219         yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
220         return 0;
221     }
222     if (!settings[offset])
223         return 0;
224     for (v = c->values; v; v = v->next)
225     {
226         if (offset == PZ_ID)
227         {
228             if (match_zurl(settings[offset]->value, v->value))
229                 break;
230         }
231         else 
232         {
233             if (!strcmp(settings[offset]->value, v->value))
234                 break;
235         }
236     }
237     if (v)
238         return 1;
239     else
240         return 0;
241 }
242
243 int database_match_criteria(struct setting **settings,
244                             struct conf_service *service,
245                             struct database_criterion *cl)
246 {
247     for (; cl; cl = cl->next)
248         if (!match_criterion(settings, service, cl))
249             break;
250     if (cl) // one of the criteria failed to match -- skip this db
251         return 0;
252     else
253         return 1;
254 }
255
256 // Cycles through databases, calling a handler function on the ones for
257 // which all criteria matched.
258 int session_grep_databases(struct session *se, struct database_criterion *cl,
259         void (*fun)(void *context, struct session_database *db))
260 {
261     struct session_database *p;
262     int i = 0;
263
264     for (p = se->databases; p; p = p->next)
265     {
266         if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
267             continue;
268         if (!p->settings[PZ_NAME])
269             continue;
270         if (database_match_criteria(p->settings, se->service, cl))
271         {
272             (*fun)(se, p);
273             i++;
274         }
275     }
276     return i;
277 }
278
279 int predef_grep_databases(void *context, struct conf_service *service,
280                           struct database_criterion *cl,
281                           void (*fun)(void *context, struct database *db))
282 {
283     struct database *p;
284     int i = 0;
285
286     for (p = service->databases; p; p = p->next)
287         if (database_match_criteria(p->settings, service, cl))
288         {
289             (*fun)(context, p);
290             i++;
291         }
292     return i;
293 }
294
295 /*
296  * Local variables:
297  * c-basic-offset: 4
298  * c-file-style: "Stroustrup"
299  * indent-tabs-mode: nil
300  * End:
301  * vim: shiftwidth=4 tabstop=8 expandtab
302  */
303