1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 Index Data
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
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
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
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
27 #include <sys/types.h>
35 #include "pazpar2_config.h"
41 #include <sys/types.h>
43 #include <sys/socket.h>
49 #include <netinet/in.h>
52 enum pazpar2_database_criterion_type {
54 PAZPAR2_SUBSTRING_MATCH
57 struct database_criterion_value {
59 struct database_criterion_value *next;
62 struct database_criterion {
64 enum pazpar2_database_criterion_type type;
65 struct database_criterion_value *values;
66 struct database_criterion *next;
70 struct database_hosts {
75 static xmlDoc *get_explain_xml(struct conf_targetprofiles *targetprofiles,
82 if (targetprofiles->type != Targetprofiles_local)
84 yaz_log(YLOG_FATAL, "Only supports local type");
87 dir = targetprofiles->src;
89 sprintf(path, "%s/%s", dir, ide);
91 return xmlParseFile(path);
96 // Create a new host structure for hostport
97 static struct host *create_host(const char *hostport, iochan_man_t iochan_man)
101 host = xmalloc(sizeof(struct host));
102 host->hostport = xstrdup(hostport);
103 host->connections = 0;
107 if (host_getaddrinfo(host, iochan_man))
109 xfree(host->hostport);
113 pazpar2_mutex_create(&host->mutex, "host");
118 static struct host *find_host(database_hosts_t hosts,
119 const char *hostport, iochan_man_t iochan_man)
122 yaz_mutex_enter(hosts->mutex);
123 for (p = hosts->hosts; p; p = p->next)
124 if (!strcmp(p->hostport, hostport))
128 p = create_host(hostport, iochan_man);
131 p->next = hosts->hosts;
135 yaz_mutex_leave(hosts->mutex);
139 int resolve_database(struct conf_service *service, struct database *db)
146 strcpy(hostport, db->url);
147 if ((p = strchr(hostport, '/')))
149 if (!(host = find_host(service->server->database_hosts,
150 hostport, service->server->iochan_man)))
157 void resolve_databases(struct conf_service *service)
159 struct database *db = service->databases;
160 for (; db; db = db->next)
161 resolve_database(service, db);
164 struct database *new_database(const char *id, NMEM nmem)
169 struct setting *idset;
171 if (strlen(id) > 255)
173 strcpy(hostport, id);
174 if ((dbname = strchr(hostport, '/')))
178 db = nmem_malloc(nmem, sizeof(*db));
179 memset(db, 0, sizeof(*db));
181 db->url = nmem_strdup(nmem, id);
182 db->databases = nmem_malloc(nmem, 2 * sizeof(char *));
183 db->databases[0] = nmem_strdup(nmem, dbname);
184 db->databases[1] = 0;
188 db->num_settings = PZ_NEGOTIATION_CHARSET+1;
189 db->settings = nmem_malloc(nmem, sizeof(struct settings*) *
191 memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
192 idset = nmem_malloc(nmem, sizeof(*idset));
193 idset->precedence = 0;
194 idset->name = "pz:id";
195 idset->target = idset->value = db->url;
197 db->settings[PZ_ID] = idset;
203 static struct database *load_database(const char *id,
204 struct conf_service *service)
207 struct zr_explain *explain = 0;
210 if (service->targetprofiles
211 && (doc = get_explain_xml(service->targetprofiles, id)))
213 explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
217 db = new_database(id, service->nmem);
218 db->explain = explain;
219 db->next = service->databases;
220 service->databases = db;
225 // Return a database structure by ID. Load and add to list if necessary
226 // new==1 just means we know it's not in the list
227 struct database *find_database(const char *id, struct conf_service *service)
230 for (p = service->databases; p; p = p->next)
231 if (!strcmp(p->url, id))
233 return load_database(id, service);
236 // This whole session_grep database thing should be moved elsewhere
238 int match_zurl(const char *zurl, const char *pattern)
242 if (!strcmp(pattern, "*"))
244 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
246 char *db = strchr(zurl, '/');
249 if (!strcmp(pattern + 2, db))
254 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
256 if (!strncmp(pattern, zurl, len))
261 else if (!strcmp(pattern, zurl))
267 // This will be generalized at some point
268 static int match_criterion(struct setting **settings,
269 struct conf_service *service,
270 struct database_criterion *c)
272 int offset = settings_lookup_offset(service, c->name);
273 struct database_criterion_value *v;
277 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
280 if (!settings[offset])
282 for (v = c->values; v; v = v->next)
284 if (c->type == PAZPAR2_STRING_MATCH)
288 if (match_zurl(settings[offset]->value, v->value))
293 if (!strcmp(settings[offset]->value, v->value))
297 else if (c->type == PAZPAR2_SUBSTRING_MATCH)
299 if (strstr(settings[offset]->value, v->value))
309 // parses crit1=val1,crit2=val2|val3,...
310 static struct database_criterion *create_database_criterion(NMEM m,
313 struct database_criterion *res = 0;
320 nmem_strsplit(m, ",", buf, &values, &num);
321 for (i = 0; i < num; i++)
326 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
328 if ((eq = strchr(values[i], '=')))
329 new->type = PAZPAR2_STRING_MATCH;
330 else if ((eq = strchr(values[i], '~')))
331 new->type = PAZPAR2_SUBSTRING_MATCH;
334 yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
338 new->name = values[i];
339 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
341 for (subi = 0; subi < subnum; subi++)
343 struct database_criterion_value *newv
344 = nmem_malloc(m, sizeof(*newv));
345 newv->value = subvalues[subi];
346 newv->next = new->values;
355 static int database_match_criteria(struct setting **settings,
356 struct conf_service *service,
357 struct database_criterion *cl)
359 for (; cl; cl = cl->next)
360 if (!match_criterion(settings, service, cl))
362 if (cl) // one of the criteria failed to match -- skip this db
368 // Cycles through databases, calling a handler function on the ones for
369 // which all criteria matched.
370 int session_grep_databases(struct session *se, const char *filter,
371 void (*fun)(void *context, struct session_database *db))
373 struct session_database *p;
374 NMEM nmem = nmem_create();
376 struct database_criterion *cl = create_database_criterion(nmem, filter);
378 for (p = se->databases; p; p = p->next)
380 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
382 if (!p->settings[PZ_NAME])
384 if (database_match_criteria(p->settings, se->service, cl))
394 int predef_grep_databases(void *context, struct conf_service *service,
395 void (*fun)(void *context, struct database *db))
400 for (p = service->databases; p; p = p->next)
401 if (database_match_criteria(p->settings, service, 0))
409 database_hosts_t database_hosts_create(void)
411 database_hosts_t p = xmalloc(sizeof(*p));
414 pazpar2_mutex_create(&p->mutex, "database");
418 void database_hosts_destroy(database_hosts_t *pp)
422 struct host *p = (*pp)->hosts;
425 struct host *p_next = p->next;
426 yaz_mutex_destroy(&p->mutex);
432 yaz_mutex_destroy(&(*pp)->mutex);
440 * c-file-style: "Stroustrup"
441 * indent-tabs-mode: nil
443 * vim: shiftwidth=4 tabstop=8 expandtab