Bump copyright year
[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)
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))
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)
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);
117 }
118
119 int resolve_database(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)))
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(db);
141 }
142
143 static struct database *load_database(const char *id,
144     struct conf_service *service)
145 {
146     xmlDoc *doc = 0;
147     struct zr_explain *explain = 0;
148     struct database *db;
149     char hostport[256];
150     char *dbname;
151     struct setting *idset;
152
153     if (service->targetprofiles 
154         && (doc = get_explain_xml(service->targetprofiles, id)))
155     {
156         explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
157         if (!explain)
158             return 0;
159     }
160
161     if (strlen(id) > 255)
162         return 0;
163     strcpy(hostport, id);
164     if ((dbname = strchr(hostport, '/')))
165         *(dbname++) = '\0';
166     else
167         dbname = "";
168     db = nmem_malloc(service->nmem, sizeof(*db));
169     memset(db, 0, sizeof(*db));
170     db->host = 0;
171     db->url = nmem_strdup(service->nmem, id);
172     db->databases = nmem_malloc(service->nmem, 2 * sizeof(char *));
173     db->databases[0] = nmem_strdup(service->nmem, dbname);
174     db->databases[1] = 0;
175     db->errors = 0;
176     db->explain = explain;
177
178     db->num_settings = settings_num(service);
179     db->settings = nmem_malloc(service->nmem, sizeof(struct settings*) * 
180                                db->num_settings);
181     memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
182     idset = nmem_malloc(service->nmem, sizeof(*idset));
183     idset->precedence = 0;
184     idset->name = "pz:id";
185     idset->target = idset->value = db->url;
186     idset->next = 0;
187     db->settings[PZ_ID] = idset;
188
189     db->next = service->databases;
190     service->databases = db;
191
192     return db;
193 }
194
195 // Return a database structure by ID. Load and add to list if necessary
196 // new==1 just means we know it's not in the list
197 struct database *find_database(const char *id, struct conf_service *service)
198 {
199     struct database *p;
200     for (p = service->databases; p; p = p->next)
201         if (!strcmp(p->url, id))
202             return p;
203     return load_database(id, service);
204 }
205
206 // This whole session_grep database thing should be moved elsewhere
207
208 int match_zurl(const char *zurl, const char *pattern)
209 {
210     int len;
211
212     if (!strcmp(pattern, "*"))
213         return 1;
214     else if (!strncmp(pattern, "*/", 2))   // host wildcard.. what the heck is that for?
215     {
216         char *db = strchr(zurl, '/');
217         if (!db)
218             return 0;
219         if (!strcmp(pattern + 2, db))
220             return 1;
221         else
222             return 0;
223     }
224     else if (*(pattern + (len = strlen(pattern) - 1)) == '*')  // db wildcard
225     {
226         if (!strncmp(pattern, zurl, len))
227             return 1;
228         else
229             return 2;
230     }
231     else if (!strcmp(pattern, zurl))
232         return 1;
233     else
234         return 0;
235 }
236
237 // This will be generalized at some point
238 static int match_criterion(struct setting **settings,
239                            struct conf_service *service, 
240                            struct database_criterion *c)
241 {
242     int offset = settings_lookup_offset(service, c->name);
243     struct database_criterion_value *v;
244
245     if (offset < 0)
246     {
247         yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
248         return 0;
249     }
250     if (!settings[offset])
251         return 0;
252     for (v = c->values; v; v = v->next)
253     {
254         if (c->type == PAZPAR2_STRING_MATCH)
255         {
256             if (offset == PZ_ID)
257             {
258                 if (match_zurl(settings[offset]->value, v->value))
259                     break;
260             }
261             else 
262             {
263                 if (!strcmp(settings[offset]->value, v->value))
264                     break;
265             }
266         }            
267         else if (c->type == PAZPAR2_SUBSTRING_MATCH)
268         {
269             if (strstr(settings[offset]->value, v->value))
270                 break;
271         }
272     }
273     if (v)
274         return 1;
275     else
276         return 0;
277 }
278
279 // parses crit1=val1,crit2=val2|val3,...
280 static struct database_criterion *create_database_criterion(NMEM m,
281                                                             const char *buf)
282 {
283     struct database_criterion *res = 0;
284     char **values;
285     int num;
286     int i;
287
288     if (!buf || !*buf)
289         return 0;
290     nmem_strsplit(m, ",", buf,  &values, &num);
291     for (i = 0; i < num; i++)
292     {
293         char **subvalues;
294         int subnum;
295         int subi;
296         struct database_criterion *new = nmem_malloc(m, sizeof(*new));
297         char *eq;
298         if ((eq = strchr(values[i], '=')))
299             new->type = PAZPAR2_STRING_MATCH;
300         else if ((eq = strchr(values[i], '~')))
301             new->type = PAZPAR2_SUBSTRING_MATCH;
302         else
303         {
304             yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
305             return 0;
306         }
307         *(eq++) = '\0';
308         new->name = values[i];
309         nmem_strsplit(m, "|", eq, &subvalues, &subnum);
310         new->values = 0;
311         for (subi = 0; subi < subnum; subi++)
312         {
313             struct database_criterion_value *newv
314                 = nmem_malloc(m, sizeof(*newv));
315             newv->value = subvalues[subi];
316             newv->next = new->values;
317             new->values = newv;
318         }
319         new->next = res;
320         res = new;
321     }
322     return res;
323 }
324
325 static int database_match_criteria(struct setting **settings,
326                                    struct conf_service *service,
327                                    struct database_criterion *cl)
328 {
329     for (; cl; cl = cl->next)
330         if (!match_criterion(settings, service, cl))
331             break;
332     if (cl) // one of the criteria failed to match -- skip this db
333         return 0;
334     else
335         return 1;
336 }
337
338 // Cycles through databases, calling a handler function on the ones for
339 // which all criteria matched.
340 int session_grep_databases(struct session *se, const char *filter,
341                            void (*fun)(void *context, struct session_database *db))
342 {
343     struct session_database *p;
344     NMEM nmem = nmem_create();
345     int i = 0;
346     struct database_criterion *cl = create_database_criterion(nmem, filter);
347
348     for (p = se->databases; p; p = p->next)
349     {
350         if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
351             continue;
352         if (!p->settings[PZ_NAME])
353             continue;
354         if (database_match_criteria(p->settings, se->service, cl))
355         {
356             (*fun)(se, p);
357             i++;
358         }
359     }
360     nmem_destroy(nmem);
361     return i;
362 }
363
364 int predef_grep_databases(void *context, struct conf_service *service,
365                           void (*fun)(void *context, struct database *db))
366 {
367     struct database *p;
368     int i = 0;
369
370     for (p = service->databases; p; p = p->next)
371         if (database_match_criteria(p->settings, service, 0))
372         {
373             (*fun)(context, p);
374             i++;
375         }
376     return i;
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