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