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