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