1 /* $Id: database.c,v 1.18 2007-04-13 00:29:14 quinn Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include <libxml/parser.h>
23 #include <libxml/tree.h>
24 #include <libxslt/xslt.h>
25 #include <libxslt/transform.h>
26 #include <libxslt/xsltutils.h>
28 #include <sys/types.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
40 #include <netinet/in.h>
42 static struct host *hosts = 0; // The hosts we know about
43 static struct database *databases = 0; // The databases we know about
46 static xmlDoc *get_explain_xml(const char *id)
52 if (!config || !config->targetprofiles)
54 yaz_log(YLOG_WARN, "Config must be loaded and specify targetprofiles");
57 if (config->targetprofiles->type != Targetprofiles_local)
59 yaz_log(YLOG_FATAL, "Only supports local type");
62 dir = config->targetprofiles->src;
64 sprintf(path, "%s/%s", dir, ide);
66 return xmlParseFile(path);
71 // Create a new host structure for hostport
72 static struct host *create_host(const char *hostport)
74 struct addrinfo *addrinfo, hints;
78 unsigned char addrbuf[4];
81 host = xmalloc(sizeof(struct host));
82 host->hostport = xstrdup(hostport);
83 host->connections = 0;
85 if ((port = strchr(hostport, ':')))
91 hints.ai_family = PF_INET;
92 hints.ai_socktype = SOCK_STREAM;
93 hints.ai_protocol = IPPROTO_TCP;
96 hints.ai_canonname = 0;
98 // This is not robust code. It assumes that getaddrinfo always
99 // returns AF_INET address.
100 if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
102 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", hostport, gai_strerror(res));
103 xfree(host->hostport);
107 assert(addrinfo->ai_family == PF_INET);
108 memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
109 sprintf(ipport, "%u.%u.%u.%u:%s",
110 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
111 host->ipport = xstrdup(ipport);
112 freeaddrinfo(addrinfo);
118 static struct host *find_host(const char *hostport)
121 for (p = hosts; p; p = p->next)
122 if (!strcmp(p->hostport, hostport))
124 return create_host(hostport);
127 static struct database *load_database(const char *id)
130 struct zr_explain *explain = 0;
136 yaz_log(YLOG_LOG, "New database: %s", id);
138 nmem = nmem_create();
140 if (config && config->targetprofiles
141 && (doc = get_explain_xml(id)))
143 explain = zr_read_xml(nmem, xmlDocGetRootElement(doc));
148 if (strlen(id) > 255)
150 strcpy(hostport, id);
151 if ((dbname = strchr(hostport, '/')))
155 if (!(host = find_host(hostport)))
157 db = nmem_malloc(nmem, sizeof(*db));
158 memset(db, 0, sizeof(*db));
160 db->url = nmem_strdup(nmem, id);
161 db->databases = xmalloc(2 * sizeof(char *));
162 db->databases[0] = nmem_strdup(nmem, dbname);
163 db->databases[1] = 0;
165 db->explain = explain;
167 db->next = databases;
176 // Return a database structure by ID. Load and add to list if necessary
177 // new==1 just means we know it's not in the list
178 struct database *find_database(const char *id, int new)
183 for (p = databases; p; p = p->next)
184 if (!strcmp(p->url, id))
187 return load_database(id);
190 // This whole session_grep database thing should be moved to pazpar2.c
192 int match_zurl(const char *zurl, const char *pattern)
194 if (!strcmp(pattern, "*"))
196 else if (!strncmp(pattern, "*/", 2))
198 char *db = strchr(zurl, '/');
201 if (!strcmp(pattern + 2, db))
206 else if (!strcmp(pattern, zurl))
212 // This will be generalized at some point
213 static int match_criterion(struct setting **settings, struct database_criterion *c)
215 int offset = settings_offset(c->name);
216 struct database_criterion_value *v;
220 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
223 if (!settings[offset])
225 for (v = c->values; v; v = v->next)
229 if (match_zurl(settings[offset]->value, v->value))
236 if (!strcmp(settings[offset]->value, v->value))
245 int database_match_criteria(struct setting **settings, struct database_criterion *cl)
247 for (; cl; cl = cl->next)
248 if (!match_criterion(settings, cl))
250 if (cl) // one of the criteria failed to match -- skip this db
256 // Cycles through databases, calling a handler function on the ones for
257 // which all criteria matched.
258 int session_grep_databases(struct session *se, struct database_criterion *cl,
259 void (*fun)(void *context, struct session_database *db))
261 struct session_database *p;
264 for (p = se->databases; p; p = p->next)
266 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
268 if (database_match_criteria(p->settings, cl))
277 int grep_databases(void *context, struct database_criterion *cl,
278 void (*fun)(void *context, struct database *db))
283 for (p = databases; p; p = p->next)
284 if (database_match_criteria(p->settings, cl))
292 // Initialize CCL map for a target
293 // Note: This approach ignores user-specific CCL maps, for which I
294 // don't presently see any application.
295 static void prepare_cclmap(void *ignore, struct database *db)
301 db->ccl_map = ccl_qual_mk();
302 for (s = db->settings[PZ_CCLMAP]; s; s = s->next)
304 char *p = strchr(s->name + 3, ':');
307 yaz_log(YLOG_FATAL, "Malformed cclmap name: %s", s->name);
311 ccl_qual_fitem(db->ccl_map, s->value, p);
315 // Initialize YAZ Map structures for MARC-based targets
316 static void prepare_yazmarc(void *ignore, struct database *db)
322 for (s = db->settings[PZ_NATIVESYNTAX]; s; s = s->next)
323 if (!strcmp(s->value, "iso2709"))
325 char *encoding = "marc-8s";
328 db->yaz_marc = yaz_marc_create();
329 yaz_marc_subfield_str(db->yaz_marc, "\t");
331 // See if a native encoding is specified
332 if (db->settings[PZ_ENCODING])
333 encoding = db->settings[PZ_ENCODING]->value;
335 cm = yaz_iconv_open("utf-8", encoding);
339 "Unable to map from %s to UTF-8 for target %s",
343 yaz_marc_iconv(db->yaz_marc, cm);
347 // Prepare XSLT stylesheets for record normalization
348 static void prepare_map(void *ignore, struct database *db)
354 for (s = db->settings[PZ_XSLT]; s; s = s->next)
357 struct database_retrievalmap **m = &db->map;
360 nmem_strsplit(nmem, ",", s->value, &stylesheets, &num);
361 for (i = 0; i < num; i++)
363 (*m) = nmem_malloc(nmem, sizeof(**m));
365 if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
367 yaz_log(YLOG_FATAL, "Unable to load stylesheet: %s",
375 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s", db->url);
378 // Read settings for each database, and prepare support data structures
379 void prepare_databases(void)
381 grep_databases(0, 0, prepare_cclmap);
382 grep_databases(0, 0, prepare_yazmarc);
383 grep_databases(0, 0, prepare_map);
386 // This function will most likely vanish when a proper target profile mechanism is
388 void load_simpletargets(const char *fn)
390 FILE *f = fopen(fn, "r");
395 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
399 while (fgets(line, 255, f))
405 if (strncmp(line, "target ", 7))
407 line[strlen(line) - 1] = '\0';
409 if ((name = strchr(line, ';')))
414 if (!(db = find_database(url, 0)))
415 yaz_log(YLOG_WARN, "Unable to load database %s", url);
424 * indent-tabs-mode: nil
426 * vim: shiftwidth=4 tabstop=8 expandtab