lowered logging to DEBUG
[pazpar2-moved-to-github.git] / src / database.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2012 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 *new_database(const char *id, NMEM nmem)
58 {
59     return new_database_inherit_settings(id, nmem, 0);
60 }
61 struct database *new_database_inherit_settings(const char *id, NMEM nmem, struct settings *service_settings)
62 {
63     struct database *db;
64     struct setting *idset;
65
66     db = nmem_malloc(nmem, sizeof(*db));
67     db->id = nmem_strdup(nmem, id);
68     db->next = 0;
69
70     if (service_settings && service_settings->num_settings > 0) {
71         yaz_log(YLOG_DEBUG, "copying settings from service to database %s settings", db->id);
72         db->num_settings = service_settings->num_settings;
73         db->settings = nmem_malloc(nmem, sizeof(struct settings*) * db->num_settings);
74         // Initialize database settings with service settings
75         memcpy(db->settings, service_settings->settings,  sizeof(struct settings*) * db->num_settings);
76
77     }
78     else {
79         yaz_log(YLOG_DEBUG, "No service settings to database %s ", db->id);
80         db->num_settings = PZ_MAX_EOF;
81         db->settings = nmem_malloc(nmem, sizeof(struct settings*) * db->num_settings);
82         memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
83     }
84     idset = nmem_malloc(nmem, sizeof(*idset));
85     idset->precedence = 0;
86     idset->name = "pz:id";
87     idset->target = idset->value = db->id;
88     idset->next = db->settings[PZ_ID];
89     db->settings[PZ_ID] = idset;
90
91     return db;
92 }
93
94 // Return a database structure by ID. Load and add to list if necessary
95 // new==1 just means we know it's not in the list
96 struct database *create_database_for_service(const char *id,
97                                              struct conf_service *service)
98 {
99     struct database *p;
100     for (p = service->databases; p; p = p->next)
101         if (!strcmp(p->id, id))
102             return p;
103     
104     yaz_log(YLOG_DEBUG, "new database %s under service %s values %p", id, service->id);
105     p = new_database_inherit_settings(id, service->nmem, service->settings);
106
107     p->next = service->databases;
108     service->databases = p;
109
110     return p;
111 }
112
113 // This whole session_grep database thing should be moved elsewhere
114
115 int match_zurl(const char *zurl, const char *pattern)
116 {
117     int len;
118
119     if (!strcmp(pattern, "*"))
120         return 1;
121     else if (!strncmp(pattern, "*/", 2))   // host wildcard.. what the heck is that for?
122     {
123         char *db = strchr(zurl, '/');
124         if (!db)
125             return 0;
126         if (!strcmp(pattern + 2, db))
127             return 1;
128         else
129             return 0;
130     }
131     else if (*(pattern + (len = strlen(pattern) - 1)) == '*')  // db wildcard
132     {
133         if (!strncmp(pattern, zurl, len))
134             return 1;
135         else
136             return 0;
137     }
138     else if (!strcmp(pattern, zurl))
139         return 1;
140     else
141         return 0;
142 }
143
144 // This will be generalized at some point
145 static int match_criterion(struct setting **settings,
146                            struct conf_service *service, 
147                            struct database_criterion *c)
148 {
149     int offset = settings_lookup_offset(service, c->name);
150     struct database_criterion_value *v;
151
152     if (offset < 0)
153     {
154         yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
155         return 0;
156     }
157     if (!settings[offset])
158         return 0;
159     for (v = c->values; v; v = v->next)
160     {
161         if (c->type == PAZPAR2_STRING_MATCH)
162         {
163             if (offset == PZ_ID)
164             {
165                 if (match_zurl(settings[offset]->value, v->value))
166                     break;
167             }
168             else 
169             {
170                 if (!strcmp(settings[offset]->value, v->value))
171                     break;
172             }
173         }            
174         else if (c->type == PAZPAR2_SUBSTRING_MATCH)
175         {
176             if (strstr(settings[offset]->value, v->value))
177                 break;
178         }
179     }
180     if (v)
181         return 1;
182     else
183         return 0;
184 }
185
186 // parses crit1=val1,crit2=val2|val3,...
187 static struct database_criterion *create_database_criterion(NMEM m,
188                                                             const char *buf)
189 {
190     struct database_criterion *res = 0;
191     char **values;
192     int num;
193     int i;
194
195     if (!buf || !*buf)
196         return 0;
197     nmem_strsplit(m, ",", buf,  &values, &num);
198     for (i = 0; i < num; i++)
199     {
200         char **subvalues;
201         int subnum;
202         int subi;
203         struct database_criterion *new = nmem_malloc(m, sizeof(*new));
204         char *eq;
205         if ((eq = strchr(values[i], '=')))
206             new->type = PAZPAR2_STRING_MATCH;
207         else if ((eq = strchr(values[i], '~')))
208             new->type = PAZPAR2_SUBSTRING_MATCH;
209         else
210         {
211             yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
212             return 0;
213         }
214         *(eq++) = '\0';
215         new->name = values[i];
216         nmem_strsplit(m, "|", eq, &subvalues, &subnum);
217         new->values = 0;
218         for (subi = 0; subi < subnum; subi++)
219         {
220             struct database_criterion_value *newv
221                 = nmem_malloc(m, sizeof(*newv));
222             newv->value = subvalues[subi];
223             newv->next = new->values;
224             new->values = newv;
225         }
226         new->next = res;
227         res = new;
228     }
229     return res;
230 }
231
232 static int database_match_criteria(struct setting **settings,
233                                    struct conf_service *service,
234                                    struct database_criterion *cl)
235 {
236     for (; cl; cl = cl->next)
237         if (!match_criterion(settings, service, cl))
238             break;
239     if (cl) // one of the criteria failed to match -- skip this db
240         return 0;
241     else
242         return 1;
243 }
244
245 // Cycles through databases, calling a handler function on the ones for
246 // which all criteria matched.
247 int session_grep_databases(struct session *se, const char *filter,
248                            void (*fun)(struct session *se, struct session_database *db))
249 {
250     struct session_database *p;
251     NMEM nmem = nmem_create();
252     int i = 0;
253     struct database_criterion *cl = create_database_criterion(nmem, filter);
254
255     for (p = se->databases; p; p = p->next)
256     {
257         if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
258             continue;
259         if (!p->settings[PZ_NAME])
260             continue;
261         if (database_match_criteria(p->settings, se->service, cl))
262         {
263             (*fun)(se, p);
264             i++;
265         }
266     }
267     nmem_destroy(nmem);
268     return i;
269 }
270
271 int predef_grep_databases(void *context, struct conf_service *service,
272                           void (*fun)(void *context, struct database *db))
273 {
274     struct database *p;
275     int i = 0;
276
277     for (p = service->databases; p; p = p->next)
278         if (database_match_criteria(p->settings, service, 0))
279         {
280             (*fun)(context, p);
281             i++;
282         }
283     return i;
284 }
285
286 /*
287  * Local variables:
288  * c-basic-offset: 4
289  * c-file-style: "Stroustrup"
290  * indent-tabs-mode: nil
291  * End:
292  * vim: shiftwidth=4 tabstop=8 expandtab
293  */
294