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