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