Resolve host for database just before search
[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 struct database_hosts {
69     struct host *hosts;
70     YAZ_MUTEX mutex;
71 };
72
73 // Create a new host structure for hostport
74 static struct host *create_host(const char *hostport)
75 {
76     struct host *host;
77     char *db_comment;
78
79     host = xmalloc(sizeof(struct host));
80     host->hostport = xstrdup(hostport);
81     db_comment = strchr(host->hostport, '#');
82     if (db_comment)
83         *db_comment = '\0';
84     host->connections = 0;
85     host->mutex = 0;
86
87     pazpar2_mutex_create(&host->mutex, "host");
88
89     yaz_cond_create(&host->cond_ready);
90
91     return host;
92 }
93
94 static struct host *find_host(database_hosts_t hosts,
95                               const char *hostport)
96 {
97     struct host *p;
98     yaz_mutex_enter(hosts->mutex);
99     for (p = hosts->hosts; p; p = p->next)
100         if (!strcmp(p->hostport, hostport))
101             break;
102     if (!p)
103     {
104         p = create_host(hostport);
105         if (p)
106         {
107             p->next = hosts->hosts;
108             hosts->hosts = p;
109         }
110     }
111     yaz_mutex_leave(hosts->mutex);
112     return p;
113 }
114
115 int resolve_database(struct conf_service *service, struct database *db)
116 {
117     if (db->host == 0)
118     {
119         struct host *host;
120         if (!(host = find_host(service->server->database_hosts, db->url)))
121             return -1;
122         db->host = host;
123     }
124     return 0;
125 }
126
127 struct database *new_database(const char *id, NMEM nmem)
128 {
129     struct database *db;
130     struct setting *idset;
131
132     db = nmem_malloc(nmem, sizeof(*db));
133     memset(db, 0, sizeof(*db));
134
135     db->url = nmem_strdup(nmem, id);
136     db->errors = 0;
137     db->host = 0;
138
139     db->num_settings = PZ_MAX_EOF;
140     db->settings = nmem_malloc(nmem, sizeof(struct settings*) * 
141                                db->num_settings);
142     memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
143     idset = nmem_malloc(nmem, sizeof(*idset));
144     idset->precedence = 0;
145     idset->name = "pz:id";
146     idset->target = idset->value = db->url;
147     idset->next = 0;
148     db->settings[PZ_ID] = idset;
149     db->next = 0;
150
151     return db;
152 }
153
154 static struct database *load_database(const char *id,
155                                       struct conf_service *service)
156 {
157     struct database *db;
158
159     db = new_database(id, service->nmem);
160     
161     db->next = service->databases;
162     service->databases = db;
163
164     return db;
165 }
166
167 // Return a database structure by ID. Load and add to list if necessary
168 // new==1 just means we know it's not in the list
169 struct database *find_database(const char *id, struct conf_service *service)
170 {
171     struct database *p;
172     for (p = service->databases; p; p = p->next)
173         if (!strcmp(p->url, id))
174             return p;
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_lookup_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 (c->type == PAZPAR2_STRING_MATCH)
227         {
228             if (offset == PZ_ID)
229             {
230                 if (match_zurl(settings[offset]->value, v->value))
231                     break;
232             }
233             else 
234             {
235                 if (!strcmp(settings[offset]->value, v->value))
236                     break;
237             }
238         }            
239         else if (c->type == PAZPAR2_SUBSTRING_MATCH)
240         {
241             if (strstr(settings[offset]->value, v->value))
242                 break;
243         }
244     }
245     if (v)
246         return 1;
247     else
248         return 0;
249 }
250
251 // parses crit1=val1,crit2=val2|val3,...
252 static struct database_criterion *create_database_criterion(NMEM m,
253                                                             const char *buf)
254 {
255     struct database_criterion *res = 0;
256     char **values;
257     int num;
258     int i;
259
260     if (!buf || !*buf)
261         return 0;
262     nmem_strsplit(m, ",", buf,  &values, &num);
263     for (i = 0; i < num; i++)
264     {
265         char **subvalues;
266         int subnum;
267         int subi;
268         struct database_criterion *new = nmem_malloc(m, sizeof(*new));
269         char *eq;
270         if ((eq = strchr(values[i], '=')))
271             new->type = PAZPAR2_STRING_MATCH;
272         else if ((eq = strchr(values[i], '~')))
273             new->type = PAZPAR2_SUBSTRING_MATCH;
274         else
275         {
276             yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
277             return 0;
278         }
279         *(eq++) = '\0';
280         new->name = values[i];
281         nmem_strsplit(m, "|", eq, &subvalues, &subnum);
282         new->values = 0;
283         for (subi = 0; subi < subnum; subi++)
284         {
285             struct database_criterion_value *newv
286                 = nmem_malloc(m, sizeof(*newv));
287             newv->value = subvalues[subi];
288             newv->next = new->values;
289             new->values = newv;
290         }
291         new->next = res;
292         res = new;
293     }
294     return res;
295 }
296
297 static int database_match_criteria(struct setting **settings,
298                                    struct conf_service *service,
299                                    struct database_criterion *cl)
300 {
301     for (; cl; cl = cl->next)
302         if (!match_criterion(settings, service, cl))
303             break;
304     if (cl) // one of the criteria failed to match -- skip this db
305         return 0;
306     else
307         return 1;
308 }
309
310 // Cycles through databases, calling a handler function on the ones for
311 // which all criteria matched.
312 int session_grep_databases(struct session *se, const char *filter,
313                            void (*fun)(struct session *se, struct session_database *db))
314 {
315     struct session_database *p;
316     NMEM nmem = nmem_create();
317     int i = 0;
318     struct database_criterion *cl = create_database_criterion(nmem, filter);
319
320     for (p = se->databases; p; p = p->next)
321     {
322         if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
323             continue;
324         if (!p->settings[PZ_NAME])
325             continue;
326         if (database_match_criteria(p->settings, se->service, cl))
327         {
328             (*fun)(se, p);
329             i++;
330         }
331     }
332     nmem_destroy(nmem);
333     return i;
334 }
335
336 int predef_grep_databases(void *context, struct conf_service *service,
337                           void (*fun)(void *context, struct database *db))
338 {
339     struct database *p;
340     int i = 0;
341
342     for (p = service->databases; p; p = p->next)
343         if (database_match_criteria(p->settings, service, 0))
344         {
345             (*fun)(context, p);
346             i++;
347         }
348     return i;
349 }
350
351 database_hosts_t database_hosts_create(void)
352 {
353     database_hosts_t p = xmalloc(sizeof(*p));
354     p->hosts = 0;
355     p->mutex = 0;
356     pazpar2_mutex_create(&p->mutex, "database");
357     return p;
358 }
359
360 void database_hosts_destroy(database_hosts_t *pp)
361 {
362     if (*pp)
363     {
364         struct host *p = (*pp)->hosts;
365         while (p)
366         {
367             struct host *p_next = p->next;
368             yaz_mutex_destroy(&p->mutex);
369             yaz_cond_destroy(&p->cond_ready);
370             xfree(p->hostport);
371             xfree(p);
372             p = p_next;
373         }
374         yaz_mutex_destroy(&(*pp)->mutex);
375         xfree(*pp);
376     }
377 }
378
379 /*
380  * Local variables:
381  * c-basic-offset: 4
382  * c-file-style: "Stroustrup"
383  * indent-tabs-mode: nil
384  * End:
385  * vim: shiftwidth=4 tabstop=8 expandtab
386  */
387