1 /* $Id: database.c,v 1.28 2007-05-25 03:58:04 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>
38 #include <sys/types.h>
39 #include <sys/socket.h>
41 #include <netinet/in.h>
43 static struct host *hosts = 0; // The hosts we know about
44 static struct database *databases = 0; // The databases we know about
47 static xmlDoc *get_explain_xml(const char *id)
53 if (!config || !config->targetprofiles)
55 yaz_log(YLOG_WARN, "Config must be loaded and specify targetprofiles");
58 if (config->targetprofiles->type != Targetprofiles_local)
60 yaz_log(YLOG_FATAL, "Only supports local type");
63 dir = config->targetprofiles->src;
65 sprintf(path, "%s/%s", dir, ide);
67 return xmlParseFile(path);
72 // Create a new host structure for hostport
73 static struct host *create_host(const char *hostport)
77 host = xmalloc(sizeof(struct host));
78 host->hostport = xstrdup(hostport);
79 host->connections = 0;
82 if (host_getaddrinfo(host))
84 xfree(host->hostport);
93 static struct host *find_host(const char *hostport)
96 for (p = hosts; p; p = p->next)
97 if (!strcmp(p->hostport, hostport))
99 return create_host(hostport);
102 static struct database *load_database(const char *id)
105 struct zr_explain *explain = 0;
110 struct setting *idset;
112 yaz_log(YLOG_LOG, "New database: %s", id);
114 nmem = nmem_create();
116 if (config && config->targetprofiles
117 && (doc = get_explain_xml(id)))
119 explain = zr_read_xml(nmem, xmlDocGetRootElement(doc));
124 if (strlen(id) > 255)
126 strcpy(hostport, id);
127 if ((dbname = strchr(hostport, '/')))
131 if (!(host = find_host(hostport)))
133 db = nmem_malloc(nmem, sizeof(*db));
134 memset(db, 0, sizeof(*db));
136 db->url = nmem_strdup(nmem, id);
137 db->databases = xmalloc(2 * sizeof(char *));
138 db->databases[0] = nmem_strdup(nmem, dbname);
139 db->databases[1] = 0;
141 db->explain = explain;
145 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * settings_num());
146 memset(db->settings, 0, sizeof(struct settings*) * settings_num());
147 idset = nmem_malloc(nmem, sizeof(*idset));
148 idset->precedence = 0;
149 idset->name = "pz:id";
150 idset->target = idset->value = db->url;
152 db->settings[PZ_ID] = idset;
154 db->next = databases;
160 // Return a database structure by ID. Load and add to list if necessary
161 // new==1 just means we know it's not in the list
162 struct database *find_database(const char *id, int new)
167 for (p = databases; p; p = p->next)
168 if (!strcmp(p->url, id))
171 return load_database(id);
174 // This whole session_grep database thing should be moved elsewhere
176 int match_zurl(const char *zurl, const char *pattern)
178 if (!strcmp(pattern, "*"))
180 else if (!strncmp(pattern, "*/", 2))
182 char *db = strchr(zurl, '/');
185 if (!strcmp(pattern + 2, db))
190 else if (!strcmp(pattern, zurl))
196 // This will be generalized at some point
197 static int match_criterion(struct setting **settings, struct database_criterion *c)
199 int offset = settings_offset(c->name);
200 struct database_criterion_value *v;
204 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
207 if (!settings[offset])
209 for (v = c->values; v; v = v->next)
213 if (match_zurl(settings[offset]->value, v->value))
218 if (!strcmp(settings[offset]->value, v->value))
228 int database_match_criteria(struct setting **settings, struct database_criterion *cl)
230 for (; cl; cl = cl->next)
231 if (!match_criterion(settings, cl))
233 if (cl) // one of the criteria failed to match -- skip this db
239 // Cycles through databases, calling a handler function on the ones for
240 // which all criteria matched.
241 int session_grep_databases(struct session *se, struct database_criterion *cl,
242 void (*fun)(void *context, struct session_database *db))
244 struct session_database *p;
247 for (p = se->databases; p; p = p->next)
249 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
251 if (!p->settings[PZ_NAME])
253 if (database_match_criteria(p->settings, cl))
262 int grep_databases(void *context, struct database_criterion *cl,
263 void (*fun)(void *context, struct database *db))
268 for (p = databases; p; p = p->next)
269 if (database_match_criteria(p->settings, cl))
280 * indent-tabs-mode: nil
282 * vim: shiftwidth=4 tabstop=8 expandtab