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>
38 #include <sys/types.h>
40 #include <sys/socket.h>
46 #include <netinet/in.h>
49 enum pazpar2_database_criterion_type {
51 PAZPAR2_SUBSTRING_MATCH
54 struct database_criterion_value {
56 struct database_criterion_value *next;
59 struct database_criterion {
61 enum pazpar2_database_criterion_type type;
62 struct database_criterion_value *values;
63 struct database_criterion *next;
66 static struct host *hosts = 0; /* thread pr */
68 static xmlDoc *get_explain_xml(struct conf_targetprofiles *targetprofiles,
75 if (targetprofiles->type != Targetprofiles_local)
77 yaz_log(YLOG_FATAL, "Only supports local type");
80 dir = targetprofiles->src;
82 sprintf(path, "%s/%s", dir, ide);
84 return xmlParseFile(path);
89 // Create a new host structure for hostport
90 static struct host *create_host(const char *hostport)
94 host = xmalloc(sizeof(struct host));
95 host->hostport = xstrdup(hostport);
96 host->connections = 0;
99 if (host_getaddrinfo(host))
101 xfree(host->hostport);
110 static struct host *find_host(const char *hostport)
113 for (p = hosts; p; p = p->next)
114 if (!strcmp(p->hostport, hostport))
116 return create_host(hostport);
119 int resolve_database(struct database *db)
126 strcpy(hostport, db->url);
127 if ((p = strchr(hostport, '/')))
129 if (!(host = find_host(hostport)))
136 void resolve_databases(struct conf_service *service)
138 struct database *db = service->databases;
139 for (; db; db = db->next)
140 resolve_database(db);
143 static struct database *load_database(const char *id,
144 struct conf_service *service)
147 struct zr_explain *explain = 0;
151 struct setting *idset;
153 if (service->targetprofiles
154 && (doc = get_explain_xml(service->targetprofiles, id)))
156 explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
161 if (strlen(id) > 255)
163 strcpy(hostport, id);
164 if ((dbname = strchr(hostport, '/')))
168 db = nmem_malloc(service->nmem, sizeof(*db));
169 memset(db, 0, sizeof(*db));
171 db->url = nmem_strdup(service->nmem, id);
172 db->databases = nmem_malloc(service->nmem, 2 * sizeof(char *));
173 db->databases[0] = nmem_strdup(service->nmem, dbname);
174 db->databases[1] = 0;
176 db->explain = explain;
178 db->num_settings = settings_num(service);
179 db->settings = nmem_malloc(service->nmem, sizeof(struct settings*) *
181 memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
182 idset = nmem_malloc(service->nmem, sizeof(*idset));
183 idset->precedence = 0;
184 idset->name = "pz:id";
185 idset->target = idset->value = db->url;
187 db->settings[PZ_ID] = idset;
189 db->next = service->databases;
190 service->databases = db;
195 // Return a database structure by ID. Load and add to list if necessary
196 // new==1 just means we know it's not in the list
197 struct database *find_database(const char *id, struct conf_service *service)
200 for (p = service->databases; p; p = p->next)
201 if (!strcmp(p->url, id))
203 return load_database(id, service);
206 // This whole session_grep database thing should be moved elsewhere
208 int match_zurl(const char *zurl, const char *pattern)
212 if (!strcmp(pattern, "*"))
214 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
216 char *db = strchr(zurl, '/');
219 if (!strcmp(pattern + 2, db))
224 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
226 if (!strncmp(pattern, zurl, len))
231 else if (!strcmp(pattern, zurl))
237 // This will be generalized at some point
238 static int match_criterion(struct setting **settings,
239 struct conf_service *service,
240 struct database_criterion *c)
242 int offset = settings_lookup_offset(service, c->name);
243 struct database_criterion_value *v;
247 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
250 if (!settings[offset])
252 for (v = c->values; v; v = v->next)
254 if (c->type == PAZPAR2_STRING_MATCH)
258 if (match_zurl(settings[offset]->value, v->value))
263 if (!strcmp(settings[offset]->value, v->value))
267 else if (c->type == PAZPAR2_SUBSTRING_MATCH)
269 if (strstr(settings[offset]->value, v->value))
279 // parses crit1=val1,crit2=val2|val3,...
280 static struct database_criterion *create_database_criterion(NMEM m,
283 struct database_criterion *res = 0;
290 nmem_strsplit(m, ",", buf, &values, &num);
291 for (i = 0; i < num; i++)
296 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
298 if ((eq = strchr(values[i], '=')))
299 new->type = PAZPAR2_STRING_MATCH;
300 else if ((eq = strchr(values[i], '~')))
301 new->type = PAZPAR2_SUBSTRING_MATCH;
304 yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
308 new->name = values[i];
309 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
311 for (subi = 0; subi < subnum; subi++)
313 struct database_criterion_value *newv
314 = nmem_malloc(m, sizeof(*newv));
315 newv->value = subvalues[subi];
316 newv->next = new->values;
325 static int database_match_criteria(struct setting **settings,
326 struct conf_service *service,
327 struct database_criterion *cl)
329 for (; cl; cl = cl->next)
330 if (!match_criterion(settings, service, cl))
332 if (cl) // one of the criteria failed to match -- skip this db
338 // Cycles through databases, calling a handler function on the ones for
339 // which all criteria matched.
340 int session_grep_databases(struct session *se, const char *filter,
341 void (*fun)(void *context, struct session_database *db))
343 struct session_database *p;
344 NMEM nmem = nmem_create();
346 struct database_criterion *cl = create_database_criterion(nmem, filter);
348 for (p = se->databases; p; p = p->next)
350 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
352 if (!p->settings[PZ_NAME])
354 if (database_match_criteria(p->settings, se->service, cl))
364 int predef_grep_databases(void *context, struct conf_service *service,
365 void (*fun)(void *context, struct database *db))
370 for (p = service->databases; p; p = p->next)
371 if (database_match_criteria(p->settings, service, 0))
382 * c-file-style: "Stroustrup"
383 * indent-tabs-mode: nil
385 * vim: shiftwidth=4 tabstop=8 expandtab