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