New host list manager: database_hosts_t
[pazpar2-moved-to-github.git] / src / database.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 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 #include <yaz/log.h>
30
31 #include "pazpar2.h"
32 #include "host.h"
33 #include "settings.h"
34 #include "http.h"
35 #include "zeerex.h"
36 #include "database.h"
37
38 #include <sys/types.h>
39 #if HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
41 #endif
42 #if HAVE_NETDB_H
43 #include <netdb.h>
44 #endif
45 #if HAVE_NETINET_IN_H
46 #include <netinet/in.h>
47 #endif
48
49 enum pazpar2_database_criterion_type {
50     PAZPAR2_STRING_MATCH,
51     PAZPAR2_SUBSTRING_MATCH
52 };
53
54 struct database_criterion_value {
55     char *value;
56     struct database_criterion_value *next;
57 };
58
59 struct database_criterion {
60     char *name;
61     enum pazpar2_database_criterion_type type;
62     struct database_criterion_value *values;
63     struct database_criterion *next;
64 };
65
66
67 struct database_hosts {
68     struct host *hosts;
69     YAZ_MUTEX mutex;
70 };
71
72 static xmlDoc *get_explain_xml(struct conf_targetprofiles *targetprofiles,
73                                const char *id)
74 {
75     struct stat st;
76     char *dir;
77     char path[256];
78     char ide[256];
79     if (targetprofiles->type != Targetprofiles_local)
80     {
81         yaz_log(YLOG_FATAL, "Only supports local type");
82         return 0;
83     }
84     dir = targetprofiles->src;
85     urlencode(id, ide);
86     sprintf(path, "%s/%s", dir, ide);
87     if (!stat(path, &st))
88         return xmlParseFile(path);
89     else
90         return 0;
91 }
92
93 // Create a new host structure for hostport
94 static struct host *create_host(const char *hostport, iochan_man_t iochan_man)
95 {
96     struct host *host;
97
98     host = xmalloc(sizeof(struct host));
99     host->hostport = xstrdup(hostport);
100     host->connections = 0;
101     host->ipport = 0;
102     host->mutex = 0;
103
104     if (host_getaddrinfo(host, iochan_man))
105     {
106         xfree(host->hostport);
107         xfree(host);
108         return 0;
109     }
110     yaz_mutex_create(&host->mutex);
111
112     return host;
113 }
114
115 static struct host *find_host(database_hosts_t hosts,
116                               const char *hostport, iochan_man_t iochan_man)
117 {
118     struct host *p;
119     yaz_mutex_enter(hosts->mutex);
120     for (p = hosts->hosts; p; p = p->next)
121         if (!strcmp(p->hostport, hostport))
122             break;
123     if (!p)
124     {
125         p = create_host(hostport, iochan_man);
126         if (p)
127         {
128             p->next = hosts->hosts;
129             hosts->hosts = p;
130         }
131     }
132     yaz_mutex_leave(hosts->mutex);
133     return p;
134 }
135
136 int resolve_database(struct conf_service *service, struct database *db)
137 {
138     if (db->host == 0)
139     {
140         struct host *host;
141         char *p;
142         char hostport[256];
143         strcpy(hostport, db->url);
144         if ((p = strchr(hostport, '/')))
145             *p = '\0';
146         if (!(host = find_host(service->server->database_hosts,
147                                hostport, service->server->iochan_man)))
148             return -1;
149         db->host = host;
150     }
151     return 0;
152 }
153
154 void resolve_databases(struct conf_service *service)
155 {
156     struct database *db = service->databases;
157     for (; db; db = db->next)
158         resolve_database(service, db);
159 }
160
161 struct database *new_database(const char *id, NMEM nmem)
162 {
163     struct database *db;
164     char hostport[256];
165     char *dbname;
166     struct setting *idset;
167
168     if (strlen(id) > 255)
169         return 0;
170     strcpy(hostport, id);
171     if ((dbname = strchr(hostport, '/')))
172         *(dbname++) = '\0';
173     else
174         dbname = "";
175     db = nmem_malloc(nmem, sizeof(*db));
176     memset(db, 0, sizeof(*db));
177     db->host = 0;
178     db->url = nmem_strdup(nmem, id);
179     db->databases = nmem_malloc(nmem, 2 * sizeof(char *));
180     db->databases[0] = nmem_strdup(nmem, dbname);
181     db->databases[1] = 0;
182     db->errors = 0;
183     db->explain = 0;
184
185     db->num_settings = PZ_NEGOTIATION_CHARSET+1;
186     db->settings = nmem_malloc(nmem, sizeof(struct settings*) * 
187                                db->num_settings);
188     memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
189     idset = nmem_malloc(nmem, sizeof(*idset));
190     idset->precedence = 0;
191     idset->name = "pz:id";
192     idset->target = idset->value = db->url;
193     idset->next = 0;
194     db->settings[PZ_ID] = idset;
195     db->next = 0;
196
197     return db;
198 }
199
200 static struct database *load_database(const char *id,
201                                       struct conf_service *service)
202 {
203     struct database *db;
204     struct zr_explain *explain = 0;
205     xmlDoc *doc = 0;
206
207     if (service->targetprofiles 
208         && (doc = get_explain_xml(service->targetprofiles, id)))
209     {
210         explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
211         if (!explain)
212             return 0;
213     }
214     db = new_database(id, service->nmem);
215     db->explain = explain;
216     db->next = service->databases;
217     service->databases = db;
218
219     return db;
220 }
221
222 // Return a database structure by ID. Load and add to list if necessary
223 // new==1 just means we know it's not in the list
224 struct database *find_database(const char *id, struct conf_service *service)
225 {
226     struct database *p;
227     for (p = service->databases; p; p = p->next)
228         if (!strcmp(p->url, id))
229             return p;
230     return load_database(id, service);
231 }
232
233 // This whole session_grep database thing should be moved elsewhere
234
235 int match_zurl(const char *zurl, const char *pattern)
236 {
237     int len;
238
239     if (!strcmp(pattern, "*"))
240         return 1;
241     else if (!strncmp(pattern, "*/", 2))   // host wildcard.. what the heck is that for?
242     {
243         char *db = strchr(zurl, '/');
244         if (!db)
245             return 0;
246         if (!strcmp(pattern + 2, db))
247             return 1;
248         else
249             return 0;
250     }
251     else if (*(pattern + (len = strlen(pattern) - 1)) == '*')  // db wildcard
252     {
253         if (!strncmp(pattern, zurl, len))
254             return 1;
255         else
256             return 2;
257     }
258     else if (!strcmp(pattern, zurl))
259         return 1;
260     else
261         return 0;
262 }
263
264 // This will be generalized at some point
265 static int match_criterion(struct setting **settings,
266                            struct conf_service *service, 
267                            struct database_criterion *c)
268 {
269     int offset = settings_lookup_offset(service, c->name);
270     struct database_criterion_value *v;
271
272     if (offset < 0)
273     {
274         yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
275         return 0;
276     }
277     if (!settings[offset])
278         return 0;
279     for (v = c->values; v; v = v->next)
280     {
281         if (c->type == PAZPAR2_STRING_MATCH)
282         {
283             if (offset == PZ_ID)
284             {
285                 if (match_zurl(settings[offset]->value, v->value))
286                     break;
287             }
288             else 
289             {
290                 if (!strcmp(settings[offset]->value, v->value))
291                     break;
292             }
293         }            
294         else if (c->type == PAZPAR2_SUBSTRING_MATCH)
295         {
296             if (strstr(settings[offset]->value, v->value))
297                 break;
298         }
299     }
300     if (v)
301         return 1;
302     else
303         return 0;
304 }
305
306 // parses crit1=val1,crit2=val2|val3,...
307 static struct database_criterion *create_database_criterion(NMEM m,
308                                                             const char *buf)
309 {
310     struct database_criterion *res = 0;
311     char **values;
312     int num;
313     int i;
314
315     if (!buf || !*buf)
316         return 0;
317     nmem_strsplit(m, ",", buf,  &values, &num);
318     for (i = 0; i < num; i++)
319     {
320         char **subvalues;
321         int subnum;
322         int subi;
323         struct database_criterion *new = nmem_malloc(m, sizeof(*new));
324         char *eq;
325         if ((eq = strchr(values[i], '=')))
326             new->type = PAZPAR2_STRING_MATCH;
327         else if ((eq = strchr(values[i], '~')))
328             new->type = PAZPAR2_SUBSTRING_MATCH;
329         else
330         {
331             yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
332             return 0;
333         }
334         *(eq++) = '\0';
335         new->name = values[i];
336         nmem_strsplit(m, "|", eq, &subvalues, &subnum);
337         new->values = 0;
338         for (subi = 0; subi < subnum; subi++)
339         {
340             struct database_criterion_value *newv
341                 = nmem_malloc(m, sizeof(*newv));
342             newv->value = subvalues[subi];
343             newv->next = new->values;
344             new->values = newv;
345         }
346         new->next = res;
347         res = new;
348     }
349     return res;
350 }
351
352 static int database_match_criteria(struct setting **settings,
353                                    struct conf_service *service,
354                                    struct database_criterion *cl)
355 {
356     for (; cl; cl = cl->next)
357         if (!match_criterion(settings, service, cl))
358             break;
359     if (cl) // one of the criteria failed to match -- skip this db
360         return 0;
361     else
362         return 1;
363 }
364
365 // Cycles through databases, calling a handler function on the ones for
366 // which all criteria matched.
367 int session_grep_databases(struct session *se, const char *filter,
368                            void (*fun)(void *context, struct session_database *db))
369 {
370     struct session_database *p;
371     NMEM nmem = nmem_create();
372     int i = 0;
373     struct database_criterion *cl = create_database_criterion(nmem, filter);
374
375     for (p = se->databases; p; p = p->next)
376     {
377         if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
378             continue;
379         if (!p->settings[PZ_NAME])
380             continue;
381         if (database_match_criteria(p->settings, se->service, cl))
382         {
383             (*fun)(se, p);
384             i++;
385         }
386     }
387     nmem_destroy(nmem);
388     return i;
389 }
390
391 int predef_grep_databases(void *context, struct conf_service *service,
392                           void (*fun)(void *context, struct database *db))
393 {
394     struct database *p;
395     int i = 0;
396
397     for (p = service->databases; p; p = p->next)
398         if (database_match_criteria(p->settings, service, 0))
399         {
400             (*fun)(context, p);
401             i++;
402         }
403     return i;
404 }
405
406 database_hosts_t database_hosts_create(void)
407 {
408     database_hosts_t p = xmalloc(sizeof(*p));
409     p->hosts = 0;
410     p->mutex = 0;
411     yaz_mutex_create(&p->mutex);
412     return p;
413 }
414
415 /*
416  * Local variables:
417  * c-basic-offset: 4
418  * c-file-style: "Stroustrup"
419  * indent-tabs-mode: nil
420  * End:
421  * vim: shiftwidth=4 tabstop=8 expandtab
422  */
423