1 /* $Id: database.c,v 1.24 2007-04-22 15:00:56 adam 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)
76 host = xmalloc(sizeof(struct host));
77 host->hostport = xstrdup(hostport);
78 host->connections = 0;
81 if (host_getaddrinfo(host))
83 xfree(host->hostport);
92 static struct host *find_host(const char *hostport)
95 for (p = hosts; p; p = p->next)
96 if (!strcmp(p->hostport, hostport))
98 return create_host(hostport);
101 static struct database *load_database(const char *id)
104 struct zr_explain *explain = 0;
110 yaz_log(YLOG_LOG, "New database: %s", id);
112 nmem = nmem_create();
114 if (config && config->targetprofiles
115 && (doc = get_explain_xml(id)))
117 explain = zr_read_xml(nmem, xmlDocGetRootElement(doc));
122 if (strlen(id) > 255)
124 strcpy(hostport, id);
125 if ((dbname = strchr(hostport, '/')))
129 if (!(host = find_host(hostport)))
131 db = nmem_malloc(nmem, sizeof(*db));
132 memset(db, 0, sizeof(*db));
134 db->url = nmem_strdup(nmem, id);
135 db->databases = xmalloc(2 * sizeof(char *));
136 db->databases[0] = nmem_strdup(nmem, dbname);
137 db->databases[1] = 0;
139 db->explain = explain;
141 db->next = databases;
147 // Return a database structure by ID. Load and add to list if necessary
148 // new==1 just means we know it's not in the list
149 struct database *find_database(const char *id, int new)
154 for (p = databases; p; p = p->next)
155 if (!strcmp(p->url, id))
158 return load_database(id);
161 // This whole session_grep database thing should be moved elsewhere
163 int match_zurl(const char *zurl, const char *pattern)
165 if (!strcmp(pattern, "*"))
167 else if (!strncmp(pattern, "*/", 2))
169 char *db = strchr(zurl, '/');
172 if (!strcmp(pattern + 2, db))
177 else if (!strcmp(pattern, zurl))
183 // This will be generalized at some point
184 static int match_criterion(struct setting **settings, struct database_criterion *c)
186 int offset = settings_offset(c->name);
187 struct database_criterion_value *v;
191 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
194 if (!settings[offset])
196 for (v = c->values; v; v = v->next)
200 if (match_zurl(settings[offset]->value, v->value))
205 if (!strcmp(settings[offset]->value, v->value))
215 int database_match_criteria(struct setting **settings, struct database_criterion *cl)
217 for (; cl; cl = cl->next)
218 if (!match_criterion(settings, cl))
220 if (cl) // one of the criteria failed to match -- skip this db
226 // Cycles through databases, calling a handler function on the ones for
227 // which all criteria matched.
228 int session_grep_databases(struct session *se, struct database_criterion *cl,
229 void (*fun)(void *context, struct session_database *db))
231 struct session_database *p;
234 for (p = se->databases; p; p = p->next)
236 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
238 if (database_match_criteria(p->settings, cl))
247 int grep_databases(void *context, struct database_criterion *cl,
248 void (*fun)(void *context, struct database *db))
253 for (p = databases; p; p = p->next)
254 if (database_match_criteria(p->settings, cl))
265 * indent-tabs-mode: nil
267 * vim: shiftwidth=4 tabstop=8 expandtab