1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2011 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
25 #include <sys/types.h>
33 #include "pazpar2_config.h"
38 #include <sys/types.h>
40 enum pazpar2_database_criterion_type {
42 PAZPAR2_SUBSTRING_MATCH
45 struct database_criterion_value {
47 struct database_criterion_value *next;
50 struct database_criterion {
52 enum pazpar2_database_criterion_type type;
53 struct database_criterion_value *values;
54 struct database_criterion *next;
57 struct database_hosts {
62 // Create a new host structure for hostport
63 static struct host *create_host(const char *hostport)
68 host = xmalloc(sizeof(struct host));
69 host->hostport = xstrdup(hostport);
70 db_comment = strchr(host->hostport, '#');
73 host->connections = 0;
76 pazpar2_mutex_create(&host->mutex, "host");
78 yaz_cond_create(&host->cond_ready);
83 static struct host *find_host(database_hosts_t hosts,
87 yaz_mutex_enter(hosts->mutex);
88 for (p = hosts->hosts; p; p = p->next)
89 if (!strcmp(p->hostport, hostport))
93 p = create_host(hostport);
96 p->next = hosts->hosts;
100 yaz_mutex_leave(hosts->mutex);
104 int resolve_database(struct conf_service *service, struct database *db)
109 if (!(host = find_host(service->server->database_hosts, db->url)))
116 struct database *new_database(const char *id, NMEM nmem)
119 struct setting *idset;
121 db = nmem_malloc(nmem, sizeof(*db));
122 memset(db, 0, sizeof(*db));
124 db->url = nmem_strdup(nmem, id);
128 db->num_settings = PZ_MAX_EOF;
129 db->settings = nmem_malloc(nmem, sizeof(struct settings*) *
131 memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
132 idset = nmem_malloc(nmem, sizeof(*idset));
133 idset->precedence = 0;
134 idset->name = "pz:id";
135 idset->target = idset->value = db->url;
137 db->settings[PZ_ID] = idset;
143 static struct database *load_database(const char *id,
144 struct conf_service *service)
148 db = new_database(id, service->nmem);
150 db->next = service->databases;
151 service->databases = db;
156 // Return a database structure by ID. Load and add to list if necessary
157 // new==1 just means we know it's not in the list
158 struct database *find_database(const char *id, struct conf_service *service)
161 for (p = service->databases; p; p = p->next)
162 if (!strcmp(p->url, id))
164 return load_database(id, service);
167 // This whole session_grep database thing should be moved elsewhere
169 int match_zurl(const char *zurl, const char *pattern)
173 if (!strcmp(pattern, "*"))
175 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
177 char *db = strchr(zurl, '/');
180 if (!strcmp(pattern + 2, db))
185 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
187 if (!strncmp(pattern, zurl, len))
192 else if (!strcmp(pattern, zurl))
198 // This will be generalized at some point
199 static int match_criterion(struct setting **settings,
200 struct conf_service *service,
201 struct database_criterion *c)
203 int offset = settings_lookup_offset(service, c->name);
204 struct database_criterion_value *v;
208 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
211 if (!settings[offset])
213 for (v = c->values; v; v = v->next)
215 if (c->type == PAZPAR2_STRING_MATCH)
219 if (match_zurl(settings[offset]->value, v->value))
224 if (!strcmp(settings[offset]->value, v->value))
228 else if (c->type == PAZPAR2_SUBSTRING_MATCH)
230 if (strstr(settings[offset]->value, v->value))
240 // parses crit1=val1,crit2=val2|val3,...
241 static struct database_criterion *create_database_criterion(NMEM m,
244 struct database_criterion *res = 0;
251 nmem_strsplit(m, ",", buf, &values, &num);
252 for (i = 0; i < num; i++)
257 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
259 if ((eq = strchr(values[i], '=')))
260 new->type = PAZPAR2_STRING_MATCH;
261 else if ((eq = strchr(values[i], '~')))
262 new->type = PAZPAR2_SUBSTRING_MATCH;
265 yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
269 new->name = values[i];
270 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
272 for (subi = 0; subi < subnum; subi++)
274 struct database_criterion_value *newv
275 = nmem_malloc(m, sizeof(*newv));
276 newv->value = subvalues[subi];
277 newv->next = new->values;
286 static int database_match_criteria(struct setting **settings,
287 struct conf_service *service,
288 struct database_criterion *cl)
290 for (; cl; cl = cl->next)
291 if (!match_criterion(settings, service, cl))
293 if (cl) // one of the criteria failed to match -- skip this db
299 // Cycles through databases, calling a handler function on the ones for
300 // which all criteria matched.
301 int session_grep_databases(struct session *se, const char *filter,
302 void (*fun)(struct session *se, struct session_database *db))
304 struct session_database *p;
305 NMEM nmem = nmem_create();
307 struct database_criterion *cl = create_database_criterion(nmem, filter);
309 for (p = se->databases; p; p = p->next)
311 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
313 if (!p->settings[PZ_NAME])
315 if (database_match_criteria(p->settings, se->service, cl))
325 int predef_grep_databases(void *context, struct conf_service *service,
326 void (*fun)(void *context, struct database *db))
331 for (p = service->databases; p; p = p->next)
332 if (database_match_criteria(p->settings, service, 0))
340 database_hosts_t database_hosts_create(void)
342 database_hosts_t p = xmalloc(sizeof(*p));
345 pazpar2_mutex_create(&p->mutex, "database");
349 void database_hosts_destroy(database_hosts_t *pp)
353 struct host *p = (*pp)->hosts;
356 struct host *p_next = p->next;
357 yaz_mutex_destroy(&p->mutex);
358 yaz_cond_destroy(&p->cond_ready);
363 yaz_mutex_destroy(&(*pp)->mutex);
371 * c-file-style: "Stroustrup"
372 * indent-tabs-mode: nil
374 * vim: shiftwidth=4 tabstop=8 expandtab