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